summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/graphql/pagination/keyset/query_builder_spec.rb
blob: fa631aa56663c5089043412d4e1b35f340068717 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Graphql::Pagination::Keyset::QueryBuilder do
  context 'when number of ordering fields is 0' do
    it 'raises an error' do
      expect { described_class.new(Issue.arel_table, [], {}, :after) }
        .to raise_error(ArgumentError, 'No ordering scopes have been supplied')
    end
  end

  describe '#conditions' do
    let(:relation) { Issue.order(relative_position: :desc).order(:id) }
    let(:order_list) { Gitlab::Graphql::Pagination::Keyset::OrderInfo.build_order_list(relation) }
    let(:arel_table) { Issue.arel_table }
    let(:builder) { described_class.new(arel_table, order_list, decoded_cursor, before_or_after) }
    let(:before_or_after) { :after }

    context 'when only a single ordering' do
      let(:relation) { Issue.order(id: :desc) }

      context 'when the value is nil' do
        let(:decoded_cursor) { { 'id' => nil } }

        it 'raises an error' do
          expect { builder.conditions }
            .to raise_error(Gitlab::Graphql::Errors::ArgumentError, 'Before/after cursor invalid: `nil` was provided as only sortable value')
        end
      end

      context 'when value is not nil' do
        let(:decoded_cursor) { { 'id' => 100 } }
        let(:conditions) { builder.conditions }

        context 'when :after' do
          it 'generates the correct condition' do
            expect(conditions.strip).to eq '("issues"."id" < 100)'
          end
        end

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

          it 'generates the correct condition' do
            expect(conditions.strip).to eq '("issues"."id" > 100)'
          end
        end
      end
    end

    context 'when two orderings' do
      let(:decoded_cursor) { { 'relative_position' => 1500, 'id' => 100 } }

      context 'when no values are nil' do
        context 'when :after' do
          it 'generates the correct condition' do
            conditions = builder.conditions

            expect(conditions).to include '"issues"."relative_position" < 1500'
            expect(conditions).to include '"issues"."id" > 100'
            expect(conditions).to include 'OR ("issues"."relative_position" IS NULL)'
          end
        end

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

          it 'generates the correct condition' do
            conditions = builder.conditions

            expect(conditions).to include '("issues"."relative_position" > 1500)'
            expect(conditions).to include '"issues"."id" < 100'
            expect(conditions).to include '"issues"."relative_position" = 1500'
          end
        end
      end

      context 'when first value is nil' do
        let(:decoded_cursor) { { 'relative_position' => nil, 'id' => 100 } }

        context 'when :after' do
          it 'generates the correct condition' do
            conditions = builder.conditions

            expect(conditions).to include '"issues"."relative_position" IS NULL'
            expect(conditions).to include '"issues"."id" > 100'
          end
        end

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

          it 'generates the correct condition' do
            conditions = builder.conditions

            expect(conditions).to include '"issues"."relative_position" IS NULL'
            expect(conditions).to include '"issues"."id" < 100'
            expect(conditions).to include 'OR ("issues"."relative_position" IS NOT NULL)'
          end
        end
      end
    end

    context 'when sorting using LOWER' do
      let(:relation) { Project.order(Arel::Table.new(:projects)['name'].lower.asc).order(:id) }
      let(:arel_table) { Project.arel_table }
      let(:decoded_cursor) { { 'name' => 'Test', 'id' => 100 } }

      context 'when no values are nil' do
        context 'when :after' do
          it 'generates the correct condition' do
            conditions = builder.conditions

            expect(conditions).to include '(LOWER("projects"."name") > \'test\')'
            expect(conditions).to include '"projects"."id" > 100'
            expect(conditions).to include 'OR (LOWER("projects"."name") IS NULL)'
          end
        end

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

          it 'generates the correct condition' do
            conditions = builder.conditions

            expect(conditions).to include '(LOWER("projects"."name") < \'test\')'
            expect(conditions).to include '"projects"."id" < 100'
            expect(conditions).to include 'LOWER("projects"."name") = \'test\''
          end
        end
      end
    end

    context 'when sorting using SIMILARITY' do
      let(:relation) { Project.sorted_by_similarity_desc('test', include_in_select: true) }
      let(:arel_table) { Project.arel_table }
      let(:decoded_cursor) { { 'similarity' => 0.5, 'id' => 100 } }
      let(:similarity_function_call) { Gitlab::Database::SimilarityScore::SIMILARITY_FUNCTION_CALL_WITH_ANNOTATION }
      let(:similarity_sql) do
        [
          "(#{similarity_function_call}(COALESCE(\"projects\".\"path\", ''), 'test') * CAST('1' AS numeric))",
          "(#{similarity_function_call}(COALESCE(\"projects\".\"name\", ''), 'test') * CAST('0.7' AS numeric))",
          "(#{similarity_function_call}(COALESCE(\"projects\".\"description\", ''), 'test') * CAST('0.2' AS numeric))"
        ].join(' + ')
      end

      context 'when no values are nil' do
        context 'when :after' do
          it 'generates the correct condition' do
            conditions = builder.conditions.gsub(/\s+/, ' ')

            expect(conditions).to include "(#{similarity_sql} < 0.5)"
            expect(conditions).to include '"projects"."id" < 100'
            expect(conditions).to include "OR (#{similarity_sql} IS NULL)"
          end
        end

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

          it 'generates the correct condition' do
            conditions = builder.conditions.gsub(/\s+/, ' ')

            expect(conditions).to include "(#{similarity_sql} > 0.5)"
            expect(conditions).to include '"projects"."id" > 100'
            expect(conditions).to include "OR ( #{similarity_sql} = 0.5"
          end
        end
      end
    end
  end
end