debian-mirror-gitlab/spec/lib/gitlab/pagination/keyset/iterator_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

171 lines
6.4 KiB
Ruby
Raw Normal View History

2021-06-08 01:23:25 +05:30
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::Pagination::Keyset::Iterator do
let_it_be(:project) { create(:project) }
let_it_be(:issue_list_with_same_pos) { create_list(:issue, 3, project: project, relative_position: 100, updated_at: 1.day.ago) }
let_it_be(:issue_list_with_null_pos) { create_list(:issue, 3, project: project, relative_position: nil, updated_at: 1.day.ago) }
let_it_be(:issue_list_with_asc_pos) { create_list(:issue, 3, :with_asc_relative_position, project: project, updated_at: 1.day.ago) }
let(:klass) { Issue }
let(:column) { 'relative_position' }
let(:direction) { :asc }
let(:reverse_direction) { ::Gitlab::Pagination::Keyset::ColumnOrderDefinition::REVERSED_ORDER_DIRECTIONS[direction] }
let(:nulls_position) { :nulls_last }
let(:reverse_nulls_position) { ::Gitlab::Pagination::Keyset::ColumnOrderDefinition::REVERSED_NULL_POSITIONS[nulls_position] }
let(:custom_reorder) do
Gitlab::Pagination::Keyset::Order.build([
Gitlab::Pagination::Keyset::ColumnOrderDefinition.new(
attribute_name: column,
2021-09-30 23:02:18 +05:30
column_expression: klass.arel_table[column],
2022-06-21 17:19:12 +05:30
order_expression: klass.arel_table[column].public_send(direction).public_send(nulls_position), # rubocop:disable GitlabSecurity/PublicSend
reversed_order_expression: klass.arel_table[column].public_send(reverse_direction).public_send(reverse_nulls_position), # rubocop:disable GitlabSecurity/PublicSend
2021-09-30 23:02:18 +05:30
order_direction: direction,
nullable: nulls_position,
distinct: false
2021-06-08 01:23:25 +05:30
),
Gitlab::Pagination::Keyset::ColumnOrderDefinition.new(
attribute_name: 'id',
2021-09-30 23:02:18 +05:30
order_expression: klass.arel_table[:id].send(direction)
2021-06-08 01:23:25 +05:30
)
])
end
2021-11-18 22:05:49 +05:30
let(:iterator_params) { nil }
2021-06-08 01:23:25 +05:30
let(:scope) { project.issues.reorder(custom_reorder) }
2021-11-18 22:05:49 +05:30
subject(:iterator) { described_class.new(**iterator_params) }
2021-09-30 23:02:18 +05:30
shared_examples 'iterator examples' do
describe '.each_batch' do
it 'yields an ActiveRecord::Relation when a block is given' do
iterator.each_batch(of: 1) do |relation|
expect(relation).to be_a_kind_of(ActiveRecord::Relation)
end
end
2021-06-08 01:23:25 +05:30
2021-09-30 23:02:18 +05:30
it 'raises error when ordering configuration cannot be automatically determined' do
expect do
described_class.new(scope: MergeRequestDiffCommit.order(:merge_request_diff_id, :relative_order))
end.to raise_error /The order on the scope does not support keyset pagination/
2021-06-08 01:23:25 +05:30
end
2021-09-30 23:02:18 +05:30
it 'accepts a custom batch size' do
count = 0
2021-06-08 01:23:25 +05:30
2021-09-30 23:02:18 +05:30
iterator.each_batch(of: 2) { |relation| count += relation.count(:all) }
2021-06-08 01:23:25 +05:30
2021-09-30 23:02:18 +05:30
expect(count).to eq(9)
end
2021-06-08 01:23:25 +05:30
2021-11-18 22:05:49 +05:30
it 'continues after the cursor' do
loaded_records = []
cursor = nil
# stopping the iterator after the first batch and storing the cursor
iterator.each_batch(of: 2) do |relation| # rubocop: disable Lint/UnreachableLoop
loaded_records.concat(relation.to_a)
record = loaded_records.last
cursor = custom_reorder.cursor_attributes_for_node(record)
break
end
expect(loaded_records).to eq(project.issues.order(custom_reorder).take(2))
new_iterator = described_class.new(**iterator_params.merge(cursor: cursor))
new_iterator.each_batch(of: 2) do |relation|
loaded_records.concat(relation.to_a)
end
expect(loaded_records).to eq(project.issues.order(custom_reorder))
end
2021-09-30 23:02:18 +05:30
it 'allows updating of the yielded relations' do
time = Time.current
2021-06-08 01:23:25 +05:30
2021-09-30 23:02:18 +05:30
iterator.each_batch(of: 2) do |relation|
Issue.connection.execute("UPDATE issues SET updated_at = '#{time.to_s(:inspect)}' WHERE id IN (#{relation.reselect(:id).to_sql})")
end
2021-06-08 01:23:25 +05:30
2021-09-30 23:02:18 +05:30
expect(Issue.pluck(:updated_at)).to all(be_within(5.seconds).of(time))
end
2021-06-08 01:23:25 +05:30
2021-09-30 23:02:18 +05:30
context 'with ordering direction' do
context 'when ordering asc' do
it 'orders ascending by default, including secondary order column' do
positions = []
2021-06-08 01:23:25 +05:30
2021-09-30 23:02:18 +05:30
iterator.each_batch(of: 2) { |rel| positions.concat(rel.pluck(:relative_position, :id)) }
2021-06-08 01:23:25 +05:30
2022-06-21 17:19:12 +05:30
expect(positions).to eq(project.issues.reorder(Issue.arel_table[:relative_position].asc.nulls_last).order(id: :asc).pluck(:relative_position, :id))
2021-09-30 23:02:18 +05:30
end
2021-06-08 01:23:25 +05:30
end
2021-09-30 23:02:18 +05:30
context 'when reversing asc order' do
let(:scope) { project.issues.order(custom_reorder.reversed_order) }
2021-06-08 01:23:25 +05:30
2021-09-30 23:02:18 +05:30
it 'orders in reverse of ascending' do
positions = []
2021-06-08 01:23:25 +05:30
2021-09-30 23:02:18 +05:30
iterator.each_batch(of: 2) { |rel| positions.concat(rel.pluck(:relative_position, :id)) }
2021-06-08 01:23:25 +05:30
2022-06-21 17:19:12 +05:30
expect(positions).to eq(project.issues.reorder(Issue.arel_table[:relative_position].desc.nulls_first).order(id: :desc).pluck(:relative_position, :id))
2021-09-30 23:02:18 +05:30
end
2021-06-08 01:23:25 +05:30
end
2021-09-30 23:02:18 +05:30
context 'when asc order, with nulls first' do
let(:nulls_position) { :nulls_first }
2021-06-08 01:23:25 +05:30
2021-09-30 23:02:18 +05:30
it 'orders ascending with nulls first' do
positions = []
2021-06-08 01:23:25 +05:30
2021-09-30 23:02:18 +05:30
iterator.each_batch(of: 2) { |rel| positions.concat(rel.pluck(:relative_position, :id)) }
2021-06-08 01:23:25 +05:30
2022-06-21 17:19:12 +05:30
expect(positions).to eq(project.issues.reorder(Issue.arel_table[:relative_position].asc.nulls_first).order(id: :asc).pluck(:relative_position, :id))
2021-09-30 23:02:18 +05:30
end
2021-06-08 01:23:25 +05:30
end
2021-09-30 23:02:18 +05:30
context 'when ordering desc' do
let(:direction) { :desc }
let(:nulls_position) { :nulls_last }
2021-06-08 01:23:25 +05:30
2021-09-30 23:02:18 +05:30
it 'orders descending' do
positions = []
2021-06-08 01:23:25 +05:30
2021-09-30 23:02:18 +05:30
iterator.each_batch(of: 2) { |rel| positions.concat(rel.pluck(:relative_position, :id)) }
2021-06-08 01:23:25 +05:30
2022-06-21 17:19:12 +05:30
expect(positions).to eq(project.issues.reorder(Issue.arel_table[:relative_position].desc.nulls_last).order(id: :desc).pluck(:relative_position, :id))
2021-09-30 23:02:18 +05:30
end
2021-06-08 01:23:25 +05:30
end
2021-09-30 23:02:18 +05:30
context 'when ordering by columns are repeated twice' do
let(:direction) { :desc }
let(:column) { :id }
2021-06-08 01:23:25 +05:30
2021-09-30 23:02:18 +05:30
it 'orders descending' do
positions = []
2021-06-08 01:23:25 +05:30
2021-09-30 23:02:18 +05:30
iterator.each_batch(of: 2) { |rel| positions.concat(rel.pluck(:id)) }
2021-06-08 01:23:25 +05:30
2021-09-30 23:02:18 +05:30
expect(positions).to eq(project.issues.reorder(id: :desc).pluck(:id))
end
2021-06-08 01:23:25 +05:30
end
end
end
end
2021-09-30 23:02:18 +05:30
context 'when use_union_optimization is used' do
2021-11-18 22:05:49 +05:30
let(:iterator_params) { { scope: scope, use_union_optimization: true } }
2021-09-30 23:02:18 +05:30
include_examples 'iterator examples'
end
context 'when use_union_optimization is not used' do
2021-11-18 22:05:49 +05:30
let(:iterator_params) { { scope: scope, use_union_optimization: false } }
2021-09-30 23:02:18 +05:30
include_examples 'iterator examples'
end
2021-06-08 01:23:25 +05:30
end