Skip to content

Commit fc74d99

Browse files
committed
Add item sync specs
A couple notes: 1. I added a new exception to rubocop to allow for the `expect { action_a }.to change { dynamic_attr }` form. 2. The 2 sec Amazon rate limit delay doesn't apply to tests. All tests are green and no new rubocop violations are added.
1 parent e7c569b commit fc74d99

File tree

4 files changed

+68
-2
lines changed

4 files changed

+68
-2
lines changed

.rubocop.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,7 @@ Style/NumericLiterals:
2222
Style/BlockDelimiters:
2323
Exclude:
2424
- spec/**/*
25+
26+
Lint/AmbiguousBlockAssociation:
27+
Exclude:
28+
- spec/**/*

app/jobs/item_sync_job.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def sync_all!
2626
# This is done in slices to avoid Amazon rate limits
2727
Item.all.each_slice(3) do |items|
2828
items.each { |item| sync! item }
29-
sleep 2.seconds
29+
sleep 2.seconds unless Rails.env.test?
3030
end
3131
end
3232

spec/jobs/item_sync_job_spec.rb

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# frozen_string_literal: true
2+
3+
describe ItemSyncJob do
4+
describe '#perform' do
5+
context 'for zero items' do
6+
it 'runs successfully' do
7+
expect { subject.perform }.not_to raise_error
8+
end
9+
end
10+
11+
context 'for one item', :external do
12+
let!(:corgi) { create(:item, asin: 'corgi_asin') }
13+
14+
context 'when an item is unchanged' do
15+
before { subject.perform }
16+
17+
it 'keeps the original item' do
18+
expect { subject.perform }.not_to change { corgi.reload.attributes }
19+
end
20+
end
21+
22+
context 'when an item is changed' do
23+
it 'updates the item name' do
24+
expect { subject.perform }.to change { corgi.reload.name }
25+
end
26+
27+
it 'updates the item price' do
28+
expect { subject.perform }.to change { corgi.reload.price_cents }
29+
end
30+
31+
it 'updates the item url' do
32+
expect { subject.perform }.to change { corgi.reload.amazon_url }
33+
end
34+
35+
it 'updates the item image url' do
36+
expect { subject.perform }.to change { corgi.reload.image_url }
37+
end
38+
end
39+
end
40+
41+
context 'for multiple items', :external do
42+
let!(:item_1) { create(:item, asin: 'corgi_asin') }
43+
let!(:item_2) { create(:item, asin: 'corgi_asin2') }
44+
45+
it 'updates the first item' do
46+
expect { subject.perform }.to change { item_1.reload.attributes }
47+
end
48+
49+
it 'updates the second item' do
50+
expect { subject.perform }.to change { item_2.reload.attributes }
51+
end
52+
end
53+
end
54+
end

spec/support/webmock.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,25 @@
77
RSpec.configure do |config|
88
config.before(:each, :external) do
99
# Item Search
10+
1011
search_response = file_fixture('amazon_corgi_search_response.xml').read
1112
stub_request(:get, 'webservices.amazon.com/onca/xml')
1213
.with(query: hash_including('Operation' => 'ItemSearch',
1314
'Keywords' => 'corgi'))
1415
.to_return(body: search_response)
1516

1617
# Item Lookup
18+
1719
lookup_response = file_fixture('amazon_corgi_lookup_response.xml').read
20+
21+
stub_request(:get, 'webservices.amazon.com/onca/xml')
22+
.with(query: hash_including('Operation' => 'ItemLookup',
23+
'ItemId' => 'corgi_asin'))
24+
.to_return(body: lookup_response)
25+
1826
stub_request(:get, 'webservices.amazon.com/onca/xml')
1927
.with(query: hash_including('Operation' => 'ItemLookup',
20-
'ItemId' => 'corgi_asin'))
28+
'ItemId' => 'corgi_asin2'))
2129
.to_return(body: lookup_response)
2230
end
2331
end

0 commit comments

Comments
 (0)