debian-mirror-gitlab/spec/support/shared_examples/graphql/sorted_paginated_query_shared_examples.rb

129 lines
4.3 KiB
Ruby
Raw Normal View History

2020-05-24 23:13:21 +05:30
# frozen_string_literal: true
# Use this for testing how a GraphQL query handles sorting and pagination.
# This is particularly important when using keyset pagination connection,
# which is the default for ActiveRecord relations, as certain sort keys
# might not be supportable.
#
# sort_param: the value to specify the sort
# data_path: the keys necessary to dig into the return GraphQL data to get the
# returned results
# first_param: number of items expected (like a page size)
# expected_results: array of comparison data of all items sorted correctly
# pagination_query: method that specifies the GraphQL query
# pagination_results_data: method that extracts the sorted data used to compare against
# the expected results
#
# Example:
# describe 'sorting and pagination' do
2021-02-22 17:27:13 +05:30
# let_it_be(:sort_project) { create(:project, :public) }
2020-05-24 23:13:21 +05:30
# let(:data_path) { [:project, :issues] }
#
2021-02-22 17:27:13 +05:30
# def pagination_query(arguments)
# graphql_query_for(:project, { full_path: sort_project.full_path },
# query_nodes(:issues, :iid, include_pagination_info: true, args: arguments)
2020-05-24 23:13:21 +05:30
# )
# end
#
2021-02-22 17:27:13 +05:30
# # 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 }
2020-05-24 23:13:21 +05:30
# end
#
# context 'when sorting by weight' do
2021-02-22 17:27:13 +05:30
# let_it_be(:issues) { make_some_issues_with_weights }
#
2020-05-24 23:13:21 +05:30
# context 'when ascending' do
2021-02-22 17:27:13 +05:30
# let(:ordered_issues) { issues.sort_by(&:weight) }
#
2020-05-24 23:13:21 +05:30
# it_behaves_like 'sorted paginated query' do
2021-02-22 17:27:13 +05:30
# let(:sort_param) { :WEIGHT_ASC }
2020-05-24 23:13:21 +05:30
# let(:first_param) { 2 }
2021-02-22 17:27:13 +05:30
# let(:expected_results) { ordered_issues.map(&:iid) }
2020-05-24 23:13:21 +05:30
# end
# end
#
RSpec.shared_examples 'sorted paginated query' do
2021-02-22 17:27:13 +05:30
# 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'] }
2020-05-24 23:13:21 +05:30
it_behaves_like 'requires variables' do
let(:required_variables) { [:sort_param, :first_param, :expected_results, :data_path, :current_user] }
end
describe do
2021-02-22 17:27:13 +05:30
let(:sort_argument) { graphql_args(sort: sort_param) }
2020-06-23 00:09:42 +05:30
let(:params) { sort_argument }
2020-05-24 23:13:21 +05:30
2021-02-22 17:27:13 +05:30
# 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)
2020-05-24 23:13:21 +05:30
super
end
2021-02-22 17:27:13 +05:30
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
2020-05-24 23:13:21 +05:30
2021-02-22 17:27:13 +05:30
def start_cursor
graphql_dig_at(graphql_data(fresh_response_data), *data_path, :page_info, :start_cursor)
2020-05-24 23:13:21 +05:30
end
2021-02-22 17:27:13 +05:30
let(:query) { pagination_query(params) }
2020-05-24 23:13:21 +05:30
before do
2021-02-22 17:27:13 +05:30
post_graphql(query, current_user: current_user)
2020-05-24 23:13:21 +05:30
end
context 'when sorting' do
it 'sorts correctly' do
2021-02-22 17:27:13 +05:30
expect(results).to eq expected_results
2020-05-24 23:13:21 +05:30
end
context 'when paginating' do
2021-02-22 17:27:13 +05:30
let(:params) { sort_argument.merge(first: first_param) }
let(:first_page) { expected_results.first(first_param) }
let(:rest) { expected_results.drop(first_param) }
2020-05-24 23:13:21 +05:30
it 'paginates correctly' do
2021-02-22 17:27:13 +05:30
expect(results).to eq first_page
2020-05-24 23:13:21 +05:30
2021-02-22 17:27:13 +05:30
fwds = pagination_query(sort_argument.merge(after: end_cursor))
post_graphql(fwds, current_user: current_user)
2020-11-24 15:15:51 +05:30
2021-02-22 17:27:13 +05:30
expect(results).to eq rest
2020-11-24 15:15:51 +05:30
2021-02-22 17:27:13 +05:30
bwds = pagination_query(sort_argument.merge(before: start_cursor))
post_graphql(bwds, current_user: current_user)
2020-05-24 23:13:21 +05:30
2021-02-22 17:27:13 +05:30
expect(results).to eq first_page
2020-05-24 23:13:21 +05:30
end
end
end
end
end