Permalink
Newer
Older
100644 446 lines (367 sloc) 20.9 KB
1
require File.dirname(__FILE__) + '/test_helper.rb'
2
3
4
# Tests EbayShopping::Request class.
5
class TestEbayShopping < Test::Unit::TestCase
6
7
def setup
8
setup_ebay_responses
9
YAML.stubs(:load_file).returns({:production => {:app_id => "foo123"}}) # stub getting of config file
10
end
11
#
12
# custom exception definition tests
13
#
14
def test_should_define_custom_exceptions
15
assert EbayShopping::EbayError.superclass == StandardError
16
assert EbayShopping::RequestError.superclass == EbayShopping::EbayError
17
assert EbayShopping::InternalTimeoutError.superclass == EbayShopping::EbayError
18
assert EbayShopping::TimeoutError.superclass == EbayShopping::EbayError
19
assert EbayShopping::SystemError.superclass == EbayShopping::EbayError
20
end
21
22
#
23
# EbayShoppingTest::Request tests
24
#
25
def test_should_get_configs_from_given_yaml_file
26
EbayShopping::Request.class_eval("@@config_params = nil") # reset config params
27
YAML.expects(:load_file).with(regexp_matches(/ebay.yml/)).returns({:production => {:app_id => "foo123"}})
28
EbayShopping::Request.config_params("/some/path/to/ebay.yml")
29
end
30
31
def test_should_get_configs_from_given_hash
32
EbayShopping::Request.class_eval("@@config_params = nil") # reset config params
33
YAML.expects(:load_file).never
34
EbayShopping::Request.config_params({:app_id => "foo123", :affiliate_id => "456bar"})
35
assert_equal "foo123", EbayShopping::Request.config_params[:app_id]
36
assert_equal "456bar", EbayShopping::Request.config_params[:affiliate_id]
37
end
38
39
def test_should_get_configs_from_yaml_config_file_and_cache_result
40
EbayShopping::Request.class_eval("@@config_params = nil") # reset config params
41
YAML.expects(:load_file).returns({:production => {:app_id => "foo123"}})
42
EbayShopping::Request.config_params("/some/path/to/ebay.yml")
43
EbayShopping::Request.config_params("/some/path/to/ebay.yml") # getting the config_params again should use the result stored in the class variable
44
end
45
46
def test_should_use_production_settings_from_config_file_by_default
47
EbayShopping::Request.class_eval("@@config_params = nil") # reset config params
48
YAML.stubs(:load_file).returns({:production => {:app_id => "foo123", :affiliate_id => "foo789"}, :development => {:app_id => "456bar"}})
49
assert_equal({:app_id => "foo123", :affiliate_id => "foo789"}, EbayShopping::Request.config_params("/some/path/to/ebay.yml"))
52
def test_should_allow_environment_to_be_specified_for_config_file
53
EbayShopping::Request.class_eval("@@config_params = nil") # reset config params
54
YAML.expects(:load_file).with(regexp_matches(/ebay.yml/)).returns({:production => {:app_id => "foo123"}, :test => {:app_id => "456bar"}})
55
assert_equal "456bar", EbayShopping::Request.config_params("/some/path/to/ebay.yml", :test)[:app_id]
56
end
57
58
def test_should_have_callname_and_call_params_accessors
59
ebay = EbayShopping::Request.new(:find_items, {:first_param => "foo", :second_param => "bar"})
60
assert_equal :find_items, ebay.callname
Chris Taggart
May 12, 2008
61
assert_equal({:first_param => "foo", :second_param => "bar"}, ebay.call_params)
62
end
63
64
def test_should_have_app_id_affiliate_id_and_affiliate_partner_accessors_set_from_config_params
65
stub_config_params(:affiliate_id => "foo456", :affiliate_partner => "bar789", :affiliate_shopper_id => "foobar")
66
assert_equal "app123", new_ebay_request.app_id
67
assert_equal "foo456", new_ebay_request.affiliate_id
68
assert_equal "bar789", new_ebay_request.affiliate_partner
69
assert_equal "foobar", new_ebay_request.affiliate_shopper_id
70
end
71
72
def test_should_use_default_site_id_if_set_in_config_params
73
stub_config_params(:site_id => "foo123")
74
assert_equal "foo123", new_ebay_request.site_id
75
end
76
77
def test_should_use_site_id_for_request_in_preference_to_default_site_id_and_remove_from_call_params
78
stub_config_params(:site_id => "foo123")
79
ebay = new_ebay_request(:site_id => "456bar")
80
assert_equal "456bar", ebay.site_id
81
assert_nil ebay.call_params[:site_id] # we don't want it being submitted as a regular param
82
end
83
84
def test_should_return_site_country_name_from_site_id
85
assert_equal "UK", new_ebay_request(:site_id => "3").site_name
86
assert_equal "Canada (French)", new_ebay_request(:site_id => "210").site_name
87
assert_equal "Sweden", new_ebay_request(:site_id => "218").site_name
88
assert_equal "US", new_ebay_request(:site_id => nil).site_name # should return US if site_id is nil (as that's what ebay API defaults to)
89
end
90
91
def test_should_require_callname_for_new_request
92
assert_raise(ArgumentError) { EbayShopping::Request.new }
93
end
94
95
def test_should_call_cache_methods_when_call_is_made
96
request = new_ebay_request
97
request.expects(:cached_xml_response)
98
request.stubs(:_http_get).returns(@find_items_response)
99
request.expects(:cache_response).with(@find_items_response)
100
101
request.send(:call, "http://some.url")
102
end
103
104
def test_should_check_error_cache_when_call_is_made
105
request = new_ebay_request
106
request.stubs(:_http_get).returns(@find_items_response)
107
request.expects(:check_error_cache)
108
109
request.send(:call, "http://some.url")
110
end
111
112
def test_should_build_request_url_from_given_params
113
stub_config_params(:site_id => "3") # no affiliate stuff here
114
url = new_ebay_request.send(:url_from, :find_items, {:item_sort => "BestMatch", :max_results => 5} ) # url_from is a protected method
115
assert_match "http://open.api.ebay.com/shopping?version=#{EbayShopping::EBAY_API_VERSION}&appid=#{new_ebay_request.app_id}&callname=FindItems&siteid=3&ItemSort=BestMatch&MaxResults=5", url
116
end
117
118
def test_should_include_affiliate_info_when_building_url_if_given
119
stub_config_params(:affiliate_id => "foo456", :affiliate_partner => "bar789", :affiliate_shopper_id => "foobar")
120
ebay = new_ebay_request
121
assert_equal "http://open.api.ebay.com/shopping?version=#{EbayShopping::EBAY_API_VERSION}&appid=app123&callname=FindItems" +
122
"&trackingpartnercode=bar789&trackingid=foo456&affiliateuserid=foobar" +
123
"&QueryKeywords=dog%20collar",
124
ebay.send(:url_from, :find_items, {:query_keywords => "dog collar"} ) # url_from is a protected method
125
end
126
127
def test_should_turn_query_array_into_comma_separated_values
128
assert_equal "SomeArray=foo,bar,hello%20world", new_ebay_request.send(:_query_params_from, {:some_array => ["foo", "bar", "hello world"]} ) # url_from is a protected method
129
end
130
131
def test_should_always_return_query_params_in_alpha_order
132
assert_equal "ItemSort=BestMatch&MaxResults=5&QueryKeywords=dog%20collar", new_ebay_request.send(:_query_params_from, {:query_keywords => "dog collar", :item_sort => "BestMatch", :max_results => 5} ) # url_from is a protected method
133
assert_equal "ItemSort=BestMatch&MaxResults=5&QueryKeywords=dog%20collar", new_ebay_request.send(:_query_params_from, {:item_sort => "BestMatch", :max_results => 5, :query_keywords => "dog collar"} ) # url_from is a protected method
134
end
135
136
def test_should_ignore_query_params_with_nil_value
137
stub_config_params
138
ebay = new_ebay_request
139
assert_equal "Foo=hello%20world", ebay.send(:_query_params_from, {:foo => "hello world", :bar => nil} )
140
end
141
142
def test_should_call_ebay_shopping_url_when_ebay_response_is_requested
143
EbayShopping::Request.any_instance.expects(:url_from).returns("http://ebay.com/some_url")
144
EbayShopping::Request.any_instance.expects(:call).with("http://ebay.com/some_url").returns(@find_items_response)
145
146
new_ebay_request.response
147
end
148
149
def test_should_return_appropriate_response_object_for_request_response
150
EbayShopping::Request.any_instance.stubs(:call).returns(@find_items_response)
151
152
assert_kind_of EbayShopping::FindItemsResponse, EbayShopping::Request.new(:find_items).response
153
assert_kind_of EbayShopping::FindItemsAdvancedResponse, EbayShopping::Request.new(:find_items_advanced).response
154
assert_kind_of EbayShopping::FindPopularItemsResponse, EbayShopping::Request.new(:find_popular_items).response
155
assert_kind_of EbayShopping::GetMultipleItemsResponse, EbayShopping::Request.new(:get_multiple_items).response
156
assert_kind_of EbayShopping::GetSingleItemResponse, EbayShopping::Request.new(:get_single_item).response
157
assert_kind_of EbayShopping::GetCategoryInfoResponse, EbayShopping::Request.new(:get_category_info).response
158
assert_kind_of EbayShopping::FindProductsResponse, EbayShopping::Request.new(:find_products).response
159
end
160
161
def test_should_pass_self_to_response_object_so_request_is_accessible_from_response_object
162
request = new_ebay_request
163
EbayShopping::Request.any_instance.stubs(:call).returns(@find_items_response)
164
EbayShopping::FindItemsResponse.expects(:new).with(anything, request)
165
166
request.response
167
end
168
169
#
170
# EbayShoppingTest::Response tests
171
#
172
def test_should_instantiate_response_object_from_xml_and_parse_into_hash
173
response = EbayShopping::Response.new(@find_items_response)
174
assert_kind_of Hash, response.full_response
175
end
176
177
def test_should_return_request_that_generated_response
178
EbayShopping::Request.any_instance.stubs(:call).returns(@find_items_response)
179
request = new_ebay_request
180
response = request.response
181
182
assert_equal request, response.request
183
end
184
185
def test_should_extract_total_results_from_xml_response
186
response = EbayShopping::Response.new(@find_items_response)
187
assert_equal 117, response.total_items
188
end
189
190
def test_should_return_nil_for_total_items_if_no_total_items_field
191
response = EbayShopping::Response.new(@get_single_item_response)
192
assert_nil response.total_items
193
end
194
195
def test_should_extract_page_number_from_xml_response
196
response = EbayShopping::Response.new(@find_items_advanced_response)
197
assert_equal 1, response.page_number
198
end
199
200
def test_should_return_nil_for_page_number_if_no_total_items_field
201
response = EbayShopping::Response.new(@get_single_item_response)
202
assert_nil response.page_number
203
end
204
205
def test_should_extract_total_pages_from_xml_response
206
response = EbayShopping::Response.new(@find_items_advanced_response)
207
assert_equal 5, response.total_pages
208
end
209
210
def test_should_return_nil_for_total_pages_from_xml_response_if_no_total_pages_field
211
response = EbayShopping::Response.new(@get_single_item_response)
212
assert_nil response.total_pages
213
end
214
215
def test_should_return_nil_for_errors_if_no_errors
216
response = EbayShopping::Response.new(@find_items_response)
217
assert_nil response.errors
218
end
219
220
def test_should_extract_items_from_response_in_basic_response_object
221
response = EbayShopping::Response.new(@find_items_response)
222
assert_kind_of Array, items = response.items
223
assert_kind_of EbayShopping::Item, items.first
224
assert_equal "1949 CADILLAC COUPE DeVILLE- WHITE DIECAST 1:43 SCALE", items.first.title
225
end
226
227
def test_should_still_return_array_of_items_from_response_even_when_only_one_item
228
response = EbayShopping::Response.new(@find_items_response)
229
response.stubs(:xml_items).returns(response.full_response["Item"].first) # simulate only one item being returned
230
assert_kind_of Array, items = response.items
231
assert_equal 1, items.size
232
assert_kind_of EbayShopping::Item, items.first
233
assert_equal "1949 CADILLAC COUPE DeVILLE- WHITE DIECAST 1:43 SCALE", items.first.title
234
end
235
236
def test_should_extract_items_from_find_items_response
237
response = EbayShopping::FindItemsResponse.new(@find_items_response)
238
assert_kind_of Array, items = response.items
239
assert_kind_of EbayShopping::Item, items.first
240
assert_equal "1949 CADILLAC COUPE DeVILLE- WHITE DIECAST 1:43 SCALE", items.first.title
241
end
242
243
def test_should_extract_items_from_find_items_advanced_response
244
response = EbayShopping::FindItemsAdvancedResponse.new(@find_items_advanced_response)
245
assert_kind_of Array, items = response.items
246
assert_kind_of EbayShopping::Item, items.first
247
assert_equal "Original Soundtrack - Harry Potter And The Philosoph...", items.first.title
248
end
249
250
def test_should_extract_items_from_get_single_item_response
251
response = EbayShopping::GetSingleItemResponse.new(@get_single_item_response)
252
assert_kind_of Array, items = response.items
253
assert_kind_of EbayShopping::Item, items.first
254
assert_equal "EMMA WATSON entertainment weekly HARRY POTTER phoenix", items.first.title
255
end
256
257
def test_should_have_item_accessor_for_single_item_response
258
response = EbayShopping::GetSingleItemResponse.new(@get_single_item_response)
259
assert_equal response.items.first, response.item
260
end
261
262
def test_should_extract_items_from_find_popular_items_response
263
response = EbayShopping::FindPopularItemsResponse.new(@find_popular_items_response)
264
assert_kind_of Array, items = response.items
265
assert_kind_of EbayShopping::Item, items.first
266
assert_equal "Harry Potter And The Chamber Of Secrets - Original S...", items.first.title
267
end
268
269
def test_should_extract_items_from_get_multiple_items_response
270
response = EbayShopping::GetMultipleItemsResponse.new(@get_multiple_items_response)
271
assert_kind_of Array, items = response.items
272
assert_kind_of EbayShopping::Item, items.first
273
assert_equal "harry potter gwq minimium", items.first.title
274
end
275
276
def test_should_extract_products_from_find_products_response
277
response = EbayShopping::FindProductsResponse.new(@find_products_response)
278
assert_kind_of Array, products = response.products
279
assert_kind_of EbayShopping::Product, products.first
280
assert_equal "Harry Potter and the Order of the Phoenix (2007, DVD)", products.first.title
281
end
282
283
def test_should_return_no_results_if_no_results
284
EbayShopping::Request.any_instance.expects(:call).returns(@no_results_response)
285
response = new_ebay_request.response
286
assert_equal [], response.items
287
end
288
289
def test_should_raise_request_error_if_request_error_response
290
EbayShopping::Request.any_instance.expects(:call).returns(@request_error_response)
291
assert_raise(EbayShopping::RequestError) { new_ebay_request.response }
292
end
293
294
def test_should_raise_system_error_if_system_error_response
295
EbayShopping::Request.any_instance.expects(:call).returns(@system_error_response)
296
assert_raise(EbayShopping::SystemError) { new_ebay_request.response }
297
end
298
299
def test_should_raise_internal_timeout_if_ebay_timeout_error_response
300
assert_raise(EbayShopping::InternalTimeoutError) { EbayShopping::Response.new(@timeout_error_response, new_ebay_request) }
301
end
302
303
def test_should_rescue_internal_timeout_error_once_and_make_call_again_and_set_repeat_call_accessor
304
EbayShopping::Request.any_instance.expects(:call).times(2).returns(@timeout_error_response).then.returns(@find_items_response)
305
response = new_ebay_request.response
306
assert response.request.repeat_call
307
end
308
309
def test_should_rescue_internal_timeout_error_only_once_and_raise_if_returned_a_second_time
310
EbayShopping::Request.any_instance.expects(:call).times(2).returns(@timeout_error_response)
311
assert_raise(EbayShopping::SystemError) { new_ebay_request.response }
312
end
313
314
def test_should_rescue_timeout_error_and_raise_ebay_timeout_instead
315
Net::HTTP.any_instance.expects(:get).raises(Timeout::Error, "Timeout::Error")
316
assert_raise(EbayShopping::TimeoutError) { new_ebay_request.response }
317
end
318
319
def test_should_raise_ebay_request_error_if_error_code_returned
320
Net::HTTP.any_instance.expects(:get).returns(Net::HTTPBadRequest.new("1.1","400","Bad Request"))
321
assert_raise(EbayShopping::RequestError) { new_ebay_request.response }
322
end
323
324
def test_should_call_ebay_error_raised_hook_when_request_error_raised
325
request = new_ebay_request
326
request.expects(:call).returns(@request_error_response)
327
request.expects(:ebay_error_raised).with(has_entry("LongMessage", "Input data for the given tag is invalid or missing. Please check API documentation."))
328
329
assert_raise(EbayShopping::RequestError) { request.response }
330
end
331
332
def test_should_call_system_error_called_hook_when_system_error_raised
333
request = new_ebay_request
334
request.expects(:call).returns(@system_error_response)
335
request.expects(:ebay_error_raised)
336
337
assert_raise(EbayShopping::SystemError) { request.response }
338
end
339
340
# EbayShoppingTest::GenericItem tests
341
def test_should_instantiate_generic_item_from_hash_and_convert_params_into_accessors
342
item = EbayShopping::GenericItem.new("Title" => "Dummy ebay item")
343
assert_equal "Dummy ebay item", item.title
344
end
345
346
def test_should_make_items_original_hash_available_as_all_params
347
item = EbayShopping::GenericItem.new("ItemID" => "foo123", "Title" => "Dummy ebay item")
Chris Taggart
May 12, 2008
348
assert_equal({"ItemID" => "foo123", "Title" => "Dummy ebay item"}, item.all_params)
349
end
350
351
def test_should_allow_access_to_original_hash_via_square_brackets
352
item = EbayShopping::GenericItem.new("ItemID" => "foo123", "Title" => "Dummy ebay item")
353
assert_equal "foo123", item["ItemID"]
354
assert_equal "Dummy ebay item", item["Title"]
355
end
356
357
def test_should_not_instantiate_instance_variable_for_attributes_with_no_accessor
358
item = EbayShopping::GenericItem.new("ItemID" => "foo123", "foo" => "bar")
359
assert !item.instance_variables.include?("@foo")
360
end
361
362
# EbayShoppingTest::Item tests
363
def test_should_instantiate_ebay_item_from_hash_and_convert_params_into_accessors
364
item = EbayShopping::Item.new("ItemID" => "foo123", "Title" => "Dummy ebay item")
365
assert_equal "foo123", item.item_id
366
assert_equal "Dummy ebay item", item.title
367
end
368
369
def test_should_return_converted_current_price_as_money_object
370
item = EbayShopping::Item.new("ItemID" => "foo123", "ConvertedCurrentPrice"=>{"currencyID"=>"GBP", "content"=>"0.99"})
371
assert_kind_of EbayShopping::Money, item.converted_current_price
372
end
373
374
def test_should_return_nil_for_converted_current_price_if_no_such_attribute
375
item = EbayShopping::Item.new("ItemID" => "foo123")
376
assert_nil item.converted_current_price
377
end
378
379
def test_should_return_formatted_time_left
380
dummy_time = Time.now + ((3*24 + 4)*60 + 32)*60+1
381
item = EbayShopping::Item.new("ItemID" => "foo123", "EndTime"=>dummy_time.xmlschema)
382
assert_equal "3 days, 4 hours, 32 minutes", item.time_left
383
end
384
385
def test_should_return_end_time_as_time_object
386
item = EbayShopping::Item.new("ItemID" => "foo123", "EndTime"=>"2008-01-06T22:50:09.000Z")
387
assert_kind_of Time, item.end_time
388
assert_equal "Sun Jan 06 22:50:09 +0000 2008", item.end_time.to_s
389
end
390
391
# EbayShoppingTest::Product tests
392
def test_should_instantiate_ebay_product_from_hash_and_convert_params_into_accessors
393
item = EbayShopping::Product.new("ProductID" => "foo123", "Title" => "Dummy ebay product")
394
assert_equal "foo123", item.product_id
395
assert_equal "Dummy ebay product", item.title
396
end
397
398
# EbayShoppingTest::Money tests
399
def test_should_instantiate_money_object_from_hash
400
money = EbayShopping::Money.new("currencyID"=>"GBP", "content"=>"5.99")
401
assert_equal "GBP", money.currency_id
402
assert_in_delta(5.99, money.content, 2 ** -20)
403
end
404
405
def test_should_convert_money_to_s_in_standard_format
406
money = EbayShopping::Money.new("currencyID"=>"FOO", "content"=>"5.99123")
407
assert_equal "FOO 5.99", money.to_s
408
end
409
410
def test_should_convert_money_to_s_in_for_common_currencies
411
assert_equal "£5.99", EbayShopping::Money.new("currencyID"=>"GBP", "content"=>"5.99123").to_s
412
assert_equal "$5.99", EbayShopping::Money.new("currencyID"=>"USD", "content"=>"5.99123").to_s
413
assert_equal "AU$5.99", EbayShopping::Money.new("currencyID"=>"AUD", "content"=>"5.99123").to_s
414
assert_equal "CA$5.99", EbayShopping::Money.new("currencyID"=>"CAD", "content"=>"5.99123").to_s
415
assert_equal "EUR 5.99", EbayShopping::Money.new("currencyID"=>"EUR", "content"=>"5.99123").to_s
416
end
417
418
private
419
def dummy_xml_response(response_name)
420
IO.read(File.join(File.dirname(__FILE__) + "/xml_responses/#{response_name.to_s}.xml"))
421
end
422
423
def new_ebay_request(options={})
424
EbayShopping::Request.new(:find_items, options)
425
end
426
427
def stub_config_params(options={})
428
EbayShopping::Request.stubs(:config_params).returns({:app_id => "app123"}.merge(options))
429
end
430
431
def setup_ebay_responses
432
@find_items_response = dummy_xml_response(:ebay_find_items)
433
@find_items_advanced_response = dummy_xml_response(:ebay_find_items_advanced)
434
@find_popular_items_response = dummy_xml_response(:ebay_find_popular_items)
435
@get_multiple_items_response = dummy_xml_response(:ebay_get_multiple_items)
436
@get_single_item_response = dummy_xml_response(:ebay_get_single_item)
437
@get_category_info_response = dummy_xml_response(:ebay_get_category_info)
438
@find_products_response = dummy_xml_response(:ebay_find_products)
439
@no_results_response = dummy_xml_response(:ebay_no_results)
440
@request_error_response = dummy_xml_response(:ebay_request_error)
441
@system_error_response = dummy_xml_response(:ebay_system_error)
442
@timeout_error_response = dummy_xml_response(:ebay_timeout_error)
443
end
444
end
445