-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworld_cat_job_spec.rb
More file actions
143 lines (108 loc) · 4.62 KB
/
Copy pathworld_cat_job_spec.rb
File metadata and controls
143 lines (108 loc) · 4.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
require 'rails_helper'
require 'support/location_contexts'
module Location
describe WorldCatJob, type: :job do
include_context('LocationRequest')
describe :perform do
before do
stub_token_request
end
it 'rejects a non-Worldcat request' do
req.update(slf: false, uc: false)
expect { WorldCatJob.perform_now(req) }.to raise_error(ArgumentError)
end
it 'retrieves the locations' do
request_records = req.location_records
oclc_numbers_expected.each do |oclc_number|
stub_wc_request_for(oclc_number)
end
WorldCatJob.perform_now(req)
expected_count = locations_by_oclc_num.size
expect(request_records.where(wc_retrieved: true).count).to eq(expected_count)
expect(request_records.where(wc_retrieved: false)).not_to exist
expect(req.wc_incomplete?).to eq(false)
request_records.find_each do |record|
verify_wc_symbols(record)
end
end
it 'completes partially-completed jobs' do
request_records = req.location_records
# Simulate previously-completed partial job
oclc_numbers_expected.each_with_index do |oclc_number, i|
if i.odd?
stub_wc_request_for(oclc_number)
else
symbols_expected = locations_by_oclc_num[oclc_number]
wc_record = request_records.find_by!(oclc_number:)
wc_record.update(wc_retrieved: true, wc_symbols: symbols_expected.join(','))
end
end
expect(req.wc_incomplete?).to eq(true)
WorldCatJob.perform_now(req)
expected_count = locations_by_oclc_num.size
expect(request_records.where(wc_retrieved: true).count).to eq(expected_count)
expect(request_records.where(wc_retrieved: false)).not_to exist
expect(req.wc_incomplete?).to eq(false)
request_records.find_each do |record|
verify_wc_symbols(record)
end
end
# rubocop:disable RSpec/ExampleLength
it 'handles network errors' do
request_records = req.location_records
oclc_numbers_expected.each_with_index do |oclc_number, i|
if i.odd?
stub_wc_request_for(oclc_number)
else
req = BerkeleyLibrary::Location::WorldCat::LibrariesRequest.new(oclc_number)
stub_request(:get, req.uri).with(query: req.params).to_return(status: 500)
end
end
WorldCatJob.perform_now(req)
expect(req.wc_incomplete?).to eq(false)
expected_count = locations_by_oclc_num.size
expect(request_records.where(wc_retrieved: true).count).to eq(expected_count)
expect(request_records.where(wc_retrieved: false)).not_to exist
oclc_numbers_expected.each_with_index do |oclc_number, i|
record = request_records.find_by!(oclc_number:)
if i.odd?
verify_wc_symbols(record)
else
expect(record.wc_symbols).to be_nil
expect(record.wc_error).to eq('500 Internal Server Error')
end
end
end
it '"handles" being interrupted/killed and then resumed' do
midpoint = oclc_numbers_expected.size / 2
first_batch = oclc_numbers_expected[0...midpoint]
second_batch = oclc_numbers_expected[midpoint..]
# Simulate being killed after retrieving half the items
first_batch.each do |oclc_number|
stub_wc_request_for(oclc_number)
end
oclc_number_midpoint = oclc_numbers_expected[midpoint]
req_midpoint = BerkeleyLibrary::Location::WorldCat::LibrariesRequest.new(oclc_number_midpoint)
stub_request(:get, req_midpoint.uri).with(query: req_midpoint.params).to_raise(SignalException.new(:KILL))
expect do
WorldCatJob.perform_now(req)
end.to raise_error(SignalException)
request_records = req.location_records
expect(request_records.where(wc_retrieved: true).count).to eq(first_batch.size)
expect(request_records.where.not(wc_error: nil)).not_to exist
expect(req.wc_incomplete?).to eq(true)
# Simulate retry after interrupt
second_batch.each do |oclc_number|
stub_wc_request_for(oclc_number)
end
WorldCatJob.perform_now(req)
expected_count = locations_by_oclc_num.size
expect(request_records.where(wc_retrieved: true).count).to eq(expected_count)
expect(request_records.where(wc_retrieved: false)).not_to exist
expect(req.wc_incomplete?).to eq(false)
request_records.find_each(&method(:verify_wc_symbols))
end
# rubocop:enable RSpec/ExampleLength
end
end
end