diff options
author | Sean McGivern <sean@mcgivern.me.uk> | 2017-07-07 20:14:42 +0300 |
---|---|---|
committer | Sean McGivern <sean@mcgivern.me.uk> | 2017-07-07 20:14:42 +0300 |
commit | 7c35ecf7e44e5b44643b41719a67e89f99f10053 (patch) | |
tree | b1b3d7ff37b4ef95d0f3724f65f6f30228845f08 /spec/models/concerns | |
parent | 5550cba09272c6406845b2972064e0aba1d51884 (diff) | |
parent | ff78af152cc3054a7bb76af718943dca7a69a3c5 (diff) |
Merge branch 'active-record-each-batch' into 'master'
Added EachBatch for iterating tables in batches
See merge request !12707
Diffstat (limited to 'spec/models/concerns')
-rw-r--r-- | spec/models/concerns/each_batch_spec.rb | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/spec/models/concerns/each_batch_spec.rb b/spec/models/concerns/each_batch_spec.rb new file mode 100644 index 00000000000..951690a217b --- /dev/null +++ b/spec/models/concerns/each_batch_spec.rb @@ -0,0 +1,53 @@ +require 'spec_helper' + +describe EachBatch do + describe '.each_batch' do + let(:model) do + Class.new(ActiveRecord::Base) do + include EachBatch + + self.table_name = 'users' + end + end + + before do + 5.times { create(:user, updated_at: 1.day.ago) } + end + + it 'yields an ActiveRecord::Relation when a block is given' do + model.each_batch do |relation| + expect(relation).to be_a_kind_of(ActiveRecord::Relation) + end + end + + it 'yields a batch index as the second argument' do + model.each_batch do |_, index| + expect(index).to eq(1) + end + end + + it 'accepts a custom batch size' do + amount = 0 + + model.each_batch(of: 1) { amount += 1 } + + expect(amount).to eq(5) + end + + 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 + end + + it 'allows updating of the yielded relations' do + time = Time.now + + model.each_batch do |relation| + relation.update_all(updated_at: time) + end + + expect(model.where(updated_at: time).count).to eq(5) + end + end +end |