google-ads-common 0.14.1 → 1.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.
@@ -1,250 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # Encoding: utf-8
3
- #
4
- # Copyright:: Copyright 2012, Google Inc. All Rights Reserved.
5
- #
6
- # License:: Licensed under the Apache License, Version 2.0 (the "License");
7
- # you may not use this file except in compliance with the License.
8
- # You may obtain a copy of the License at
9
- #
10
- # http://www.apache.org/licenses/LICENSE-2.0
11
- #
12
- # Unless required by applicable law or agreed to in writing, software
13
- # distributed under the License is distributed on an "AS IS" BASIS,
14
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
15
- # implied.
16
- # See the License for the specific language governing permissions and
17
- # limitations under the License.
18
- #
19
- # Tests the array replies from services.
20
-
21
- require 'test/unit'
22
-
23
- require 'ads_common/results_extractor'
24
-
25
- module AdsCommon
26
- class ResultsExtractor
27
-
28
- public :check_array_collapse
29
- public :normalize_item
30
- public :check_key_value_struct
31
- public :check_key_value_hash
32
- public :convert_key_value_to_hash
33
- end
34
- end
35
-
36
- class StubRegistry
37
- def get_type_signature(type_name)
38
- if type_name == :SoapResponseHeader
39
- return {:fields => []}
40
- else
41
- return nil
42
- end
43
- end
44
- end
45
-
46
- class StubResponse
47
- attr_accessor :header
48
- end
49
-
50
- class TestResultsExtractor < Test::Unit::TestCase
51
-
52
- # Initialize tests.
53
- def setup()
54
- registry = StubRegistry.new()
55
- @extractor = AdsCommon::ResultsExtractor.new(registry)
56
- end
57
-
58
- def test_normalize_item_nil()
59
- result1 = @extractor.normalize_item(nil, {:type => 'unknown'})
60
- assert_equal(nil, result1, 'bad conversion')
61
- end
62
-
63
- def test_normalize_item_int()
64
- result1 = @extractor.normalize_item(5, {:type => 'int'})
65
- assert_kind_of(Integer, result1)
66
- assert_equal(5, result1, 'bad conversion')
67
-
68
- result2 = @extractor.normalize_item(2147483648, {:type => 'int'})
69
- assert_kind_of(Integer, result2)
70
- assert_equal(2147483648, result2, 'bad conversion')
71
- end
72
-
73
- def test_normalize_item_string()
74
- result1 = @extractor.normalize_item('foobar',
75
- {:type => 'string'})
76
- assert_kind_of(String, result1)
77
- assert_equal('foobar', result1, 'bad conversion')
78
-
79
- result2 = @extractor.normalize_item('', {:type => 'string'})
80
- assert_kind_of(String, result2)
81
- assert_equal('', result2, 'bad conversion')
82
- end
83
-
84
- def test_normalize_item_long()
85
- result1 = @extractor.normalize_item(2147483648, {:type => 'long'})
86
- assert_kind_of(Integer, result1)
87
- assert_equal(2147483648, result1, 'bad conversion')
88
-
89
- result2 = @extractor.normalize_item(-1, {:type => 'long'})
90
- assert_kind_of(Integer, result2)
91
- assert_equal(-1, result2, 'bad conversion')
92
- end
93
-
94
- def test_normalize_item_boolean()
95
- result1 = @extractor.normalize_item(true, {:type => 'boolean'})
96
- assert_kind_of(TrueClass, result1)
97
-
98
- result2 = @extractor.normalize_item(false, {:type => 'boolean'})
99
- assert_kind_of(FalseClass, result2)
100
-
101
- result3 = @extractor.normalize_item('true', {:type => 'boolean'})
102
- assert_kind_of(TrueClass, result3)
103
-
104
- result4 = @extractor.normalize_item('false', {:type => 'boolean'})
105
- assert_kind_of(FalseClass, result4)
106
-
107
- result5 = @extractor.normalize_item('True', {:type => 'boolean'})
108
- assert_kind_of(TrueClass, result5)
109
-
110
- result6 = @extractor.normalize_item('False', {:type => 'boolean'})
111
- assert_kind_of(FalseClass, result6)
112
- end
113
-
114
- def test_normalize_item_object()
115
- result1 = @extractor.normalize_item({:a => 'b'}, {:type => 'StubClass'})
116
- assert_equal('b', result1[:a], 'object corrupted')
117
-
118
- result2 = @extractor.normalize_item(@extractor, {:type => 'SavonService'})
119
- assert_equal(@extractor.hash, result2.hash, 'object corrupted')
120
- end
121
-
122
- def test_normalize_item_double()
123
- result1 = @extractor.normalize_item(3.14, {:type => 'double'})
124
- assert_kind_of(Float, result1)
125
- assert_equal(3.14, result1, 'bad conversion')
126
-
127
- result2 = @extractor.normalize_item('-3.14', {:type => 'double'})
128
- assert_kind_of(Float, result2)
129
- assert_equal(-3.14, result2, 'bad conversion')
130
-
131
- result3 = @extractor.normalize_item('42', {:type => 'double'})
132
- assert_kind_of(Float, result3)
133
- assert_equal(42.0, result3, 'bad conversion')
134
- end
135
-
136
- def test_check_array_collapse()
137
- result1 = @extractor.check_array_collapse(
138
- 42.0, {:min_occurs => '0', :max_occurs => 1})
139
- assert_kind_of(Float, result1)
140
- assert_equal(42.0, result1, 'Float is expected for max_occurs 1')
141
-
142
- result2 = @extractor.check_array_collapse(
143
- 42.0, {:min_occurs => '0', :max_occurs => :unbounded})
144
- assert_instance_of(Array, result2)
145
- assert_equal(42.0, result2[0])
146
-
147
- result3 = @extractor.check_array_collapse(
148
- 42.0, {:min_occurs => '0', :max_occurs => 2})
149
- assert_instance_of(Array, result3)
150
- assert_equal(42.0, result3[0])
151
-
152
- result4 = @extractor.check_array_collapse(
153
- 42.0, {:min_occurs => '0', :max_occurs => nil})
154
- assert_instance_of(Float, result4)
155
- assert_equal(42.0, result4, 'Float is expected for nil max_occurs')
156
-
157
- result5 = @extractor.check_array_collapse(
158
- [42, -1], {:min_occurs => '0', :max_occurs => :unbounded})
159
- assert_instance_of(Array, result5)
160
- assert_equal(42, result5[0])
161
- assert_equal(-1, result5[1])
162
-
163
- result6 = @extractor.check_array_collapse(
164
- {}, {:min_occurs => '0', :max_occurs => 1})
165
- assert_equal({}, result6)
166
-
167
- result7 = @extractor.check_array_collapse(
168
- {}, {:min_occurs => '0', :max_occurs => :unbounded})
169
- assert_equal([{}], result7)
170
- end
171
-
172
- def test_check_array_collapse_exception()
173
- result1 = @extractor.check_array_collapse(
174
- 42.0, {:min_occurs => '0', :max_occurs => 2, :type => 'FloatMapEntry'})
175
- assert_kind_of(Float, result1)
176
- assert_equal(42.0, result1)
177
-
178
- result2 = @extractor.check_array_collapse(
179
- 42.0, {:min_occurs => '0', :max_occurs => 2, :type => 'MapEntryTest'})
180
- assert_kind_of(Array, result2)
181
- assert_equal(42.0, result2[0])
182
- end
183
-
184
- def test_extract_headers_empty()
185
- headers = {}
186
- response = StubResponse.new()
187
- response.header = {:response_header => headers}
188
- result = @extractor.extract_header_data(response)
189
- assert_equal(headers, result)
190
- assert_not_same(headers, result)
191
- end
192
-
193
- def test_extract_headers_passthrough()
194
- headers = {:a => 'aa', :b => '42'}
195
- response = StubResponse.new()
196
- response.header = {:response_header => headers}
197
- result = @extractor.extract_header_data(response)
198
- assert_equal(headers, result)
199
- assert_not_same(headers, result)
200
- end
201
-
202
- def test_extract_headers_attrs_removed()
203
- headers = {:a => 'aa', :@xmlns => '42'}
204
- expected = {:a => 'aa'}
205
- response = StubResponse.new()
206
- response.header = {:response_header => headers}
207
- result = @extractor.extract_header_data(response)
208
- assert_equal(expected, result)
209
- end
210
-
211
- def test_check_key_value_struct()
212
- assert(!@extractor.check_key_value_struct([]))
213
- assert(!@extractor.check_key_value_struct({}))
214
-
215
- test1 = [{:key => 'foo', :value => 'bar'}]
216
- assert(@extractor.check_key_value_struct(test1))
217
- test2 = [{:key => 'foo'}]
218
- assert(!@extractor.check_key_value_struct(test2))
219
- test3 = [{:key => 'foo', :value => 'bar', :extra => 42}]
220
- assert(!@extractor.check_key_value_struct(test3))
221
- test4 = [{:key => 'foo', :value => 'bar'}, {:baz => 'bar'}]
222
- assert(!@extractor.check_key_value_struct(test4))
223
- test5 = [
224
- {:key => 'foo', :value => 'bar'},
225
- {:key => 'bar', :value => {:bar => 'baz'}}
226
- ]
227
- assert(@extractor.check_key_value_struct(test5))
228
- test6 = {:key => 'foo', :value => 'bar'}
229
- assert(@extractor.check_key_value_struct(test6))
230
- test7 = {:key => 'foo'}
231
- assert(!@extractor.check_key_value_struct(test7))
232
- test8 = {:value => 'baz', :key => 'foo'}
233
- assert(@extractor.check_key_value_struct(test8))
234
- end
235
-
236
- def test_convert_key_value_to_hash()
237
- result1 = @extractor.convert_key_value_to_hash(
238
- [{:key => 'foo', :value => 'bar'}, {:key => 42, :value => 88.2}])
239
- assert_kind_of(Hash, result1)
240
- assert(result1.include?('foo'))
241
- assert(result1.include?(42))
242
- assert_equal('bar', result1['foo'])
243
- assert_equal(88.2, result1[42])
244
- result2 = @extractor.convert_key_value_to_hash(
245
- {:key => 'foo', :value => 'bar'})
246
- assert_kind_of(Hash, result2)
247
- assert_equal(['foo'], result2.keys)
248
- assert_equal('bar', result2['foo'])
249
- end
250
- end
@@ -1,130 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # Encoding: utf-8
3
- #
4
- # Copyright:: Copyright 2011, Google Inc. All Rights Reserved.
5
- #
6
- # License:: Licensed under the Apache License, Version 2.0 (the "License");
7
- # you may not use this file except in compliance with the License.
8
- # You may obtain a copy of the License at
9
- #
10
- # http://www.apache.org/licenses/LICENSE-2.0
11
- #
12
- # Unless required by applicable law or agreed to in writing, software
13
- # distributed under the License is distributed on an "AS IS" BASIS,
14
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
15
- # implied.
16
- # See the License for the specific language governing permissions and
17
- # limitations under the License.
18
- #
19
- # Tests the array replies from services.
20
-
21
- require 'test/unit'
22
- require 'logger'
23
-
24
- require 'ads_common/config'
25
- require 'ads_common/savon_service'
26
-
27
- # SavonService is abstract, defining a child class for the test.
28
- class StubService < AdsCommon::SavonService
29
-
30
- public :get_service_registry, :get_module
31
- public :format_headers, :sanitize_request, :format_fault
32
- public :should_log_summary, :should_log_payloads
33
-
34
- def initialize(namespace, endpoint, version)
35
- @logger = Logger.new(STDERR)
36
- @config = AdsCommon::Config.new({:library => {:logger => @logger}})
37
- super(@config, namespace, endpoint, version)
38
- end
39
- end
40
-
41
-
42
- class TestSavonService < Test::Unit::TestCase
43
-
44
- TEST_NAMESPACE = 'namespace'
45
- TEST_ENDPOINT = 'endpoint'
46
- TEST_VERSION = :vVersion
47
-
48
- # Initialize tests.
49
- def setup()
50
- @stub_service = StubService.new(TEST_NAMESPACE, TEST_ENDPOINT, TEST_VERSION)
51
- end
52
-
53
- def test_initialize_abstract()
54
- assert_raises(NoMethodError) do
55
- AdsCommon::SavonService.new(nil, TEST_NAMESPACE, TEST_ENDPOINT,
56
- TEST_VERSION)
57
- end
58
- assert_nothing_raised do
59
- StubService.new(TEST_NAMESPACE, TEST_ENDPOINT, TEST_VERSION)
60
- end
61
- end
62
-
63
- def test_get_service_registry_abstract()
64
- assert_raises(NoMethodError) { @stub_service.get_service_registry() }
65
- end
66
-
67
- def test_get_module_abstract()
68
- assert_raises(NoMethodError) { @stub_service.get_module() }
69
- end
70
-
71
- def test_format_headers()
72
- test1 = {
73
- 'header1' => 'value1',
74
- 'header2' => 'value2'
75
- }
76
- expected1 = "header1: value1, header2: value2"
77
- test2 = {
78
- 'Authorization' => 'Bearer ABCD',
79
- 'header3' => 'value3'
80
- }
81
- expected2 = "Authorization: REDACTED, header3: value3"
82
- assert_equal(expected1, @stub_service.format_headers(test1))
83
- assert_equal(expected2, @stub_service.format_headers(test2))
84
- end
85
-
86
- def test_sanitize_request()
87
- test1 = "<some_xml><developerToken>ab1cdEF2GH-IJ3KL_mn4OP" +
88
- "</developerToken></some_xml>"
89
- expected1 = "<some_xml><developerToken>REDACTED</developerToken></some_xml>"
90
- test2 = "<xml><element1></element1><element2></element2></xml>"
91
- test3 = "<xml><ns1:developerToken>w-x_Y-Z_</ns1:developerToken></xml>"
92
- expected3 = "<xml><ns1:developerToken>REDACTED</ns1:developerToken></xml>"
93
- test4 = "<xml><httpAuthorizationHeader>Authorization: Bearer " +
94
- "1/abcdEFGH1234</httpAuthorizationHeader></xml>"
95
- expected4 = "<xml><httpAuthorizationHeader>REDACTED" +
96
- "</httpAuthorizationHeader></xml>"
97
- assert_equal(expected1, @stub_service.sanitize_request(test1))
98
- assert_equal(test2, @stub_service.sanitize_request(test2))
99
- assert_equal(expected3, @stub_service.sanitize_request(test3))
100
- assert_equal(expected4, @stub_service.sanitize_request(test4))
101
- end
102
-
103
- def test_format_fault()
104
- test1 = "fault\nwith\nnewlines\nand\nunicode\n\u2713\n"+
105
- "\u3084\u3063\u305F".encode('utf-8')
106
- expected1 = "fault with newlines and unicode \u2713 \u3084\u3063\u305F"
107
- test2 = "This needs to be more than 16000 characters......." * 1000
108
- expected2 = "This needs to be more than 16000 characters......." * 320
109
- assert(test2.length > AdsCommon::SavonService::MAX_FAULT_LOG_LENGTH)
110
- assert(expected2.length == AdsCommon::SavonService::MAX_FAULT_LOG_LENGTH)
111
- assert_equal(expected1, @stub_service.format_fault(test1))
112
- assert_equal(expected2, @stub_service.format_fault(test2))
113
- end
114
-
115
- def test_log_levels()
116
- assert_equal(true, @stub_service.should_log_payloads(Logger::DEBUG, true))
117
- assert_equal(true, @stub_service.should_log_payloads(Logger::DEBUG, false))
118
- assert_equal(true, @stub_service.should_log_payloads(Logger::INFO, true))
119
- assert_equal(false, @stub_service.should_log_payloads(Logger::INFO, false))
120
- assert_equal(false, @stub_service.should_log_payloads(Logger::WARN, true))
121
- assert_equal(false, @stub_service.should_log_payloads(Logger::WARN, false))
122
-
123
- assert_equal(true, @stub_service.should_log_summary(Logger::INFO, true))
124
- assert_equal(true, @stub_service.should_log_summary(Logger::INFO, false))
125
- assert_equal(true, @stub_service.should_log_summary(Logger::WARN, true))
126
- assert_equal(false, @stub_service.should_log_summary(Logger::WARN, false))
127
- assert_equal(false, @stub_service.should_log_summary(Logger::ERROR, true))
128
- assert_equal(false, @stub_service.should_log_summary(Logger::ERROR, false))
129
- end
130
- end
@@ -1,111 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # Encoding: utf-8
3
- #
4
- # Copyright:: Copyright 2012, Google Inc. All Rights Reserved.
5
- #
6
- # License:: Licensed under the Apache License, Version 2.0 (the "License");
7
- # you may not use this file except in compliance with the License.
8
- # You may obtain a copy of the License at
9
- #
10
- # http://www.apache.org/licenses/LICENSE-2.0
11
- #
12
- # Unless required by applicable law or agreed to in writing, software
13
- # distributed under the License is distributed on an "AS IS" BASIS,
14
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
15
- # implied.
16
- # See the License for the specific language governing permissions and
17
- # limitations under the License.
18
- #
19
- # Tests the utils.
20
-
21
- require 'tempfile'
22
- require 'test/unit'
23
- require 'yaml'
24
-
25
- require 'ads_common/utils'
26
-
27
-
28
- class TestUtils < Test::Unit::TestCase
29
-
30
- def test_has_lower_camelcase()
31
- assert('str'.respond_to?(:lower_camelcase))
32
- end
33
-
34
- def test_lower_camelcase_works()
35
- data = [
36
- {:input => 'lowerCamelCase', :output => 'lowerCamelCase'},
37
- {:input => 'UpperCamelCase', :output => 'upperCamelCase'},
38
- {:input => 'snake_case', :output => 'snakeCase'},
39
- {:input => 'single1', :output => 'single1'},
40
- {:input => 'Single2', :output => 'single2'},
41
- {:input => '', :output => ''},
42
- {:input => 'strange_MixOf_all', :output => 'strangeMixOfAll'},
43
- {:input => 'number_5', :output => 'number5'},
44
- ]
45
- data.each do |arg|
46
- result = arg[:input].lower_camelcase()
47
- assert_equal(arg[:output], result)
48
- end
49
- end
50
-
51
- def test_lower_camelcase_non_destructive()
52
- str = 'str_snake'
53
- result = str.lower_camelcase()
54
- assert_not_same(str, result)
55
- assert_equal('str_snake', str)
56
- end
57
-
58
- def test_hash_keys_to_str()
59
- data = {:a => 'aa', :b5 => 43, 'xyz' => :abc}
60
- result = AdsCommon::Utils.hash_keys_to_str(data)
61
- assert_equal('aa', result['a'])
62
- assert_equal(43, result['b5'])
63
- assert_equal(:abc, result['xyz'])
64
- assert_equal(3, result.size)
65
- assert_not_same(result, data)
66
- end
67
-
68
- def test_hash_keys_to_sym()
69
- data = {:a => 'aa', :b5 => 43, 'xyz' => :abc, 'f5' => :xyz}
70
- result = AdsCommon::Utils.hash_keys_to_sym(data)
71
- assert_equal('aa', result[:a])
72
- assert_equal(43, result[:b5])
73
- assert_equal(:abc, result[:xyz])
74
- assert_equal(:xyz, result[:f5])
75
- assert_equal(4, result.size)
76
- assert_not_same(result, data)
77
- end
78
-
79
- def test_save_oauth2_token()
80
- config_stub = {:authentication => {:method => 'OAuth2'}}
81
- token = {:token_key => 'token_value', :token_number => 42}
82
- expected_result = {
83
- :authentication => {
84
- :method => 'OAuth2',
85
- :oauth2_token => token.dup
86
- }
87
- }
88
- tmp_file = Tempfile.new('ruby-tests')
89
- tmp_file.write(YAML::dump(config_stub))
90
- filename = tmp_file.path()
91
- tmp_file.close()
92
- AdsCommon::Utils.save_oauth2_token(filename, token)
93
-
94
- assert(File.exist?(filename))
95
- result = YAML::load_file(filename)
96
- assert_equal(expected_result, result)
97
- assert(File.exist?(filename + '.backup'))
98
- File.unlink(filename + '.backup')
99
- end
100
-
101
- def test_find_new_name()
102
- tmp_file = Tempfile.new('ruby-tests')
103
- name1 = AdsCommon::Utils.find_new_name(tmp_file.path())
104
- assert_equal(tmp_file.path() + '.backup', name1)
105
- File.open(name1, 'w') do |file1|
106
- name2 = AdsCommon::Utils.find_new_name(tmp_file.path())
107
- assert_equal(tmp_file.path() + '.backup1', name2)
108
- end
109
- File.unlink(name1)
110
- end
111
- end