|
| 1 | +require 'spec_helper' |
| 2 | + |
| 3 | +describe 'Bulk Insertion' do |
| 4 | + before(:all) do |
| 5 | + skip 'Bulk mode is not supported on databases without insert conflict target support' unless ActiveRecord::Base.connection.supports_insert_conflict_target? |
| 6 | + end |
| 7 | + it 'uses upsert_all when bulk option is true' do |
| 8 | + expect(BulkSeededModel).to receive(:upsert_all).with( |
| 9 | + contain_exactly( |
| 10 | + hash_including('title' => 'Bulk 1', 'login' => 'bulk1'), |
| 11 | + hash_including('title' => 'Bulk 2', 'login' => 'bulk2') |
| 12 | + ), |
| 13 | + hash_including(unique_by: [:title]) |
| 14 | + ).and_call_original |
| 15 | + |
| 16 | + SeedDo.seed("#{File.dirname(__FILE__)}/fixtures", /bulk_insert/, bulk: true) |
| 17 | + |
| 18 | + expect(BulkSeededModel.count).to eq(2) |
| 19 | + item1 = BulkSeededModel.find_by(title: 'Bulk 1') |
| 20 | + expect(item1.login).to eq 'bulk1' |
| 21 | + item2 = BulkSeededModel.find_by(title: 'Bulk 2') |
| 22 | + expect(item2.login).to eq 'bulk2' |
| 23 | + end |
| 24 | + |
| 25 | + it 'uses upsert_all with skip option for seed_once in bulk mode' do |
| 26 | + # Pre-create record |
| 27 | + BulkSeededModel.create!(title: 'Existing', login: 'original') |
| 28 | + |
| 29 | + expect(BulkSeededModel).to receive(:upsert_all).with( |
| 30 | + contain_exactly( |
| 31 | + hash_including('title' => 'Existing', 'login' => 'new'), |
| 32 | + hash_including('title' => 'New', 'login' => 'created') |
| 33 | + ), |
| 34 | + hash_including(unique_by: [:title], on_duplicate: :skip) |
| 35 | + ).and_call_original |
| 36 | + |
| 37 | + SeedDo.seed("#{File.dirname(__FILE__)}/fixtures", /bulk_seed_once/, bulk: true) |
| 38 | + |
| 39 | + expect(BulkSeededModel.count).to eq(2) |
| 40 | + existing = BulkSeededModel.find_by(title: 'Existing') |
| 41 | + expect(existing.login).to eq 'original' # Should NOT change |
| 42 | + new_rec = BulkSeededModel.find_by(title: 'New') |
| 43 | + expect(new_rec.login).to eq 'created' |
| 44 | + end |
| 45 | + |
| 46 | + it 'respects batch_size option when specified' do |
| 47 | + # With batch_size: 10, 25 records should result in 3 upsert_all calls (10, 10, 5) |
| 48 | + call_count = 0 |
| 49 | + allow(BulkSeededModel).to receive(:upsert_all).and_wrap_original do |method, data, **kwargs| |
| 50 | + call_count += 1 |
| 51 | + method.call(data, **kwargs) |
| 52 | + end |
| 53 | + |
| 54 | + SeedDo.seed("#{File.dirname(__FILE__)}/fixtures", /bulk_large/, bulk: { batch_size: 10 }) |
| 55 | + |
| 56 | + expect(call_count).to eq(3) |
| 57 | + expect(BulkSeededModel.count).to eq(25) |
| 58 | + |
| 59 | + # Verify all records were created |
| 60 | + (1..25).each do |i| |
| 61 | + record = BulkSeededModel.find_by(title: "Bulk #{i}") |
| 62 | + expect(record).to be_present |
| 63 | + expect(record.login).to eq "bulk#{i}" |
| 64 | + end |
| 65 | + end |
| 66 | + |
| 67 | + it 'uses default batch_size of 1000 when bulk: true' do |
| 68 | + call_count = 0 |
| 69 | + allow(BulkSeededModel).to receive(:upsert_all).and_wrap_original do |method, data, **kwargs| |
| 70 | + call_count += 1 |
| 71 | + method.call(data, **kwargs) |
| 72 | + end |
| 73 | + |
| 74 | + SeedDo.seed("#{File.dirname(__FILE__)}/fixtures", /bulk_insert/, bulk: true) |
| 75 | + |
| 76 | + # With default batch_size of 1000, 2 records should result in 1 upsert_all call |
| 77 | + expect(call_count).to eq(1) |
| 78 | + expect(BulkSeededModel.count).to eq(2) |
| 79 | + end |
| 80 | + |
| 81 | + it 'splits large dataset across multiple batches with custom batch_size' do |
| 82 | + call_count = 0 |
| 83 | + batch_sizes = [] |
| 84 | + allow(BulkSeededModel).to receive(:upsert_all).and_wrap_original do |method, data, **options| |
| 85 | + call_count += 1 |
| 86 | + batch_sizes << data.size |
| 87 | + method.call(data, **options) |
| 88 | + end |
| 89 | + |
| 90 | + SeedDo.seed("#{File.dirname(__FILE__)}/fixtures", /bulk_large/, bulk: { batch_size: 7 }) |
| 91 | + |
| 92 | + # With batch_size: 7, 25 records should result in 4 calls: [7, 7, 7, 4] |
| 93 | + expect(call_count).to eq(4) |
| 94 | + expect(batch_sizes).to eq([7, 7, 7, 4]) |
| 95 | + expect(BulkSeededModel.count).to eq(25) |
| 96 | + end |
| 97 | +end |
0 commit comments