|
1 | 1 | # frozen_string_literal: true
|
2 | 2 |
|
3 | 3 | describe BulkZombieUrlUploader do
|
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 |
| 4 | + let(:file_name) { 'bulk_zombie_urls.csv' } |
| 5 | + let(:file_path) { Rails.root.join('spec/fixtures/csv/bulk_zombie_urls.csv') } |
| 6 | + let(:uploader) { described_class.new(file_name, file_path) } |
| 7 | + let(:results_double) { instance_double(BulkZombieUrls::Results, add_error: nil, delete_ok: nil, increment_updated: nil) } |
16 | 8 |
|
17 | 9 | before do
|
18 |
| - allow(File).to receive(:read).with(filepath).and_return(csv_content) |
19 |
| - allow(BulkZombieUrls::Results).to receive(:new).and_return(results) |
20 |
| - allow(results).to receive(:add_error) |
21 |
| - allow(results).to receive(:delete_ok) |
22 |
| - allow(results).to receive(:increment_updated) |
23 |
| - uploader.instance_variable_set(:@results, results) # Ensure `@results` is initialized |
| 10 | + allow(File).to receive(:read).and_call_original |
| 11 | + allow(BulkZombieUrls::Results).to receive(:new).and_return(results_double) |
24 | 12 | end
|
25 | 13 |
|
26 | 14 | 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) |
| 15 | + it 'sets file name and path instance variables' do |
| 16 | + expect(uploader.instance_variable_get(:@file_name)).to eq(file_name) |
| 17 | + expect(uploader.instance_variable_get(:@file_path)).to eq(file_path) |
30 | 18 | end
|
31 | 19 | end
|
32 | 20 |
|
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 |
| 21 | + describe '#upload' do |
| 22 | + context 'successful upload' do |
| 23 | + before do |
| 24 | + allow(uploader).to receive(:process_upload).and_return(nil) |
| 25 | + allow(results_double).to receive(:delete_ok) |
| 26 | + allow(results_double).to receive(:increment_updated) |
39 | 27 | end
|
| 28 | + |
| 29 | + it 'initializes results and processes URLs' do |
| 30 | + expect { uploader.upload }.not_to raise_error |
| 31 | + expect(BulkZombieUrls::Results).to have_received(:new).with(file_name) |
| 32 | + expect(uploader).to have_received(:process_upload) |
| 33 | + end |
| 34 | + |
| 35 | + # it 'returns the results object' do |
| 36 | + # expect(uploader.upload).to eq(results_double) |
| 37 | + # expect(uploader.results).to eq(results_double) |
| 38 | + # end |
40 | 39 | end
|
| 40 | + end |
41 | 41 |
|
42 |
| - context 'with invalid CSV content' do |
43 |
| - let(:csv_error) { CSV::MalformedCSVError.new('Invalid CSV format', 'Line causing error') } |
| 42 | + describe '#parse_csv' do |
| 43 | + let(:file_content) { "URL,DOC_ID\nhttps://example.com,123\n" } |
| 44 | + let(:malformed_csv_content) { "malformed,data" } |
| 45 | + |
| 46 | + context 'when the CSV is valid' do |
| 47 | + before do |
| 48 | + allow(File).to receive(:read).with(file_path).and_return(file_content) |
| 49 | + end |
| 50 | + |
| 51 | + it 'parses the CSV successfully' do |
| 52 | + expect { uploader.send(:parse_csv) }.not_to raise_error |
| 53 | + end |
| 54 | + end |
| 55 | + |
| 56 | + context 'when the CSV raises an error' do |
| 57 | + before do |
| 58 | + allow(File).to receive(:read).with(file_path).and_return(malformed_csv_content) |
| 59 | + allow(CSV).to receive(:parse).and_raise(ArgumentError, 'Invalid argument') |
| 60 | + end |
| 61 | + |
| 62 | + it 'raises CSV::MalformedCSVError' do |
| 63 | + expect { uploader.send(:parse_csv) }.to raise_error(CSV::MalformedCSVError, /Malformed or invalid CSV: Invalid argument/) |
| 64 | + end |
| 65 | + end |
| 66 | + end |
44 | 67 |
|
| 68 | + describe '#process_upload' do |
| 69 | + let(:csv_content) { "URL,DOC_ID\nhttps://example.com,123\nhttps://example2.com,\n" } |
| 70 | + let(:malformed_csv_content) { "malformed,data" } |
| 71 | + |
| 72 | + before do |
| 73 | + uploader.instance_variable_set(:@results, results_double) |
| 74 | + end |
| 75 | + |
| 76 | + context 'when the CSV is valid' do |
45 | 77 | before do
|
46 |
| - allow(CSV).to receive(:parse).and_raise(csv_error) |
47 |
| - allow(Rails.logger).to receive(:error) |
| 78 | + allow(File).to receive(:read).with(file_path).and_return(csv_content) |
| 79 | + allow(uploader).to receive(:process_row) |
48 | 80 | end
|
49 | 81 |
|
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/) |
| 82 | + it 'parses and processes each row' do |
| 83 | + expect { uploader.send(:process_upload) }.not_to raise_error |
| 84 | + expect(uploader).to have_received(:process_row).twice |
| 85 | + end |
| 86 | + end |
| 87 | + |
| 88 | + context 'when the CSV is malformed' do |
| 89 | + before do |
| 90 | + allow(File).to receive(:read).with(file_path).and_return(malformed_csv_content) |
| 91 | + allow(CSV).to receive(:parse).and_raise(CSV::MalformedCSVError.new('CSV', 'Malformed or invalid CSV')) |
| 92 | + allow(Rails.logger).to receive(:error) |
| 93 | + end |
| 94 | + |
| 95 | + it 'logs the error and adds it to results' do |
| 96 | + expect { uploader.send(:process_upload) }.not_to raise_error |
| 97 | + expect(results_double).to have_received(:add_error).with('Invalid CSV format', 'Entire file') |
| 98 | + expect(Rails.logger).to have_received(:error).with(/Malformed or invalid CSV/) |
54 | 99 | end
|
55 | 100 | end
|
56 | 101 | end
|
57 | 102 |
|
58 | 103 | describe '#process_row' do
|
59 |
| - let(:row) { { 'URL' => 'http://example.com', 'DOC_ID' => 'doc1' } } |
| 104 | + let(:row) { { 'URL' => 'https://example.com', 'DOC_ID' => '123' } } |
60 | 105 |
|
61 |
| - context 'when DOC_ID is blank' do |
62 |
| - let(:row) { { 'URL' => 'http://example.com', 'DOC_ID' => nil } } |
| 106 | + before do |
| 107 | + uploader.instance_variable_set(:@results, results_double) |
| 108 | + allow(uploader).to receive(:handle_url_processing).and_return(nil) |
| 109 | + end |
63 | 110 |
|
64 |
| - it 'adds an error and logs it' do |
65 |
| - allow(Rails.logger).to receive(:error) |
| 111 | + context 'missing DOC_ID' do |
| 112 | + let(:row) { { 'URL' => 'https://example.com', 'DOC_ID' => '' } } |
| 113 | + |
| 114 | + it 'adds an error and logs the issue' do |
| 115 | + uploader.send(:process_row, row) |
| 116 | + expect(results_double).to have_received(:add_error).with('Document ID is missing', 'https://example.com') |
| 117 | + end |
| 118 | + end |
| 119 | + |
| 120 | + context 'valid DOC_ID' do |
| 121 | + it 'handles URL processing successfully' do |
66 | 122 | 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/) |
| 123 | + expect(uploader).to have_received(:handle_url_processing).with('https://example.com', '123', row) |
69 | 124 | end
|
70 | 125 | end
|
71 | 126 | end
|
72 | 127 |
|
73 |
| - describe '#process_url_with_rescue' do |
74 |
| - let(:row) { { 'URL' => 'http://example.com', 'DOC_ID' => 'doc1' } } |
| 128 | + describe '#handle_url_processing' do |
| 129 | + let(:url) { 'https://example.com' } |
| 130 | + let(:document_id) { '123' } |
| 131 | + let(:row) { { 'URL' => url, 'DOC_ID' => document_id } } |
75 | 132 |
|
76 | 133 | before do
|
77 |
| - allow(uploader).to receive(:process_url) |
| 134 | + uploader.instance_variable_set(:@results, results_double) |
| 135 | + allow(uploader).to receive(:update_results) |
78 | 136 | end
|
79 | 137 |
|
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 |
| 138 | + context 'successful processing' do |
| 139 | + before do |
| 140 | + allow(uploader).to receive(:process_url).and_return(nil) |
| 141 | + end |
85 | 142 |
|
86 |
| - context 'when an error occurs during processing' do |
87 |
| - let(:error) { StandardError.new('Processing error') } |
| 143 | + it 'updates results without error' do |
| 144 | + expect { uploader.send(:handle_url_processing, url, document_id, row) }.not_to raise_error |
| 145 | + expect(uploader).to have_received(:update_results) |
| 146 | + end |
| 147 | + end |
88 | 148 |
|
| 149 | + context 'failed processing' do |
89 | 150 | before do
|
90 |
| - allow(uploader).to receive(:process_url).and_raise(error) |
| 151 | + allow(uploader).to receive(:process_url).and_raise(StandardError, 'Processing error') |
91 | 152 | allow(Rails.logger).to receive(:error)
|
92 | 153 | end
|
93 | 154 |
|
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') |
| 155 | + it 'logs the error and adds it to results' do |
| 156 | + expect { uploader.send(:handle_url_processing, url, document_id, row) }.not_to raise_error |
| 157 | + expect(results_double).to have_received(:add_error).with('Processing error', url) |
97 | 158 | expect(Rails.logger).to have_received(:error).with(/Failure to process bulk upload zombie URL row/)
|
98 | 159 | end
|
99 | 160 | end
|
|
0 commit comments