summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/graphql/pagination/keyset/conditions/null_condition_spec.rb
blob: 1c74f2fb0abb6787325b83140cd67cf5e2e4e517 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# frozen_string_literal: true

require 'spec_helper'

describe Gitlab::Graphql::Pagination::Keyset::Conditions::NullCondition do
  describe '#build' do
    let(:values) { [nil, 500] }
    let(:operators) { [nil, '>'] }
    let(:before_or_after) { :after }
    let(:condition) { described_class.new(arel_table, order_list, values, operators, before_or_after) }

    context 'when ordering by a column attribute' do
      let(:arel_table) { Issue.arel_table }
      let(:order_list) { [double(named_function: nil, attribute_name: 'relative_position'), double(named_function: nil, attribute_name: 'id')] }

      shared_examples ':after condition' do
        it 'generates sql' do
          expected_sql = <<~SQL
            (
              "issues"."relative_position" IS NULL
              AND
              "issues"."id" > 500
            )
          SQL

          expect(condition.build.squish).to eq expected_sql.squish
        end
      end

      context 'when :after' do
        it_behaves_like ':after condition'
      end

      context 'when :before' do
        let(:before_or_after) { :before }

        it 'generates :before sql' do
          expected_sql = <<~SQL
            (
              "issues"."relative_position" IS NULL
              AND
              "issues"."id" > 500
            )
            OR ("issues"."relative_position" IS NOT NULL)
          SQL

          expect(condition.build.squish).to eq expected_sql.squish
        end
      end

      context 'when :foo' do
        let(:before_or_after) { :foo }

        it_behaves_like ':after condition'
      end
    end

    context 'when ordering by LOWER' do
      let(:arel_table) { Project.arel_table }
      let(:relation)   { Project.order(arel_table['name'].lower.asc).order(:id) }
      let(:order_list) { Gitlab::Graphql::Pagination::Keyset::OrderInfo.build_order_list(relation) }

      context 'when :after' do
        it 'generates sql' do
          expected_sql = <<~SQL
        (
          LOWER("projects"."name") IS NULL
          AND
          "projects"."id" > 500
        )
          SQL

          expect(condition.build.squish).to eq expected_sql.squish
        end
      end

      context 'when :before' do
        let(:before_or_after) { :before }

        it 'generates :before sql' do
          expected_sql = <<~SQL
          (
            LOWER("projects"."name") IS NULL
            AND
            "projects"."id" > 500
          )
          OR (LOWER("projects"."name") IS NOT NULL)
          SQL

          expect(condition.build.squish).to eq expected_sql.squish
        end
      end
    end
  end
end