Read about our upcoming Code of Conduct on this issue

connection_spec.rb 10.3 KB
Newer Older
1
2
3
4
5
6
7
# frozen_string_literal: true

require 'spec_helper'

describe Gitlab::Graphql::Connections::Keyset::Connection do
  let(:nodes) { Project.all.order(id: :asc) }
  let(:arguments) { {} }
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
  subject(:connection) do
    described_class.new(nodes, arguments, max_page_size: 3)
  end

  def encoded_cursor(node)
    described_class.new(nodes, {}).cursor_from_node(node)
  end

  def decoded_cursor(cursor)
    JSON.parse(Base64Bp.urlsafe_decode64(cursor))
  end

  describe '#cursor_from_nodes' do
    let(:project) { create(:project) }
    let(:cursor)  { connection.cursor_from_node(project) }

    it 'returns an encoded ID' do
      expect(decoded_cursor(cursor)).to eq('id' => project.id.to_s)
    end

    context 'when an order is specified' do
      let(:nodes) { Project.order(:updated_at) }

      it 'returns the encoded value of the order' do
        expect(decoded_cursor(cursor)).to include('updated_at' => project.updated_at.to_s)
      end

      it 'includes the :id even when not specified in the order' do
        expect(decoded_cursor(cursor)).to include('id' => project.id.to_s)
      end
    end

    context 'when multiple orders are specified' do
      let(:nodes) { Project.order(:updated_at).order(:created_at) }

      it 'returns the encoded value of the order' do
        expect(decoded_cursor(cursor)).to include('updated_at' => project.updated_at.to_s)
      end
    end

    context 'when multiple orders with SQL are specified' do
      let(:nodes) { Project.order(Arel.sql('projects.updated_at IS NULL')).order(:updated_at).order(:id) }

      it 'returns the encoded value of the order' do
        expect(decoded_cursor(cursor)).to include('updated_at' => project.updated_at.to_s)
      end
    end
  end

  describe '#sliced_nodes' do
    let(:projects) { create_list(:project, 4) }

    context 'when before is passed' do
      let(:arguments) { { before: encoded_cursor(projects[1]) } }

      it 'only returns the project before the selected one' do
        expect(subject.sliced_nodes).to contain_exactly(projects.first)
      end

      context 'when the sort order is descending' do
        let(:nodes) { Project.all.order(id: :desc) }

        it 'returns the correct nodes' do
          expect(subject.sliced_nodes).to contain_exactly(*projects[2..-1])
        end
      end
    end

    context 'when after is passed' do
      let(:arguments) { { after: encoded_cursor(projects[1]) } }

      it 'only returns the project before the selected one' do
        expect(subject.sliced_nodes).to contain_exactly(*projects[2..-1])
      end

      context 'when the sort order is descending' do
        let(:nodes) { Project.all.order(id: :desc) }

        it 'returns the correct nodes' do
          expect(subject.sliced_nodes).to contain_exactly(projects.first)
        end
      end
    end

    context 'when both before and after are passed' do
      let(:arguments) do
        {
          after: encoded_cursor(projects[1]),
          before: encoded_cursor(projects[3])
        }
      end

      it 'returns the expected set' do
        expect(subject.sliced_nodes).to contain_exactly(projects[2])
      end
    end

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
174
    shared_examples 'nodes are in ascending order' do
      context 'when no cursor is passed' do
        let(:arguments) { {} }

        it 'returns projects in ascending order' do
          expect(subject.sliced_nodes).to eq(ascending_nodes)
        end
      end

      context 'when before cursor value is not NULL' do
        let(:arguments) { { before: encoded_cursor(ascending_nodes[2]) } }

        it 'returns all projects before the cursor' do
          expect(subject.sliced_nodes).to eq(ascending_nodes.first(2))
        end
      end

      context 'when after cursor value is not NULL' do
        let(:arguments) { { after: encoded_cursor(ascending_nodes[1]) } }

        it 'returns all projects after the cursor' do
          expect(subject.sliced_nodes).to eq(ascending_nodes.last(3))
        end
      end

      context 'when before and after cursor' do
        let(:arguments) { { before: encoded_cursor(ascending_nodes.last), after: encoded_cursor(ascending_nodes.first) } }

        it 'returns all projects after the cursor' do
          expect(subject.sliced_nodes).to eq(ascending_nodes[1..3])
        end
      end
    end

    shared_examples 'nodes are in descending order' do
      context 'when no cursor is passed' do
        let(:arguments) { {} }

        it 'only returns projects in descending order' do
          expect(subject.sliced_nodes).to eq(descending_nodes)
        end
      end

      context 'when before cursor value is not NULL' do
        let(:arguments) { { before: encoded_cursor(descending_nodes[2]) } }

        it 'returns all projects before the cursor' do
          expect(subject.sliced_nodes).to eq(descending_nodes.first(2))
        end
      end

      context 'when after cursor value is not NULL' do
        let(:arguments) { { after: encoded_cursor(descending_nodes[1]) } }

        it 'returns all projects after the cursor' do
          expect(subject.sliced_nodes).to eq(descending_nodes.last(3))
        end
      end

      context 'when before and after cursor' do
        let(:arguments) { { before: encoded_cursor(descending_nodes.last), after: encoded_cursor(descending_nodes.first) } }

        it 'returns all projects after the cursor' do
          expect(subject.sliced_nodes).to eq(descending_nodes[1..3])
        end
      end
    end

    context 'when multiple orders with nil values are defined' do
175
176
177
178
179
180
181
182
183
184
      let!(:project1) { create(:project, last_repository_check_at: 10.days.ago) }    # Asc: project5  Desc: project3
      let!(:project2) { create(:project, last_repository_check_at: nil) }            # Asc: project1  Desc: project1
      let!(:project3) { create(:project, last_repository_check_at: 5.days.ago) }     # Asc: project3  Desc: project5
      let!(:project4) { create(:project, last_repository_check_at: nil) }            # Asc: project2  Desc: project2
      let!(:project5) { create(:project, last_repository_check_at: 20.days.ago) }    # Asc: project4  Desc: project4

      context 'when ascending' do
        let(:nodes) do
          Project.order(Arel.sql('projects.last_repository_check_at IS NULL')).order(last_repository_check_at: :asc).order(id: :asc)
        end
185
        let(:ascending_nodes) { [project5, project1, project3, project2, project4] }
186

187
        it_behaves_like 'nodes are in ascending order'
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209

        context 'when before cursor value is NULL' do
          let(:arguments) { { before: encoded_cursor(project4) } }

          it 'returns all projects before the cursor' do
            expect(subject.sliced_nodes).to eq([project5, project1, project3, project2])
          end
        end

        context 'when after cursor value is NULL' do
          let(:arguments) { { after: encoded_cursor(project2) } }

          it 'returns all projects after the cursor' do
            expect(subject.sliced_nodes).to eq([project4])
          end
        end
      end

      context 'when descending' do
        let(:nodes) do
          Project.order(Arel.sql('projects.last_repository_check_at IS NULL')).order(last_repository_check_at: :desc).order(id: :asc)
        end
210
        let(:descending_nodes) { [project3, project1, project5, project2, project4] }
211

212
        it_behaves_like 'nodes are in descending order'
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228

        context 'when before cursor value is NULL' do
          let(:arguments) { { before: encoded_cursor(project4) } }

          it 'returns all projects before the cursor' do
            expect(subject.sliced_nodes).to eq([project3, project1, project5, project2])
          end
        end

        context 'when after cursor value is NULL' do
          let(:arguments) { { after: encoded_cursor(project2) } }

          it 'returns all projects after the cursor' do
            expect(subject.sliced_nodes).to eq([project4])
          end
        end
229
230
      end
    end
231

232
233
234
235
236
237
    context 'when ordering uses LOWER' do
      let!(:project1) { create(:project, name: 'A') } # Asc: project1  Desc: project4
      let!(:project2) { create(:project, name: 'c') } # Asc: project5  Desc: project2
      let!(:project3) { create(:project, name: 'b') } # Asc: project3  Desc: project3
      let!(:project4) { create(:project, name: 'd') } # Asc: project2  Desc: project5
      let!(:project5) { create(:project, name: 'a') } # Asc: project4  Desc: project1
238

239
240
241
      context 'when ascending' do
        let(:nodes) do
          Project.order(Arel::Table.new(:projects)['name'].lower.asc).order(id: :asc)
242
        end
243
        let(:ascending_nodes) { [project1, project5, project3, project2, project4] }
244

245
246
        it_behaves_like 'nodes are in ascending order'
      end
247

248
249
250
      context 'when descending' do
        let(:nodes) do
          Project.order(Arel::Table.new(:projects)['name'].lower.desc).order(id: :desc)
251
        end
252
253
254
        let(:descending_nodes) { [project4, project2, project3, project5, project1] }

        it_behaves_like 'nodes are in descending order'
255
256
257
      end
    end

258
259
    context 'when an invalid cursor is provided' do
      let(:arguments) { { before: Base64Bp.urlsafe_encode64('invalidcursor', padding: false) } }
260

261
262
      it 'raises an error' do
        expect { subject.sliced_nodes }.to raise_error(Gitlab::Graphql::Errors::ArgumentError)
263
264
265
266
267
      end
    end
  end

  describe '#paged_nodes' do
268
269
    let_it_be(:all_nodes) { create_list(:project, 5) }
    let(:paged_nodes) { subject.paged_nodes }
270

271
272
273
    it_behaves_like "connection with paged nodes" do
      let(:paged_nodes_size) { 3 }
    end
274
275
276
277
278

    context 'when both are passed' do
      let(:arguments) { { first: 2, last: 2 } }

      it 'raises an error' do
279
        expect { paged_nodes }.to raise_error(Gitlab::Graphql::Errors::ArgumentError)
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
      end
    end

    context 'when primary key is not in original order' do
      let(:nodes) { Project.order(last_repository_check_at: :desc) }

      it 'is added to end' do
        sliced = subject.sliced_nodes
        last_order_name = sliced.order_values.last.expr.name

        expect(last_order_name).to eq sliced.primary_key
      end
    end

    context 'when there is no primary key' do
      let(:nodes) { NoPrimaryKey.all }

      it 'raises an error' do
        expect(NoPrimaryKey.primary_key).to be_nil
        expect { subject.sliced_nodes }.to raise_error(ArgumentError, 'Relation must have a primary key')
      end
    end
  end

  class NoPrimaryKey < ActiveRecord::Base
    self.table_name  = 'no_primary_key'
    self.primary_key = nil
  end
end