google-ads-common 0.6.4 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
data/test/test_config.rb CHANGED
@@ -53,7 +53,7 @@ class TestConfig < Test::Unit::TestCase
53
53
  end
54
54
 
55
55
  # Test initializer with filename argument.
56
- def test_initialize_filename
56
+ def test_initialize_filename_correct
57
57
  config = AdsCommon::Config.new(DEFAULT_CONFIG_FILENAME)
58
58
  assert_equal(false, config.read('service.use_ruby_names'))
59
59
  assert_equal('sandbox', config.read('service.environment'))
@@ -63,13 +63,12 @@ class TestConfig < Test::Unit::TestCase
63
63
  end
64
64
 
65
65
  # Test initializer with an incorrect existing file.
66
- def test_initialize_filename
66
+ def test_initialize_filename_incorrect
67
67
  assert_raises (AdsCommon::Errors::Error) do
68
68
  config = AdsCommon::Config.new('/dev/null')
69
69
  end
70
70
  end
71
71
 
72
-
73
72
  # Test default result.
74
73
  def test_read_default_result
75
74
  config = AdsCommon::Config.new(DEFAULT_CONFIG_HASH)
@@ -89,7 +88,7 @@ class TestConfig < Test::Unit::TestCase
89
88
  end
90
89
 
91
90
  # Test subhash.
92
- def test_set
91
+ def test_get_hash
93
92
  config = AdsCommon::Config.new(DEFAULT_CONFIG_HASH)
94
93
  result = config.read('service')
95
94
  assert_instance_of(Hash, result)
@@ -0,0 +1,55 @@
1
+ #!/usr/bin/env ruby
2
+ # Encoding: utf-8
3
+ #
4
+ # Author:: api.dklimkin@gmail.com (Danial Klimkin)
5
+ #
6
+ # Copyright:: Copyright 2012, Google Inc. All Rights Reserved.
7
+ #
8
+ # License:: Licensed under the Apache License, Version 2.0 (the "License");
9
+ # you may not use this file except in compliance with the License.
10
+ # You may obtain a copy of the License at
11
+ #
12
+ # http://www.apache.org/licenses/LICENSE-2.0
13
+ #
14
+ # Unless required by applicable law or agreed to in writing, software
15
+ # distributed under the License is distributed on an "AS IS" BASIS,
16
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
17
+ # implied.
18
+ # See the License for the specific language governing permissions and
19
+ # limitations under the License.
20
+ #
21
+ # Tests credential handler methods.
22
+
23
+ require 'logger'
24
+ require 'test/unit'
25
+
26
+ require 'ads_common/config'
27
+ require 'ads_common/credential_handler'
28
+
29
+ class TestCredentialHandler < Test::Unit::TestCase
30
+
31
+ def setup()
32
+ logger = Logger.new(STDERR)
33
+ @default_credentials = {:client_customer_id => '1234567890', :foo => 'bar'}
34
+ config = AdsCommon::Config.new({
35
+ :library => {:logger => logger},
36
+ :authentication => @default_credentials
37
+ })
38
+ @handler = AdsCommon::CredentialHandler.new(config)
39
+ end
40
+
41
+ def test_credentials_simple()
42
+ credentials = @handler.credentials()
43
+ assert_equal(@default_credentials, credentials)
44
+ assert_not_same(@default_credentials, credentials)
45
+ end
46
+
47
+ def test_credentials_override()
48
+ @override = {:client_customer_id => 42}
49
+ credentials = @handler.credentials(@override)
50
+ assert_not_equal(@default_credentials, credentials)
51
+ assert_not_same(@default_credentials, credentials)
52
+ assert_equal(42, credentials[:client_customer_id])
53
+ assert_equal('bar', credentials[:foo])
54
+ end
55
+ end
@@ -28,7 +28,7 @@ require 'ads_common/parameters_validator'
28
28
  module AdsCommon
29
29
  class ParametersValidator
30
30
  public :deep_copy, :add_attribute, :array_from_named_list
31
- public :check_required_argument_present
31
+ public :check_required_argument_present, :arrayize
32
32
  end
33
33
  end
34
34
 
@@ -129,4 +129,27 @@ class TestParametersValidator < Test::Unit::TestCase
129
129
  @validator.check_required_argument_present([field1, field2], field2)
130
130
  end
131
131
  end
132
+
133
+ def test_arrayize_empty
134
+ result1 = @validator.arrayize(nil)
135
+ assert_instance_of(Array, result1, 'returned object is not an Array')
136
+ assert_equal(0, result1.size, 'array is not empty')
137
+
138
+ result2 = @validator.arrayize([])
139
+ assert_instance_of(Array, result2, 'returned object is not an Array')
140
+ assert_equal(0, result2.size, 'array is not empty')
141
+ end
142
+
143
+ def test_arrayize_on_array
144
+ result1 = @validator.arrayize([nil])
145
+ assert_instance_of(Array, result1, 'returned object is not an Array')
146
+ assert_equal(1, result1.size, 'array changed size')
147
+ assert_equal(nil, result1[0], 'array changed data')
148
+
149
+ result2 = @validator.arrayize(['a', 'b'])
150
+ assert_instance_of(Array, result2, 'returned object is not an Array')
151
+ assert_equal(2, result2.size, 'array changed size')
152
+ assert_equal('a', result2[0], 'array changed data')
153
+ assert_equal('b', result2[1], 'array changed data')
154
+ end
132
155
  end
@@ -0,0 +1,165 @@
1
+ #!/usr/bin/env ruby
2
+ # Encoding: utf-8
3
+ #
4
+ # Author:: api.dklimkin@gmail.com (Danial Klimkin)
5
+ #
6
+ # Copyright:: Copyright 2012, Google Inc. All Rights Reserved.
7
+ #
8
+ # License:: Licensed under the Apache License, Version 2.0 (the "License");
9
+ # you may not use this file except in compliance with the License.
10
+ # You may obtain a copy of the License at
11
+ #
12
+ # http://www.apache.org/licenses/LICENSE-2.0
13
+ #
14
+ # Unless required by applicable law or agreed to in writing, software
15
+ # distributed under the License is distributed on an "AS IS" BASIS,
16
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
17
+ # implied.
18
+ # See the License for the specific language governing permissions and
19
+ # limitations under the License.
20
+ #
21
+ # Tests the array replies from services.
22
+
23
+ require 'test/unit'
24
+
25
+ require 'ads_common/results_extractor'
26
+
27
+ module AdsCommon
28
+ class ResultsExtractor
29
+
30
+ public :check_array_collapse
31
+ public :normalize_item
32
+ end
33
+ end
34
+
35
+ class StubRegistry
36
+ end
37
+
38
+ class TestResultsExtractor < Test::Unit::TestCase
39
+
40
+ # Initialize tests.
41
+ def setup()
42
+ registry = StubRegistry.new()
43
+ @extractor = AdsCommon::ResultsExtractor.new(registry)
44
+ end
45
+
46
+ def test_normalize_item_nil()
47
+ result1 = @extractor.normalize_item(nil, {:type => 'unknown'})
48
+ assert_equal(nil, result1, 'bad conversion')
49
+ end
50
+
51
+ def test_normalize_item_int()
52
+ result1 = @extractor.normalize_item(5, {:type => 'int'})
53
+ assert_kind_of(Integer, result1)
54
+ assert_equal(5, result1, 'bad conversion')
55
+
56
+ result2 = @extractor.normalize_item(2147483648, {:type => 'int'})
57
+ assert_kind_of(Integer, result2)
58
+ assert_equal(2147483648, result2, 'bad conversion')
59
+ end
60
+
61
+ def test_normalize_item_string()
62
+ result1 = @extractor.normalize_item('foobar',
63
+ {:type => 'string'})
64
+ assert_kind_of(String, result1)
65
+ assert_equal('foobar', result1, 'bad conversion')
66
+
67
+ result2 = @extractor.normalize_item('', {:type => 'string'})
68
+ assert_kind_of(String, result2)
69
+ assert_equal('', result2, 'bad conversion')
70
+ end
71
+
72
+ def test_normalize_item_long()
73
+ result1 = @extractor.normalize_item(2147483648,
74
+ {:type => 'long'})
75
+ assert_kind_of(Integer, result1)
76
+ assert_equal(2147483648, result1, 'bad conversion')
77
+
78
+ result2 = @extractor.normalize_item(-1, {:type => 'long'})
79
+ assert_kind_of(Integer, result2)
80
+ assert_equal(-1, result2, 'bad conversion')
81
+ end
82
+
83
+ def test_normalize_item_boolean()
84
+ result1 = @extractor.normalize_item(true, {:type => 'boolean'})
85
+ assert_kind_of(TrueClass, result1)
86
+
87
+ result2 = @extractor.normalize_item(false, {:type => 'boolean'})
88
+ assert_kind_of(FalseClass, result2)
89
+
90
+ result3 = @extractor.normalize_item('true', {:type => 'boolean'})
91
+ assert_kind_of(TrueClass, result3)
92
+
93
+ result4 = @extractor.normalize_item('false',
94
+ {:type => 'boolean'})
95
+ assert_kind_of(FalseClass, result4)
96
+
97
+ result5 = @extractor.normalize_item('True',
98
+ {:type => 'boolean'})
99
+ assert_kind_of(TrueClass, result3)
100
+
101
+ result6 = @extractor.normalize_item('False',
102
+ {:type => 'boolean'})
103
+ assert_kind_of(FalseClass, result4)
104
+ end
105
+
106
+ def test_normalize_item_object()
107
+ result1 = @extractor.normalize_item({:a => 'b'},
108
+ {:type => 'StubClass'})
109
+ assert_equal('b', result1[:a], 'object corrupted')
110
+
111
+ result2 = @extractor.normalize_item(@extractor,
112
+ {:type => 'SavonService'})
113
+ assert_equal(@extractor.hash, result2.hash, 'object corrupted')
114
+ end
115
+
116
+ def test_normalize_item_double()
117
+ result1 = @extractor.normalize_item(3.14, {:type => 'double'})
118
+ assert_kind_of(Float, result1)
119
+ assert_equal(3.14, result1, 'bad conversion')
120
+
121
+ result2 = @extractor.normalize_item('-3.14', {:type => 'double'})
122
+ assert_kind_of(Float, result2)
123
+ assert_equal(-3.14, result2, 'bad conversion')
124
+
125
+ result3 = @extractor.normalize_item('42', {:type => 'double'})
126
+ assert_kind_of(Float, result3)
127
+ assert_equal(42.0, result3, 'bad conversion')
128
+ end
129
+
130
+ def test_check_array_collapse()
131
+ result1 = @extractor.check_array_collapse(
132
+ 42.0, {:min_occurs => '0', :max_occurs => 1})
133
+ assert_kind_of(Float, result1)
134
+ assert_equal(42.0, result1, 'Float is expected for max_occurs 1')
135
+
136
+ result2 = @extractor.check_array_collapse(
137
+ 42.0, {:min_occurs => '0', :max_occurs => :unbounded})
138
+ assert_instance_of(Array, result2)
139
+ assert_equal(42.0, result2[0])
140
+
141
+ result3 = @extractor.check_array_collapse(
142
+ 42.0, {:min_occurs => '0', :max_occurs => 2})
143
+ assert_instance_of(Array, result3)
144
+ assert_equal(42.0, result3[0])
145
+
146
+ result4 = @extractor.check_array_collapse(
147
+ 42.0, {:min_occurs => '0', :max_occurs => nil})
148
+ assert_instance_of(Float, result4)
149
+ assert_equal(42.0, result4, 'Float is expected for nil max_occurs')
150
+
151
+ result5 = @extractor.check_array_collapse(
152
+ [42, -1], {:min_occurs => '0', :max_occurs => :unbounded})
153
+ assert_instance_of(Array, result5)
154
+ assert_equal(42, result5[0])
155
+ assert_equal(-1, result5[1])
156
+
157
+ result6 = @extractor.check_array_collapse(
158
+ {}, {:min_occurs => '0', :max_occurs => 1})
159
+ assert_equal({}, result6)
160
+
161
+ result7 = @extractor.check_array_collapse(
162
+ {}, {:min_occurs => '0', :max_occurs => :unbounded})
163
+ assert_equal([{}], result7)
164
+ end
165
+ end
@@ -25,57 +25,31 @@ require 'test/unit'
25
25
  require 'ads_common/config'
26
26
  require 'ads_common/savon_service'
27
27
 
28
- # AdsCommon::Api is abstract, defining a stub class for the test.
29
- class StubApi
30
- attr_accessor :config
31
- def initialize()
32
- @config = AdsCommon::Config.new
33
- end
34
- def self.get_instance()
35
- @api ||= StubApi.new
36
- return @api
37
- end
38
- end
39
-
40
28
  # SavonService is abstract, defining a child class for the test.
41
29
  class StubService < AdsCommon::SavonService
30
+
31
+ public :get_service_registry, :get_module
32
+
42
33
  def initialize(namespace, endpoint, version)
43
- super(StubApi.get_instance(), namespace, endpoint, version)
44
- end
45
- def private_get_service_registry()
46
- return get_service_registry
47
- end
48
- def private_get_module()
49
- return get_module
50
- end
51
- def private_normalize_type(data, field)
52
- return normalize_type(data, field)
53
- end
54
- def private_get_field_by_name(fields_list, name)
55
- return get_field_by_name(fields_list, name)
56
- end
57
- def private_arrayize(object)
58
- return arrayize(object)
59
- end
60
- def private_deep_copy(object)
61
- return deep_copy(object)
62
- end
63
- def private_add_attribute(node, key, name, value)
64
- return add_attribute(node, key, name, value)
34
+ @logger = Logger.new(STDERR)
35
+ @config = AdsCommon::Config.new({:library => {:logger => @logger}})
36
+ super(@config, namespace, endpoint, version)
65
37
  end
66
38
  end
67
39
 
40
+
68
41
  class TestSavonService < Test::Unit::TestCase
42
+
69
43
  TEST_NAMESPACE = 'namespace'
70
44
  TEST_ENDPOINT = 'endpoint'
71
45
  TEST_VERSION = :vVersion
72
46
 
73
47
  # Initialize tests.
74
- def setup
48
+ def setup()
75
49
  @stub_service = StubService.new(TEST_NAMESPACE, TEST_ENDPOINT, TEST_VERSION)
76
50
  end
77
51
 
78
- def test_initialize_abstract
52
+ def test_initialize_abstract()
79
53
  assert_raises(NoMethodError) do
80
54
  AdsCommon::SavonService.new(nil, TEST_NAMESPACE, TEST_ENDPOINT,
81
55
  TEST_VERSION)
@@ -85,138 +59,11 @@ class TestSavonService < Test::Unit::TestCase
85
59
  end
86
60
  end
87
61
 
88
- def test_get_service_registry_abstract
89
- assert_raises(NoMethodError) { @stub_service.private_get_service_registry }
90
- end
91
-
92
- def test_get_module_abstract
93
- assert_raises(NoMethodError) { @stub_service.private_get_module }
94
- end
95
-
96
- def test_arrayize_empty
97
- result1 = @stub_service.private_arrayize(nil)
98
- assert_instance_of(Array, result1, 'returned object is not an Array')
99
- assert_equal(0, result1.size, 'array is not empty')
100
-
101
- result2 = @stub_service.private_arrayize([])
102
- assert_instance_of(Array, result2, 'returned object is not an Array')
103
- assert_equal(0, result2.size, 'array is not empty')
104
- end
105
-
106
- def test_arrayize_on_array
107
- result1 = @stub_service.private_arrayize([nil])
108
- assert_instance_of(Array, result1, 'returned object is not an Array')
109
- assert_equal(1, result1.size, 'array changed size')
110
- assert_equal(nil, result1[0], 'array changed data')
111
-
112
- result2 = @stub_service.private_arrayize(['a', 'b'])
113
- assert_instance_of(Array, result2, 'returned object is not an Array')
114
- assert_equal(2, result2.size, 'array changed size')
115
- assert_equal('a', result2[0], 'array changed data')
116
- assert_equal('b', result2[1], 'array changed data')
62
+ def test_get_service_registry_abstract()
63
+ assert_raises(NoMethodError) { @stub_service.get_service_registry() }
117
64
  end
118
65
 
119
- def test_normalize_type_int
120
- result1 = @stub_service.private_normalize_type(5, {:type => 'int'})
121
- assert_kind_of(Integer, result1)
122
- assert_equal(5, result1, 'bad conversion')
123
-
124
- result2 = @stub_service.private_normalize_type(2147483648, {:type => 'int'})
125
- assert_kind_of(Integer, result2)
126
- assert_equal(2147483648, result2, 'bad conversion')
127
- end
128
-
129
- def test_normalize_type_string
130
- result1 = @stub_service.private_normalize_type('foobar',
131
- {:type => 'string'})
132
- assert_kind_of(String, result1)
133
- assert_equal('foobar', result1, 'bad conversion')
134
-
135
- result2 = @stub_service.private_normalize_type('', {:type => 'string'})
136
- assert_kind_of(String, result2)
137
- assert_equal('', result2, 'bad conversion')
138
- end
139
-
140
- def test_normalize_type_long
141
- result1 = @stub_service.private_normalize_type(2147483648,
142
- {:type => 'long'})
143
- assert_kind_of(Integer, result1)
144
- assert_equal(2147483648, result1, 'bad conversion')
145
-
146
- result2 = @stub_service.private_normalize_type(-1, {:type => 'long'})
147
- assert_kind_of(Integer, result2)
148
- assert_equal(-1, result2, 'bad conversion')
149
- end
150
-
151
- def test_normalize_type_boolean
152
- result1 = @stub_service.private_normalize_type(true, {:type => 'boolean'})
153
- assert_kind_of(TrueClass, result1)
154
-
155
- result2 = @stub_service.private_normalize_type(false, {:type => 'boolean'})
156
- assert_kind_of(FalseClass, result2)
157
-
158
- result3 = @stub_service.private_normalize_type('true', {:type => 'boolean'})
159
- assert_kind_of(TrueClass, result3)
160
-
161
- result4 = @stub_service.private_normalize_type('false',
162
- {:type => 'boolean'})
163
- assert_kind_of(FalseClass, result4)
164
-
165
- result5 = @stub_service.private_normalize_type('True',
166
- {:type => 'boolean'})
167
- assert_kind_of(TrueClass, result3)
168
-
169
- result6 = @stub_service.private_normalize_type('False',
170
- {:type => 'boolean'})
171
- assert_kind_of(FalseClass, result4)
172
- end
173
-
174
- def test_normalize_type_object
175
- result1 = @stub_service.private_normalize_type({:a => 'b'},
176
- {:type => 'StubClass'})
177
- assert_equal('b', result1[:a], 'object corrupted')
178
-
179
- result2 = @stub_service.private_normalize_type(@stub_service,
180
- {:type => 'SavonService'})
181
- assert_equal(@stub_service.hash, result2.hash, 'object corrupted')
182
- end
183
-
184
- def test_normalize_type_double
185
- result1 = @stub_service.send(:private_normalize_type, 3.14,
186
- {:type => 'double'})
187
- assert_kind_of(Float, result1)
188
- assert_equal(3.14, result1, 'bad conversion')
189
-
190
- result2 = @stub_service.send(:private_normalize_type, '-3.14',
191
- {:type => 'double'})
192
- assert_kind_of(Float, result2)
193
- assert_equal(-3.14, result2, 'bad conversion')
194
-
195
- result3 = @stub_service.send(:private_normalize_type, '42',
196
- {:type => 'double'})
197
- assert_kind_of(Float, result3)
198
- assert_equal(42.0, result3, 'bad conversion')
199
- end
200
-
201
- def test_normalize_type_single_array_item
202
- result1 = @stub_service.private_normalize_type('42',
203
- {:type => 'double', :min_occurs => '0', :max_occurs => 1})
204
- assert_kind_of(Float, result1)
205
- assert_equal(42.0, result1, 'Float is expected for max_occurs 1')
206
-
207
- result2 = @stub_service.private_normalize_type('42',
208
- {:type => 'double', :min_occurs => '0', :max_occurs => :unbounded})
209
- assert_instance_of(Array, result2)
210
- assert_equal(42.0, result2[0], 'Array is expected for unbounded max_occurs')
211
-
212
- result3 = @stub_service.private_normalize_type('42',
213
- {:type => 'double', :min_occurs => '0', :max_occurs => 2})
214
- assert_instance_of(Array, result3)
215
- assert_equal(42.0, result3[0], 'Array is expected for max_occurs > 1')
216
-
217
- result4 = @stub_service.private_normalize_type('42',
218
- {:type => 'double', :min_occurs => '0', :max_occurs => nil})
219
- assert_instance_of(Float, result4)
220
- assert_equal(42.0, result4, 'Float is expected for nil max_occurs')
66
+ def test_get_module_abstract()
67
+ assert_raises(NoMethodError) { @stub_service.get_module() }
221
68
  end
222
69
  end