Commit f05c3228 authored by Michael Kozono's avatar Michael Kozono

Merge branch 'ajk-gql-pagination-specs' into 'master'

Improve GraphQL pagination specs

See merge request gitlab-org/gitlab!48496
parents 78e1bbde 37198186
......@@ -10,7 +10,7 @@ module Resolvers
argument :sort, Types::IssueSortEnum,
description: 'Sort issues by this criteria',
required: false,
default_value: 'created_desc'
default_value: :created_desc
type Types::IssueType.connection_type, null: true
......
......@@ -52,7 +52,7 @@ module Resolvers
argument :sort, Types::MergeRequestSortEnum,
description: 'Sort merge requests by this criteria',
required: false,
default_value: 'created_desc'
default_value: :created_desc
def self.single
::Resolvers::MergeRequestResolver
......
......@@ -17,7 +17,7 @@ module Resolvers
argument :sort, Types::SortEnum,
description: 'Sort users by this criteria',
required: false,
default_value: 'created_desc'
default_value: :created_desc
argument :search, GraphQL::STRING_TYPE,
required: false,
......
......@@ -7,10 +7,10 @@ module Types
# Deprecated, as we prefer uppercase enums
# https://gitlab.com/groups/gitlab-org/-/epics/1838
value 'updated_desc', 'Updated at descending order', deprecated: { reason: 'Use UPDATED_DESC', milestone: '13.5' }
value 'updated_asc', 'Updated at ascending order', deprecated: { reason: 'Use UPDATED_ASC', milestone: '13.5' }
value 'created_desc', 'Created at descending order', deprecated: { reason: 'Use CREATED_DESC', milestone: '13.5' }
value 'created_asc', 'Created at ascending order', deprecated: { reason: 'Use CREATED_ASC', milestone: '13.5' }
value 'updated_desc', 'Updated at descending order', value: :updated_desc, deprecated: { reason: 'Use UPDATED_DESC', milestone: '13.5' }
value 'updated_asc', 'Updated at ascending order', value: :updated_asc, deprecated: { reason: 'Use UPDATED_ASC', milestone: '13.5' }
value 'created_desc', 'Created at descending order', value: :created_desc, deprecated: { reason: 'Use CREATED_DESC', milestone: '13.5' }
value 'created_asc', 'Created at ascending order', value: :created_asc, deprecated: { reason: 'Use CREATED_ASC', milestone: '13.5' }
value 'UPDATED_DESC', 'Updated at descending order', value: :updated_desc
value 'UPDATED_ASC', 'Updated at ascending order', value: :updated_asc
......
......@@ -294,28 +294,30 @@ The shared example requires certain `let` variables and methods to be set up:
```ruby
describe 'sorting and pagination' do
let(:sort_project) { create(:project, :public) }
let_it_be(:sort_project) { create(:project, :public) }
let(:data_path) { [:project, :issues] }
def pagination_query(params, page_info)
graphql_query_for(
'project',
{ 'fullPath' => sort_project.full_path },
query_graphql_field('issues', params, "#{page_info} edges { node { id } }")
def pagination_query(params)
graphql_query_for( :project, { full_path: sort_project.full_path },
query_nodes(:issues, :id, include_pagination_info: true, args: params))
)
end
def pagination_results_data(data)
data.map { |issue| issue.dig('node', 'iid').to_i }
def pagination_results_data(nodes)
nodes.map { |issue| issue['iid'].to_i }
end
context 'when sorting by weight' do
...
let_it_be(:issues) { make_some_issues_with_weights }
context 'when ascending' do
let(:ordered_issues) { issues.sort_by(&:weight) }
it_behaves_like 'sorted paginated query' do
let(:sort_param) { 'WEIGHT_ASC' }
let(:sort_param) { :WEIGHT_ASC }
let(:first_param) { 2 }
let(:expected_results) { [weight_issue3.iid, weight_issue5.iid, weight_issue1.iid, weight_issue4.iid, weight_issue2.iid] }
let(:expected_results) { ordered_issues.map(&:iid) }
end
end
end
```
......@@ -79,7 +79,7 @@ module RequirementsManagement
def sort(items)
sorts = RequirementsManagement::Requirement.simple_sorts.keys
sort = sorts.include?(params[:sort]) ? params[:sort] : 'id_desc'
sort = sorts.include?(params[:sort]&.to_s) ? params[:sort] : 'id_desc'
items.order_by(sort)
end
......
......@@ -64,28 +64,22 @@ RSpec.describe 'get board lists' do
end
describe 'sorting and pagination' do
let(:data_path) { [board_parent_type, :boards, :edges, 0, :node, :lists] }
let(:data_path) { [board_parent_type, :boards, :nodes, 0, :lists] }
def pagination_query(params, page_info)
def pagination_query(params)
graphql_query_for(
board_parent_type,
{ 'fullPath' => board_parent.full_path },
<<~BOARDS
boards(first: 1) {
edges {
node {
#{query_graphql_field('lists', params, "#{page_info} edges { node { id } }")}
}
nodes {
#{query_nodes(:lists, :id, args: params, include_pagination_info: true)}
}
}
BOARDS
)
end
def pagination_results_data(data)
data.map { |list| list.dig('node', 'id') }
end
context 'when using default sorting' do
let!(:milestone_list) { create(:milestone_list, board: board, milestone: milestone, position: 10) }
let!(:milestone_list2) { create(:milestone_list, board: board, milestone: milestone2, position: 2) }
......@@ -98,7 +92,7 @@ RSpec.describe 'get board lists' do
it_behaves_like 'sorted paginated query' do
let(:sort_param) { }
let(:first_param) { 2 }
let(:expected_results) { lists.map { |list| list.to_global_id.to_s } }
let(:expected_results) { lists.map { |list| global_id_of(list) } }
end
end
end
......
......@@ -359,35 +359,33 @@ RSpec.describe 'getting group information' do
let(:data_path) { [:group, :codeCoverageActivities] }
def pagination_query(params, page_info)
def pagination_query(params)
graphql_query_for(
'group',
{ 'fullPath' => group.full_path },
:group, { full_path: group.full_path },
<<~QUERY
codeCoverageActivities(startDate: "#{start_date}" #{params}) {
#{page_info} edges {
node {
averageCoverage
}
}
#{page_info}
nodes { averageCoverage }
}
QUERY
)
end
def pagination_results_data(data)
data.map { |coverage| coverage.dig('node', 'averageCoverage') }
end
context 'when default sorting' do
let!(:coverage_1) { create(:ci_daily_build_group_report_result, project: project_1) }
let!(:coverage_2) { create(:ci_daily_build_group_report_result, project: project_2, coverage: 88.8, date: 1.week.ago) }
let(:start_date) { 1.week.ago.to_date.to_s }
let_it_be(:cov_1) { create(:ci_daily_build_group_report_result, project: project_1, coverage: 77.0) }
let_it_be(:cov_2) { create(:ci_daily_build_group_report_result, project: project_2, coverage: 88.8, date: 1.week.ago) }
let_it_be(:cov_3) { create(:ci_daily_build_group_report_result, project: project_1, coverage: 66.6, date: 2.weeks.ago) }
let_it_be(:cov_4) { create(:ci_daily_build_group_report_result, project: project_2, coverage: 99.9, date: 3.weeks.ago) }
let_it_be(:cov_5) { create(:ci_daily_build_group_report_result, project: project_1, coverage: 44.4, date: 4.weeks.ago) }
let_it_be(:cov_6) { create(:ci_daily_build_group_report_result, project: project_1, coverage: 100.0, date: 6.weeks.ago) }
let(:start_date) { 5.weeks.ago.to_date.to_s }
it_behaves_like 'sorted paginated query' do
let(:node_path) { ['averageCoverage'] }
let(:sort_param) { }
let(:first_param) { 2 }
let(:expected_results) { [88.8, 77.0] }
let(:expected_results) { [cov_1, cov_2, cov_3, cov_4, cov_5].reverse.map(&:coverage) }
end
end
end
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe 'Namespace.projects' do
include GraphqlHelpers
describe 'sorting and pagination' do
let_it_be(:ns) { create(:group) }
let_it_be(:current_user) { create(:user) }
let!(:project_1) { create(:project, namespace: ns, name: 'Project', path: 'project') }
let!(:project_2) { create(:project, namespace: ns, name: 'Test Project', path: 'test-project') }
let!(:project_3) { create(:project, namespace: ns, name: 'Test', path: 'test') }
let!(:project_4) { create(:project, namespace: ns, name: 'Test Project Other', path: 'other-test-project') }
let(:data_path) { [:namespace, :projects] }
let(:ns_args) { graphql_args(full_path: ns.full_path) }
let(:project_args) { graphql_args(include_subgroups: true, search: 'test') }
before do
ns.add_owner(current_user)
end
def pagination_query(params)
graphql_query_for(:namespace, ns_args,
query_nodes(:projects, :name, include_pagination_info: true, args: params + project_args))
end
context 'when sorting by STORAGE' do
before do
project_4.statistics.update!(lfs_objects_size: 1, repository_size: 4.gigabytes)
project_2.statistics.update!(lfs_objects_size: 1, repository_size: 2.gigabytes)
project_3.statistics.update!(lfs_objects_size: 2, repository_size: 1.gigabytes)
end
it_behaves_like 'sorted paginated query' do
let(:node_path) { %w[name] }
let(:sort_param) { :STORAGE }
let(:first_param) { 2 }
let(:expected_results) { [project_4, project_2, project_3].map(&:name) }
end
end
end
end
......@@ -11,18 +11,12 @@ RSpec.describe 'getting an issue list for a project' do
let(:sort_project) { create(:project, :public) }
let(:data_path) { [:project, :issues] }
def pagination_query(params, page_info)
graphql_query_for(
'project',
{ 'fullPath' => sort_project.full_path },
query_graphql_field('issues', params, "#{page_info} edges { node { iid weight} }")
def pagination_query(params)
graphql_query_for(:project, { full_path: sort_project.full_path },
query_nodes(:issues, :iid, args: params, include_pagination_info: true)
)
end
def pagination_results_data(data)
data.map { |issue| issue.dig('node', 'iid').to_i }
end
context 'when sorting by weight' do
let!(:weight_issue1) { create(:issue, project: sort_project, weight: 5) }
let!(:weight_issue2) { create(:issue, project: sort_project, weight: nil) }
......@@ -32,17 +26,19 @@ RSpec.describe 'getting an issue list for a project' do
context 'when ascending' do
it_behaves_like 'sorted paginated query' do
let(:sort_param) { 'WEIGHT_ASC' }
let(:node_path) { %w[iid] }
let(:sort_param) { :WEIGHT_ASC }
let(:first_param) { 2 }
let(:expected_results) { [weight_issue3.iid, weight_issue5.iid, weight_issue1.iid, weight_issue4.iid, weight_issue2.iid] }
let(:expected_results) { [weight_issue3, weight_issue5, weight_issue1, weight_issue4, weight_issue2].map { |i| i.iid.to_s } }
end
end
context 'when descending' do
it_behaves_like 'sorted paginated query' do
let(:sort_param) { 'WEIGHT_DESC' }
let(:node_path) { %w[iid] }
let(:sort_param) { :WEIGHT_DESC }
let(:first_param) { 2 }
let(:expected_results) { [weight_issue1.iid, weight_issue5.iid, weight_issue3.iid, weight_issue4.iid, weight_issue2.iid] }
let(:expected_results) { [weight_issue1, weight_issue5, weight_issue3, weight_issue4, weight_issue2].map { |i| i.iid.to_s } }
end
end
end
......
......@@ -161,16 +161,12 @@ RSpec.describe 'getting a requirement list for a project' do
describe 'sorting and pagination' do
let_it_be(:data_path) { [:project, :requirements] }
def pagination_query(params, page_info)
graphql_query_for(
'project',
{ 'fullPath' => sort_project.full_path },
query_graphql_field('requirements', params, "#{page_info} edges { node { iid createdAt} }")
)
def pagination_query(params)
nested_internal_id_query(:project, sort_project, :requirements, params)
end
def pagination_results_data(data)
data.map { |issue| issue.dig('node', 'iid').to_i }
data.map { |issue| issue.dig('iid').to_i }
end
context 'when sorting by created_at' do
......@@ -183,7 +179,7 @@ RSpec.describe 'getting a requirement list for a project' do
context 'when ascending' do
it_behaves_like 'sorted paginated query' do
let(:sort_param) { 'created_asc' }
let(:sort_param) { :CREATED_ASC }
let(:first_param) { 2 }
let(:expected_results) { [requirement4.iid, requirement3.iid, requirement5.iid, requirement1.iid, requirement2.iid] }
end
......@@ -191,7 +187,7 @@ RSpec.describe 'getting a requirement list for a project' do
context 'when descending' do
it_behaves_like 'sorted paginated query' do
let(:sort_param) { 'created_desc' }
let(:sort_param) { :CREATED_DESC }
let(:first_param) { 2 }
let(:expected_results) { [requirement2.iid, requirement1.iid, requirement5.iid, requirement3.iid, requirement4.iid] }
end
......
......@@ -63,27 +63,29 @@ RSpec.describe 'getting test reports of a requirement' do
let_it_be(:data_path) { [:project, :requirement, :testReports] }
let_it_be(:test_report_3) { create(:test_report, requirement: requirement, created_at: 4.days.ago) }
def pagination_query(params, page_info)
graphql_query_for(
'project',
{ 'fullPath' => project.full_path },
"requirement { testReports(#{params}) { #{page_info} edges { node { id } } } }"
def pagination_query(params)
graphql_query_for(:project, { full_path: project.full_path },
"requirement { testReports(#{params}) { #{page_info} nodes { id } } }"
)
end
def pagination_results_data(data)
data.map { |test_report| test_report.dig('node', 'id') }
let(:in_creation_order) do
[test_report_3, test_report_2, test_report_1]
end
it_behaves_like 'sorted paginated query' do
let(:sort_param) { 'created_asc' }
let(:sort_param) { :CREATED_ASC }
let(:first_param) { 2 }
let(:expected_results) do
[
test_report_3.to_global_id.to_s,
test_report_2.to_global_id.to_s,
test_report_1.to_global_id.to_s
]
in_creation_order.map { |r| global_id_of(r) }
end
end
it_behaves_like 'sorted paginated query' do
let(:sort_param) { :CREATED_DESC }
let(:first_param) { 2 }
let(:expected_results) do
in_creation_order.reverse.map { |r| global_id_of(r) }
end
end
end
......
......@@ -66,28 +66,22 @@ RSpec.describe 'get board lists' do
describe 'sorting and pagination' do
let_it_be(:current_user) { user }
let(:data_path) { [board_parent_type, :boards, :edges, 0, :node, :lists] }
let(:data_path) { [board_parent_type, :boards, :nodes, 0, :lists] }
def pagination_query(params, page_info)
def pagination_query(params)
graphql_query_for(
board_parent_type,
{ 'fullPath' => board_parent.full_path },
<<~BOARDS
boards(first: 1) {
edges {
node {
#{query_graphql_field('lists', params, "#{page_info} edges { node { id } }")}
}
nodes {
#{query_graphql_field(:lists, params, "#{page_info} nodes { id }")}
}
}
BOARDS
)
end
def pagination_results_data(data)
data.map { |list| list.dig('node', 'id') }
end
context 'when using default sorting' do
let!(:label_list) { create(:list, board: board, label: label, position: 10) }
let!(:label_list2) { create(:list, board: board, label: label2, position: 2) }
......@@ -99,7 +93,7 @@ RSpec.describe 'get board lists' do
it_behaves_like 'sorted paginated query' do
let(:sort_param) { }
let(:first_param) { 2 }
let(:expected_results) { lists.map { |list| list.to_global_id.to_s } }
let(:expected_results) { lists.map { |list| global_id_of(list) } }
end
end
end
......
......@@ -80,38 +80,34 @@ RSpec.describe 'getting projects' do
end
describe 'sorting and pagination' do
let_it_be(:ns) { create(:group) }
let_it_be(:current_user) { create(:user) }
let_it_be(:project_1) { create(:project, name: 'Project', path: 'project', namespace: ns) }
let_it_be(:project_2) { create(:project, name: 'Test Project', path: 'test-project', namespace: ns) }
let_it_be(:project_3) { create(:project, name: 'Test', path: 'test', namespace: ns) }
let_it_be(:project_4) { create(:project, name: 'Test Project Other', path: 'other-test-project', namespace: ns) }
let(:data_path) { [:namespace, :projects] }
def pagination_query(params, page_info)
graphql_query_for(
'namespace',
{ 'fullPath' => subject.full_path },
<<~QUERY
projects(includeSubgroups: #{include_subgroups}, search: "#{search}", #{params}) {
#{page_info} edges {
node {
#{all_graphql_fields_for('Project')}
}
}
}
QUERY
)
let(:ns_args) { { full_path: ns.full_path } }
let(:search) { 'test' }
before do
ns.add_owner(current_user)
end
def pagination_results_data(data)
data.map { |project| project.dig('node', 'name') }
def pagination_query(params)
arguments = params.merge(include_subgroups: include_subgroups, search: search)
graphql_query_for(:namespace, ns_args, query_graphql_field(:projects, arguments, <<~GQL))
#{page_info}
nodes { name }
GQL
end
context 'when sorting by similarity' do
let!(:project_1) { create(:project, name: 'Project', path: 'project', namespace: subject) }
let!(:project_2) { create(:project, name: 'Test Project', path: 'test-project', namespace: subject) }
let!(:project_3) { create(:project, name: 'Test', path: 'test', namespace: subject) }
let!(:project_4) { create(:project, name: 'Test Project Other', path: 'other-test-project', namespace: subject) }
let(:search) { 'test' }
let(:current_user) { user }
it_behaves_like 'sorted paginated query' do
let(:sort_param) { 'SIMILARITY' }
let(:node_path) { %w[name] }
let(:sort_param) { :SIMILARITY }
let(:first_param) { 2 }
let(:expected_results) { [project_3.name, project_2.name, project_4.name] }
end
......
......@@ -142,16 +142,14 @@ RSpec.describe 'getting an issue list for a project' do
describe 'sorting and pagination' do
let_it_be(:data_path) { [:project, :issues] }
def pagination_query(params, page_info)
graphql_query_for(
'project',
{ 'fullPath' => sort_project.full_path },
query_graphql_field('issues', params, "#{page_info} edges { node { iid dueDate} }")
def pagination_query(params)
graphql_query_for(:project, { full_path: sort_project.full_path },
query_graphql_field(:issues, params, "#{page_info} nodes { iid }")
)
end
def pagination_results_data(data)
data.map { |issue| issue.dig('node', 'iid').to_i }
data.map { |issue| issue.dig('iid').to_i }
end
context 'when sorting by due date' do
......@@ -164,7 +162,7 @@ RSpec.describe 'getting an issue list for a project' do
context 'when ascending' do
it_behaves_like 'sorted paginated query' do
let(:sort_param) { 'DUE_DATE_ASC' }
let(:sort_param) { :DUE_DATE_ASC }
let(:first_param) { 2 }
let(:expected_results) { [due_issue3.iid, due_issue5.iid, due_issue1.iid, due_issue4.iid, due_issue2.iid] }
end
......@@ -172,7 +170,7 @@ RSpec.describe 'getting an issue list for a project' do
context 'when descending' do
it_behaves_like 'sorted paginated query' do
let(:sort_param) { 'DUE_DATE_DESC' }
let(:sort_param) { :DUE_DATE_DESC }
let(:first_param) { 2 }
let(:expected_results) { [due_issue1.iid, due_issue5.iid, due_issue3.iid, due_issue4.iid, due_issue2.iid] }
end
......@@ -189,7 +187,7 @@ RSpec.describe 'getting an issue list for a project' do
context 'when ascending' do
it_behaves_like 'sorted paginated query' do
let(:sort_param) { 'RELATIVE_POSITION_ASC' }
let(:sort_param) { :RELATIVE_POSITION_ASC }
let(:first_param) { 2 }
let(:expected_results) { [relative_issue5.iid, relative_issue3.iid, relative_issue1.iid, relative_issue4.iid, relative_issue2.iid] }
end
......@@ -209,7 +207,7 @@ RSpec.describe 'getting an issue list for a project' do
context 'when ascending' do
it_behaves_like 'sorted paginated query' do
let(:sort_param) { 'PRIORITY_ASC' }
let(:sort_param) { :PRIORITY_ASC }
let(:first_param) { 2 }
let(:expected_results) { [priority_issue3.iid, priority_issue1.iid, priority_issue2.iid, priority_issue4.iid] }
end
......@@ -217,7 +215,7 @@ RSpec.describe 'getting an issue list for a project' do
context 'when descending' do
it_behaves_like 'sorted paginated query' do
let(:sort_param) { 'PRIORITY_DESC' }
let(:sort_param) { :PRIORITY_DESC }
let(:first_param) { 2 }
let(:expected_results) { [priority_issue1.iid, priority_issue3.iid, priority_issue2.iid, priority_issue4.iid] }
end
......@@ -236,7 +234,7 @@ RSpec.describe 'getting an issue list for a project' do
context 'when ascending' do
it_behaves_like 'sorted paginated query' do
let(:sort_param) { 'LABEL_PRIORITY_ASC' }
let(:sort_param) { :LABEL_PRIORITY_ASC }
let(:first_param) { 2 }
let(:expected_results) { [label_issue3.iid, label_issue1.iid, label_issue2.iid, label_issue4.iid] }
end
......@@ -244,7 +242,7 @@ RSpec.describe 'getting an issue list for a project' do
context 'when descending' do
it_behaves_like 'sorted paginated query' do
let(:sort_param) { 'LABEL_PRIORITY_DESC' }
let(:sort_param) { :LABEL_PRIORITY_DESC }
let(:first_param) { 2 }
let(:expected_results) { [label_issue2.iid, label_issue3.iid, label_issue1.iid, label_issue4.iid] }
end
......@@ -261,7 +259,7 @@ RSpec.describe 'getting an issue list for a project' do
context 'when ascending' do
it_behaves_like 'sorted paginated query' do
let(:sort_param) { 'MILESTONE_DUE_ASC' }
let(:sort_param) { :MILESTONE_DUE_ASC }
let(:first_param) { 2 }
let(:expected_results) { [milestone_issue2.iid, milestone_issue3.iid, milestone_issue1.iid] }
end
......@@ -269,7 +267,7 @@ RSpec.describe 'getting an issue list for a project' do
context 'when descending' do
it_behaves_like 'sorted paginated query' do
let(:sort_param) { 'MILESTONE_DUE_DESC' }
let(:sort_param) { :MILESTONE_DUE_DESC }
let(:first_param) { 2 }
let(:expected_results) { [milestone_issue3.iid, milestone_issue2.iid, milestone_issue1.iid] }
end
......
......@@ -259,29 +259,19 @@ RSpec.describe 'getting merge request listings nested in a project' do
describe 'sorting and pagination' do
let(:data_path) { [:project, :mergeRequests] }
def pagination_query(params, page_info)
graphql_query_for(
:project,
{ full_path: project.full_path },
def pagination_query(params)
graphql_query_for(:project, { full_path: project.full_path },
<<~QUERY
mergeRequests(#{params}) {
#{page_info} edges {
node {
id
}
}
#{page_info} nodes { id }
}
QUERY
)
end
def pagination_results_data(data)
data.map { |project| project.dig('node', 'id') }
end
context 'when sorting by merged_at DESC' do
it_behaves_like 'sorted paginated query' do
let(:sort_param) { 'MERGED_AT_DESC' }
let(:sort_param) { :MERGED_AT_DESC }
let(:first_param) { 2 }
let(:expected_results) do
......@@ -291,7 +281,7 @@ RSpec.describe 'getting merge request listings nested in a project' do
merge_request_c,
merge_request_e,
merge_request_a
].map(&:to_gid).map(&:to_s)
].map { |mr| global_id_of(mr) }
end
before do
......@@ -304,33 +294,6 @@ RSpec.describe 'getting merge request listings nested in a project' do
merge_request_b.metrics.update!(merged_at: 1.day.ago)
end
context 'when paginating backwards' do
let(:params) { 'first: 2, sort: MERGED_AT_DESC' }
let(:page_info) { 'pageInfo { startCursor endCursor }' }
before do
post_graphql(pagination_query(params, page_info), current_user: current_user)
end
it 'paginates backwards correctly' do
# first page
first_page_response_data = graphql_dig_at(Gitlab::Json.parse(response.body), :data, *data_path, :edges)
end_cursor = graphql_dig_at(Gitlab::Json.parse(response.body), :data, :project, :mergeRequests, :pageInfo, :endCursor)
# second page
params = "first: 2, after: \"#{end_cursor}\", sort: MERGED_AT_DESC"
post_graphql(pagination_query(params, page_info), current_user: current_user)
start_cursor = graphql_dig_at(Gitlab::Json.parse(response.body), :data, :project, :mergeRequests, :pageInfo, :start_cursor)
# going back to the first page
params = "last: 2, before: \"#{start_cursor}\", sort: MERGED_AT_DESC"
post_graphql(pagination_query(params, page_info), current_user: current_user)
backward_paginated_response_data = graphql_dig_at(Gitlab::Json.parse(response.body), :data, *data_path, :edges)
expect(first_page_response_data).to eq(backward_paginated_response_data)
end
end
end
end
end
......
......@@ -59,20 +59,16 @@ RSpec.describe 'Users' do
describe 'sorting and pagination' do
let_it_be(:data_path) { [:users] }
def pagination_query(params, page_info)
graphql_query_for("users", params, "#{page_info} edges { node { id } }")
end
def pagination_results_data(data)
data.map { |user| user.dig('node', 'id') }
def pagination_query(params)
graphql_query_for(:users, params, "#{page_info} nodes { id }")
end
context 'when sorting by created_at' do
let_it_be(:ascending_users) { [user3, user2, user1, current_user].map(&:to_global_id).map(&:to_s) }
let_it_be(:ascending_users) { [user3, user2, user1, current_user].map { |u| global_id_of(u) } }
context 'when ascending' do
it_behaves_like 'sorted paginated query' do
let(:sort_param) { 'created_asc' }
let(:sort_param) { :CREATED_ASC }
let(:first_param) { 1 }
let(:expected_results) { ascending_users }
end
......@@ -80,7 +76,7 @@ RSpec.describe 'Users' do
context 'when descending' do
it_behaves_like 'sorted paginated query' do
let(:sort_param) { 'created_desc' }
let(:sort_param) { :CREATED_DESC }
let(:first_param) { 1 }
let(:expected_results) { ascending_users.reverse }
end
......
......@@ -16,80 +16,111 @@
#
# Example:
# describe 'sorting and pagination' do
# let(:sort_project) { create(:project, :public) }
# let_it_be(:sort_project) { create(:project, :public) }
# let(:data_path) { [:project, :issues] }
#
# def pagination_query(params, page_info)
# graphql_query_for(
# 'project',
# { 'fullPath' => sort_project.full_path },
# query_graphql_field('issues', params, "#{page_info} edges { node { id } }")
# def pagination_query(arguments)
# graphql_query_for(:project, { full_path: sort_project.full_path },
# query_nodes(:issues, :iid, include_pagination_info: true, args: arguments)
# )
# end
#
# def pagination_results_data(data)
# data.map { |issue| issue.dig('node', 'iid').to_i }
# # A method transforming nodes to data to match against
# # default: the identity function
# def pagination_results_data(issues)
# issues.map { |issue| issue['iid].to_i }
# end
#
# context 'when sorting by weight' do
# ...
# let_it_be(:issues) { make_some_issues_with_weights }
#
# context 'when ascending' do
# let(:ordered_issues) { issues.sort_by(&:weight) }
#
# it_behaves_like 'sorted paginated query' do
# let(:sort_param) { 'WEIGHT_ASC' }
# let(:sort_param) { :WEIGHT_ASC }
# let(:first_param) { 2 }
# let(:expected_results) { [weight_issue3.iid, weight_issue5.iid, weight_issue1.iid, weight_issue4.iid, weight_issue2.iid] }
# let(:expected_results) { ordered_issues.map(&:iid) }
# end
# end
#
RSpec.shared_examples 'sorted paginated query' do
# Provided as a convenience when constructing queries using string concatenation
let(:page_info) { 'pageInfo { startCursor endCursor }' }
# Convenience for using default implementation of pagination_results_data
let(:node_path) { ['id'] }
it_behaves_like 'requires variables' do
let(:required_variables) { [:sort_param, :first_param, :expected_results, :data_path, :current_user] }
end
describe do
let(:sort_argument) { "sort: #{sort_param}" if sort_param.present? }
let(:first_argument) { "first: #{first_param}" if first_param.present? }
let(:sort_argument) { graphql_args(sort: sort_param) }
let(:params) { sort_argument }
let(:start_cursor) { graphql_data_at(*data_path, :pageInfo, :startCursor) }
let(:end_cursor) { graphql_data_at(*data_path, :pageInfo, :endCursor) }
let(:sorted_edges) { graphql_data_at(*data_path, :edges) }
let(:page_info) { "pageInfo { startCursor endCursor }" }
def pagination_query(params, page_info)
raise('pagination_query(params, page_info) must be defined in the test, see example in comment') unless defined?(super)
# Convenience helper for the large number of queries defined as a projection
# from some root value indexed by full_path to a collection of objects with IID
def nested_internal_id_query(root_field, parent, field, args, selection: :iid)
graphql_query_for(root_field, { full_path: parent.full_path },
query_nodes(field, selection, args: args, include_pagination_info: true)
)
end
def pagination_query(params)
raise('pagination_query(params) must be defined in the test, see example in comment') unless defined?(super)
super
end
def pagination_results_data(data)
raise('pagination_results_data(data) must be defined in the test, see example in comment') unless defined?(super)
def pagination_results_data(nodes)
if defined?(super)
super(nodes)
else
nodes.map { |n| n.dig(*node_path) }
end
end
def results
nodes = graphql_dig_at(graphql_data(fresh_response_data), *data_path, :nodes)
pagination_results_data(nodes)
end
def end_cursor
graphql_dig_at(graphql_data(fresh_response_data), *data_path, :page_info, :end_cursor)
end
super(data)
def start_cursor
graphql_dig_at(graphql_data(fresh_response_data), *data_path, :page_info, :start_cursor)
end
let(:query) { pagination_query(params) }
before do
post_graphql(pagination_query(params, page_info), current_user: current_user)
post_graphql(query, current_user: current_user)
end
context 'when sorting' do
it 'sorts correctly' do
expect(pagination_results_data(sorted_edges)).to eq expected_results
expect(results).to eq expected_results
end
context 'when paginating' do
let(:params) { [sort_argument, first_argument].compact.join(',') }
let(:params) { sort_argument.merge(first: first_param) }
let(:first_page) { expected_results.first(first_param) }
let(:rest) { expected_results.drop(first_param) }
it 'paginates correctly' do
expect(pagination_results_data(sorted_edges)).to eq expected_results.first(first_param)
expect(results).to eq first_page
cursored_query = pagination_query([sort_argument, "after: \"#{end_cursor}\""].compact.join(','), page_info)
post_graphql(cursored_query, current_user: current_user)
fwds = pagination_query(sort_argument.merge(after: end_cursor))
post_graphql(fwds, current_user: current_user)
expect(response).to have_gitlab_http_status(:ok)
expect(results).to eq rest
response_data = graphql_dig_at(Gitlab::Json.parse(response.body), :data, *data_path, :edges)
bwds = pagination_query(sort_argument.merge(before: start_cursor))
post_graphql(bwds, current_user: current_user)
expect(pagination_results_data(response_data)).to eq expected_results.drop(first_param)
expect(results).to eq first_page
end
end
end
......
......@@ -47,18 +47,12 @@ RSpec.shared_examples 'group and project boards query' do
describe 'sorting and pagination' do
let(:data_path) { [board_parent_type, :boards] }
def pagination_query(params, page_info)
graphql_query_for(
board_parent_type,
{ 'fullPath' => board_parent.full_path },
query_graphql_field('boards', params, "#{page_info} edges { node { id } }")
def pagination_query(params)
graphql_query_for(board_parent_type, { full_path: board_parent.full_path },
query_nodes(:boards, :id, include_pagination_info: true, args: params)
)
end
def pagination_results_data(data)
data.map { |board| board.dig('node', 'id') }
end
context 'when using default sorting' do
let!(:board_B) { create(:board, resource_parent: board_parent, name: 'B') }
let!(:board_C) { create(:board, resource_parent: board_parent, name: 'C') }
......@@ -72,9 +66,9 @@ RSpec.shared_examples 'group and project boards query' do
let(:first_param) { 2 }
let(:expected_results) do
if board_parent.multiple_issue_boards_available?
boards.map { |board| board.to_global_id.to_s }
boards.map { |board| global_id_of(board) }
else
[boards.first.to_global_id.to_s]
[global_id_of(boards.first)]
end
end
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment