debian-mirror-gitlab/spec/models/concerns/each_batch_spec.rb

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

90 lines
2.2 KiB
Ruby
Raw Normal View History

2019-07-07 11:18:12 +05:30
# frozen_string_literal: true
2017-09-10 17:25:29 +05:30
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe EachBatch do
2017-09-10 17:25:29 +05:30
describe '.each_batch' do
let(:model) do
Class.new(ActiveRecord::Base) do
include EachBatch
self.table_name = 'users'
2021-10-27 15:23:28 +05:30
scope :never_signed_in, -> { where(sign_in_count: 0) }
2017-09-10 17:25:29 +05:30
end
end
before do
2020-03-13 15:44:24 +05:30
create_list(:user, 5, updated_at: 1.day.ago)
2017-09-10 17:25:29 +05:30
end
2018-12-13 13:39:08 +05:30
shared_examples 'each_batch handling' do |kwargs|
it 'yields an ActiveRecord::Relation when a block is given' do
2021-01-03 14:25:43 +05:30
model.each_batch(**kwargs) do |relation|
2018-12-13 13:39:08 +05:30
expect(relation).to be_a_kind_of(ActiveRecord::Relation)
end
2017-09-10 17:25:29 +05:30
end
2018-12-13 13:39:08 +05:30
it 'yields a batch index as the second argument' do
2021-01-03 14:25:43 +05:30
model.each_batch(**kwargs) do |_, index|
2018-12-13 13:39:08 +05:30
expect(index).to eq(1)
end
2017-09-10 17:25:29 +05:30
end
2018-12-13 13:39:08 +05:30
it 'accepts a custom batch size' do
amount = 0
2017-09-10 17:25:29 +05:30
2021-01-03 14:25:43 +05:30
model.each_batch(**kwargs.merge({ of: 1 })) { amount += 1 }
2017-09-10 17:25:29 +05:30
2018-12-13 13:39:08 +05:30
expect(amount).to eq(5)
end
2017-09-10 17:25:29 +05:30
2018-12-13 13:39:08 +05:30
it 'does not include ORDER BYs in the yielded relations' do
model.each_batch do |relation|
expect(relation.to_sql).not_to include('ORDER BY')
end
2017-09-10 17:25:29 +05:30
end
2018-12-13 13:39:08 +05:30
it 'allows updating of the yielded relations' do
2020-06-23 00:09:42 +05:30
time = Time.current
2017-09-10 17:25:29 +05:30
2018-12-13 13:39:08 +05:30
model.each_batch do |relation|
relation.update_all(updated_at: time)
end
2017-09-10 17:25:29 +05:30
2018-12-13 13:39:08 +05:30
expect(model.where(updated_at: time).count).to eq(5)
end
2017-09-10 17:25:29 +05:30
end
2018-12-13 13:39:08 +05:30
it_behaves_like 'each_batch handling', {}
it_behaves_like 'each_batch handling', { order_hint: :updated_at }
2021-03-08 18:12:59 +05:30
it 'orders ascending by default' do
ids = []
model.each_batch(of: 1) { |rel| ids.concat(rel.ids) }
expect(ids).to eq(ids.sort)
end
it 'accepts descending order' do
ids = []
model.each_batch(of: 1, order: :desc) { |rel| ids.concat(rel.ids) }
expect(ids).to eq(ids.sort.reverse)
end
2021-10-27 15:23:28 +05:30
describe 'current scope' do
let(:entry) { create(:user, sign_in_count: 1) }
let(:ids_with_new_relation) { model.where(id: entry.id).pluck(:id) }
it 'does not leak current scope to block being executed' do
model.never_signed_in.each_batch(of: 5) do |relation|
expect(ids_with_new_relation).to include(entry.id)
end
end
end
2017-09-10 17:25:29 +05:30
end
end