-
Notifications
You must be signed in to change notification settings - Fork 499
/
Copy pathutil_test.py
212 lines (170 loc) · 7.91 KB
/
util_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Tests for the Google Ads API client library utilities."""
from importlib import import_module
from unittest import TestCase
from google import protobuf
from google.protobuf.message import Message as ProtobufMessageType
import proto
# Checks if the current major version of protobuf is less than 4. If so,
# the _pb2 fixture generated by libprotoc 3.12.2 is imported, otherwise the
# _pb2 fixture generated by libprotoc 3.21.5 is imported.
if int(protobuf.__version__.split(".")[0]) < 4:
from fixtures.protobuf_fixture_3_12_1_pb2 import ProtobufFixture
else:
from fixtures.protobuf_fixture_3_21_5_pb2 import ProtobufFixture
from fixtures.proto_plus_fixture import ProtoPlusFixture
from google.ads.googleads import util
from google.ads.googleads import client
default_version = client._DEFAULT_VERSION
ClickConversion = import_module(
f"google.ads.googleads.{default_version}."
"services.types.conversion_upload_service"
).ClickConversion
class ConvertStringTest(TestCase):
def test_convert_upper_case_to_snake_case(self):
string = "GoogleAdsServiceClientTransport"
expected = "google_ads_service_client_transport"
result = util.convert_upper_case_to_snake_case(string)
self.assertEqual(result, expected)
def test_convert_snake_case_to_upper_case(self):
string = "google_ads_service_client_transport"
expected = "GoogleAdsServiceClientTransport"
result = util.convert_snake_case_to_upper_case(string)
self.assertEqual(result, expected)
class SetNestedMessageFieldTest(TestCase):
def test_set_nested_message_field(self):
val = "test value"
click_conversion = ClickConversion()
util.set_nested_message_field(
click_conversion,
"external_attribution_data.external_attribution_model",
val,
)
self.assertEqual(
click_conversion.external_attribution_data.external_attribution_model,
val,
)
class GetNestedMessageFieldTest(TestCase):
def test_get_nested_message_field(self):
val = "test value"
click_conversion = ClickConversion()
click_conversion.external_attribution_data.external_attribution_model = (
val
)
self.assertEqual(
util.get_nested_attr(
click_conversion,
"external_attribution_data.external_attribution_model",
),
val,
)
class ConvertProtoPlusToProtobufTest(TestCase):
def test_convert_proto_plus_to_protobuf(self):
"""A proto_plus proto is converted to a protobuf one."""
proto_plus = ProtoPlusFixture()
converted = util.convert_proto_plus_to_protobuf(proto_plus)
# Assert that the converted proto is an instance of the protobuf
# protobuf message class.
self.assertIsInstance(converted, ProtobufMessageType)
def test_convert_proto_plus_to_protobuf_if_protobuf(self):
"""If a protobuf proto is given then it is returned to the caller."""
protobuf = ProtobufFixture()
converted = util.convert_proto_plus_to_protobuf(protobuf)
self.assertEqual(protobuf, converted)
def test_proto_plus_to_protobuf_raises_type_error(self):
"""Method raises TypeError if not given a proto_plus proto."""
wrong_type = dict()
self.assertRaises(
TypeError, util.convert_proto_plus_to_protobuf, wrong_type
)
class ConvertProtobufToProtoPlusTest(TestCase):
def test_convert_protobuf_to_proto_plus(self):
"""A protobuf proto is converted to a proto_plus one."""
protobuf = ProtobufFixture()
converted = util.convert_protobuf_to_proto_plus(protobuf)
# Assert that the converted proto is an instance of the Message
# wrapper class.
self.assertIsInstance(converted, proto.Message)
def test_convert_protobuf_to_proto_plus_if_proto_plus(self):
"""If a protobuf proto is given then it is returned to the caller."""
proto_plus = ProtoPlusFixture()
converted = util.convert_protobuf_to_proto_plus(proto_plus)
self.assertEqual(proto_plus, converted)
def test_raises_type_error(self):
"""Method raises TypeError if not given a protobuf proto."""
wrong_type = dict()
self.assertRaises(
TypeError, util.convert_protobuf_to_proto_plus, wrong_type
)
class ProtoCopyFromTest(TestCase):
def test_client_copy_from_both_proto_plus(self):
"""util.proto_copy_from works with two proto_plus proto messages."""
destination = ProtoPlusFixture()
origin = ProtoPlusFixture()
origin.name = "Test"
util.proto_copy_from(destination, origin)
self.assertEqual(destination.name, "Test")
self.assertIsNot(destination, origin)
def test_client_copy_from_both_protobuf(self):
"""util.proto_copy_from works with two protobuf proto messages."""
destination = ProtobufFixture()
origin = ProtobufFixture()
origin.name = "Test"
util.proto_copy_from(destination, origin)
self.assertEqual(destination.name, "Test")
self.assertIsNot(destination, origin)
def test_client_copy_from_protobuf_origin(self):
"""util.proto_copy_from works with a proto_plus dest and a protobuf origin."""
destination = ProtoPlusFixture()
origin = ProtoPlusFixture()
origin = type(origin).pb(origin)
origin.name = "Test"
util.proto_copy_from(destination, origin)
self.assertEqual(destination.name, "Test")
self.assertIsNot(destination, origin)
def test_client_copy_from_protobuf_destination(self):
"""util.proto_copy_from works with a protobuf dest and a proto_plus origin."""
destination = ProtoPlusFixture()
destination = type(destination).pb(destination)
origin = ProtoPlusFixture()
origin.name = "Test"
util.proto_copy_from(destination, origin)
self.assertEqual(destination.name, "Test")
self.assertIsNot(destination, origin)
def test_client_copy_from_different_types_proto_plus(self):
"""TypeError is raised with different types of proto_plus messages."""
destination = ProtobufFixture()
destination = proto.Message.wrap(destination)
origin = ProtoPlusFixture()
origin.name = "Test"
self.assertRaises(TypeError, util.proto_copy_from, destination, origin)
def test_client_copy_from_different_types_protobuf(self):
"""TypeError is raised with different types of protobuf messages."""
destination = ProtoPlusFixture()
destination = type(destination).pb(destination)
origin = ProtobufFixture()
origin.name = "Test"
self.assertRaises(TypeError, util.proto_copy_from, destination, origin)
def test_client_copy_from_different_types_protobuf_origin(self):
"""TypeError is raised with different types and protobuf origin."""
destination = ProtoPlusFixture()
origin = ProtobufFixture()
origin.name = "Test"
self.assertRaises(TypeError, util.proto_copy_from, destination, origin)
def test_client_copy_from_non_proto_message(self):
"""ValueError is raised if an object other than a protobuf is given"""
destination = ProtoPlusFixture()
origin = {"name": "Test"}
self.assertRaises(ValueError, util.proto_copy_from, destination, origin)