hollownest-ruby-aws 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +138 -0
- data/lib/amazon.rb +153 -0
- data/lib/amazon/aws.rb +1339 -0
- data/lib/amazon/aws/cache.rb +141 -0
- data/lib/amazon/aws/search.rb +356 -0
- data/lib/amazon/aws/shoppingcart.rb +504 -0
- data/lib/amazon/locale.rb +102 -0
- data/test/local/cache_files/books.xml +802 -0
- data/test/local/cache_files/electronics.xml +842 -0
- data/test/local/cache_files/movies.xml +1056 -0
- data/test/local/cache_files/music.xml +744 -0
- data/test/local/tc_local_search.rb +31 -0
- data/test/network/tc_amazon.rb +17 -0
- data/test/network/tc_aws.rb +136 -0
- data/test/network/tc_item_search.rb +25 -0
- data/test/network/tc_multiple_operation.rb +70 -0
- data/test/network/tc_operation_request.rb +62 -0
- data/test/network/tc_serialisation.rb +107 -0
- data/test/network/tc_shopping_cart.rb +217 -0
- data/test/network/tc_vehicle_operations.rb +109 -0
- data/test/test_helper.rb +16 -0
- metadata +79 -0
@@ -0,0 +1,31 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require File.dirname(__FILE__) + '/../test_helper'
|
3
|
+
require 'flexmock/test_unit'
|
4
|
+
|
5
|
+
class TestLocalSearch < Test::Unit::TestCase
|
6
|
+
|
7
|
+
def setup
|
8
|
+
@rg = ResponseGroup.new( :Small )
|
9
|
+
@req = Request.new(@@key_id, @@associates_id)
|
10
|
+
@req.locale = 'us'
|
11
|
+
@req.cache = false
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_movie_search
|
15
|
+
flexmock(Amazon::AWS).should_receive(:get_page).and_return(read_cache('movies.xml'))
|
16
|
+
|
17
|
+
is = ItemSearch.new( 'Books', { 'Title' => 'Ruby' } )
|
18
|
+
response = @req.search( is, @rg )
|
19
|
+
|
20
|
+
results = response.kernel
|
21
|
+
|
22
|
+
# Ensure we got some actual results back.
|
23
|
+
#
|
24
|
+
assert( results.size == 10 )
|
25
|
+
end
|
26
|
+
|
27
|
+
def read_cache(file)
|
28
|
+
data = File.open( File.dirname(__FILE__) + "/cache_files/#{file}" ).readlines.to_s
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require File.dirname(__FILE__) + '/../test_helper'
|
3
|
+
|
4
|
+
class TestAmazonBasics < Test::Unit::TestCase
|
5
|
+
|
6
|
+
def test_uncamelise
|
7
|
+
str = 'ALongStringWithACRONYM'
|
8
|
+
uncamel_str = 'a_long_string_with_acronym'
|
9
|
+
|
10
|
+
# Ensure uncamelisation of strings occurs properly.
|
11
|
+
#
|
12
|
+
assert_equal( uncamel_str, Amazon::uncamelise( str ) )
|
13
|
+
assert_equal( 'asin', Amazon::uncamelise( 'ASIN' ) )
|
14
|
+
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
@@ -0,0 +1,136 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require File.dirname(__FILE__) + '/../test_helper'
|
3
|
+
require 'fileutils'
|
4
|
+
require 'tmpdir'
|
5
|
+
require 'amazon/locale'
|
6
|
+
|
7
|
+
class TestAWSBasics < Test::Unit::TestCase
|
8
|
+
|
9
|
+
CACHE_DIR = Amazon::AWS::Cache::DEFAULT_CACHE_DIR
|
10
|
+
CACHE_PATH = File.join( Dir.tmpdir, 'aws_cache' )
|
11
|
+
|
12
|
+
def setup
|
13
|
+
@rg = ResponseGroup.new( :Small )
|
14
|
+
@req = Request.new(@@key_id, @@associates_id)
|
15
|
+
@req.locale = 'uk'
|
16
|
+
@req.cache = false
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_version
|
20
|
+
v = '1.8.6'
|
21
|
+
assert( RUBY_VERSION >= v, "Ruby version is lower than #{v}." )
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_cache
|
25
|
+
|
26
|
+
# Assure we can create a cache with a non-default path.
|
27
|
+
#
|
28
|
+
c = Cache.new( CACHE_PATH )
|
29
|
+
assert( CACHE_PATH, c.path )
|
30
|
+
assert( File.directory?( c.path ) )
|
31
|
+
FileUtils.rmdir( c.path )
|
32
|
+
|
33
|
+
c = Cache.new
|
34
|
+
|
35
|
+
# Ensure that default cache path has been chosen.
|
36
|
+
#
|
37
|
+
assert( CACHE_DIR, c.path )
|
38
|
+
|
39
|
+
# Ensure that cache directory has been created.
|
40
|
+
#
|
41
|
+
assert( File.directory?( c.path ) )
|
42
|
+
|
43
|
+
f = File.join( c.path, 'foobar' )
|
44
|
+
|
45
|
+
# Just over a day ago.
|
46
|
+
#
|
47
|
+
t = Time.now - 86460
|
48
|
+
|
49
|
+
FileUtils.touch f, { :mtime => t }
|
50
|
+
assert( File.exist?( f ) )
|
51
|
+
|
52
|
+
# Ensure expired file is properly flushed.
|
53
|
+
#
|
54
|
+
c.flush_expired
|
55
|
+
assert( ! File.exist?( f ) )
|
56
|
+
|
57
|
+
FileUtils.touch f
|
58
|
+
assert( File.exist?( f ) )
|
59
|
+
|
60
|
+
# Ensure unexpired file is properly retained.
|
61
|
+
#
|
62
|
+
c.flush_expired
|
63
|
+
assert( File.exist?( f ) )
|
64
|
+
|
65
|
+
# Ensure file is properly flushed.
|
66
|
+
#
|
67
|
+
c.flush_all
|
68
|
+
assert( ! File.exist?( f ) )
|
69
|
+
|
70
|
+
h = Help.new( :ResponseGroup, :Large )
|
71
|
+
h_rg = ResponseGroup.new( :Help )
|
72
|
+
|
73
|
+
# Ensure that file is not cached when no cache is desired.
|
74
|
+
#
|
75
|
+
@req.cache = false
|
76
|
+
resp = @req.search( h, h_rg )
|
77
|
+
assert_equal( 0, Dir.glob( File.join( c.path, '*' ) ).size )
|
78
|
+
|
79
|
+
# Ensure that file is cached when cache is desired.
|
80
|
+
#
|
81
|
+
@req.cache = c
|
82
|
+
resp = @req.search( h, h_rg )
|
83
|
+
assert_equal( 1, Dir.glob( File.join( c.path, '*' ) ).size )
|
84
|
+
|
85
|
+
# Flush it away.
|
86
|
+
#
|
87
|
+
c.flush_all
|
88
|
+
assert_equal( 0, Dir.glob( File.join( c.path, '*' ) ).size )
|
89
|
+
|
90
|
+
FileUtils.rmdir( c.path )
|
91
|
+
|
92
|
+
# Turn off caching for future tests. This happens in the setup method,
|
93
|
+
# anyway, but it can't hurt to tidy up after ourselves.
|
94
|
+
#
|
95
|
+
@req.cache = false
|
96
|
+
|
97
|
+
end
|
98
|
+
|
99
|
+
def test_config
|
100
|
+
# Test bad quoting.
|
101
|
+
#
|
102
|
+
assert_raise( Amazon::Config::ConfigError ) do
|
103
|
+
cf = Amazon::Config.new( <<' EOF' )
|
104
|
+
bad_syntax = 'bad quotes"
|
105
|
+
EOF
|
106
|
+
end
|
107
|
+
|
108
|
+
# Test good quoting.
|
109
|
+
#
|
110
|
+
assert_nothing_raised do
|
111
|
+
cf = Amazon::Config.new( <<' EOF' )
|
112
|
+
good_syntax = 'good quotes'
|
113
|
+
EOF
|
114
|
+
end
|
115
|
+
|
116
|
+
# Test that config files are properly read from $AMAZONRC.
|
117
|
+
#
|
118
|
+
Dir.mktmpdir do |td|
|
119
|
+
ENV['AMAZONRCDIR'] = td
|
120
|
+
ENV['AMAZONRCFILE'] = '.user_defined_name'
|
121
|
+
File.open( File.join( td, '.user_defined_name' ), 'w' ) do |tf|
|
122
|
+
tf.puts( 'foo = bar' )
|
123
|
+
end
|
124
|
+
|
125
|
+
cf = Amazon::Config.new
|
126
|
+
assert_equal( 'bar', cf['foo'] )
|
127
|
+
ENV['AMAZONRCDIR'] = nil
|
128
|
+
ENV['AMAZONRCFILE'] = nil
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
def test_exceptions
|
133
|
+
assert_raise( Amazon::AWS::HTTPError ) { raise Amazon::AWS::HTTPError }
|
134
|
+
end
|
135
|
+
|
136
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require File.dirname(__FILE__) + '/../test_helper'
|
3
|
+
|
4
|
+
class TestItemSearch < Test::Unit::TestCase
|
5
|
+
|
6
|
+
def setup
|
7
|
+
@rg = ResponseGroup.new( :Small )
|
8
|
+
@req = Request.new(@@key_id, @@associates_id)
|
9
|
+
@req.locale = 'uk'
|
10
|
+
@req.cache = false
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_item_search
|
14
|
+
is = ItemSearch.new( 'Books', { 'Title' => 'Ruby' } )
|
15
|
+
response = @req.search( is, @rg )
|
16
|
+
|
17
|
+
results = response.kernel
|
18
|
+
|
19
|
+
# Ensure we got some actual results back.
|
20
|
+
#
|
21
|
+
assert( results.size > 0 )
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require File.dirname(__FILE__) + '/../test_helper'
|
3
|
+
|
4
|
+
class TestMultipleOperation < Test::Unit::TestCase
|
5
|
+
|
6
|
+
def setup
|
7
|
+
@rg = ResponseGroup.new( :Small )
|
8
|
+
@req = Request.new(@@key_id, @@associates_id)
|
9
|
+
@req.locale = 'uk'
|
10
|
+
@req.cache = false
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_item_search
|
14
|
+
il = ItemLookup.new( 'ASIN', { 'ItemId' => 'B000AE4QEC',
|
15
|
+
'MerchantId' => 'Amazon' } )
|
16
|
+
il2 = ItemLookup.new( 'ASIN', { 'ItemId' => 'B000051WBE',
|
17
|
+
'MerchantId' => 'Amazon' } )
|
18
|
+
|
19
|
+
# Create a batch request of the two ItemLookup operations.
|
20
|
+
#
|
21
|
+
il.batch( il2 )
|
22
|
+
|
23
|
+
is = ItemSearch.new( 'Books', { 'Title' => 'Ruby' } )
|
24
|
+
|
25
|
+
# Create a multiple operation of the ItemSearch operation and the two
|
26
|
+
# batched ItemLookup operations.
|
27
|
+
#
|
28
|
+
mo = MultipleOperation.new( is, il )
|
29
|
+
|
30
|
+
response = @req.search( mo, @rg )
|
31
|
+
|
32
|
+
mor = response.multi_operation_response[0]
|
33
|
+
|
34
|
+
# Ensure we received a MultiOperationResponse.
|
35
|
+
#
|
36
|
+
assert_instance_of( Amazon::AWS::AWSObject::MultiOperationResponse, mor )
|
37
|
+
|
38
|
+
# Ensure response contains an ItemSearchResponse.
|
39
|
+
#
|
40
|
+
assert_instance_of( Amazon::AWS::AWSObject::ItemSearchResponse,
|
41
|
+
mor.item_search_response[0] )
|
42
|
+
|
43
|
+
# Ensure response also contains an ItemLookupResponse.
|
44
|
+
#
|
45
|
+
assert_instance_of( Amazon::AWS::AWSObject::ItemLookupResponse,
|
46
|
+
mor.item_lookup_response[0] )
|
47
|
+
|
48
|
+
is_set = response.multi_operation_response.item_search_response[0].items
|
49
|
+
il_set = response.multi_operation_response.item_lookup_response[0].items
|
50
|
+
is_arr = is_set.item
|
51
|
+
il_arr1 = il_set[0].item
|
52
|
+
il_arr2 = il_set[1].item
|
53
|
+
|
54
|
+
# Ensure that there's one <ItemSet> for the ItemSearch.
|
55
|
+
#
|
56
|
+
assert_equal( 1, is_set.size )
|
57
|
+
|
58
|
+
# Ensure that there are two <ItemSet>s for the ItemLookup, because it was
|
59
|
+
# a batched operation.
|
60
|
+
#
|
61
|
+
assert_equal( 2, il_set.size )
|
62
|
+
|
63
|
+
# Assure that all item sets have some results.
|
64
|
+
#
|
65
|
+
assert( is_arr.size > 0 )
|
66
|
+
assert( il_arr1.size > 0 )
|
67
|
+
assert( il_arr2.size > 0 )
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require File.dirname(__FILE__) + '/../test_helper'
|
3
|
+
|
4
|
+
class TestOperationRequest < Test::Unit::TestCase
|
5
|
+
|
6
|
+
def setup
|
7
|
+
@rg = ResponseGroup.new( :Small )
|
8
|
+
@req = Request.new(@@key_id, @@associates_id)
|
9
|
+
@req.locale = 'uk'
|
10
|
+
@req.cache = false
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_operation_request
|
14
|
+
is = ItemSearch.new( 'Books', { 'Title' => 'Ruby' } )
|
15
|
+
response = @req.search( is, ResponseGroup.new( 'Request' ) )
|
16
|
+
|
17
|
+
# Same again with Symbols.
|
18
|
+
#
|
19
|
+
is = ItemSearch.new( :Books, { :Title => 'Ruby' } )
|
20
|
+
response = @req.search( is, ResponseGroup.new( :Request ) )
|
21
|
+
|
22
|
+
# Make sure undocumented AWSObject#results provides an accurate shortcut
|
23
|
+
# to the most interesting part of the data returned by AWS.
|
24
|
+
#
|
25
|
+
assert_equal( response.item_search_response[0].items[0].item,
|
26
|
+
response.kernel )
|
27
|
+
|
28
|
+
# Ensure response is an Amazon::AWS::AWSObject.
|
29
|
+
#
|
30
|
+
assert_instance_of( Amazon::AWS::AWSObject, response )
|
31
|
+
|
32
|
+
# Ensure non-existent instance variables return nil.
|
33
|
+
#
|
34
|
+
assert_nil( response.foo_bar_baz )
|
35
|
+
|
36
|
+
# Ensure top level of response is an Amazon::AWS::AWSArray.
|
37
|
+
#
|
38
|
+
assert_instance_of( Amazon::AWS::AWSArray, response.item_search_response )
|
39
|
+
|
40
|
+
# Ensure delegation of method from AWSArray to single element.
|
41
|
+
#
|
42
|
+
assert_equal( response.item_search_response[0].operation_request,
|
43
|
+
response.item_search_response.operation_request )
|
44
|
+
|
45
|
+
# Test for correct user-agent in response.
|
46
|
+
#
|
47
|
+
assert_equal( Amazon::AWS::USER_AGENT,
|
48
|
+
response.item_search_response[0].operation_request[0].
|
49
|
+
http_headers[0].header[0].attrib['value'] )
|
50
|
+
|
51
|
+
# Ensure that the correct version of the AWS API was requested.
|
52
|
+
#
|
53
|
+
response.item_search_response[0].operation_request[0].arguments[0].
|
54
|
+
argument.each do |arg|
|
55
|
+
next unless arg.attrib['name'] == 'Version'
|
56
|
+
assert_equal( Amazon::AWS::SERVICE['Version'], arg.attrib['value'] )
|
57
|
+
break
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
@@ -0,0 +1,107 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require File.dirname(__FILE__) + '/../test_helper'
|
3
|
+
require 'yaml'
|
4
|
+
require 'tempfile'
|
5
|
+
|
6
|
+
class AWSSerialisationTest < Test::Unit::TestCase
|
7
|
+
|
8
|
+
def setup
|
9
|
+
@rg = ResponseGroup.new( :Small )
|
10
|
+
@req = Request.new(@@key_id, @@associates_id)
|
11
|
+
@req.locale = 'uk'
|
12
|
+
@req.cache = false
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_yaml_load
|
16
|
+
|
17
|
+
results_file = Tempfile.new( 'ruby_aws' )
|
18
|
+
|
19
|
+
# Serialise some results.
|
20
|
+
#
|
21
|
+
is = ItemSearch.new( 'Music', { 'Artist' => 'Voice Of The Beehive' } )
|
22
|
+
response = @req.search( is, @rg )
|
23
|
+
results = response.kernel
|
24
|
+
|
25
|
+
YAML.dump( results, results_file )
|
26
|
+
results = nil
|
27
|
+
|
28
|
+
# Remove knowledge of Amazon::AWS::AWSObject, so that YAML.load knows
|
29
|
+
# nothing of its subclasses.
|
30
|
+
#
|
31
|
+
Amazon::AWS.module_eval( %Q( remove_const :AWSObject ) )
|
32
|
+
|
33
|
+
# Silence warnings about redefined constants.
|
34
|
+
#
|
35
|
+
v = $VERBOSE
|
36
|
+
$VERBOSE = nil
|
37
|
+
|
38
|
+
# Reload Amazon::AWS and friends.
|
39
|
+
#
|
40
|
+
load 'amazon/aws.rb'
|
41
|
+
|
42
|
+
# Reset warning status.
|
43
|
+
#
|
44
|
+
$VERBOSE = v
|
45
|
+
|
46
|
+
# Demonstrate that normal YAML.load can't cope with instantiating objects
|
47
|
+
# from classes it knows nothing about.
|
48
|
+
#
|
49
|
+
results_file.open
|
50
|
+
results = YAML.load( results_file )
|
51
|
+
assert_instance_of( YAML::Object, results[0] )
|
52
|
+
|
53
|
+
# Ensure that AWSObject.yaml_load does the right thing.
|
54
|
+
#
|
55
|
+
results_file.open
|
56
|
+
results = Amazon::AWS::AWSObject.yaml_load( results_file )
|
57
|
+
assert_instance_of( Amazon::AWS::AWSObject::Item, results[0] )
|
58
|
+
end
|
59
|
+
|
60
|
+
|
61
|
+
def test_marshal_load
|
62
|
+
|
63
|
+
results_file = Tempfile.new( 'ruby_aws' )
|
64
|
+
|
65
|
+
# Serialise some results.
|
66
|
+
#
|
67
|
+
is = ItemSearch.new( 'Music', { 'Artist' => 'Voice Of The Beehive' } )
|
68
|
+
response = @req.search( is, @rg )
|
69
|
+
results = response.kernel
|
70
|
+
|
71
|
+
results_file.puts Marshal.dump( results )
|
72
|
+
results = nil
|
73
|
+
|
74
|
+
# Remove knowledge of Amazon::AWS::AWSObject, so that Marshal.load knows
|
75
|
+
# nothing of its subclasses.
|
76
|
+
#
|
77
|
+
Amazon::AWS.module_eval( %Q( remove_const :AWSObject ) )
|
78
|
+
|
79
|
+
# Silence warnings about redefined constants.
|
80
|
+
#
|
81
|
+
v = $VERBOSE
|
82
|
+
$VERBOSE = nil
|
83
|
+
|
84
|
+
# Reload Amazon::AWS and friends.
|
85
|
+
#
|
86
|
+
load 'amazon/aws.rb'
|
87
|
+
|
88
|
+
# Reset warning status.
|
89
|
+
#
|
90
|
+
$VERBOSE = v
|
91
|
+
|
92
|
+
# Demonstrate that normal Marshal.load can't cope with instantiating
|
93
|
+
# objects from classes it knows nothing about.
|
94
|
+
#
|
95
|
+
results_file.open
|
96
|
+
|
97
|
+
assert_raise ArgumentError do
|
98
|
+
Marshal.load( results_file )
|
99
|
+
end
|
100
|
+
|
101
|
+
# Ensure that Amazon::AWS::AWSObject.load does the right thing.
|
102
|
+
#
|
103
|
+
results_file.open
|
104
|
+
results = Amazon::AWS::AWSObject.load( results_file )
|
105
|
+
assert_instance_of( Amazon::AWS::AWSObject::Item, results[0] )
|
106
|
+
end
|
107
|
+
end
|