summaryrefslogtreecommitdiff
path: root/spec/support/shared_examples/lib/gitlab/sql/set_operator_shared_examples.rb
blob: aa6a51c36465091aa5c8a44dd7e15090fe408b24 (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# frozen_string_literal: true

RSpec.shared_examples 'SQL set operator' do |operator_keyword|
  operator_keyword = operator_keyword.upcase

  let(:relation_1) { User.where(email: 'alice@example.com').select(:id) }
  let(:relation_2) { User.where(email: 'bob@example.com').select(:id) }

  def to_sql(relation)
    relation.reorder(nil).to_sql
  end

  describe '.operator_keyword' do
    it { expect(described_class.operator_keyword).to eq operator_keyword }
  end

  describe '#to_sql' do
    it "returns a String joining relations together using a #{operator_keyword}" do
      set_operator = described_class.new([relation_1, relation_2])

      expect(set_operator.to_sql).to eq("(#{to_sql(relation_1)})\n#{operator_keyword}\n(#{to_sql(relation_2)})")
    end

    it 'skips Model.none segements' do
      empty_relation = User.none
      set_operator = described_class.new([empty_relation, relation_1, relation_2])

      expect {User.where("users.id IN (#{set_operator.to_sql})").to_a}.not_to raise_error
      expect(set_operator.to_sql).to eq("(#{to_sql(relation_1)})\n#{operator_keyword}\n(#{to_sql(relation_2)})")
    end

    it "uses #{operator_keyword} ALL when removing duplicates is disabled" do
      set_operator = described_class
        .new([relation_1, relation_2], remove_duplicates: false)

      expect(set_operator.to_sql).to include("#{operator_keyword} ALL")
    end

    it 'returns `NULL` if all relations are empty' do
      empty_relation = User.none
      set_operator = described_class.new([empty_relation, empty_relation])

      expect(set_operator.to_sql).to eq('NULL')
    end
  end

  describe 'remove_order parameter' do
    let(:scopes) do
      [
        User.where(id: 1).order(id: :desc).limit(1),
        User.where(id: 2).order(id: :asc).limit(1)
      ]
    end

    subject(:union_query) { described_class.new(scopes, remove_order: remove_order).to_sql }

    context 'when remove_order: true' do
      let(:remove_order) { true }

      it 'removes the ORDER BY from the query' do
        expect(union_query).not_to include('ORDER BY "users"."id" DESC')
        expect(union_query).not_to include('ORDER BY "users"."id" ASC')
      end
    end

    context 'when remove_order: false' do
      let(:remove_order) { false }

      it 'does not remove the ORDER BY from the query' do
        expect(union_query).to include('ORDER BY "users"."id" DESC')
        expect(union_query).to include('ORDER BY "users"."id" ASC')
      end
    end
  end
end