|
1 | 1 | # frozen_string_literal: true
|
2 | 2 |
|
3 |
| -require 'spec_helper' |
4 |
| - |
5 | 3 | describe BulkZombieUrlUploader do
|
6 |
| - let(:valid_file_path) { 'spec/fixtures/files/valid_zombie_urls.csv' } |
7 |
| - let(:invalid_file_path) { 'spec/fixtures/files/invalid_zombie_urls.csv' } |
8 |
| - let(:filename) { 'valid_zombie_urls.csv' } |
9 |
| - let(:uploader) { described_class.new(filename, valid_file_path) } |
10 |
| - let(:results) { instance_spy(BulkZombieUrls::Results) } |
11 |
| - let(:logger) { instance_double(Logger, error: nil, info: nil, debug: nil) } |
| 4 | + let(:filename) { 'test_file.csv' } |
| 5 | + let(:filepath) { '/path/to/test_file.csv' } |
| 6 | + let(:uploader) { described_class.new(filename, filepath) } |
| 7 | + let(:results) { instance_double(BulkZombieUrls::Results) } |
| 8 | + let(:csv_content) do |
| 9 | + <<~CSV |
| 10 | + URL,DOC_ID |
| 11 | + http://example.com,doc1 |
| 12 | + ,doc2 |
| 13 | + http://missingdoc.com, |
| 14 | + CSV |
| 15 | + end |
12 | 16 |
|
13 | 17 | before do
|
| 18 | + allow(File).to receive(:read).with(filepath).and_return(csv_content) |
14 | 19 | allow(BulkZombieUrls::Results).to receive(:new).and_return(results)
|
15 | 20 | allow(results).to receive(:add_error)
|
16 | 21 | allow(results).to receive(:delete_ok)
|
17 | 22 | allow(results).to receive(:increment_updated)
|
18 |
| - allow(File).to receive(:read).with(valid_file_path).and_return("URL,DOC_ID\nhttp://example.com,123\n") |
19 |
| - allow(File).to receive(:read).with(invalid_file_path).and_raise(CSV::MalformedCSVError.new('Malformed CSV', 1)) |
20 |
| - allow(SearchgovUrl).to receive(:find_by).and_return(instance_double('SearchgovUrl', destroy: true)) |
21 |
| - allow(I14yDocument).to receive(:delete).and_return(true) |
22 |
| - allow(Rails).to receive(:logger).and_return(logger) |
23 |
| - uploader.instance_variable_set(:@results, results) |
| 23 | + uploader.instance_variable_set(:@results, results) # Ensure `@results` is initialized |
| 24 | + end |
| 25 | + |
| 26 | + describe '#initialize' do |
| 27 | + it 'assigns filename and filepath' do |
| 28 | + expect(uploader.instance_variable_get(:@file_name)).to eq(filename) |
| 29 | + expect(uploader.instance_variable_get(:@file_path)).to eq(filepath) |
| 30 | + end |
24 | 31 | end
|
25 | 32 |
|
26 |
| - describe '#upload' do |
27 |
| - context 'with a valid CSV' do |
28 |
| - it 'processes valid CSV rows successfully' do |
29 |
| - uploader.upload |
30 |
| - expect(results).to have_received(:delete_ok).once |
31 |
| - expect(results).to have_received(:increment_updated).once |
| 33 | + describe '#upload_urls' do |
| 34 | + context 'with valid CSV content' do |
| 35 | + it 'processes each row in the CSV' do |
| 36 | + allow(uploader).to receive(:process_row) |
| 37 | + uploader.send(:upload_urls) |
| 38 | + expect(uploader).to have_received(:process_row).exactly(3).times |
32 | 39 | end
|
33 | 40 | end
|
34 | 41 |
|
35 |
| - context 'with an invalid CSV format' do |
36 |
| - it 'handles invalid CSV format gracefully' do |
37 |
| - uploader = described_class.new(filename, invalid_file_path) |
38 |
| - uploader.upload |
39 |
| - expect(results).to have_received(:add_error).with('Invalid CSV format', 'Entire file').once |
40 |
| - expect(logger).to have_received(:error).with(/Error parsing CSV/) |
| 42 | + context 'with invalid CSV content' do |
| 43 | + let(:csv_error) { CSV::MalformedCSVError.new('Invalid CSV format', 'Line causing error') } |
| 44 | + |
| 45 | + before do |
| 46 | + allow(CSV).to receive(:parse).and_raise(csv_error) |
| 47 | + allow(Rails.logger).to receive(:error) |
| 48 | + end |
| 49 | + |
| 50 | + it 'handles the CSV error and logs it' do |
| 51 | + expect(results).to receive(:add_error).with('Invalid CSV format', 'Entire file') |
| 52 | + uploader.send(:upload_urls) |
| 53 | + expect(Rails.logger).to have_received(:error).with(/Error parsing CSV/) |
41 | 54 | end
|
42 | 55 | end
|
43 | 56 | end
|
44 | 57 |
|
45 | 58 | describe '#process_row' do
|
46 |
| - let(:row) { { 'URL' => 'http://example.com', 'DOC_ID' => '123' } } |
| 59 | + let(:row) { { 'URL' => 'http://example.com', 'DOC_ID' => 'doc1' } } |
| 60 | + |
| 61 | + context 'when DOC_ID is blank' do |
| 62 | + let(:row) { { 'URL' => 'http://example.com', 'DOC_ID' => nil } } |
| 63 | + |
| 64 | + it 'adds an error and logs it' do |
| 65 | + allow(Rails.logger).to receive(:error) |
| 66 | + uploader.send(:process_row, row) |
| 67 | + expect(results).to have_received(:add_error).with('Document ID is missing', 'http://example.com') |
| 68 | + expect(Rails.logger).to have_received(:error).with(/Document ID is mandatory/) |
| 69 | + end |
| 70 | + end |
| 71 | + end |
| 72 | + |
| 73 | + describe '#process_url_with_rescue' do |
| 74 | + let(:row) { { 'URL' => 'http://example.com', 'DOC_ID' => 'doc1' } } |
47 | 75 |
|
48 |
| - it 'processes rows with valid data' do |
49 |
| - uploader.send(:process_row, row) |
50 |
| - expect(results).to have_received(:delete_ok).once |
51 |
| - expect(results).to have_received(:increment_updated).once |
| 76 | + before do |
| 77 | + allow(uploader).to receive(:process_url) |
| 78 | + end |
| 79 | + |
| 80 | + it 'processes the URL and updates results' do |
| 81 | + uploader.send(:process_url_with_rescue, 'http://example.com', 'doc1', row) |
| 82 | + expect(results).to have_received(:delete_ok) |
| 83 | + expect(results).to have_received(:increment_updated) |
| 84 | + end |
| 85 | + |
| 86 | + context 'when an error occurs during processing' do |
| 87 | + let(:error) { StandardError.new('Processing error') } |
| 88 | + |
| 89 | + before do |
| 90 | + allow(uploader).to receive(:process_url).and_raise(error) |
| 91 | + allow(Rails.logger).to receive(:error) |
| 92 | + end |
| 93 | + |
| 94 | + it 'handles the error and logs it' do |
| 95 | + uploader.send(:process_url_with_rescue, 'http://example.com', 'doc1', row) |
| 96 | + expect(results).to have_received(:add_error).with('Processing error', 'http://example.com') |
| 97 | + expect(Rails.logger).to have_received(:error).with(/Failure to process bulk upload zombie URL row/) |
| 98 | + end |
52 | 99 | end
|
53 | 100 | end
|
54 | 101 | end
|
0 commit comments