Skip to content

Commit 3596c55

Browse files
committed
update errors on email template
1 parent 7d79965 commit 3596c55

File tree

7 files changed

+120
-76
lines changed

7 files changed

+120
-76
lines changed

app/services/bulk_zombie_url_uploader.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ def initialize(filename, filepath)
1212

1313
def upload
1414
@results = BulkZombieUrls::Results.new(@file_name)
15+
raise 'Results object not initialized' if @results.nil?
16+
1517
begin
1618
upload_urls
1719
rescue => e

app/services/bulk_zombie_urls/results.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,12 @@ def delete_ok
2222
end
2323

2424
def add_error(error_message, key)
25-
@error_count += 1
25+
self.error_count += 1
2626
@errors[key] << error_message
2727
end
2828

2929
def total_count
3030
ok_count + error_count
3131
end
32-
33-
def urls_with(id)
34-
@errors[id]
35-
end
3632
end
3733
end

app/views/admin/bulk_zombie_url_upload/index.html.haml

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,18 @@
44
To upload a list of URLs to be deleted, follow these instructions:
55
%ul.bulk-upload-instructions
66
%li
7-
Create a new text file containing one URL per line. An example of this format
7+
Create a new CSV file containing one URL and Document id per line.
8+
%li
9+
Document ID is mandatorily required to delete URL from Kibana when URL is not present in Search DB. An example of this format
810
is shown below.
911
%li
1012
Save the file on your computer; you can name the file whatever you like, as
11-
long as it's plain text and has a .txt extension.
13+
long as it has a .txt extension.
1214
%li
1315
%b
1416
Do not use Microsoft Word files, or any other file type except plain text.
1517
%li
16-
The maximum file size is #{number_to_human_size(BulkUrlUploader::MAXIMUM_FILE_SIZE)}.
18+
The maximum file size is #{number_to_human_size(BulkZombieUrls::FileValidator::MAXIMUM_FILE_SIZE)}.
1719
%li
1820
Browse for the file on your computer.
1921
%li
@@ -25,9 +27,10 @@
2527
%code
2628
%pre
2729
URL,DOC_ID
28-
http://www.sample.gov/1.html,sample121
29-
http://www.sample.gov/2.html,sample122
30-
http://www.sample.gov/3.html,sample123
30+
http://www.sample.gov/1.html,docid121
31+
http://www.sample.gov/2.html,docid122
32+
http://www.sample.gov/3.html,docid123
33+
,docid124
3134
= form_tag upload_admin_bulk_zombie_url_upload_index_path, :multipart => true, :class => 'form' do
3235
= file_field_tag 'bulk_upload_zombie_urls', :accept => 'text/csv'
3336
%p

app/views/bulk_zombie_url_upload_results_mailer/results_email.html.erb

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,9 @@
1919
<p>
2020
<%= @results.error_count %> URLs failed validation.
2121
</p>
22-
23-
<% @results.error_messages.each do |error_message| %>
24-
<h3><%= error_message %></h3>
25-
<% @results.urls_with(error_message).each do |url| %>
26-
<%= url %>
27-
<br />
28-
<% end %>
22+
<% @results.errors.each do |k,error_message| %>
23+
<h3><%= k %></h3>
24+
<%= error_message %>
25+
<br />
2926
<% end %>
3027
<% end %>

app/views/bulk_zombie_url_upload_results_mailer/results_email.text.erb

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@ finished.
44
There were <%= @results.total_count %> URLs.
55

66
<% if @results.error_count == 0 %>
7-
There were no errors.
7+
There were no errors.
88
<% else %>
9-
<%= @results.ok_count %> URLs were deleted successfully.
10-
<%= @results.error_count %> URLs failed validation.
9+
<%= @results.ok_count %> URLs were deleted successfully.
10+
<%= @results.error_count %> URLs failed validation.
1111

12-
<% @results.error_messages.each do |error_message| %>
13-
<%= error_message %>
14-
<% @results.urls_with(error_message).each do |url| %>
15-
<%= url %>
16-
<% end %>
12+
<% @results.errors.each do |k,error_message| %>
13+
<h3><%= k %></h3>
14+
<%= error_message %>
15+
<br />
1716
<% end %>
18-
<% end %>
17+
<% end %>
Lines changed: 78 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,101 @@
11
# frozen_string_literal: true
22

3-
require 'spec_helper'
4-
53
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
1216

1317
before do
18+
allow(File).to receive(:read).with(filepath).and_return(csv_content)
1419
allow(BulkZombieUrls::Results).to receive(:new).and_return(results)
1520
allow(results).to receive(:add_error)
1621
allow(results).to receive(:delete_ok)
1722
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
2431
end
2532

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
3239
end
3340
end
3441

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/)
4154
end
4255
end
4356
end
4457

4558
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' } }
4775

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
5299
end
53100
end
54101
end

spec/services/bulk_zombie_urls/results_spec.rb

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,10 @@
77
it 'initializes with correct attributes' do
88
expect(results.file_name).to eq('Test File')
99
expect(results.ok_count).to eq(0)
10-
expect(results.updated).to eq(0)
1110
expect(results.error_count).to eq(0)
12-
end
13-
end
14-
15-
describe '#delete_ok' do
16-
it 'increments the ok_count' do
17-
expect { results.delete_ok }.to change { results.ok_count }.by(1)
11+
expect(results.updated).to eq(0)
12+
expect(results.errors).to be_a(Hash)
13+
expect(results.errors).to be_empty
1814
end
1915
end
2016

@@ -24,6 +20,12 @@
2420
end
2521
end
2622

23+
describe '#delete_ok' do
24+
it 'increments the ok_count' do
25+
expect { results.delete_ok }.to change { results.ok_count }.by(1)
26+
end
27+
end
28+
2729
describe '#add_error' do
2830
let(:error_message) { 'Sample error message' }
2931
let(:key) { 'http://example.com' }
@@ -36,25 +38,23 @@
3638
results.add_error(error_message, key)
3739
expect(results.errors[key]).to include(error_message)
3840
end
41+
42+
it 'creates a new key in the errors hash if it does not exist' do
43+
expect(results.errors[key]).to be_empty
44+
results.add_error(error_message, key)
45+
expect(results.errors).to have_key(key)
46+
end
3947
end
4048

4149
describe '#total_count' do
4250
it 'calculates the total of ok_count and error_count' do
4351
results.delete_ok
44-
results.add_error('Error 1', 'key1')
52+
results.add_error('Error message', 'key1')
4553
expect(results.total_count).to eq(2)
4654
end
47-
end
48-
49-
describe '#urls_with' do
50-
it 'retrieves all errors associated with a specific key' do
51-
results.add_error('Error 1', 'key1')
52-
results.add_error('Error 2', 'key1')
53-
expect(results.urls_with('key1')).to eq(['Error 1', 'Error 2'])
54-
end
5555

56-
it 'returns an empty array if no errors exist for the key' do
57-
expect(results.urls_with('nonexistent_key')).to be_empty
56+
it 'returns 0 when there are no ok_count or error_count' do
57+
expect(results.total_count).to eq(0)
5858
end
5959
end
6060
end

0 commit comments

Comments
 (0)