2019-10-29 08:06:40 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 14:09:03 -04:00
|
|
|
RSpec.describe Gitlab::Graphql::Pagination::Keyset::Connection do
|
2019-10-29 08:06:40 -04:00
|
|
|
let(:nodes) { Project.all.order(id: :asc) }
|
|
|
|
let(:arguments) { {} }
|
2020-04-07 23:09:31 -04:00
|
|
|
let(:query_type) { GraphQL::ObjectType.new }
|
|
|
|
let(:schema) { GraphQL::Schema.define(query: query_type, mutation: nil)}
|
|
|
|
let(:context) { GraphQL::Query::Context.new(query: OpenStruct.new(schema: schema), values: nil, object: nil) }
|
2019-12-12 07:07:33 -05:00
|
|
|
|
2019-10-29 08:06:40 -04:00
|
|
|
subject(:connection) do
|
2020-04-07 23:09:31 -04:00
|
|
|
described_class.new(nodes, { context: context, max_page_size: 3 }.merge(arguments))
|
2019-10-29 08:06:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def encoded_cursor(node)
|
2020-04-07 23:09:31 -04:00
|
|
|
described_class.new(nodes, { context: context }).cursor_for(node)
|
2019-10-29 08:06:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def decoded_cursor(cursor)
|
2020-04-30 20:09:59 -04:00
|
|
|
Gitlab::Json.parse(Base64Bp.urlsafe_decode64(cursor))
|
2019-10-29 08:06:40 -04:00
|
|
|
end
|
|
|
|
|
2020-04-07 23:09:31 -04:00
|
|
|
describe '#cursor_for' do
|
2019-10-29 08:06:40 -04:00
|
|
|
let(:project) { create(:project) }
|
2020-04-07 23:09:31 -04:00
|
|
|
let(:cursor) { connection.cursor_for(project) }
|
2019-10-29 08:06:40 -04:00
|
|
|
|
|
|
|
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
|
2020-06-16 17:08:20 -04:00
|
|
|
expect(decoded_cursor(cursor)).to include('updated_at' => project.updated_at.strftime('%Y-%m-%d %H:%M:%S.%N %Z'))
|
2019-10-29 08:06:40 -04:00
|
|
|
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
|
2020-06-16 17:08:20 -04:00
|
|
|
expect(decoded_cursor(cursor)).to include('updated_at' => project.updated_at.strftime('%Y-%m-%d %H:%M:%S.%N %Z'))
|
2019-10-29 08:06:40 -04:00
|
|
|
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
|
2020-06-16 17:08:20 -04:00
|
|
|
expect(decoded_cursor(cursor)).to include('updated_at' => project.updated_at.strftime('%Y-%m-%d %H:%M:%S.%N %Z'))
|
2019-10-29 08:06:40 -04:00
|
|
|
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
|
|
|
|
|
2020-02-11 10:08:44 -05:00
|
|
|
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
|
2019-10-29 08:06:40 -04:00
|
|
|
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
|
2020-02-11 10:08:44 -05:00
|
|
|
let(:ascending_nodes) { [project5, project1, project3, project2, project4] }
|
2019-10-29 08:06:40 -04:00
|
|
|
|
2020-02-11 10:08:44 -05:00
|
|
|
it_behaves_like 'nodes are in ascending order'
|
2019-10-29 08:06:40 -04:00
|
|
|
|
|
|
|
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
|
2020-02-11 10:08:44 -05:00
|
|
|
let(:descending_nodes) { [project3, project1, project5, project2, project4] }
|
2019-10-29 08:06:40 -04:00
|
|
|
|
2020-02-11 10:08:44 -05:00
|
|
|
it_behaves_like 'nodes are in descending order'
|
2019-10-29 08:06:40 -04:00
|
|
|
|
|
|
|
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
|
2020-02-11 10:08:44 -05:00
|
|
|
end
|
|
|
|
end
|
2019-10-29 08:06:40 -04:00
|
|
|
|
2020-02-11 10:08:44 -05:00
|
|
|
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
|
2019-10-29 08:06:40 -04:00
|
|
|
|
2020-02-11 10:08:44 -05:00
|
|
|
context 'when ascending' do
|
|
|
|
let(:nodes) do
|
|
|
|
Project.order(Arel::Table.new(:projects)['name'].lower.asc).order(id: :asc)
|
2019-10-29 08:06:40 -04:00
|
|
|
end
|
2020-02-11 10:08:44 -05:00
|
|
|
let(:ascending_nodes) { [project1, project5, project3, project2, project4] }
|
2019-10-29 08:06:40 -04:00
|
|
|
|
2020-02-11 10:08:44 -05:00
|
|
|
it_behaves_like 'nodes are in ascending order'
|
|
|
|
end
|
2019-10-29 08:06:40 -04:00
|
|
|
|
2020-02-11 10:08:44 -05:00
|
|
|
context 'when descending' do
|
|
|
|
let(:nodes) do
|
|
|
|
Project.order(Arel::Table.new(:projects)['name'].lower.desc).order(id: :desc)
|
2019-10-29 08:06:40 -04:00
|
|
|
end
|
2020-02-11 10:08:44 -05:00
|
|
|
let(:descending_nodes) { [project4, project2, project3, project5, project1] }
|
|
|
|
|
|
|
|
it_behaves_like 'nodes are in descending order'
|
2019-10-29 08:06:40 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-11-25 07:06:13 -05:00
|
|
|
context 'when an invalid cursor is provided' do
|
|
|
|
let(:arguments) { { before: Base64Bp.urlsafe_encode64('invalidcursor', padding: false) } }
|
2019-10-29 08:06:40 -04:00
|
|
|
|
2019-11-25 07:06:13 -05:00
|
|
|
it 'raises an error' do
|
|
|
|
expect { subject.sliced_nodes }.to raise_error(Gitlab::Graphql::Errors::ArgumentError)
|
2019-10-29 08:06:40 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-07 23:09:31 -04:00
|
|
|
describe '#nodes' do
|
2019-11-15 07:06:12 -05:00
|
|
|
let_it_be(:all_nodes) { create_list(:project, 5) }
|
2020-04-07 23:09:31 -04:00
|
|
|
let(:paged_nodes) { subject.nodes }
|
2019-10-29 08:06:40 -04:00
|
|
|
|
2020-04-07 23:09:31 -04:00
|
|
|
it_behaves_like 'connection with paged nodes' do
|
2020-01-07 16:07:50 -05:00
|
|
|
let(:paged_nodes_size) { 3 }
|
|
|
|
end
|
2019-10-29 08:06:40 -04:00
|
|
|
|
|
|
|
context 'when both are passed' do
|
|
|
|
let(:arguments) { { first: 2, last: 2 } }
|
|
|
|
|
|
|
|
it 'raises an error' do
|
2019-11-15 07:06:12 -05:00
|
|
|
expect { paged_nodes }.to raise_error(Gitlab::Graphql::Errors::ArgumentError)
|
2019-10-29 08:06:40 -04:00
|
|
|
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
|
2020-05-27 05:08:30 -04:00
|
|
|
before do
|
|
|
|
stub_const('NoPrimaryKey', Class.new(ActiveRecord::Base))
|
|
|
|
NoPrimaryKey.class_eval do
|
|
|
|
self.table_name = 'no_primary_key'
|
|
|
|
self.primary_key = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-29 08:06:40 -04:00
|
|
|
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
|
2020-05-26 20:08:11 -04:00
|
|
|
|
|
|
|
describe '#has_previous_page and #has_next_page' do
|
|
|
|
# using a list of 5 items with a max_page of 3
|
|
|
|
let_it_be(:project_list) { create_list(:project, 5) }
|
|
|
|
let_it_be(:nodes) { Project.order(:id) }
|
|
|
|
|
|
|
|
context 'when default query' do
|
|
|
|
let(:arguments) { {} }
|
|
|
|
|
|
|
|
it 'has no previous, but a next' do
|
|
|
|
expect(subject.has_previous_page).to be_falsey
|
|
|
|
expect(subject.has_next_page).to be_truthy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when before is first item' do
|
|
|
|
let(:arguments) { { before: encoded_cursor(project_list.first) } }
|
|
|
|
|
|
|
|
it 'has no previous, but a next' do
|
|
|
|
expect(subject.has_previous_page).to be_falsey
|
|
|
|
expect(subject.has_next_page).to be_truthy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'using `before`' do
|
|
|
|
context 'when before is the last item' do
|
|
|
|
let(:arguments) { { before: encoded_cursor(project_list.last) } }
|
|
|
|
|
|
|
|
it 'has no previous, but a next' do
|
|
|
|
expect(subject.has_previous_page).to be_falsey
|
|
|
|
expect(subject.has_next_page).to be_truthy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when before and last specified' do
|
|
|
|
let(:arguments) { { before: encoded_cursor(project_list.last), last: 2 } }
|
|
|
|
|
|
|
|
it 'has a previous and a next' do
|
|
|
|
expect(subject.has_previous_page).to be_truthy
|
|
|
|
expect(subject.has_next_page).to be_truthy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when before and last does not request all remaining nodes' do
|
|
|
|
let(:arguments) { { before: encoded_cursor(project_list.last), last: 2 } }
|
|
|
|
|
|
|
|
it 'has a previous and a next' do
|
|
|
|
expect(subject.has_previous_page).to be_truthy
|
|
|
|
expect(subject.has_next_page).to be_truthy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when before and last does request all remaining nodes' do
|
|
|
|
let(:arguments) { { before: encoded_cursor(project_list[1]), last: 3 } }
|
|
|
|
|
|
|
|
it 'has a previous and a next' do
|
|
|
|
expect(subject.has_previous_page).to be_falsey
|
|
|
|
expect(subject.has_next_page).to be_truthy
|
|
|
|
expect(subject.nodes).to eq [project_list[0]]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'using `after`' do
|
|
|
|
context 'when after is the first item' do
|
|
|
|
let(:arguments) { { after: encoded_cursor(project_list.first) } }
|
|
|
|
|
|
|
|
it 'has a previous, and a next' do
|
|
|
|
expect(subject.has_previous_page).to be_truthy
|
|
|
|
expect(subject.has_next_page).to be_truthy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when after and first specified' do
|
|
|
|
let(:arguments) { { after: encoded_cursor(project_list.first), first: 2 } }
|
|
|
|
|
|
|
|
it 'has a previous and a next' do
|
|
|
|
expect(subject.has_previous_page).to be_truthy
|
|
|
|
expect(subject.has_next_page).to be_truthy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when before and last does request all remaining nodes' do
|
|
|
|
let(:arguments) { { after: encoded_cursor(project_list[2]), last: 3 } }
|
|
|
|
|
|
|
|
it 'has a previous but no next' do
|
|
|
|
expect(subject.has_previous_page).to be_truthy
|
|
|
|
expect(subject.has_next_page).to be_falsey
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-10-29 08:06:40 -04:00
|
|
|
end
|