sprout-ruby-aws 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (70) hide show
  1. data/History.txt +38 -0
  2. data/LICENSE.txt +202 -0
  3. data/Manifest.txt +69 -0
  4. data/NOTICE.txt +4 -0
  5. data/README.txt +105 -0
  6. data/Rakefile +20 -0
  7. data/bin/ruby-aws +9 -0
  8. data/lib/amazon/util.rb +10 -0
  9. data/lib/amazon/util/binder.rb +44 -0
  10. data/lib/amazon/util/data_reader.rb +157 -0
  11. data/lib/amazon/util/filter_chain.rb +79 -0
  12. data/lib/amazon/util/hash_nesting.rb +93 -0
  13. data/lib/amazon/util/lazy_results.rb +59 -0
  14. data/lib/amazon/util/logging.rb +23 -0
  15. data/lib/amazon/util/paginated_iterator.rb +70 -0
  16. data/lib/amazon/util/proactive_results.rb +116 -0
  17. data/lib/amazon/util/threadpool.rb +129 -0
  18. data/lib/amazon/util/user_data_store.rb +100 -0
  19. data/lib/amazon/webservices/mechanical_turk.rb +117 -0
  20. data/lib/amazon/webservices/mechanical_turk_requester.rb +340 -0
  21. data/lib/amazon/webservices/mturk/mechanical_turk_error_handler.rb +136 -0
  22. data/lib/amazon/webservices/mturk/question_generator.rb +58 -0
  23. data/lib/amazon/webservices/util/amazon_authentication_relay.rb +64 -0
  24. data/lib/amazon/webservices/util/command_line.rb +156 -0
  25. data/lib/amazon/webservices/util/convenience_wrapper.rb +90 -0
  26. data/lib/amazon/webservices/util/filter_proxy.rb +45 -0
  27. data/lib/amazon/webservices/util/mock_transport.rb +70 -0
  28. data/lib/amazon/webservices/util/request_signer.rb +42 -0
  29. data/lib/amazon/webservices/util/rest_transport.rb +108 -0
  30. data/lib/amazon/webservices/util/soap_simplifier.rb +48 -0
  31. data/lib/amazon/webservices/util/soap_transport.rb +38 -0
  32. data/lib/amazon/webservices/util/soap_transport_header_handler.rb +27 -0
  33. data/lib/amazon/webservices/util/unknown_result_exception.rb +27 -0
  34. data/lib/amazon/webservices/util/validation_exception.rb +55 -0
  35. data/lib/amazon/webservices/util/xml_simplifier.rb +61 -0
  36. data/lib/ruby-aws.rb +21 -0
  37. data/lib/ruby-aws/version.rb +8 -0
  38. data/samples/mturk/best_image/BestImage.rb +61 -0
  39. data/samples/mturk/best_image/best_image.properties +39 -0
  40. data/samples/mturk/best_image/best_image.question +82 -0
  41. data/samples/mturk/blank_slate/BlankSlate.rb +63 -0
  42. data/samples/mturk/blank_slate/BlankSlate_multithreaded.rb +67 -0
  43. data/samples/mturk/helloworld/MTurkHelloWorld.rb +56 -0
  44. data/samples/mturk/helloworld/mturk.yml +8 -0
  45. data/samples/mturk/reviewer/Reviewer.rb +103 -0
  46. data/samples/mturk/reviewer/mturk.yml +8 -0
  47. data/samples/mturk/simple_survey/SimpleSurvey.rb +90 -0
  48. data/samples/mturk/simple_survey/simple_survey.question +30 -0
  49. data/samples/mturk/site_category/SiteCategory.rb +87 -0
  50. data/samples/mturk/site_category/externalpage.htm +71 -0
  51. data/samples/mturk/site_category/site_category.input +6 -0
  52. data/samples/mturk/site_category/site_category.properties +45 -0
  53. data/samples/mturk/site_category/site_category.question +9 -0
  54. data/test/mturk/test_changehittypeofhit.rb +130 -0
  55. data/test/mturk/test_error_handler.rb +135 -0
  56. data/test/mturk/test_mechanical_turk_requester.rb +178 -0
  57. data/test/mturk/test_mock_mechanical_turk_requester.rb +205 -0
  58. data/test/test_ruby-aws.rb +22 -0
  59. data/test/unit/test_binder.rb +89 -0
  60. data/test/unit/test_data_reader.rb +135 -0
  61. data/test/unit/test_exceptions.rb +32 -0
  62. data/test/unit/test_hash_nesting.rb +93 -0
  63. data/test/unit/test_lazy_results.rb +89 -0
  64. data/test/unit/test_mock_transport.rb +132 -0
  65. data/test/unit/test_paginated_iterator.rb +58 -0
  66. data/test/unit/test_proactive_results.rb +108 -0
  67. data/test/unit/test_question_generator.rb +54 -0
  68. data/test/unit/test_threadpool.rb +50 -0
  69. data/test/unit/test_user_data_store.rb +80 -0
  70. metadata +177 -0
@@ -0,0 +1,135 @@
1
+ # Copyright:: Copyright (c) 2007 Amazon Technologies, Inc.
2
+ # License:: Apache License, Version 2.0
3
+
4
+ require 'test/unit/testcase'
5
+ require 'ruby-aws'
6
+ require 'amazon/webservices/util/mock_transport'
7
+ require 'timeout'
8
+
9
+ class TestErrorHandler < Test::Unit::TestCase
10
+
11
+ def setup
12
+ @mock = Amazon::WebServices::Util::MockTransport.new
13
+ @mturk = Amazon::WebServices::MechanicalTurkRequester.new( :Transport => @mock, :AWSAccessKey => 'bogus', :AWSAccessKeyId => 'fake' )
14
+ end
15
+
16
+ def testTimeoutOnce
17
+
18
+ # mock will timeout first time, return success on second call
19
+ toggle = false
20
+ @mock.listen do |call|
21
+ Timeout.timeout(1) do
22
+ if toggle
23
+ toggle = !toggle
24
+ else
25
+ toggle = !toggle
26
+ sleep(5)
27
+ end
28
+ end
29
+ nil
30
+ end
31
+
32
+ assert_equal false, toggle
33
+ # invoke a retryable call once, should auto-retry and return success
34
+ @mturk.getAccountBalance
35
+ assert_equal 2, @mock.call_buffer.size, "Should have retried once"
36
+ @mock.each {|call| assert_equal :GetAccountBalance, call.name, "Should have been a GetAccountBalance call" }
37
+
38
+ @mock.flush
39
+ assert_equal false, toggle
40
+ # but invoking a non-retryable call will throw an exception
41
+ begin
42
+ @mturk.grantBonus
43
+ fail "Should have thrown an exception"
44
+ rescue Timeout::Error => e
45
+ # expect this exception
46
+ end
47
+ assert_equal true, toggle
48
+ assert_equal :GrantBonus, @mock.next.name
49
+ assert_nil @mock.next
50
+ end
51
+
52
+ def testTimeoutAlways
53
+
54
+ #mock will always timeout
55
+ @mock.listen do |call|
56
+ Timeout.timeout(1) { sleep(2) }
57
+ end
58
+
59
+ begin
60
+ @mturk.searchHITs
61
+ fail "Should have thrown an exception"
62
+ rescue Timeout::Error => e
63
+ # expect this exception
64
+ end
65
+
66
+ 7.times do
67
+ assert_equal :SearchHITs, @mock.next.name
68
+ end
69
+ assert_nil @mock.next
70
+ end
71
+
72
+
73
+ def testRuntimeError
74
+ @mock.listen do |call|
75
+ raise "Blah"
76
+ end
77
+
78
+ begin
79
+ @mturk.searchHITs
80
+ fail "Should have thrown an exception"
81
+ rescue RuntimeError => e
82
+ assert_equal "Blah", e.to_s
83
+ end
84
+ end
85
+
86
+ def testRuntimeErrorThrottled
87
+ @mock.listen do |call|
88
+ raise "Throttled"
89
+ end
90
+
91
+ begin
92
+ @mturk.searchHITs
93
+ fail "Should have thrown an exception"
94
+ rescue RuntimeError => e
95
+ assert_equal "Throttled", e.to_s
96
+ end
97
+ end
98
+
99
+ def testSOAPFaultError
100
+ arg = (Struct.new :faultcode, :faultstring, :faultactor, :detail).new
101
+ arg.faultcode = (Struct.new :data).new 'aws:blarg'
102
+ arg.faultstring = (Struct.new :data).new 'blarg blarg blarg'
103
+ s = SOAP::FaultError.new( arg )
104
+
105
+ @mock.listen do |call|
106
+ raise s
107
+ end
108
+
109
+ begin
110
+ @mturk.searchHITs
111
+ fail "Should have thrown an exception"
112
+ rescue SOAP::FaultError => e
113
+ assert_equal s, e
114
+ end
115
+ end
116
+
117
+ def testSOAPFaultErrorThrottled
118
+ arg = (Struct.new :faultcode, :faultstring, :faultactor, :detail).new
119
+ arg.faultcode = (Struct.new :data).new 'aws:Server.ServiceUnavailable'
120
+ arg.faultstring = (Struct.new :data).new 'Hey, give us a break!'
121
+ s = SOAP::FaultError.new( arg )
122
+
123
+ @mock.listen do |call|
124
+ raise s
125
+ end
126
+
127
+ begin
128
+ @mturk.searchHITs
129
+ fail "Should have thrown an exception"
130
+ rescue SOAP::FaultError => e
131
+ assert_equal s, e
132
+ end
133
+ end
134
+
135
+ end
@@ -0,0 +1,178 @@
1
+ # Copyright:: Copyright (c) 2007 Amazon Technologies, Inc.
2
+ # License:: Apache License, Version 2.0
3
+
4
+ require 'test/unit/testcase'
5
+ require 'ruby-aws'
6
+ require 'amazon/webservices/mturk/question_generator'
7
+ require 'amazon/webservices/util/soap_transport'
8
+ require 'amazon/webservices/util/rest_transport'
9
+
10
+ # Allow user to skip these tests by setting the DISABLE_MTURK_SANDBOX_TEST environment variable
11
+ if ENV['DISABLE_MTURK_SANDBOX_TEST'].nil?
12
+
13
+ class TestMechanicalTurkRequester < Test::Unit::TestCase
14
+ include Amazon::WebServices
15
+
16
+ def setup
17
+ # Setting up the default requester interface.
18
+ # This will default to running against Sandbox, but we'll the host explicitly for safety.
19
+ # Access Key ID and Secret Access Key will be loaded from user preference file
20
+ # If not found, the user will be interatively queried for their authentication information.
21
+ @@mturk ||= MechanicalTurkRequester.new( :Host => :Sandbox )
22
+
23
+ # utilize the SOAP transport, if we can
24
+ @@mturk_soap ||= MechanicalTurkRequester.new( :Host => :Sandbox, :Transport => :SOAP ) if Util::SOAPTransport.canSOAP?
25
+ # utilize the REST transport with default HTTP method
26
+ @@mturk_rest ||= MechanicalTurkRequester.new( :Host => :Sandbox, :Transport => :REST )
27
+ # utilize the REST transport via Get
28
+ @@mturk_rest_get ||= MechanicalTurkRequester.new( :Host => :Sandbox, :Transport => :REST, :RestStyle => :Get )
29
+ # utilize the REST transport via Post, if we can
30
+ @@mturk_rest_post ||= MechanicalTurkRequester.new( :Host => :Sandbox, :Transport => :REST, :RestStyle => :Post ) if Util::RESTTransport.canPost?
31
+ end
32
+
33
+ def testAvailableFunds
34
+ funds = @@mturk.availableFunds
35
+ assert_equal 10000.0, funds, "Sandbox should always show 10k available funds"
36
+ end
37
+
38
+ def testCreateHIT
39
+ question = MTurk::QuestionGenerator.build { |q| q.ask 'Have you tried Ruby?' }
40
+ result = @@mturk.createHIT :Title => 'Ruby SDK Test Hit',
41
+ :Description => 'Autogenerated hit from ruby-aws test cases',
42
+ :Keywords => 'Ruby, test, SDK',
43
+ :Reward => { :Amount => 23.44, :CurrencyCode => 'USD' },
44
+ :Question => question
45
+ assert_not_nil( result[:HITId] )
46
+ @@mturk.forceExpireHIT( :HITId => result[:HITId] )
47
+ @@mturk.disposeHIT( :HITId => result[:HITId] )
48
+ end
49
+
50
+ def testCreateHITFailure
51
+ begin
52
+ result = @@mturk.createHIT :Title => 'Ruby SDK Test Hit',
53
+ :Description => 'Autogenerated hit from ruby-aws test cases',
54
+ :Keywords => 'Ruby, test, SDK',
55
+ :Reward => { :Amount => 23.44, :CurrencyCode => 'USD' },
56
+ :Question => "Invalid Question"
57
+ fail "Mechanical Turk should have thrown an exception"
58
+ rescue => e
59
+ raise e unless e.is_a?( Util::ValidationException )
60
+ assert_equal "AWS.MechanicalTurk.XMLParseError", e.message
61
+ end
62
+ end
63
+
64
+ if Util::SOAPTransport.canSOAP?
65
+
66
+ def testSOAPCreateHIT
67
+ question = MTurk::QuestionGenerator.build { |q| q.ask 'Have you tried Ruby?' }
68
+ result = @@mturk_soap.createHIT :Title => 'Ruby SDK Test Hit',
69
+ :Description => 'Autogenerated hit from ruby-aws test cases',
70
+ :Keywords => 'Ruby, test, SDK',
71
+ :Reward => { :Amount => 23.44, :CurrencyCode => 'USD' },
72
+ :Question => question
73
+ assert_not_nil( result[:HITId] )
74
+ @@mturk_soap.forceExpireHIT( :HITId => result[:HITId] )
75
+ @@mturk_soap.disposeHIT( :HITId => result[:HITId] )
76
+ end
77
+
78
+ def testSOAPCreateHITFailure
79
+ begin
80
+ result = @@mturk_soap.createHIT :Title => 'Ruby SDK Test Hit',
81
+ :Description => 'Autogenerated hit from ruby-aws test cases',
82
+ :Keywords => 'Ruby, test, SDK',
83
+ :Reward => { :Amount => 23.44, :CurrencyCode => 'USD' },
84
+ :Question => "Invalid Question"
85
+ fail "Mechanical Turk should have thrown an exception"
86
+ rescue => e
87
+ raise e unless e.is_a?( Util::ValidationException )
88
+ assert_equal "AWS.MechanicalTurk.XMLParseError", e.message
89
+ end
90
+ end
91
+
92
+ end # if Util::SOAPTransport.canSOAP?
93
+
94
+ def testRESTCreateHIT
95
+ question = MTurk::QuestionGenerator.build { |q| q.ask 'Have you tried Ruby?' }
96
+ result = @@mturk_rest.createHIT :Title => 'Ruby SDK Test Hit',
97
+ :Description => 'Autogenerated hit from ruby-aws test cases',
98
+ :Keywords => 'Ruby, test, SDK',
99
+ :Reward => { :Amount => 23.44, :CurrencyCode => 'USD' },
100
+ :Question => question
101
+ assert_not_nil( result[:HITId] )
102
+ @@mturk_rest.forceExpireHIT( :HITId => result[:HITId] )
103
+ @@mturk_rest.disposeHIT( :HITId => result[:HITId] )
104
+ end
105
+
106
+ def testRESTCreateHITFailure
107
+ begin
108
+ result = @@mturk_rest.createHIT :Title => 'Ruby SDK Test Hit',
109
+ :Description => 'Autogenerated hit from ruby-aws test cases',
110
+ :Keywords => 'Ruby, test, SDK',
111
+ :Reward => { :Amount => 23.44, :CurrencyCode => 'USD' },
112
+ :Question => "Invalid Question"
113
+ fail "Mechanical Turk should have thrown an exception"
114
+ rescue => e
115
+ raise e unless e.is_a?( Util::ValidationException )
116
+ assert_equal "AWS.MechanicalTurk.XMLParseError", e.message
117
+ end
118
+ end
119
+
120
+ def testGetRESTCreateHIT
121
+ question = MTurk::QuestionGenerator.build { |q| q.ask 'Have you tried Ruby?' }
122
+ result = @@mturk_rest_get.createHIT :Title => 'Ruby SDK Test Hit',
123
+ :Description => 'Autogenerated hit from ruby-aws test cases',
124
+ :Keywords => 'Ruby, test, SDK',
125
+ :Reward => { :Amount => 23.44, :CurrencyCode => 'USD' },
126
+ :Question => question
127
+ assert_not_nil( result[:HITId] )
128
+ @@mturk_rest_get.forceExpireHIT( :HITId => result[:HITId] )
129
+ @@mturk_rest_get.disposeHIT( :HITId => result[:HITId] )
130
+ end
131
+
132
+ def testGetRESTCreateHITFailure
133
+ begin
134
+ result = @@mturk_rest_get.createHIT :Title => 'Ruby SDK Test Hit',
135
+ :Description => 'Autogenerated hit from ruby-aws test cases',
136
+ :Keywords => 'Ruby, test, SDK',
137
+ :Reward => { :Amount => 23.44, :CurrencyCode => 'USD' },
138
+ :Question => "Invalid Question"
139
+ fail "Mechanical Turk should have thrown an exception"
140
+ rescue => e
141
+ raise e unless e.is_a?( Util::ValidationException )
142
+ assert_equal "AWS.MechanicalTurk.XMLParseError", e.message
143
+ end
144
+ end
145
+
146
+ if Util::RESTTransport.canPost?
147
+
148
+ def testPostRESTCreateHIT
149
+ question = MTurk::QuestionGenerator.build { |q| q.ask 'Have you tried Ruby?' }
150
+ result = @@mturk_rest_post.createHIT :Title => 'Ruby SDK Test Hit',
151
+ :Description => 'Autogenerated hit from ruby-aws test cases',
152
+ :Keywords => 'Ruby, test, SDK',
153
+ :Reward => { :Amount => 23.44, :CurrencyCode => 'USD' },
154
+ :Question => question
155
+ assert_not_nil( result[:HITId] )
156
+ @@mturk_rest_post.forceExpireHIT( :HITId => result[:HITId] )
157
+ @@mturk_rest_post.disposeHIT( :HITId => result[:HITId] )
158
+ end
159
+
160
+ def testPostRESTCreateHITFailure
161
+ begin
162
+ result = @@mturk_rest_post.createHIT :Title => 'Ruby SDK Test Hit',
163
+ :Description => 'Autogenerated hit from ruby-aws test cases',
164
+ :Keywords => 'Ruby, test, SDK',
165
+ :Reward => { :Amount => 23.44, :CurrencyCode => 'USD' },
166
+ :Question => "Invalid Question"
167
+ fail "Mechanical Turk should have thrown an exception"
168
+ rescue => e
169
+ raise e unless e.is_a?( Util::ValidationException )
170
+ assert_equal "AWS.MechanicalTurk.XMLParseError", e.message
171
+ end
172
+ end
173
+
174
+ end # if Util::RESTTransport.canPost?
175
+
176
+ end
177
+
178
+ end # if not DISABLE_MTURK_SANDBOX_TEST
@@ -0,0 +1,205 @@
1
+ # Copyright:: Copyright (c) 2007 Amazon Technologies, Inc.
2
+ # License:: Apache License, Version 2.0
3
+
4
+ require 'test/unit/testcase'
5
+ require 'ruby-aws'
6
+ require 'amazon/webservices/util/mock_transport'
7
+
8
+ class TestMockMechanicalTurkRequester < Test::Unit::TestCase
9
+
10
+ def setup
11
+ @mock = Amazon::WebServices::Util::MockTransport.new
12
+ @mturk = Amazon::WebServices::MechanicalTurkRequester.new( :Transport => @mock, :AWSAccessKey => 'bogus', :AWSAccessKeyId => 'fake' )
13
+ end
14
+
15
+ def testGetAccountBalance
16
+ res = @mturk.getAccountBalance # using the convenience layer method
17
+ assert_equal nil, res
18
+ res = @mturk.getAccountBalanceRaw({}) # using the raw method ( no default parameters )
19
+ assert_equal nil, res
20
+ res = @mturk.GetAccountBalance # using pass-through method ( no convenience processing )
21
+ assert_equal true, res[:MockResult][:Mock]
22
+
23
+ assert_equal 3, @mock.call_buffer.size
24
+ @mock.each do |request|
25
+ assert_equal :GetAccountBalance, request.name
26
+ assert request.args
27
+ [:AWSAccessKeyId, :Signature, :Timestamp, :Request].each { |key| assert request.args[key]}
28
+ assert_equal( {}, request.request )
29
+ end
30
+ end
31
+
32
+ def testCreateHIT
33
+ res = @mturk.createHIT # convenience layer, will auto-populate some default parameters
34
+ res = @mturk.createHITRaw({}) # raw method, no default parameters
35
+ res = @mturk.CreateHIT # pass-through method ( no convenience processing )
36
+
37
+ assert_equal 3, @mock.call_buffer.size
38
+
39
+ default_call = @mock.next # request from convenience layer
40
+ request = default_call.request
41
+ assert !request.keys.empty?
42
+ expected = [:MaxAssignments, :AssignmentDurationInSeconds, :AutoApprovalDelayInSeconds, :LifetimeInSeconds]
43
+ assert_equal [], request.keys - expected, 'Convenience layer should not populate unexpected arguments'
44
+ assert_equal [], expected - request.keys, 'Convenience layer should populate all expected arguments'
45
+
46
+ @mock.each do |call|
47
+ # both remaining calls should have no arguments
48
+ assert_equal( {}, call.request, 'Raw calls should not auto-populate arguments')
49
+ end
50
+ end
51
+
52
+ def testCreateHITs
53
+ @mock.listen do |call|
54
+ {:RegisterHITTypeResult => {:HITTypeId => 'specialType', :Request => {}} } if call.name == :RegisterHITType
55
+ end
56
+
57
+ template = { :Arg1 => 'Param2', :RequesterAnnotation => 'blub' }
58
+ question_template = "blarg <%= @zip %> foo"
59
+ data_set = [ { :zip => 'poodle' }, { :zip => 'fizz' } ]
60
+
61
+ result = @mturk.createHITs( template, question_template, data_set )
62
+
63
+ assert_equal 2, result[:Created].size
64
+ assert_equal 0, result[:Failed].size
65
+
66
+ register_call = @mock.next
67
+ assert_equal :RegisterHITType, register_call.name
68
+ assert_equal(
69
+ { :AssignmentDurationInSeconds=>3600, # default
70
+ :AutoApprovalDelayInSeconds=>604800, # default
71
+ :Arg1=>"Param2" # there's our arg!
72
+ },
73
+ register_call.request )
74
+ expected_questions = [ "blarg poodle foo", "blarg fizz foo" ]
75
+ @mock.each do |call|
76
+ assert_equal :CreateHIT, call.name
77
+ assert_equal 'specialType', call.request[:HITTypeId]
78
+ assert_equal call.request[:Question], expected_questions.delete( call.request[:Question] )
79
+ end
80
+ end
81
+
82
+ def testCreateHITsWithMaxAssignment
83
+ @mock.listen do |call|
84
+ case call.name
85
+ when :RegisterHITType
86
+ {:RegisterHITTypeResult => {:HITTypeId => 'mockHITType', :Request => {} } }
87
+ else
88
+ {}
89
+ end
90
+ end
91
+
92
+ template = { :Description => 'foo bar', :MaxAssignments => 2, :RequesterAnnotation => "Funky <%= @jazz %>" }
93
+ question = "who what <%= @where %>"
94
+ data_set = [ { :jazz => "LaLa", :where => 1}, {:jazz => nil, :where => 2, :MaxAssignments => 1}, {:jazz => "Poodle", :where => nil} ]
95
+
96
+ results = @mturk.createHITs( template, question, data_set )
97
+
98
+ assert_equal [], results[:Failed]
99
+ assert_equal 3, results[:Created].size
100
+
101
+ ht = @mock.next
102
+ assert_equal :RegisterHITType, ht.name
103
+ assert_equal 'foo bar', ht.request[:Description]
104
+ assert_equal nil, ht.request[:MaxAssignments]
105
+
106
+ 3.times do
107
+ hit = @mock.next
108
+ assert_equal :CreateHIT, hit.name
109
+ assert_equal 'mockHITType', hit.request[:HITTypeId]
110
+ case hit.request[:Question]
111
+ when 'who what 1'
112
+ assert_equal 2, hit.request[:MaxAssignments]
113
+ assert_equal 'Funky LaLa', hit.request[:RequesterAnnotation]
114
+ when 'who what 2'
115
+ assert_equal 1, hit.request[:MaxAssignments]
116
+ assert_equal 'Funky ', hit.request[:RequesterAnnotation]
117
+ when 'who what '
118
+ assert_equal 2, hit.request[:MaxAssignments]
119
+ assert_equal 'Funky Poodle', hit.request[:RequesterAnnotation]
120
+ else
121
+ fail 'generated an unexpected Question'
122
+ end
123
+ end
124
+
125
+ assert_equal nil, @mock.next
126
+ end
127
+
128
+ def testCreatHITsWithFailure
129
+ @mock.listen do |call|
130
+ raise "Mock hates you" if call.request[:Question] and call.request[:Question] =~ /poodle/
131
+ {:RegisterHITTypeResult => {:HITTypeId => 'specialType', :Request => {}} } if call.name == :RegisterHITType
132
+ end
133
+
134
+ template = { :Arg1 => 'Param2', :RequesterAnnotation => 'blub' }
135
+ question_template = "blarg <%= @zip %> foo"
136
+ data_set = [ { :zip => 'poodle' }, { :zip => 'fizz' } ]
137
+
138
+ result = @mturk.createHITs( template, question_template, data_set )
139
+
140
+ assert_equal 1, result[:Created].size
141
+ assert_equal 1, result[:Failed].size
142
+
143
+ assert_equal "Mock hates you", result[:Failed].first[:Error]
144
+ end
145
+
146
+ def testGetHITResults
147
+ # need to set up a listener to feed back hit and assignment attributes for testing results and work with pagination
148
+ assignments_per_hit = 31
149
+ @mock.mock_reply = {:OperationRequest => {}}
150
+ @mock.listen do |call|
151
+ case call.name
152
+ when :GetHIT
153
+ {:HIT => { :HITId => call.request[:HITId], :MockHITAttribute => 'amazing', :Request => {} } }
154
+ when :GetAssignmentsForHIT
155
+ size = call.request[:PageSize]
156
+ num = call.request[:PageNumber]
157
+ index = size * (num-1)
158
+ max = ( assignments_per_hit > index+size ) ? index+size : assignments_per_hit
159
+ res = []
160
+ index.upto(max-1) do |i|
161
+ res << { :HITId => call.request[:HITId], :AssignmentId => i, :MockAssignmentAttribute => 'stunning' }
162
+ end
163
+ { :GetAssignmentsForHITResult => { :Assignment => res, :Request => {} } }
164
+ else
165
+ nil
166
+ end
167
+ end
168
+
169
+ list = %w( hitid1 hitid2 amazinghit3 lamehit4 ).collect {|id| { :HITId => id } }
170
+ results = @mturk.getHITResults( list )
171
+
172
+ assert_equal assignments_per_hit*list.size, results.size
173
+ results.each { |item|
174
+ assert_not_nil item[:HITId]
175
+ assert_equal 'amazing', item[:MockHITAttribute]
176
+ assert_not_nil item[:AssignmentId]
177
+ assert_equal 'stunning', item[:MockAssignmentAttribute]
178
+ }
179
+
180
+ end
181
+
182
+ def testAvailableFunds
183
+ # TODO
184
+ end
185
+
186
+ def testSimplifyAnswer
187
+ rawAnswer = <<EOF
188
+ <?xml version="1.0" encoding="UTF-8"?>
189
+ <QuestionFormAnswers xmlns="http://mechanicalturk.amazonaws.com/AWSMechanicalTurkDataSchemas/2005-10-01/QuestionFormAnswers.xsd">
190
+ <Answer>
191
+ <QuestionIdentifier>1</QuestionIdentifier>
192
+ <FreeText>yes, sir</FreeText>
193
+ </Answer>
194
+ <Answer>
195
+ <QuestionIdentifier>pudding</QuestionIdentifier>
196
+ <SelectionIdentifier>B+</SelectionIdentifier>
197
+ </Answer>
198
+ </QuestionFormAnswers>
199
+ EOF
200
+ expected = { 1 => 'yes, sir', 'pudding' => 'B+' }
201
+ result = @mturk.simplifyAnswer( rawAnswer )
202
+ assert_equal expected, result
203
+ end
204
+
205
+ end