2019-03-30 03:23:56 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-07-07 08:49:05 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 14:09:03 -04:00
|
|
|
RSpec.describe EachBatch do
|
2022-06-22 20:09:13 -04:00
|
|
|
let(:model) do
|
|
|
|
Class.new(ActiveRecord::Base) do
|
|
|
|
include EachBatch
|
2017-07-07 08:49:05 -04:00
|
|
|
|
2022-06-22 20:09:13 -04:00
|
|
|
self.table_name = 'users'
|
2021-08-06 08:10:15 -04:00
|
|
|
|
2022-06-22 20:09:13 -04:00
|
|
|
scope :never_signed_in, -> { where(sign_in_count: 0) }
|
2017-07-07 08:49:05 -04:00
|
|
|
end
|
2022-06-22 20:09:13 -04:00
|
|
|
end
|
2017-07-07 08:49:05 -04:00
|
|
|
|
2022-06-22 20:09:13 -04:00
|
|
|
describe '.each_batch' do
|
2017-07-07 08:49:05 -04:00
|
|
|
before do
|
2020-01-09 13:07:52 -05:00
|
|
|
create_list(:user, 5, updated_at: 1.day.ago)
|
2017-07-07 08:49:05 -04:00
|
|
|
end
|
|
|
|
|
2018-11-04 08:59:13 -05:00
|
|
|
shared_examples 'each_batch handling' do |kwargs|
|
|
|
|
it 'yields an ActiveRecord::Relation when a block is given' do
|
2020-10-08 14:08:32 -04:00
|
|
|
model.each_batch(**kwargs) do |relation|
|
2018-11-04 08:59:13 -05:00
|
|
|
expect(relation).to be_a_kind_of(ActiveRecord::Relation)
|
|
|
|
end
|
2017-07-07 08:49:05 -04:00
|
|
|
end
|
|
|
|
|
2018-11-04 08:59:13 -05:00
|
|
|
it 'yields a batch index as the second argument' do
|
2020-10-08 14:08:32 -04:00
|
|
|
model.each_batch(**kwargs) do |_, index|
|
2018-11-04 08:59:13 -05:00
|
|
|
expect(index).to eq(1)
|
|
|
|
end
|
2017-07-07 08:49:05 -04:00
|
|
|
end
|
|
|
|
|
2018-11-04 08:59:13 -05:00
|
|
|
it 'accepts a custom batch size' do
|
|
|
|
amount = 0
|
2017-07-07 08:49:05 -04:00
|
|
|
|
2020-10-08 14:08:32 -04:00
|
|
|
model.each_batch(**kwargs.merge({ of: 1 })) { amount += 1 }
|
2017-07-07 08:49:05 -04:00
|
|
|
|
2018-11-04 08:59:13 -05:00
|
|
|
expect(amount).to eq(5)
|
|
|
|
end
|
2017-07-07 08:49:05 -04:00
|
|
|
|
2018-11-04 08:59:13 -05:00
|
|
|
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-07-07 08:49:05 -04:00
|
|
|
end
|
|
|
|
|
2018-11-04 08:59:13 -05:00
|
|
|
it 'allows updating of the yielded relations' do
|
2020-05-22 05:08:09 -04:00
|
|
|
time = Time.current
|
2017-07-07 08:49:05 -04:00
|
|
|
|
2018-11-04 08:59:13 -05:00
|
|
|
model.each_batch do |relation|
|
|
|
|
relation.update_all(updated_at: time)
|
|
|
|
end
|
2017-07-07 08:49:05 -04:00
|
|
|
|
2018-11-04 08:59:13 -05:00
|
|
|
expect(model.where(updated_at: time).count).to eq(5)
|
|
|
|
end
|
2017-07-07 08:49:05 -04:00
|
|
|
end
|
2018-11-04 08:59:13 -05:00
|
|
|
|
|
|
|
it_behaves_like 'each_batch handling', {}
|
|
|
|
it_behaves_like 'each_batch handling', { order_hint: :updated_at }
|
2020-12-22 07:10:09 -05:00
|
|
|
|
|
|
|
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-08-06 08:10:15 -04:00
|
|
|
|
|
|
|
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-07-07 08:49:05 -04:00
|
|
|
end
|
2022-06-22 20:09:13 -04:00
|
|
|
|
|
|
|
describe '.distinct_each_batch' do
|
|
|
|
let_it_be(:users) { create_list(:user, 5, sign_in_count: 0) }
|
|
|
|
|
|
|
|
let(:params) { {} }
|
|
|
|
|
|
|
|
subject(:values) do
|
|
|
|
values = []
|
|
|
|
|
|
|
|
model.distinct_each_batch(**params) { |rel| values.concat(rel.pluck(params[:column])) }
|
|
|
|
values
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when iterating over a unique column' do
|
|
|
|
context 'when using ascending order' do
|
|
|
|
let(:expected_values) { users.pluck(:id).sort }
|
|
|
|
let(:params) { { column: :id, of: 1, order: :asc } }
|
|
|
|
|
|
|
|
it { is_expected.to eq(expected_values) }
|
|
|
|
|
|
|
|
context 'when using larger batch size' do
|
|
|
|
before do
|
|
|
|
params[:of] = 3
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to eq(expected_values) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when using larger batch size than the result size' do
|
|
|
|
before do
|
|
|
|
params[:of] = 100
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to eq(expected_values) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when using descending order' do
|
|
|
|
let(:expected_values) { users.pluck(:id).sort.reverse }
|
|
|
|
let(:params) { { column: :id, of: 1, order: :desc } }
|
|
|
|
|
|
|
|
it { is_expected.to eq(expected_values) }
|
|
|
|
|
|
|
|
context 'when using larger batch size' do
|
|
|
|
before do
|
|
|
|
params[:of] = 3
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to eq(expected_values) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when iterating over a non-unique column' do
|
|
|
|
let(:params) { { column: :sign_in_count, of: 2, order: :asc } }
|
|
|
|
|
|
|
|
context 'when only one value is present' do
|
|
|
|
it { is_expected.to eq([0]) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when duplicated values present' do
|
|
|
|
let(:expected_values) { [2, 5] }
|
|
|
|
|
|
|
|
before do
|
|
|
|
users[0].reload.update!(sign_in_count: 5)
|
|
|
|
users[1].reload.update!(sign_in_count: 2)
|
|
|
|
users[2].reload.update!(sign_in_count: 5)
|
|
|
|
users[3].reload.update!(sign_in_count: 2)
|
|
|
|
users[4].reload.update!(sign_in_count: 5)
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to eq(expected_values) }
|
|
|
|
|
|
|
|
context 'when using descending order' do
|
|
|
|
let(:expected_values) { [5, 2] }
|
|
|
|
|
|
|
|
before do
|
|
|
|
params[:order] = :desc
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to eq(expected_values) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-07-07 08:49:05 -04:00
|
|
|
end
|