Commit 37198186 authored by Alex Kalderimis's avatar Alex Kalderimis

Add before test to pagination shared examples

This mainly adds a new test to the pagination shared examples that
verifies that we can paginate backwards as well as forwards.

It takes the opportunity to:

- update the queries use `nodes` rather than `edges.nodes`
- update some of the query generation to be simpler
- add some helper methods and bindings to DRY up this code
parent bf324e09
......@@ -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
```
......@@ -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
......
......@@ -23,13 +23,9 @@ RSpec.describe 'Namespace.projects' do
ns.add_owner(current_user)
end
def pagination_query(params, page_info)
def pagination_query(params)
graphql_query_for(:namespace, ns_args,
query_graphql_field(:projects, params + project_args, "#{page_info} edges { node { name } }"))
end
def pagination_results_data(data)
data.map { |project| project.dig('node', 'name') }
query_nodes(:projects, :name, include_pagination_info: true, args: params + project_args))
end
context 'when sorting by STORAGE' do
......@@ -40,36 +36,11 @@ RSpec.describe 'Namespace.projects' do
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
it_behaves_like 'sorted pagable query' do
let!(:project_5) { create(:project, namespace: ns, name: 'Test 5') }
let!(:project_6) { create(:project, namespace: ns, name: 'Test 6') }
let!(:project_7) { create(:project, namespace: ns, name: 'Test 7') }
let!(:project_8) { create(:project, namespace: ns, name: 'Test 8') }
let(:all_results) { expected_order.map { |p| { 'name' => p.name } } }
let(:sort_value) { :STORAGE }
let(:expected_order) do
# Some jumbled random order, to ensure we aren't testing ID desc or asc
[project_5, project_3, project_7, project_8, project_2, project_6, project_4]
end
before do
expected_order.reverse.each_with_index do |p, i|
n = i + 1
p.statistics.update!(lfs_objects_size: n, repository_size: n.gigabytes)
end
end
def paging_query(params)
graphql_query_for(:namespace, ns_args,
query_graphql_field(:projects, params + project_args, "#{page_info} nodes { name }"))
end
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(: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(: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
......
......@@ -63,18 +63,12 @@ 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') }
end
let(:in_creation_order) do
[test_report_3, test_report_2, test_report_1]
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
......
......@@ -96,20 +96,17 @@ RSpec.describe 'getting projects' do
ns.add_owner(current_user)
end
def pagination_query(params, page_info)
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}
edges { node { name } }
nodes { name }
GQL
end
def pagination_results_data(data)
data.map { |project| project.dig('node', 'name') }
end
context 'when sorting by similarity' do
it_behaves_like 'sorted paginated query' do
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] }
......
......@@ -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
......
......@@ -259,26 +259,16 @@ 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 }
......@@ -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,16 +59,12 @@ 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
......
......@@ -16,32 +16,40 @@
#
# 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
......@@ -49,30 +57,43 @@ RSpec.shared_examples 'sorted paginated query' do
describe do
let(:sort_argument) { graphql_args(sort: sort_param) }
let(:params) { sort_argument }
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)
super(data)
def pagination_results_data(nodes)
if defined?(super)
super(nodes)
else
nodes.map { |n| n.dig(*node_path) }
end
end
def results
edges = graphql_dig_at(graphql_data(fresh_response_data), *data_path, :edges)
pagination_results_data(edges)
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
let(:query) { pagination_query(params, page_info) }
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(query, current_user: current_user)
......@@ -91,58 +112,15 @@ RSpec.shared_examples 'sorted paginated query' do
it 'paginates correctly' do
expect(results).to eq first_page
cursored_query = pagination_query(sort_argument.merge(after: end_cursor), 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(results).to eq rest
end
end
end
end
end
RSpec.shared_examples 'sorted pagable query' do
let(:sort_argument) { graphql_args(sort: sort_value) }
let(:page_info) { "pageInfo { startCursor endCursor }" }
def paging_query(params)
raise('paging_query(params) must be defined in the test, see example in comment') unless defined?(super)
super
end
def nodes
graphql_dig_at(graphql_data(fresh_response_data), *data_path, :nodes)
end
def end_cursor
graphql_dig_at(graphql_data(fresh_response_data), *data_path, :page_info, :end_cursor)
end
context 'when sorting' do
it 'sorts correctly' do
post_graphql(paging_query(sort_argument), current_user: current_user)
expect(nodes).to eq all_results
end
bwds = pagination_query(sort_argument.merge(before: start_cursor))
post_graphql(bwds, current_user: current_user)
it 'has at least 5 items' do
# We need to page a few times - this makes sure we can page at least twice
expect(all_results.size).to be >= 5
end
context 'when paginating' do
let(:page_size) { 2 }
it 'paginates correctly' do
all_results.in_groups_of(page_size, false).reduce(nil) do |cursor, group|
q = paging_query(sort_argument.merge(first: page_size, after: cursor))
post_graphql(q, current_user: current_user)
expect(nodes).to eq(group)
end_cursor
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