row_worker_spec.rb 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe Import::RowWorker do
  4. subject { described_class.new }
  5. let(:row) { Fabricate(:bulk_import_row, bulk_import: import) }
  6. describe '#perform' do
  7. before do
  8. allow(BulkImportRowService).to receive(:new).and_return(service_double)
  9. end
  10. shared_examples 'clean failure' do
  11. let(:service_double) { instance_double(BulkImportRowService, call: false) }
  12. it 'calls BulkImportRowService' do
  13. subject.perform(row.id)
  14. expect(service_double).to have_received(:call).with(row)
  15. end
  16. it 'increases the number of processed items' do
  17. expect { subject.perform(row.id) }.to(change { import.reload.processed_items }.by(+1))
  18. end
  19. it 'does not increase the number of imported items' do
  20. expect { subject.perform(row.id) }.to_not(change { import.reload.imported_items })
  21. end
  22. it 'does not delete the row' do
  23. subject.perform(row.id)
  24. expect(BulkImportRow.exists?(row.id)).to be true
  25. end
  26. end
  27. shared_examples 'unclean failure' do
  28. let(:service_double) { instance_double(BulkImportRowService) }
  29. before do
  30. allow(service_double).to receive(:call) do
  31. raise 'dummy error'
  32. end
  33. end
  34. it 'raises an error and does not change processed items count' do
  35. expect { subject.perform(row.id) }.to raise_error(StandardError, 'dummy error').and(not_change { import.reload.processed_items })
  36. end
  37. it 'does not delete the row' do
  38. expect { subject.perform(row.id) }.to raise_error(StandardError, 'dummy error').and(not_change { BulkImportRow.exists?(row.id) })
  39. end
  40. end
  41. shared_examples 'clean success' do
  42. let(:service_double) { instance_double(BulkImportRowService, call: true) }
  43. it 'calls BulkImportRowService' do
  44. subject.perform(row.id)
  45. expect(service_double).to have_received(:call).with(row)
  46. end
  47. it 'increases the number of processed items' do
  48. expect { subject.perform(row.id) }.to(change { import.reload.processed_items }.by(+1))
  49. end
  50. it 'increases the number of imported items' do
  51. expect { subject.perform(row.id) }.to(change { import.reload.imported_items }.by(+1))
  52. end
  53. it 'deletes the row' do
  54. expect { subject.perform(row.id) }.to change { BulkImportRow.exists?(row.id) }.from(true).to(false)
  55. end
  56. end
  57. context 'when there are multiple rows to process' do
  58. let(:import) { Fabricate(:bulk_import, total_items: 2, processed_items: 0, imported_items: 0, state: :in_progress) }
  59. context 'with a clean failure' do
  60. include_examples 'clean failure'
  61. it 'does not mark the import as finished' do
  62. expect { subject.perform(row.id) }.to_not(change { import.reload.state.to_sym })
  63. end
  64. end
  65. context 'with an unclean failure' do
  66. include_examples 'unclean failure'
  67. it 'does not mark the import as finished' do
  68. expect { subject.perform(row.id) }.to raise_error(StandardError).and(not_change { import.reload.state.to_sym })
  69. end
  70. end
  71. context 'with a clean success' do
  72. include_examples 'clean success'
  73. it 'does not mark the import as finished' do
  74. expect { subject.perform(row.id) }.to_not(change { import.reload.state.to_sym })
  75. end
  76. end
  77. end
  78. context 'when this is the last row to process' do
  79. let(:import) { Fabricate(:bulk_import, total_items: 2, processed_items: 1, imported_items: 0, state: :in_progress) }
  80. context 'with a clean failure' do
  81. include_examples 'clean failure'
  82. it 'marks the import as finished' do
  83. expect { subject.perform(row.id) }.to change { import.reload.state.to_sym }.from(:in_progress).to(:finished)
  84. end
  85. end
  86. # NOTE: sidekiq retry logic may be a bit too difficult to test, so leaving this blind spot for now
  87. it_behaves_like 'unclean failure'
  88. context 'with a clean success' do
  89. include_examples 'clean success'
  90. it 'marks the import as finished' do
  91. expect { subject.perform(row.id) }.to change { import.reload.state.to_sym }.from(:in_progress).to(:finished)
  92. end
  93. end
  94. end
  95. end
  96. end