Welcome to mirror list, hosted at ThFree Co, Russian Federation.

joins_spec.rb « relation_parsers « names_suggestions « metrics « usage « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3e72d118ac6d3d5cd6f6f2a1cdcbb400ad0ea00c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Usage::Metrics::NamesSuggestions::RelationParsers::Joins do
  describe '#accept' do
    let(:collector) do
      Arel::Collectors::SubstituteBinds.new(ApplicationRecord.connection, Arel::Collectors::SQLString.new)
    end

    context 'with join added via string' do
      it 'collects join parts' do
        arel = Issue.joins('LEFT JOIN projects ON projects.id = issue.project_id')

        arel = arel.arel
        result = described_class.new(ApplicationRecord.connection).accept(arel)

        expect(result).to match_array [{ source: "projects", constraints: "projects.id = issue.project_id" }]
      end
    end

    context 'with join added via arel node' do
      it 'collects join parts' do
        source_table = Arel::Table.new('records')
        joined_table = Arel::Table.new('joins')
        second_level_joined_table = Arel::Table.new('second_level_joins')

        arel = source_table
                 .from
                 .project(source_table['id'].count)
                 .join(joined_table, Arel::Nodes::OuterJoin)
                 .on(source_table[:id].eq(joined_table[:records_id]))
                 .join(second_level_joined_table, Arel::Nodes::OuterJoin)
                 .on(joined_table[:id].eq(second_level_joined_table[:joins_id]))

        result = described_class.new(ApplicationRecord.connection).accept(arel)

        expect(result).to match_array [
          { source: "joins", constraints: "records.id = joins.records_id" },
          { source: "second_level_joins", constraints: "joins.id = second_level_joins.joins_id" }
        ]
      end
    end
  end
end