bare-ruby-aws 0.1

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.
@@ -0,0 +1,56 @@
1
+ # $Id: setup.rb,v 1.6 2009/09/16 22:31:07 ianmacd Exp $
2
+ #
3
+
4
+ # Attempt to load Ruby/AWS using RubyGems.
5
+ #
6
+ begin
7
+ require 'rubygems'
8
+ gem 'ruby-aws'
9
+ rescue LoadError
10
+ # Either we don't have RubyGems or we don't have a gem of Ruby/AWS.
11
+ end
12
+
13
+ # Require the essential library, be it via RubyGems or the default way.
14
+ #
15
+ require 'amazon/aws/search'
16
+
17
+ include Amazon::AWS
18
+ include Amazon::AWS::Search
19
+
20
+ class AWSTest < Test::Unit::TestCase
21
+
22
+ # Figure out where the user config file is.
23
+ #
24
+ home = ENV['AMAZONRCDIR'] ||
25
+ ENV['HOME'] || ENV['HOMEDRIVE'] + ENV['HOMEPATH'] ||
26
+ ENV['USERPROFILE']
27
+ user_rcfile = ENV['AMAZONRCFILE'] || '.amazonrc'
28
+ rc = File.expand_path( File.join( home, user_rcfile ) )
29
+
30
+ # Replace the locale with UK.
31
+ #
32
+ lines = File.open( rc ) { |f| f.readlines }
33
+ lines.each { |l| l.sub!( /(locale\s*=\s*['"])..(['"])/, "\\1uk\\2" ) }
34
+
35
+ # Write a new config file for the purpose of unit testing.
36
+ #
37
+ test_rc = File.join( Dir.pwd, '.amazonrc' )
38
+ File.open( test_rc, 'w' ) { |f| f.puts lines } unless File.exist?( test_rc )
39
+
40
+ # Make sure the unit tests use the new config file.
41
+ #
42
+ ENV['AMAZONRCDIR'] = Dir.pwd
43
+
44
+ def setup
45
+ @rg = ResponseGroup.new( :Small )
46
+ @req = Request.new
47
+ @req.locale = 'uk'
48
+ @req.cache = false
49
+ @req.encoding = 'utf-8'
50
+ end
51
+
52
+ # The default_test method needs to be removed before Ruby 1.9.0.
53
+ #
54
+ undef_method :default_test if method_defined? :default_test
55
+
56
+ end
@@ -0,0 +1,20 @@
1
+ # $Id: tc_amazon.rb,v 1.1 2008/05/19 10:17:26 ianmacd Exp $
2
+ #
3
+
4
+ require 'test/unit'
5
+ require './setup'
6
+
7
+ class TestAmazonBasics < AWSTest
8
+
9
+ def test_uncamelise
10
+ str = 'ALongStringWithACRONYM'
11
+ uncamel_str = 'a_long_string_with_acronym'
12
+
13
+ # Ensure uncamelisation of strings occurs properly.
14
+ #
15
+ assert_equal( uncamel_str, Amazon::uncamelise( str ) )
16
+ assert_equal( 'asin', Amazon::uncamelise( 'ASIN' ) )
17
+
18
+ end
19
+
20
+ end
@@ -0,0 +1,160 @@
1
+ # $Id: tc_aws.rb,v 1.15 2010/02/20 23:59:02 ianmacd Exp $
2
+ #
3
+
4
+ require 'test/unit'
5
+ require './setup'
6
+ require 'fileutils'
7
+ require 'tmpdir'
8
+ require 'amazon/locale'
9
+
10
+ class TestAWSBasics < AWSTest
11
+
12
+ CACHE_DIR = Amazon::AWS::Cache::DEFAULT_CACHE_DIR
13
+ CACHE_PATH = File.join( Dir.tmpdir, 'aws_cache' )
14
+
15
+ def test_version
16
+ v = '1.8.6'
17
+ assert( RUBY_VERSION >= v, "Ruby version is lower than #{v}." )
18
+ end
19
+
20
+ def test_cache
21
+
22
+ # Assure we can create a cache with a non-default path.
23
+ #
24
+ c = Cache.new( CACHE_PATH )
25
+ assert( CACHE_PATH, c.path )
26
+ assert( File.directory?( c.path ) )
27
+ FileUtils.rmdir( c.path )
28
+
29
+ c = Cache.new
30
+
31
+ # Ensure that default cache path has been chosen.
32
+ #
33
+ assert( CACHE_DIR, c.path )
34
+
35
+ # Ensure that cache directory has been created.
36
+ #
37
+ assert( File.directory?( c.path ) )
38
+
39
+ f = File.join( c.path, 'foobar' )
40
+
41
+ # Just over a day ago.
42
+ #
43
+ t = Time.now - 86460
44
+
45
+ FileUtils.touch f, { :mtime => t }
46
+ assert( File.exist?( f ) )
47
+
48
+ # Ensure expired file is properly flushed.
49
+ #
50
+ c.flush_expired
51
+ assert( ! File.exist?( f ) )
52
+
53
+ FileUtils.touch f
54
+ assert( File.exist?( f ) )
55
+
56
+ # Ensure unexpired file is properly retained.
57
+ #
58
+ c.flush_expired
59
+ assert( File.exist?( f ) )
60
+
61
+ # Ensure file is properly flushed.
62
+ #
63
+ c.flush_all
64
+ assert( ! File.exist?( f ) )
65
+
66
+ h = Help.new( :ResponseGroup, :Large )
67
+ h_rg = ResponseGroup.new( :Help )
68
+ h.response_group = h_rg
69
+
70
+ # Ensure that file is not cached when no cache is desired.
71
+ #
72
+ @req.cache = false
73
+ resp = @req.search( h )
74
+ assert_equal( 0, Dir.glob( File.join( c.path, '*' ) ).size )
75
+
76
+ # Ensure that file is cached when cache is desired.
77
+ #
78
+ @req.cache = c
79
+ resp = @req.search( h )
80
+ assert_equal( 1, Dir.glob( File.join( c.path, '*' ) ).size )
81
+
82
+ # Flush it away.
83
+ #
84
+ c.flush_all
85
+ assert_equal( 0, Dir.glob( File.join( c.path, '*' ) ).size )
86
+
87
+ FileUtils.rmdir( c.path )
88
+
89
+ # Turn off caching for future tests. This happens in the setup method,
90
+ # anyway, but it can't hurt to tidy up after ourselves.
91
+ #
92
+ @req.cache = false
93
+
94
+ end
95
+
96
+ def test_config
97
+ # Test bad quoting.
98
+ #
99
+ assert_raise( Amazon::Config::ConfigError ) do
100
+ cf = Amazon::Config.new( <<' EOF' )
101
+ bad_syntax = 'bad quotes"
102
+ EOF
103
+ end
104
+
105
+ # Test good quoting.
106
+ #
107
+ assert_nothing_raised do
108
+ cf = Amazon::Config.new( <<' EOF' )
109
+ good_syntax = 'good quotes'
110
+ EOF
111
+ end
112
+
113
+ # Test that config files are properly read from $AMAZONRC.
114
+ #
115
+ Dir.mktmpdir do |td|
116
+ # First save old locations, in case they're not the default.
117
+ #
118
+ old_rcdir = ENV['AMAZONRCDIR']
119
+ old_rc_file = ENV['AMAZONRCFILE']
120
+
121
+ ENV['AMAZONRCDIR'] = td
122
+ ENV['AMAZONRCFILE'] = '.user_defined_name'
123
+ File.open( File.join( td, '.user_defined_name' ), 'w' ) do |tf|
124
+ tf.puts( 'foo = bar' )
125
+ end
126
+
127
+ cf = Amazon::Config.new
128
+ assert_equal( 'bar', cf['foo'] )
129
+
130
+ # Restore old locations.
131
+ #
132
+ ENV['AMAZONRCDIR'] = old_rcdir
133
+ ENV['AMAZONRCFILE'] = old_rc_file
134
+ end
135
+ end
136
+
137
+ def test_exceptions
138
+ assert_raise( Amazon::AWS::HTTPError ) { raise Amazon::AWS::HTTPError }
139
+ end
140
+
141
+ include Amazon
142
+
143
+ def test_geo
144
+
145
+ require 'net/geoip'
146
+
147
+ assert_equal( 'de', Locale.get_locale_by_name( 'www.marxer.org' ) )
148
+ assert_equal( 'jp', Locale.get_locale_by_name( 'ruby-lang.org' ) )
149
+ assert_equal( 'uk', Locale.get_locale_by_name( 'xs1.xs4all.nl' ) )
150
+ assert_equal( 'uk', Locale.get_locale_by_name( 'caliban.org' ) )
151
+ assert_equal( 'us', Locale.get_locale_by_name( 'google.com' ) )
152
+
153
+ # Ensure non-existent hostname causes an Amazon::Locale::GeoError.
154
+ #
155
+ assert_raise( Amazon::Locale::GeoError ) do
156
+ Locale.get_locale_by_name( 'marxer.org' )
157
+ end
158
+ end
159
+
160
+ end
@@ -0,0 +1,105 @@
1
+ # encoding: ASCII-8BIT
2
+ #
3
+ # $Id: tc_item_search.rb,v 1.8 2010/02/20 23:57:26 ianmacd Exp $
4
+ #
5
+ # The encoding at the top of this file is necessary for Ruby 1.9, which will
6
+ # otherwise choke with 'invalid multibyte char (US-ASCII)' when it reads the
7
+ # ISO-8859-1 encoded accented 'e' in the test_item_search_iso_8859_15 method.
8
+
9
+ require 'test/unit'
10
+ require './setup'
11
+
12
+ class TestItemSearch < AWSTest
13
+
14
+ def test_item_search_iso_8859_15
15
+
16
+ # Ensure that character set encoding works properly by manually trying
17
+ # ISO-8859-15, a.k.a. Latin-15.
18
+ #
19
+ @req.encoding = 'iso-8859-15'
20
+
21
+ str = 'Caf�'
22
+ is = ItemSearch.new( 'Books', { 'Title' => str } )
23
+ response = @req.search( is )
24
+
25
+ results = response.kernel
26
+
27
+ # Ensure we got some actual results back.
28
+ #
29
+ assert( results.size > 0 )
30
+
31
+ end
32
+
33
+ def test_item_search_utf8
34
+
35
+ # Manually set UTF-8 encoding.
36
+ #
37
+ @req.encoding = 'utf-8'
38
+
39
+ str = 'Café'
40
+ is = ItemSearch.new( 'Books', { 'Title' => str } )
41
+ response = @req.search( is )
42
+
43
+ results = response.kernel
44
+
45
+ # Ensure we got some actual results back.
46
+ #
47
+ assert( results.size > 0 )
48
+
49
+ end
50
+
51
+ def test_item_search_obsolete_rg_passing
52
+
53
+ # Manually set UTF-8 encoding.
54
+ #
55
+ @req.encoding = 'utf-8'
56
+
57
+ str = 'Café'
58
+ is = ItemSearch.new( 'Books', { 'Title' => str } )
59
+
60
+ assert_raise( Amazon::AWS::ObsolescenceError ) do
61
+ response = @req.search( is, @rg )
62
+ end
63
+
64
+ end
65
+
66
+ def test_item_search_multiple_pages
67
+
68
+ @req.encoding = 'utf-8'
69
+ is = ItemSearch.new( 'Books', { 'Title' => 'programming' } )
70
+ responses = @req.search( is, 5 )
71
+
72
+ results = []
73
+ responses.each { |response| results += response.kernel }
74
+
75
+ # Ensure we got more than 10 results back.
76
+ #
77
+ assert( results.size > 10 )
78
+
79
+ end
80
+
81
+ def test_item_search_class_method
82
+
83
+ response = Amazon::AWS.item_search( 'Books', { 'Title' => 'programming' } )
84
+
85
+ results = response.kernel
86
+
87
+ # Ensure we got some actual results back.
88
+ #
89
+ assert( results.size > 0 )
90
+
91
+ end
92
+
93
+ def test_item_search_class_method_block
94
+
95
+ Amazon::AWS.item_search( 'Books', { 'Title' => 'programming' } ) do |r|
96
+
97
+ results = r.kernel
98
+
99
+ # Ensure we got some actual results back.
100
+ #
101
+ assert( results.size > 0 )
102
+ end
103
+ end
104
+
105
+ end
@@ -0,0 +1,64 @@
1
+ # $Id: tc_operation_request.rb,v 1.2 2010/02/20 17:15:18 ianmacd Exp $
2
+ #
3
+
4
+ require 'test/unit'
5
+ require './setup'
6
+
7
+ class TestOperationRequest < AWSTest
8
+
9
+ def test_operation_request
10
+ is = ItemSearch.new( 'Books', { 'Title' => 'Ruby' } )
11
+ is_rg = ResponseGroup.new( 'Request' )
12
+ is.response_group = is_rg
13
+
14
+ response = @req.search( is )
15
+
16
+ # Same again with Symbols.
17
+ #
18
+ is = ItemSearch.new( :Books, { :Title => 'Ruby' } )
19
+ is_rg = ResponseGroup.new( :Request )
20
+ is.response_group = is_rg
21
+
22
+ response = @req.search( is )
23
+
24
+ # Make sure undocumented AWSObject#results provides an accurate shortcut
25
+ # to the most interesting part of the data returned by AWS.
26
+ #
27
+ assert_equal( response.item_search_response[0].items[0].item,
28
+ response.kernel )
29
+
30
+ # Ensure response is an Amazon::AWS::AWSObject.
31
+ #
32
+ assert_instance_of( Amazon::AWS::AWSObject, response )
33
+
34
+ # Ensure non-existent instance variables return nil.
35
+ #
36
+ assert_nil( response.foo_bar_baz )
37
+
38
+ # Ensure top level of response is an Amazon::AWS::AWSArray.
39
+ #
40
+ assert_instance_of( Amazon::AWS::AWSArray, response.item_search_response )
41
+
42
+ # Ensure delegation of method from AWSArray to single element.
43
+ #
44
+ assert_equal( response.item_search_response[0].operation_request,
45
+ response.item_search_response.operation_request )
46
+
47
+ # Test for correct user-agent in response.
48
+ #
49
+ assert_equal( Amazon::AWS::USER_AGENT,
50
+ response.item_search_response[0].operation_request[0].
51
+ http_headers[0].header[0].attrib['value'] )
52
+
53
+ # Ensure that the correct version of the AWS API was requested.
54
+ #
55
+ response.item_search_response[0].operation_request[0].arguments[0].
56
+ argument.each do |arg|
57
+ next unless arg.attrib['name'] == 'Version'
58
+ assert_equal( Amazon::AWS::SERVICE['Version'], arg.attrib['value'] )
59
+ break
60
+ end
61
+
62
+ end
63
+
64
+ end
@@ -0,0 +1,107 @@
1
+ # $Id: tc_serialisation.rb,v 1.3 2010/02/20 17:15:18 ianmacd Exp $
2
+ #
3
+
4
+ require 'test/unit'
5
+ require './setup'
6
+ require 'yaml'
7
+ require 'tempfile'
8
+
9
+ class AWSSerialisationTest < AWSTest
10
+
11
+ def test_yaml_load
12
+
13
+ results_file = Tempfile.new( 'ruby_aws' )
14
+
15
+ # Serialise some results.
16
+ #
17
+ is = ItemSearch.new( 'Music', { 'Artist' => 'Voice Of The Beehive' } )
18
+ is.response_group = @rg
19
+
20
+ response = @req.search( is )
21
+ results = response.kernel
22
+
23
+ YAML.dump( results, results_file )
24
+ results = nil
25
+
26
+ # Remove knowledge of Amazon::AWS::AWSObject, so that YAML.load knows
27
+ # nothing of its subclasses.
28
+ #
29
+ Amazon::AWS.module_eval( %Q( remove_const :AWSObject ) )
30
+
31
+ # Silence warnings about redefined constants.
32
+ #
33
+ v = $VERBOSE
34
+ $VERBOSE = nil
35
+
36
+ # Reload Amazon::AWS and friends.
37
+ #
38
+ load 'amazon/aws.rb'
39
+
40
+ # Reset warning status.
41
+ #
42
+ $VERBOSE = v
43
+
44
+ # Demonstrate that normal YAML.load can't cope with instantiating objects
45
+ # from classes it knows nothing about.
46
+ #
47
+ results_file.open
48
+ results = YAML.load( results_file )
49
+ assert_instance_of( YAML::Object, results[0] )
50
+
51
+ # Ensure that AWSObject.yaml_load does the right thing.
52
+ #
53
+ results_file.open
54
+ results = Amazon::AWS::AWSObject.yaml_load( results_file )
55
+ assert_instance_of( Amazon::AWS::AWSObject::Item, results[0] )
56
+ end
57
+
58
+
59
+ def test_marshal_load
60
+
61
+ results_file = Tempfile.new( 'ruby_aws' )
62
+
63
+ # Serialise some results.
64
+ #
65
+ is = ItemSearch.new( 'Music', { 'Artist' => 'Voice Of The Beehive' } )
66
+ is.response_group = @rg
67
+
68
+ response = @req.search( is )
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