matlab-prodserver-client 24.2__py3-none-any.whl
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.
- google/__init__.py +7 -0
- google/protobuf_ml/__init__.py +33 -0
- google/protobuf_ml/descriptor.py +1230 -0
- google/protobuf_ml/descriptor_database.py +177 -0
- google/protobuf_ml/descriptor_pb2.py +1925 -0
- google/protobuf_ml/descriptor_pool.py +1295 -0
- google/protobuf_ml/internal/__init__.py +0 -0
- google/protobuf_ml/internal/_parameterized.py +443 -0
- google/protobuf_ml/internal/api_implementation.py +168 -0
- google/protobuf_ml/internal/builder.py +130 -0
- google/protobuf_ml/internal/containers.py +708 -0
- google/protobuf_ml/internal/decoder.py +1068 -0
- google/protobuf_ml/internal/descriptor_database_test.py +127 -0
- google/protobuf_ml/internal/descriptor_pool_test.py +1149 -0
- google/protobuf_ml/internal/descriptor_test.py +1076 -0
- google/protobuf_ml/internal/encoder.py +829 -0
- google/protobuf_ml/internal/enum_type_wrapper.py +124 -0
- google/protobuf_ml/internal/extension_dict.py +213 -0
- google/protobuf_ml/internal/generator_test.py +354 -0
- google/protobuf_ml/internal/import_test.py +49 -0
- google/protobuf_ml/internal/json_format_test.py +1285 -0
- google/protobuf_ml/internal/keywords_test.py +103 -0
- google/protobuf_ml/internal/message_factory_test.py +299 -0
- google/protobuf_ml/internal/message_listener.py +78 -0
- google/protobuf_ml/internal/message_test.py +2573 -0
- google/protobuf_ml/internal/proto_builder_test.py +106 -0
- google/protobuf_ml/internal/python_message.py +1539 -0
- google/protobuf_ml/internal/reflection_test.py +3381 -0
- google/protobuf_ml/internal/service_reflection_test.py +139 -0
- google/protobuf_ml/internal/symbol_database_test.py +133 -0
- google/protobuf_ml/internal/test_util.py +878 -0
- google/protobuf_ml/internal/testing_refleaks.py +142 -0
- google/protobuf_ml/internal/text_encoding_test.py +67 -0
- google/protobuf_ml/internal/text_format_test.py +2447 -0
- google/protobuf_ml/internal/type_checkers.py +435 -0
- google/protobuf_ml/internal/unknown_fields_test.py +461 -0
- google/protobuf_ml/internal/well_known_types.py +887 -0
- google/protobuf_ml/internal/well_known_types_test.py +1013 -0
- google/protobuf_ml/internal/wire_format.py +268 -0
- google/protobuf_ml/internal/wire_format_test.py +252 -0
- google/protobuf_ml/json_format.py +912 -0
- google/protobuf_ml/message.py +424 -0
- google/protobuf_ml/message_factory.py +189 -0
- google/protobuf_ml/proto_builder.py +134 -0
- google/protobuf_ml/reflection.py +95 -0
- google/protobuf_ml/service.py +228 -0
- google/protobuf_ml/service_reflection.py +295 -0
- google/protobuf_ml/symbol_database.py +194 -0
- google/protobuf_ml/text_encoding.py +110 -0
- google/protobuf_ml/text_format.py +1842 -0
- google/protobuf_ml/unknown_fields.py +120 -0
- matlab/__init__.py +43 -0
- matlab/bin/win64/libmwfoundation_matlabdata.dll +0 -0
- matlab/bin/win64/libmwfoundation_matlabdata_standalone.dll +0 -0
- matlab/extern/bin/win64/libMatlabDataArray.dll +0 -0
- matlab/extern/bin/win64/matlabmultidimarrayforpython.cp310-win_amd64.pyd +0 -0
- matlab/extern/bin/win64/matlabmultidimarrayforpython.cp39-win_amd64.pyd +0 -0
- matlab/extern/bin/win64/matlabmultidimarrayforpython.pyd +0 -0
- matlab/production_server/__init__.py +2 -0
- matlab/production_server/_internal_mps/__init__.py +2 -0
- matlab/production_server/_internal_mps/feval_handler.py +76 -0
- matlab/production_server/_internal_mps/invocation_handler_utils.py +52 -0
- matlab/production_server/_internal_mps/matlab_array_builder.py +201 -0
- matlab/production_server/_internal_mps/matlab_response_builder.py +134 -0
- matlab/production_server/_proto_generated/MATLABArray_pb2.py +129 -0
- matlab/production_server/_proto_generated/MATLABError_pb2.py +27 -0
- matlab/production_server/_proto_generated/MATLABParams_pb2.py +26 -0
- matlab/production_server/_proto_generated/MATLABResult_pb2.py +27 -0
- matlab/production_server/_proto_generated/__init__.py +0 -0
- matlab/production_server/client.py +140 -0
- matlab/production_server/mpsexceptions.py +56 -0
- matlab_prodserver_client-24.2.dist-info/LICENSE.txt +27 -0
- matlab_prodserver_client-24.2.dist-info/METADATA +94 -0
- matlab_prodserver_client-24.2.dist-info/RECORD +77 -0
- matlab_prodserver_client-24.2.dist-info/WHEEL +5 -0
- matlab_prodserver_client-24.2.dist-info/top_level.txt +3 -0
- six.py +953 -0
|
@@ -0,0 +1,1013 @@
|
|
|
1
|
+
# Protocol Buffers - Google's data interchange format
|
|
2
|
+
# Copyright 2008 Google Inc. All rights reserved.
|
|
3
|
+
# https://developers.google.com/protocol-buffers/
|
|
4
|
+
#
|
|
5
|
+
# Redistribution and use in source and binary forms, with or without
|
|
6
|
+
# modification, are permitted provided that the following conditions are
|
|
7
|
+
# met:
|
|
8
|
+
#
|
|
9
|
+
# * Redistributions of source code must retain the above copyright
|
|
10
|
+
# notice, this list of conditions and the following disclaimer.
|
|
11
|
+
# * Redistributions in binary form must reproduce the above
|
|
12
|
+
# copyright notice, this list of conditions and the following disclaimer
|
|
13
|
+
# in the documentation and/or other materials provided with the
|
|
14
|
+
# distribution.
|
|
15
|
+
# * Neither the name of Google Inc. nor the names of its
|
|
16
|
+
# contributors may be used to endorse or promote products derived from
|
|
17
|
+
# this software without specific prior written permission.
|
|
18
|
+
#
|
|
19
|
+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
20
|
+
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
21
|
+
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
22
|
+
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
23
|
+
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
24
|
+
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
25
|
+
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
26
|
+
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
27
|
+
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
28
|
+
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
29
|
+
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
30
|
+
|
|
31
|
+
"""Test for google.protobuf_ml.internal.well_known_types."""
|
|
32
|
+
|
|
33
|
+
__author__ = 'jieluo@google.com (Jie Luo)'
|
|
34
|
+
|
|
35
|
+
import datetime
|
|
36
|
+
import unittest
|
|
37
|
+
from six.moves import collections_abc
|
|
38
|
+
|
|
39
|
+
from google.protobuf_ml import any_pb2
|
|
40
|
+
from google.protobuf_ml import duration_pb2
|
|
41
|
+
from google.protobuf_ml import field_mask_pb2
|
|
42
|
+
from google.protobuf_ml import struct_pb2
|
|
43
|
+
from google.protobuf_ml import timestamp_pb2
|
|
44
|
+
from google.protobuf_ml import map_unittest_pb2
|
|
45
|
+
from google.protobuf_ml import unittest_pb2
|
|
46
|
+
from google.protobuf_ml.internal import any_test_pb2
|
|
47
|
+
from google.protobuf_ml.internal import test_util
|
|
48
|
+
from google.protobuf_ml.internal import well_known_types
|
|
49
|
+
from google.protobuf_ml import descriptor
|
|
50
|
+
from google.protobuf_ml import text_format
|
|
51
|
+
from google.protobuf_ml.internal import _parameterized
|
|
52
|
+
|
|
53
|
+
try:
|
|
54
|
+
# New module in Python 3.9:
|
|
55
|
+
import zoneinfo # pylint:disable=g-import-not-at-top
|
|
56
|
+
_TZ_JAPAN = zoneinfo.ZoneInfo('Japan')
|
|
57
|
+
_TZ_PACIFIC = zoneinfo.ZoneInfo('US/Pacific')
|
|
58
|
+
except ImportError:
|
|
59
|
+
_TZ_JAPAN = datetime.timezone(datetime.timedelta(hours=9), 'Japan')
|
|
60
|
+
_TZ_PACIFIC = datetime.timezone(datetime.timedelta(hours=-8), 'US/Pacific')
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
class TimeUtilTestBase(_parameterized.TestCase):
|
|
64
|
+
|
|
65
|
+
def CheckTimestampConversion(self, message, text):
|
|
66
|
+
self.assertEqual(text, message.ToJsonString())
|
|
67
|
+
parsed_message = timestamp_pb2.Timestamp()
|
|
68
|
+
parsed_message.FromJsonString(text)
|
|
69
|
+
self.assertEqual(message, parsed_message)
|
|
70
|
+
|
|
71
|
+
def CheckDurationConversion(self, message, text):
|
|
72
|
+
self.assertEqual(text, message.ToJsonString())
|
|
73
|
+
parsed_message = duration_pb2.Duration()
|
|
74
|
+
parsed_message.FromJsonString(text)
|
|
75
|
+
self.assertEqual(message, parsed_message)
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
class TimeUtilTest(TimeUtilTestBase):
|
|
79
|
+
|
|
80
|
+
def testTimestampSerializeAndParse(self):
|
|
81
|
+
message = timestamp_pb2.Timestamp()
|
|
82
|
+
# Generated output should contain 3, 6, or 9 fractional digits.
|
|
83
|
+
message.seconds = 0
|
|
84
|
+
message.nanos = 0
|
|
85
|
+
self.CheckTimestampConversion(message, '1970-01-01T00:00:00Z')
|
|
86
|
+
message.nanos = 10000000
|
|
87
|
+
self.CheckTimestampConversion(message, '1970-01-01T00:00:00.010Z')
|
|
88
|
+
message.nanos = 10000
|
|
89
|
+
self.CheckTimestampConversion(message, '1970-01-01T00:00:00.000010Z')
|
|
90
|
+
message.nanos = 10
|
|
91
|
+
self.CheckTimestampConversion(message, '1970-01-01T00:00:00.000000010Z')
|
|
92
|
+
# Test min timestamps.
|
|
93
|
+
message.seconds = -62135596800
|
|
94
|
+
message.nanos = 0
|
|
95
|
+
self.CheckTimestampConversion(message, '0001-01-01T00:00:00Z')
|
|
96
|
+
# Test max timestamps.
|
|
97
|
+
message.seconds = 253402300799
|
|
98
|
+
message.nanos = 999999999
|
|
99
|
+
self.CheckTimestampConversion(message, '9999-12-31T23:59:59.999999999Z')
|
|
100
|
+
# Test negative timestamps.
|
|
101
|
+
message.seconds = -1
|
|
102
|
+
self.CheckTimestampConversion(message, '1969-12-31T23:59:59.999999999Z')
|
|
103
|
+
|
|
104
|
+
# Parsing accepts an fractional digits as long as they fit into nano
|
|
105
|
+
# precision.
|
|
106
|
+
message.FromJsonString('1970-01-01T00:00:00.1Z')
|
|
107
|
+
self.assertEqual(0, message.seconds)
|
|
108
|
+
self.assertEqual(100000000, message.nanos)
|
|
109
|
+
# Parsing accepts offsets.
|
|
110
|
+
message.FromJsonString('1970-01-01T00:00:00-08:00')
|
|
111
|
+
self.assertEqual(8 * 3600, message.seconds)
|
|
112
|
+
self.assertEqual(0, message.nanos)
|
|
113
|
+
|
|
114
|
+
# It is not easy to check with current time. For test coverage only.
|
|
115
|
+
message.GetCurrentTime()
|
|
116
|
+
self.assertNotEqual(8 * 3600, message.seconds)
|
|
117
|
+
|
|
118
|
+
def testDurationSerializeAndParse(self):
|
|
119
|
+
message = duration_pb2.Duration()
|
|
120
|
+
# Generated output should contain 3, 6, or 9 fractional digits.
|
|
121
|
+
message.seconds = 0
|
|
122
|
+
message.nanos = 0
|
|
123
|
+
self.CheckDurationConversion(message, '0s')
|
|
124
|
+
message.nanos = 10000000
|
|
125
|
+
self.CheckDurationConversion(message, '0.010s')
|
|
126
|
+
message.nanos = 10000
|
|
127
|
+
self.CheckDurationConversion(message, '0.000010s')
|
|
128
|
+
message.nanos = 10
|
|
129
|
+
self.CheckDurationConversion(message, '0.000000010s')
|
|
130
|
+
|
|
131
|
+
# Test min and max
|
|
132
|
+
message.seconds = 315576000000
|
|
133
|
+
message.nanos = 999999999
|
|
134
|
+
self.CheckDurationConversion(message, '315576000000.999999999s')
|
|
135
|
+
message.seconds = -315576000000
|
|
136
|
+
message.nanos = -999999999
|
|
137
|
+
self.CheckDurationConversion(message, '-315576000000.999999999s')
|
|
138
|
+
|
|
139
|
+
# Parsing accepts an fractional digits as long as they fit into nano
|
|
140
|
+
# precision.
|
|
141
|
+
message.FromJsonString('0.1s')
|
|
142
|
+
self.assertEqual(100000000, message.nanos)
|
|
143
|
+
message.FromJsonString('0.0000001s')
|
|
144
|
+
self.assertEqual(100, message.nanos)
|
|
145
|
+
|
|
146
|
+
def testTimestampIntegerConversion(self):
|
|
147
|
+
message = timestamp_pb2.Timestamp()
|
|
148
|
+
message.FromNanoseconds(1)
|
|
149
|
+
self.assertEqual('1970-01-01T00:00:00.000000001Z',
|
|
150
|
+
message.ToJsonString())
|
|
151
|
+
self.assertEqual(1, message.ToNanoseconds())
|
|
152
|
+
|
|
153
|
+
message.FromNanoseconds(-1)
|
|
154
|
+
self.assertEqual('1969-12-31T23:59:59.999999999Z',
|
|
155
|
+
message.ToJsonString())
|
|
156
|
+
self.assertEqual(-1, message.ToNanoseconds())
|
|
157
|
+
|
|
158
|
+
message.FromMicroseconds(1)
|
|
159
|
+
self.assertEqual('1970-01-01T00:00:00.000001Z',
|
|
160
|
+
message.ToJsonString())
|
|
161
|
+
self.assertEqual(1, message.ToMicroseconds())
|
|
162
|
+
|
|
163
|
+
message.FromMicroseconds(-1)
|
|
164
|
+
self.assertEqual('1969-12-31T23:59:59.999999Z',
|
|
165
|
+
message.ToJsonString())
|
|
166
|
+
self.assertEqual(-1, message.ToMicroseconds())
|
|
167
|
+
|
|
168
|
+
message.FromMilliseconds(1)
|
|
169
|
+
self.assertEqual('1970-01-01T00:00:00.001Z',
|
|
170
|
+
message.ToJsonString())
|
|
171
|
+
self.assertEqual(1, message.ToMilliseconds())
|
|
172
|
+
|
|
173
|
+
message.FromMilliseconds(-1)
|
|
174
|
+
self.assertEqual('1969-12-31T23:59:59.999Z',
|
|
175
|
+
message.ToJsonString())
|
|
176
|
+
self.assertEqual(-1, message.ToMilliseconds())
|
|
177
|
+
|
|
178
|
+
message.FromSeconds(1)
|
|
179
|
+
self.assertEqual('1970-01-01T00:00:01Z',
|
|
180
|
+
message.ToJsonString())
|
|
181
|
+
self.assertEqual(1, message.ToSeconds())
|
|
182
|
+
|
|
183
|
+
message.FromSeconds(-1)
|
|
184
|
+
self.assertEqual('1969-12-31T23:59:59Z',
|
|
185
|
+
message.ToJsonString())
|
|
186
|
+
self.assertEqual(-1, message.ToSeconds())
|
|
187
|
+
|
|
188
|
+
message.FromNanoseconds(1999)
|
|
189
|
+
self.assertEqual(1, message.ToMicroseconds())
|
|
190
|
+
# For negative values, Timestamp will be rounded down.
|
|
191
|
+
# For example, "1969-12-31T23:59:59.5Z" (i.e., -0.5s) rounded to seconds
|
|
192
|
+
# will be "1969-12-31T23:59:59Z" (i.e., -1s) rather than
|
|
193
|
+
# "1970-01-01T00:00:00Z" (i.e., 0s).
|
|
194
|
+
message.FromNanoseconds(-1999)
|
|
195
|
+
self.assertEqual(-2, message.ToMicroseconds())
|
|
196
|
+
|
|
197
|
+
def testDurationIntegerConversion(self):
|
|
198
|
+
message = duration_pb2.Duration()
|
|
199
|
+
message.FromNanoseconds(1)
|
|
200
|
+
self.assertEqual('0.000000001s',
|
|
201
|
+
message.ToJsonString())
|
|
202
|
+
self.assertEqual(1, message.ToNanoseconds())
|
|
203
|
+
|
|
204
|
+
message.FromNanoseconds(-1)
|
|
205
|
+
self.assertEqual('-0.000000001s',
|
|
206
|
+
message.ToJsonString())
|
|
207
|
+
self.assertEqual(-1, message.ToNanoseconds())
|
|
208
|
+
|
|
209
|
+
message.FromMicroseconds(1)
|
|
210
|
+
self.assertEqual('0.000001s',
|
|
211
|
+
message.ToJsonString())
|
|
212
|
+
self.assertEqual(1, message.ToMicroseconds())
|
|
213
|
+
|
|
214
|
+
message.FromMicroseconds(-1)
|
|
215
|
+
self.assertEqual('-0.000001s',
|
|
216
|
+
message.ToJsonString())
|
|
217
|
+
self.assertEqual(-1, message.ToMicroseconds())
|
|
218
|
+
|
|
219
|
+
message.FromMilliseconds(1)
|
|
220
|
+
self.assertEqual('0.001s',
|
|
221
|
+
message.ToJsonString())
|
|
222
|
+
self.assertEqual(1, message.ToMilliseconds())
|
|
223
|
+
|
|
224
|
+
message.FromMilliseconds(-1)
|
|
225
|
+
self.assertEqual('-0.001s',
|
|
226
|
+
message.ToJsonString())
|
|
227
|
+
self.assertEqual(-1, message.ToMilliseconds())
|
|
228
|
+
|
|
229
|
+
message.FromSeconds(1)
|
|
230
|
+
self.assertEqual('1s', message.ToJsonString())
|
|
231
|
+
self.assertEqual(1, message.ToSeconds())
|
|
232
|
+
|
|
233
|
+
message.FromSeconds(-1)
|
|
234
|
+
self.assertEqual('-1s',
|
|
235
|
+
message.ToJsonString())
|
|
236
|
+
self.assertEqual(-1, message.ToSeconds())
|
|
237
|
+
|
|
238
|
+
# Test truncation behavior.
|
|
239
|
+
message.FromNanoseconds(1999)
|
|
240
|
+
self.assertEqual(1, message.ToMicroseconds())
|
|
241
|
+
|
|
242
|
+
# For negative values, Duration will be rounded towards 0.
|
|
243
|
+
message.FromNanoseconds(-1999)
|
|
244
|
+
self.assertEqual(-1, message.ToMicroseconds())
|
|
245
|
+
|
|
246
|
+
def testTimezoneNaiveDatetimeConversion(self):
|
|
247
|
+
message = timestamp_pb2.Timestamp()
|
|
248
|
+
naive_utc_epoch = datetime.datetime(1970, 1, 1)
|
|
249
|
+
message.FromDatetime(naive_utc_epoch)
|
|
250
|
+
self.assertEqual(0, message.seconds)
|
|
251
|
+
self.assertEqual(0, message.nanos)
|
|
252
|
+
|
|
253
|
+
self.assertEqual(naive_utc_epoch, message.ToDatetime())
|
|
254
|
+
|
|
255
|
+
naive_epoch_morning = datetime.datetime(1970, 1, 1, 8, 0, 0, 1)
|
|
256
|
+
message.FromDatetime(naive_epoch_morning)
|
|
257
|
+
self.assertEqual(8 * 3600, message.seconds)
|
|
258
|
+
self.assertEqual(1000, message.nanos)
|
|
259
|
+
|
|
260
|
+
self.assertEqual(naive_epoch_morning, message.ToDatetime())
|
|
261
|
+
|
|
262
|
+
message.FromMilliseconds(1999)
|
|
263
|
+
self.assertEqual(1, message.seconds)
|
|
264
|
+
self.assertEqual(999_000_000, message.nanos)
|
|
265
|
+
|
|
266
|
+
self.assertEqual(datetime.datetime(1970, 1, 1, 0, 0, 1, 999000),
|
|
267
|
+
message.ToDatetime())
|
|
268
|
+
|
|
269
|
+
naive_future = datetime.datetime(2555, 2, 22, 1, 2, 3, 456789)
|
|
270
|
+
message.FromDatetime(naive_future)
|
|
271
|
+
self.assertEqual(naive_future, message.ToDatetime())
|
|
272
|
+
|
|
273
|
+
naive_end_of_time = datetime.datetime.max
|
|
274
|
+
message.FromDatetime(naive_end_of_time)
|
|
275
|
+
self.assertEqual(naive_end_of_time, message.ToDatetime())
|
|
276
|
+
|
|
277
|
+
# Two hours after the Unix Epoch, around the world.
|
|
278
|
+
@_parameterized.named_parameters(
|
|
279
|
+
('London', [1970, 1, 1, 2], datetime.timezone.utc),
|
|
280
|
+
('Tokyo', [1970, 1, 1, 11], _TZ_JAPAN),
|
|
281
|
+
('LA', [1969, 12, 31, 18], _TZ_PACIFIC),
|
|
282
|
+
)
|
|
283
|
+
def testTimezoneAwareDatetimeConversion(self, date_parts, tzinfo):
|
|
284
|
+
original_datetime = datetime.datetime(*date_parts, tzinfo=tzinfo) # pylint:disable=g-tzinfo-datetime
|
|
285
|
+
|
|
286
|
+
message = timestamp_pb2.Timestamp()
|
|
287
|
+
message.FromDatetime(original_datetime)
|
|
288
|
+
self.assertEqual(7200, message.seconds)
|
|
289
|
+
self.assertEqual(0, message.nanos)
|
|
290
|
+
|
|
291
|
+
# ToDatetime() with no parameters produces a naive UTC datetime, i.e. it not
|
|
292
|
+
# only loses the original timezone information (e.g. US/Pacific) as it's
|
|
293
|
+
# "normalised" to UTC, but also drops the information that the datetime
|
|
294
|
+
# represents a UTC one.
|
|
295
|
+
naive_datetime = message.ToDatetime()
|
|
296
|
+
self.assertEqual(datetime.datetime(1970, 1, 1, 2), naive_datetime)
|
|
297
|
+
self.assertIsNone(naive_datetime.tzinfo)
|
|
298
|
+
self.assertNotEqual(original_datetime, naive_datetime) # not even for UTC!
|
|
299
|
+
|
|
300
|
+
# In contrast, ToDatetime(tzinfo=) produces an aware datetime in the given
|
|
301
|
+
# timezone.
|
|
302
|
+
aware_datetime = message.ToDatetime(tzinfo=tzinfo)
|
|
303
|
+
self.assertEqual(original_datetime, aware_datetime)
|
|
304
|
+
self.assertEqual(
|
|
305
|
+
datetime.datetime(1970, 1, 1, 2, tzinfo=datetime.timezone.utc),
|
|
306
|
+
aware_datetime)
|
|
307
|
+
self.assertEqual(tzinfo, aware_datetime.tzinfo)
|
|
308
|
+
|
|
309
|
+
def testTimedeltaConversion(self):
|
|
310
|
+
message = duration_pb2.Duration()
|
|
311
|
+
message.FromNanoseconds(1999999999)
|
|
312
|
+
td = message.ToTimedelta()
|
|
313
|
+
self.assertEqual(1, td.seconds)
|
|
314
|
+
self.assertEqual(999999, td.microseconds)
|
|
315
|
+
|
|
316
|
+
message.FromNanoseconds(-1999999999)
|
|
317
|
+
td = message.ToTimedelta()
|
|
318
|
+
self.assertEqual(-1, td.days)
|
|
319
|
+
self.assertEqual(86398, td.seconds)
|
|
320
|
+
self.assertEqual(1, td.microseconds)
|
|
321
|
+
|
|
322
|
+
message.FromMicroseconds(-1)
|
|
323
|
+
td = message.ToTimedelta()
|
|
324
|
+
self.assertEqual(-1, td.days)
|
|
325
|
+
self.assertEqual(86399, td.seconds)
|
|
326
|
+
self.assertEqual(999999, td.microseconds)
|
|
327
|
+
converted_message = duration_pb2.Duration()
|
|
328
|
+
converted_message.FromTimedelta(td)
|
|
329
|
+
self.assertEqual(message, converted_message)
|
|
330
|
+
|
|
331
|
+
def testInvalidTimestamp(self):
|
|
332
|
+
message = timestamp_pb2.Timestamp()
|
|
333
|
+
self.assertRaisesRegex(
|
|
334
|
+
ValueError, 'Failed to parse timestamp: missing valid timezone offset.',
|
|
335
|
+
message.FromJsonString, '')
|
|
336
|
+
self.assertRaisesRegex(
|
|
337
|
+
ValueError, 'Failed to parse timestamp: invalid trailing data '
|
|
338
|
+
'1970-01-01T00:00:01Ztrail.', message.FromJsonString,
|
|
339
|
+
'1970-01-01T00:00:01Ztrail')
|
|
340
|
+
self.assertRaisesRegex(
|
|
341
|
+
ValueError, 'time data \'10000-01-01T00:00:00\' does not match'
|
|
342
|
+
' format \'%Y-%m-%dT%H:%M:%S\'', message.FromJsonString,
|
|
343
|
+
'10000-01-01T00:00:00.00Z')
|
|
344
|
+
self.assertRaisesRegex(
|
|
345
|
+
ValueError, 'nanos 0123456789012 more than 9 fractional digits.',
|
|
346
|
+
message.FromJsonString, '1970-01-01T00:00:00.0123456789012Z')
|
|
347
|
+
self.assertRaisesRegex(
|
|
348
|
+
ValueError,
|
|
349
|
+
(r'Invalid timezone offset value: \+08.'),
|
|
350
|
+
message.FromJsonString,
|
|
351
|
+
'1972-01-01T01:00:00.01+08',
|
|
352
|
+
)
|
|
353
|
+
self.assertRaisesRegex(ValueError, 'year (0 )?is out of range',
|
|
354
|
+
message.FromJsonString, '0000-01-01T00:00:00Z')
|
|
355
|
+
message.seconds = 253402300800
|
|
356
|
+
self.assertRaisesRegex(OverflowError, 'date value out of range',
|
|
357
|
+
message.ToJsonString)
|
|
358
|
+
|
|
359
|
+
def testInvalidDuration(self):
|
|
360
|
+
message = duration_pb2.Duration()
|
|
361
|
+
self.assertRaisesRegex(ValueError, 'Duration must end with letter "s": 1.',
|
|
362
|
+
message.FromJsonString, '1')
|
|
363
|
+
self.assertRaisesRegex(ValueError, 'Couldn\'t parse duration: 1...2s.',
|
|
364
|
+
message.FromJsonString, '1...2s')
|
|
365
|
+
text = '-315576000001.000000000s'
|
|
366
|
+
self.assertRaisesRegex(
|
|
367
|
+
ValueError,
|
|
368
|
+
r'Duration is not valid\: Seconds -315576000001 must be in range'
|
|
369
|
+
r' \[-315576000000\, 315576000000\].', message.FromJsonString, text)
|
|
370
|
+
text = '315576000001.000000000s'
|
|
371
|
+
self.assertRaisesRegex(
|
|
372
|
+
ValueError,
|
|
373
|
+
r'Duration is not valid\: Seconds 315576000001 must be in range'
|
|
374
|
+
r' \[-315576000000\, 315576000000\].', message.FromJsonString, text)
|
|
375
|
+
message.seconds = -315576000001
|
|
376
|
+
message.nanos = 0
|
|
377
|
+
self.assertRaisesRegex(
|
|
378
|
+
ValueError,
|
|
379
|
+
r'Duration is not valid\: Seconds -315576000001 must be in range'
|
|
380
|
+
r' \[-315576000000\, 315576000000\].', message.ToJsonString)
|
|
381
|
+
message.seconds = 0
|
|
382
|
+
message.nanos = 999999999 + 1
|
|
383
|
+
self.assertRaisesRegex(
|
|
384
|
+
ValueError, r'Duration is not valid\: Nanos 1000000000 must be in range'
|
|
385
|
+
r' \[-999999999\, 999999999\].', message.ToJsonString)
|
|
386
|
+
message.seconds = -1
|
|
387
|
+
message.nanos = 1
|
|
388
|
+
self.assertRaisesRegex(ValueError,
|
|
389
|
+
r'Duration is not valid\: Sign mismatch.',
|
|
390
|
+
message.ToJsonString)
|
|
391
|
+
|
|
392
|
+
|
|
393
|
+
class FieldMaskTest(unittest.TestCase):
|
|
394
|
+
|
|
395
|
+
def testStringFormat(self):
|
|
396
|
+
mask = field_mask_pb2.FieldMask()
|
|
397
|
+
self.assertEqual('', mask.ToJsonString())
|
|
398
|
+
mask.paths.append('foo')
|
|
399
|
+
self.assertEqual('foo', mask.ToJsonString())
|
|
400
|
+
mask.paths.append('bar')
|
|
401
|
+
self.assertEqual('foo,bar', mask.ToJsonString())
|
|
402
|
+
|
|
403
|
+
mask.FromJsonString('')
|
|
404
|
+
self.assertEqual('', mask.ToJsonString())
|
|
405
|
+
mask.FromJsonString('foo')
|
|
406
|
+
self.assertEqual(['foo'], mask.paths)
|
|
407
|
+
mask.FromJsonString('foo,bar')
|
|
408
|
+
self.assertEqual(['foo', 'bar'], mask.paths)
|
|
409
|
+
|
|
410
|
+
# Test camel case
|
|
411
|
+
mask.Clear()
|
|
412
|
+
mask.paths.append('foo_bar')
|
|
413
|
+
self.assertEqual('fooBar', mask.ToJsonString())
|
|
414
|
+
mask.paths.append('bar_quz')
|
|
415
|
+
self.assertEqual('fooBar,barQuz', mask.ToJsonString())
|
|
416
|
+
|
|
417
|
+
mask.FromJsonString('')
|
|
418
|
+
self.assertEqual('', mask.ToJsonString())
|
|
419
|
+
self.assertEqual([], mask.paths)
|
|
420
|
+
mask.FromJsonString('fooBar')
|
|
421
|
+
self.assertEqual(['foo_bar'], mask.paths)
|
|
422
|
+
mask.FromJsonString('fooBar,barQuz')
|
|
423
|
+
self.assertEqual(['foo_bar', 'bar_quz'], mask.paths)
|
|
424
|
+
|
|
425
|
+
def testDescriptorToFieldMask(self):
|
|
426
|
+
mask = field_mask_pb2.FieldMask()
|
|
427
|
+
msg_descriptor = unittest_pb2.TestAllTypes.DESCRIPTOR
|
|
428
|
+
mask.AllFieldsFromDescriptor(msg_descriptor)
|
|
429
|
+
self.assertEqual(76, len(mask.paths))
|
|
430
|
+
self.assertTrue(mask.IsValidForDescriptor(msg_descriptor))
|
|
431
|
+
for field in msg_descriptor.fields:
|
|
432
|
+
self.assertTrue(field.name in mask.paths)
|
|
433
|
+
|
|
434
|
+
def testIsValidForDescriptor(self):
|
|
435
|
+
msg_descriptor = unittest_pb2.TestAllTypes.DESCRIPTOR
|
|
436
|
+
# Empty mask
|
|
437
|
+
mask = field_mask_pb2.FieldMask()
|
|
438
|
+
self.assertTrue(mask.IsValidForDescriptor(msg_descriptor))
|
|
439
|
+
# All fields from descriptor
|
|
440
|
+
mask.AllFieldsFromDescriptor(msg_descriptor)
|
|
441
|
+
self.assertTrue(mask.IsValidForDescriptor(msg_descriptor))
|
|
442
|
+
# Child under optional message
|
|
443
|
+
mask.paths.append('optional_nested_message.bb')
|
|
444
|
+
self.assertTrue(mask.IsValidForDescriptor(msg_descriptor))
|
|
445
|
+
# Repeated field is only allowed in the last position of path
|
|
446
|
+
mask.paths.append('repeated_nested_message.bb')
|
|
447
|
+
self.assertFalse(mask.IsValidForDescriptor(msg_descriptor))
|
|
448
|
+
# Invalid top level field
|
|
449
|
+
mask = field_mask_pb2.FieldMask()
|
|
450
|
+
mask.paths.append('xxx')
|
|
451
|
+
self.assertFalse(mask.IsValidForDescriptor(msg_descriptor))
|
|
452
|
+
# Invalid field in root
|
|
453
|
+
mask = field_mask_pb2.FieldMask()
|
|
454
|
+
mask.paths.append('xxx.zzz')
|
|
455
|
+
self.assertFalse(mask.IsValidForDescriptor(msg_descriptor))
|
|
456
|
+
# Invalid field in internal node
|
|
457
|
+
mask = field_mask_pb2.FieldMask()
|
|
458
|
+
mask.paths.append('optional_nested_message.xxx.zzz')
|
|
459
|
+
self.assertFalse(mask.IsValidForDescriptor(msg_descriptor))
|
|
460
|
+
# Invalid field in leaf
|
|
461
|
+
mask = field_mask_pb2.FieldMask()
|
|
462
|
+
mask.paths.append('optional_nested_message.xxx')
|
|
463
|
+
self.assertFalse(mask.IsValidForDescriptor(msg_descriptor))
|
|
464
|
+
|
|
465
|
+
def testCanonicalFrom(self):
|
|
466
|
+
mask = field_mask_pb2.FieldMask()
|
|
467
|
+
out_mask = field_mask_pb2.FieldMask()
|
|
468
|
+
# Paths will be sorted.
|
|
469
|
+
mask.FromJsonString('baz.quz,bar,foo')
|
|
470
|
+
out_mask.CanonicalFormFromMask(mask)
|
|
471
|
+
self.assertEqual('bar,baz.quz,foo', out_mask.ToJsonString())
|
|
472
|
+
# Duplicated paths will be removed.
|
|
473
|
+
mask.FromJsonString('foo,bar,foo')
|
|
474
|
+
out_mask.CanonicalFormFromMask(mask)
|
|
475
|
+
self.assertEqual('bar,foo', out_mask.ToJsonString())
|
|
476
|
+
# Sub-paths of other paths will be removed.
|
|
477
|
+
mask.FromJsonString('foo.b1,bar.b1,foo.b2,bar')
|
|
478
|
+
out_mask.CanonicalFormFromMask(mask)
|
|
479
|
+
self.assertEqual('bar,foo.b1,foo.b2', out_mask.ToJsonString())
|
|
480
|
+
|
|
481
|
+
# Test more deeply nested cases.
|
|
482
|
+
mask.FromJsonString(
|
|
483
|
+
'foo.bar.baz1,foo.bar.baz2.quz,foo.bar.baz2')
|
|
484
|
+
out_mask.CanonicalFormFromMask(mask)
|
|
485
|
+
self.assertEqual('foo.bar.baz1,foo.bar.baz2',
|
|
486
|
+
out_mask.ToJsonString())
|
|
487
|
+
mask.FromJsonString(
|
|
488
|
+
'foo.bar.baz1,foo.bar.baz2,foo.bar.baz2.quz')
|
|
489
|
+
out_mask.CanonicalFormFromMask(mask)
|
|
490
|
+
self.assertEqual('foo.bar.baz1,foo.bar.baz2',
|
|
491
|
+
out_mask.ToJsonString())
|
|
492
|
+
mask.FromJsonString(
|
|
493
|
+
'foo.bar.baz1,foo.bar.baz2,foo.bar.baz2.quz,foo.bar')
|
|
494
|
+
out_mask.CanonicalFormFromMask(mask)
|
|
495
|
+
self.assertEqual('foo.bar', out_mask.ToJsonString())
|
|
496
|
+
mask.FromJsonString(
|
|
497
|
+
'foo.bar.baz1,foo.bar.baz2,foo.bar.baz2.quz,foo')
|
|
498
|
+
out_mask.CanonicalFormFromMask(mask)
|
|
499
|
+
self.assertEqual('foo', out_mask.ToJsonString())
|
|
500
|
+
|
|
501
|
+
def testUnion(self):
|
|
502
|
+
mask1 = field_mask_pb2.FieldMask()
|
|
503
|
+
mask2 = field_mask_pb2.FieldMask()
|
|
504
|
+
out_mask = field_mask_pb2.FieldMask()
|
|
505
|
+
mask1.FromJsonString('foo,baz')
|
|
506
|
+
mask2.FromJsonString('bar,quz')
|
|
507
|
+
out_mask.Union(mask1, mask2)
|
|
508
|
+
self.assertEqual('bar,baz,foo,quz', out_mask.ToJsonString())
|
|
509
|
+
# Overlap with duplicated paths.
|
|
510
|
+
mask1.FromJsonString('foo,baz.bb')
|
|
511
|
+
mask2.FromJsonString('baz.bb,quz')
|
|
512
|
+
out_mask.Union(mask1, mask2)
|
|
513
|
+
self.assertEqual('baz.bb,foo,quz', out_mask.ToJsonString())
|
|
514
|
+
# Overlap with paths covering some other paths.
|
|
515
|
+
mask1.FromJsonString('foo.bar.baz,quz')
|
|
516
|
+
mask2.FromJsonString('foo.bar,bar')
|
|
517
|
+
out_mask.Union(mask1, mask2)
|
|
518
|
+
self.assertEqual('bar,foo.bar,quz', out_mask.ToJsonString())
|
|
519
|
+
src = unittest_pb2.TestAllTypes()
|
|
520
|
+
with self.assertRaises(ValueError):
|
|
521
|
+
out_mask.Union(src, mask2)
|
|
522
|
+
|
|
523
|
+
def testIntersect(self):
|
|
524
|
+
mask1 = field_mask_pb2.FieldMask()
|
|
525
|
+
mask2 = field_mask_pb2.FieldMask()
|
|
526
|
+
out_mask = field_mask_pb2.FieldMask()
|
|
527
|
+
# Test cases without overlapping.
|
|
528
|
+
mask1.FromJsonString('foo,baz')
|
|
529
|
+
mask2.FromJsonString('bar,quz')
|
|
530
|
+
out_mask.Intersect(mask1, mask2)
|
|
531
|
+
self.assertEqual('', out_mask.ToJsonString())
|
|
532
|
+
self.assertEqual(len(out_mask.paths), 0)
|
|
533
|
+
self.assertEqual(out_mask.paths, [])
|
|
534
|
+
# Overlap with duplicated paths.
|
|
535
|
+
mask1.FromJsonString('foo,baz.bb')
|
|
536
|
+
mask2.FromJsonString('baz.bb,quz')
|
|
537
|
+
out_mask.Intersect(mask1, mask2)
|
|
538
|
+
self.assertEqual('baz.bb', out_mask.ToJsonString())
|
|
539
|
+
# Overlap with paths covering some other paths.
|
|
540
|
+
mask1.FromJsonString('foo.bar.baz,quz')
|
|
541
|
+
mask2.FromJsonString('foo.bar,bar')
|
|
542
|
+
out_mask.Intersect(mask1, mask2)
|
|
543
|
+
self.assertEqual('foo.bar.baz', out_mask.ToJsonString())
|
|
544
|
+
mask1.FromJsonString('foo.bar,bar')
|
|
545
|
+
mask2.FromJsonString('foo.bar.baz,quz')
|
|
546
|
+
out_mask.Intersect(mask1, mask2)
|
|
547
|
+
self.assertEqual('foo.bar.baz', out_mask.ToJsonString())
|
|
548
|
+
# Intersect '' with ''
|
|
549
|
+
mask1.Clear()
|
|
550
|
+
mask2.Clear()
|
|
551
|
+
mask1.paths.append('')
|
|
552
|
+
mask2.paths.append('')
|
|
553
|
+
self.assertEqual(mask1.paths, [''])
|
|
554
|
+
self.assertEqual('', mask1.ToJsonString())
|
|
555
|
+
out_mask.Intersect(mask1, mask2)
|
|
556
|
+
self.assertEqual(out_mask.paths, [])
|
|
557
|
+
|
|
558
|
+
def testMergeMessageWithoutMapFields(self):
|
|
559
|
+
# Test merge one field.
|
|
560
|
+
src = unittest_pb2.TestAllTypes()
|
|
561
|
+
test_util.SetAllFields(src)
|
|
562
|
+
for field in src.DESCRIPTOR.fields:
|
|
563
|
+
if field.containing_oneof:
|
|
564
|
+
continue
|
|
565
|
+
field_name = field.name
|
|
566
|
+
dst = unittest_pb2.TestAllTypes()
|
|
567
|
+
# Only set one path to mask.
|
|
568
|
+
mask = field_mask_pb2.FieldMask()
|
|
569
|
+
mask.paths.append(field_name)
|
|
570
|
+
mask.MergeMessage(src, dst)
|
|
571
|
+
# The expected result message.
|
|
572
|
+
msg = unittest_pb2.TestAllTypes()
|
|
573
|
+
if field.label == descriptor.FieldDescriptor.LABEL_REPEATED:
|
|
574
|
+
repeated_src = getattr(src, field_name)
|
|
575
|
+
repeated_msg = getattr(msg, field_name)
|
|
576
|
+
if field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_MESSAGE:
|
|
577
|
+
for item in repeated_src:
|
|
578
|
+
repeated_msg.add().CopyFrom(item)
|
|
579
|
+
else:
|
|
580
|
+
repeated_msg.extend(repeated_src)
|
|
581
|
+
elif field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_MESSAGE:
|
|
582
|
+
getattr(msg, field_name).CopyFrom(getattr(src, field_name))
|
|
583
|
+
else:
|
|
584
|
+
setattr(msg, field_name, getattr(src, field_name))
|
|
585
|
+
# Only field specified in mask is merged.
|
|
586
|
+
self.assertEqual(msg, dst)
|
|
587
|
+
|
|
588
|
+
# Test merge nested fields.
|
|
589
|
+
nested_src = unittest_pb2.NestedTestAllTypes()
|
|
590
|
+
nested_dst = unittest_pb2.NestedTestAllTypes()
|
|
591
|
+
nested_src.child.payload.optional_int32 = 1234
|
|
592
|
+
nested_src.child.child.payload.optional_int32 = 5678
|
|
593
|
+
mask = field_mask_pb2.FieldMask()
|
|
594
|
+
mask.FromJsonString('child.payload')
|
|
595
|
+
mask.MergeMessage(nested_src, nested_dst)
|
|
596
|
+
self.assertEqual(1234, nested_dst.child.payload.optional_int32)
|
|
597
|
+
self.assertEqual(0, nested_dst.child.child.payload.optional_int32)
|
|
598
|
+
|
|
599
|
+
mask.FromJsonString('child.child.payload')
|
|
600
|
+
mask.MergeMessage(nested_src, nested_dst)
|
|
601
|
+
self.assertEqual(1234, nested_dst.child.payload.optional_int32)
|
|
602
|
+
self.assertEqual(5678, nested_dst.child.child.payload.optional_int32)
|
|
603
|
+
|
|
604
|
+
nested_dst.Clear()
|
|
605
|
+
mask.FromJsonString('child.child.payload')
|
|
606
|
+
mask.MergeMessage(nested_src, nested_dst)
|
|
607
|
+
self.assertEqual(0, nested_dst.child.payload.optional_int32)
|
|
608
|
+
self.assertEqual(5678, nested_dst.child.child.payload.optional_int32)
|
|
609
|
+
|
|
610
|
+
nested_dst.Clear()
|
|
611
|
+
mask.FromJsonString('child')
|
|
612
|
+
mask.MergeMessage(nested_src, nested_dst)
|
|
613
|
+
self.assertEqual(1234, nested_dst.child.payload.optional_int32)
|
|
614
|
+
self.assertEqual(5678, nested_dst.child.child.payload.optional_int32)
|
|
615
|
+
|
|
616
|
+
# Test MergeOptions.
|
|
617
|
+
nested_dst.Clear()
|
|
618
|
+
nested_dst.child.payload.optional_int64 = 4321
|
|
619
|
+
# Message fields will be merged by default.
|
|
620
|
+
mask.FromJsonString('child.payload')
|
|
621
|
+
mask.MergeMessage(nested_src, nested_dst)
|
|
622
|
+
self.assertEqual(1234, nested_dst.child.payload.optional_int32)
|
|
623
|
+
self.assertEqual(4321, nested_dst.child.payload.optional_int64)
|
|
624
|
+
# Change the behavior to replace message fields.
|
|
625
|
+
mask.FromJsonString('child.payload')
|
|
626
|
+
mask.MergeMessage(nested_src, nested_dst, True, False)
|
|
627
|
+
self.assertEqual(1234, nested_dst.child.payload.optional_int32)
|
|
628
|
+
self.assertEqual(0, nested_dst.child.payload.optional_int64)
|
|
629
|
+
|
|
630
|
+
# By default, fields missing in source are not cleared in destination.
|
|
631
|
+
nested_dst.payload.optional_int32 = 1234
|
|
632
|
+
self.assertTrue(nested_dst.HasField('payload'))
|
|
633
|
+
mask.FromJsonString('payload')
|
|
634
|
+
mask.MergeMessage(nested_src, nested_dst)
|
|
635
|
+
self.assertTrue(nested_dst.HasField('payload'))
|
|
636
|
+
# But they are cleared when replacing message fields.
|
|
637
|
+
nested_dst.Clear()
|
|
638
|
+
nested_dst.payload.optional_int32 = 1234
|
|
639
|
+
mask.FromJsonString('payload')
|
|
640
|
+
mask.MergeMessage(nested_src, nested_dst, True, False)
|
|
641
|
+
self.assertFalse(nested_dst.HasField('payload'))
|
|
642
|
+
|
|
643
|
+
nested_src.payload.repeated_int32.append(1234)
|
|
644
|
+
nested_dst.payload.repeated_int32.append(5678)
|
|
645
|
+
# Repeated fields will be appended by default.
|
|
646
|
+
mask.FromJsonString('payload.repeatedInt32')
|
|
647
|
+
mask.MergeMessage(nested_src, nested_dst)
|
|
648
|
+
self.assertEqual(2, len(nested_dst.payload.repeated_int32))
|
|
649
|
+
self.assertEqual(5678, nested_dst.payload.repeated_int32[0])
|
|
650
|
+
self.assertEqual(1234, nested_dst.payload.repeated_int32[1])
|
|
651
|
+
# Change the behavior to replace repeated fields.
|
|
652
|
+
mask.FromJsonString('payload.repeatedInt32')
|
|
653
|
+
mask.MergeMessage(nested_src, nested_dst, False, True)
|
|
654
|
+
self.assertEqual(1, len(nested_dst.payload.repeated_int32))
|
|
655
|
+
self.assertEqual(1234, nested_dst.payload.repeated_int32[0])
|
|
656
|
+
|
|
657
|
+
# Test Merge oneof field.
|
|
658
|
+
new_msg = unittest_pb2.TestOneof2()
|
|
659
|
+
dst = unittest_pb2.TestOneof2()
|
|
660
|
+
dst.foo_message.moo_int = 1
|
|
661
|
+
mask = field_mask_pb2.FieldMask()
|
|
662
|
+
mask.FromJsonString('fooMessage,fooLazyMessage.mooInt')
|
|
663
|
+
mask.MergeMessage(new_msg, dst)
|
|
664
|
+
self.assertTrue(dst.HasField('foo_message'))
|
|
665
|
+
self.assertFalse(dst.HasField('foo_lazy_message'))
|
|
666
|
+
|
|
667
|
+
def testMergeMessageWithMapField(self):
|
|
668
|
+
empty_map = map_unittest_pb2.TestRecursiveMapMessage()
|
|
669
|
+
src_level_2 = map_unittest_pb2.TestRecursiveMapMessage()
|
|
670
|
+
src_level_2.a['src level 2'].CopyFrom(empty_map)
|
|
671
|
+
src = map_unittest_pb2.TestRecursiveMapMessage()
|
|
672
|
+
src.a['common key'].CopyFrom(src_level_2)
|
|
673
|
+
src.a['src level 1'].CopyFrom(src_level_2)
|
|
674
|
+
|
|
675
|
+
dst_level_2 = map_unittest_pb2.TestRecursiveMapMessage()
|
|
676
|
+
dst_level_2.a['dst level 2'].CopyFrom(empty_map)
|
|
677
|
+
dst = map_unittest_pb2.TestRecursiveMapMessage()
|
|
678
|
+
dst.a['common key'].CopyFrom(dst_level_2)
|
|
679
|
+
dst.a['dst level 1'].CopyFrom(empty_map)
|
|
680
|
+
|
|
681
|
+
mask = field_mask_pb2.FieldMask()
|
|
682
|
+
mask.FromJsonString('a')
|
|
683
|
+
mask.MergeMessage(src, dst)
|
|
684
|
+
|
|
685
|
+
# map from dst is replaced with map from src.
|
|
686
|
+
self.assertEqual(dst.a['common key'], src_level_2)
|
|
687
|
+
self.assertEqual(dst.a['src level 1'], src_level_2)
|
|
688
|
+
self.assertEqual(dst.a['dst level 1'], empty_map)
|
|
689
|
+
|
|
690
|
+
def testMergeErrors(self):
|
|
691
|
+
src = unittest_pb2.TestAllTypes()
|
|
692
|
+
dst = unittest_pb2.TestAllTypes()
|
|
693
|
+
mask = field_mask_pb2.FieldMask()
|
|
694
|
+
test_util.SetAllFields(src)
|
|
695
|
+
mask.FromJsonString('optionalInt32.field')
|
|
696
|
+
with self.assertRaises(ValueError) as e:
|
|
697
|
+
mask.MergeMessage(src, dst)
|
|
698
|
+
self.assertEqual('Error: Field optional_int32 in message '
|
|
699
|
+
'protobuf_unittest.TestAllTypes is not a singular '
|
|
700
|
+
'message field and cannot have sub-fields.',
|
|
701
|
+
str(e.exception))
|
|
702
|
+
|
|
703
|
+
def testSnakeCaseToCamelCase(self):
|
|
704
|
+
self.assertEqual('fooBar',
|
|
705
|
+
well_known_types._SnakeCaseToCamelCase('foo_bar'))
|
|
706
|
+
self.assertEqual('FooBar',
|
|
707
|
+
well_known_types._SnakeCaseToCamelCase('_foo_bar'))
|
|
708
|
+
self.assertEqual('foo3Bar',
|
|
709
|
+
well_known_types._SnakeCaseToCamelCase('foo3_bar'))
|
|
710
|
+
|
|
711
|
+
# No uppercase letter is allowed.
|
|
712
|
+
self.assertRaisesRegex(
|
|
713
|
+
ValueError,
|
|
714
|
+
'Fail to print FieldMask to Json string: Path name Foo must '
|
|
715
|
+
'not contain uppercase letters.',
|
|
716
|
+
well_known_types._SnakeCaseToCamelCase, 'Foo')
|
|
717
|
+
# Any character after a "_" must be a lowercase letter.
|
|
718
|
+
# 1. "_" cannot be followed by another "_".
|
|
719
|
+
# 2. "_" cannot be followed by a digit.
|
|
720
|
+
# 3. "_" cannot appear as the last character.
|
|
721
|
+
self.assertRaisesRegex(
|
|
722
|
+
ValueError,
|
|
723
|
+
'Fail to print FieldMask to Json string: The character after a '
|
|
724
|
+
'"_" must be a lowercase letter in path name foo__bar.',
|
|
725
|
+
well_known_types._SnakeCaseToCamelCase, 'foo__bar')
|
|
726
|
+
self.assertRaisesRegex(
|
|
727
|
+
ValueError,
|
|
728
|
+
'Fail to print FieldMask to Json string: The character after a '
|
|
729
|
+
'"_" must be a lowercase letter in path name foo_3bar.',
|
|
730
|
+
well_known_types._SnakeCaseToCamelCase, 'foo_3bar')
|
|
731
|
+
self.assertRaisesRegex(
|
|
732
|
+
ValueError,
|
|
733
|
+
'Fail to print FieldMask to Json string: Trailing "_" in path '
|
|
734
|
+
'name foo_bar_.', well_known_types._SnakeCaseToCamelCase, 'foo_bar_')
|
|
735
|
+
|
|
736
|
+
def testCamelCaseToSnakeCase(self):
|
|
737
|
+
self.assertEqual('foo_bar',
|
|
738
|
+
well_known_types._CamelCaseToSnakeCase('fooBar'))
|
|
739
|
+
self.assertEqual('_foo_bar',
|
|
740
|
+
well_known_types._CamelCaseToSnakeCase('FooBar'))
|
|
741
|
+
self.assertEqual('foo3_bar',
|
|
742
|
+
well_known_types._CamelCaseToSnakeCase('foo3Bar'))
|
|
743
|
+
self.assertRaisesRegex(
|
|
744
|
+
ValueError,
|
|
745
|
+
'Fail to parse FieldMask: Path name foo_bar must not contain "_"s.',
|
|
746
|
+
well_known_types._CamelCaseToSnakeCase, 'foo_bar')
|
|
747
|
+
|
|
748
|
+
|
|
749
|
+
class StructTest(unittest.TestCase):
|
|
750
|
+
|
|
751
|
+
def testStruct(self):
|
|
752
|
+
struct = struct_pb2.Struct()
|
|
753
|
+
self.assertIsInstance(struct, collections_abc.Mapping)
|
|
754
|
+
self.assertEqual(0, len(struct))
|
|
755
|
+
struct_class = struct.__class__
|
|
756
|
+
|
|
757
|
+
struct['key1'] = 5
|
|
758
|
+
struct['key2'] = 'abc'
|
|
759
|
+
struct['key3'] = True
|
|
760
|
+
struct.get_or_create_struct('key4')['subkey'] = 11.0
|
|
761
|
+
struct_list = struct.get_or_create_list('key5')
|
|
762
|
+
self.assertIsInstance(struct_list, collections_abc.Sequence)
|
|
763
|
+
struct_list.extend([6, 'seven', True, False, None])
|
|
764
|
+
struct_list.add_struct()['subkey2'] = 9
|
|
765
|
+
struct['key6'] = {'subkey': {}}
|
|
766
|
+
struct['key7'] = [2, False]
|
|
767
|
+
|
|
768
|
+
self.assertEqual(7, len(struct))
|
|
769
|
+
self.assertTrue(isinstance(struct, well_known_types.Struct))
|
|
770
|
+
self.assertEqual(5, struct['key1'])
|
|
771
|
+
self.assertEqual('abc', struct['key2'])
|
|
772
|
+
self.assertIs(True, struct['key3'])
|
|
773
|
+
self.assertEqual(11, struct['key4']['subkey'])
|
|
774
|
+
inner_struct = struct_class()
|
|
775
|
+
inner_struct['subkey2'] = 9
|
|
776
|
+
self.assertEqual([6, 'seven', True, False, None, inner_struct],
|
|
777
|
+
list(struct['key5'].items()))
|
|
778
|
+
self.assertEqual({}, dict(struct['key6']['subkey'].fields))
|
|
779
|
+
self.assertEqual([2, False], list(struct['key7'].items()))
|
|
780
|
+
|
|
781
|
+
serialized = struct.SerializeToString()
|
|
782
|
+
struct2 = struct_pb2.Struct()
|
|
783
|
+
struct2.ParseFromString(serialized)
|
|
784
|
+
|
|
785
|
+
self.assertEqual(struct, struct2)
|
|
786
|
+
for key, value in struct.items():
|
|
787
|
+
self.assertIn(key, struct)
|
|
788
|
+
self.assertIn(key, struct2)
|
|
789
|
+
self.assertEqual(value, struct2[key])
|
|
790
|
+
|
|
791
|
+
self.assertEqual(7, len(struct.keys()))
|
|
792
|
+
self.assertEqual(7, len(struct.values()))
|
|
793
|
+
for key in struct.keys():
|
|
794
|
+
self.assertIn(key, struct)
|
|
795
|
+
self.assertIn(key, struct2)
|
|
796
|
+
self.assertEqual(struct[key], struct2[key])
|
|
797
|
+
|
|
798
|
+
item = (next(iter(struct.keys())), next(iter(struct.values())))
|
|
799
|
+
self.assertEqual(item, next(iter(struct.items())))
|
|
800
|
+
|
|
801
|
+
self.assertTrue(isinstance(struct2, well_known_types.Struct))
|
|
802
|
+
self.assertEqual(5, struct2['key1'])
|
|
803
|
+
self.assertEqual('abc', struct2['key2'])
|
|
804
|
+
self.assertIs(True, struct2['key3'])
|
|
805
|
+
self.assertEqual(11, struct2['key4']['subkey'])
|
|
806
|
+
self.assertEqual([6, 'seven', True, False, None, inner_struct],
|
|
807
|
+
list(struct2['key5'].items()))
|
|
808
|
+
|
|
809
|
+
struct_list = struct2['key5']
|
|
810
|
+
self.assertEqual(6, struct_list[0])
|
|
811
|
+
self.assertEqual('seven', struct_list[1])
|
|
812
|
+
self.assertEqual(True, struct_list[2])
|
|
813
|
+
self.assertEqual(False, struct_list[3])
|
|
814
|
+
self.assertEqual(None, struct_list[4])
|
|
815
|
+
self.assertEqual(inner_struct, struct_list[5])
|
|
816
|
+
|
|
817
|
+
struct_list[1] = 7
|
|
818
|
+
self.assertEqual(7, struct_list[1])
|
|
819
|
+
|
|
820
|
+
struct_list.add_list().extend([1, 'two', True, False, None])
|
|
821
|
+
self.assertEqual([1, 'two', True, False, None],
|
|
822
|
+
list(struct_list[6].items()))
|
|
823
|
+
struct_list.extend([{'nested_struct': 30}, ['nested_list', 99], {}, []])
|
|
824
|
+
self.assertEqual(11, len(struct_list.values))
|
|
825
|
+
self.assertEqual(30, struct_list[7]['nested_struct'])
|
|
826
|
+
self.assertEqual('nested_list', struct_list[8][0])
|
|
827
|
+
self.assertEqual(99, struct_list[8][1])
|
|
828
|
+
self.assertEqual({}, dict(struct_list[9].fields))
|
|
829
|
+
self.assertEqual([], list(struct_list[10].items()))
|
|
830
|
+
struct_list[0] = {'replace': 'set'}
|
|
831
|
+
struct_list[1] = ['replace', 'set']
|
|
832
|
+
self.assertEqual('set', struct_list[0]['replace'])
|
|
833
|
+
self.assertEqual(['replace', 'set'], list(struct_list[1].items()))
|
|
834
|
+
|
|
835
|
+
text_serialized = str(struct)
|
|
836
|
+
struct3 = struct_pb2.Struct()
|
|
837
|
+
text_format.Merge(text_serialized, struct3)
|
|
838
|
+
self.assertEqual(struct, struct3)
|
|
839
|
+
|
|
840
|
+
struct.get_or_create_struct('key3')['replace'] = 12
|
|
841
|
+
self.assertEqual(12, struct['key3']['replace'])
|
|
842
|
+
|
|
843
|
+
# Tests empty list.
|
|
844
|
+
struct.get_or_create_list('empty_list')
|
|
845
|
+
empty_list = struct['empty_list']
|
|
846
|
+
self.assertEqual([], list(empty_list.items()))
|
|
847
|
+
list2 = struct_pb2.ListValue()
|
|
848
|
+
list2.add_list()
|
|
849
|
+
empty_list = list2[0]
|
|
850
|
+
self.assertEqual([], list(empty_list.items()))
|
|
851
|
+
|
|
852
|
+
# Tests empty struct.
|
|
853
|
+
struct.get_or_create_struct('empty_struct')
|
|
854
|
+
empty_struct = struct['empty_struct']
|
|
855
|
+
self.assertEqual({}, dict(empty_struct.fields))
|
|
856
|
+
list2.add_struct()
|
|
857
|
+
empty_struct = list2[1]
|
|
858
|
+
self.assertEqual({}, dict(empty_struct.fields))
|
|
859
|
+
|
|
860
|
+
self.assertEqual(9, len(struct))
|
|
861
|
+
del struct['key3']
|
|
862
|
+
del struct['key4']
|
|
863
|
+
self.assertEqual(7, len(struct))
|
|
864
|
+
self.assertEqual(6, len(struct['key5']))
|
|
865
|
+
del struct['key5'][1]
|
|
866
|
+
self.assertEqual(5, len(struct['key5']))
|
|
867
|
+
self.assertEqual([6, True, False, None, inner_struct],
|
|
868
|
+
list(struct['key5'].items()))
|
|
869
|
+
|
|
870
|
+
def testStructAssignment(self):
|
|
871
|
+
# Tests struct assignment from another struct
|
|
872
|
+
s1 = struct_pb2.Struct()
|
|
873
|
+
s2 = struct_pb2.Struct()
|
|
874
|
+
for value in [1, 'a', [1], ['a'], {'a': 'b'}]:
|
|
875
|
+
s1['x'] = value
|
|
876
|
+
s2['x'] = s1['x']
|
|
877
|
+
self.assertEqual(s1['x'], s2['x'])
|
|
878
|
+
|
|
879
|
+
def testMergeFrom(self):
|
|
880
|
+
struct = struct_pb2.Struct()
|
|
881
|
+
struct_class = struct.__class__
|
|
882
|
+
|
|
883
|
+
dictionary = {
|
|
884
|
+
'key1': 5,
|
|
885
|
+
'key2': 'abc',
|
|
886
|
+
'key3': True,
|
|
887
|
+
'key4': {'subkey': 11.0},
|
|
888
|
+
'key5': [6, 'seven', True, False, None, {'subkey2': 9}],
|
|
889
|
+
'key6': [['nested_list', True]],
|
|
890
|
+
'empty_struct': {},
|
|
891
|
+
'empty_list': []
|
|
892
|
+
}
|
|
893
|
+
struct.update(dictionary)
|
|
894
|
+
self.assertEqual(5, struct['key1'])
|
|
895
|
+
self.assertEqual('abc', struct['key2'])
|
|
896
|
+
self.assertIs(True, struct['key3'])
|
|
897
|
+
self.assertEqual(11, struct['key4']['subkey'])
|
|
898
|
+
inner_struct = struct_class()
|
|
899
|
+
inner_struct['subkey2'] = 9
|
|
900
|
+
self.assertEqual([6, 'seven', True, False, None, inner_struct],
|
|
901
|
+
list(struct['key5'].items()))
|
|
902
|
+
self.assertEqual(2, len(struct['key6'][0].values))
|
|
903
|
+
self.assertEqual('nested_list', struct['key6'][0][0])
|
|
904
|
+
self.assertEqual(True, struct['key6'][0][1])
|
|
905
|
+
empty_list = struct['empty_list']
|
|
906
|
+
self.assertEqual([], list(empty_list.items()))
|
|
907
|
+
empty_struct = struct['empty_struct']
|
|
908
|
+
self.assertEqual({}, dict(empty_struct.fields))
|
|
909
|
+
|
|
910
|
+
# According to documentation: "When parsing from the wire or when merging,
|
|
911
|
+
# if there are duplicate map keys the last key seen is used".
|
|
912
|
+
duplicate = {
|
|
913
|
+
'key4': {'replace': 20},
|
|
914
|
+
'key5': [[False, 5]]
|
|
915
|
+
}
|
|
916
|
+
struct.update(duplicate)
|
|
917
|
+
self.assertEqual(1, len(struct['key4'].fields))
|
|
918
|
+
self.assertEqual(20, struct['key4']['replace'])
|
|
919
|
+
self.assertEqual(1, len(struct['key5'].values))
|
|
920
|
+
self.assertEqual(False, struct['key5'][0][0])
|
|
921
|
+
self.assertEqual(5, struct['key5'][0][1])
|
|
922
|
+
|
|
923
|
+
|
|
924
|
+
class AnyTest(unittest.TestCase):
|
|
925
|
+
|
|
926
|
+
def testAnyMessage(self):
|
|
927
|
+
# Creates and sets message.
|
|
928
|
+
msg = any_test_pb2.TestAny()
|
|
929
|
+
msg_descriptor = msg.DESCRIPTOR
|
|
930
|
+
all_types = unittest_pb2.TestAllTypes()
|
|
931
|
+
all_descriptor = all_types.DESCRIPTOR
|
|
932
|
+
all_types.repeated_string.append(u'\u00fc\ua71f')
|
|
933
|
+
# Packs to Any.
|
|
934
|
+
msg.value.Pack(all_types)
|
|
935
|
+
self.assertEqual(msg.value.type_url,
|
|
936
|
+
'type.googleapis.com/%s' % all_descriptor.full_name)
|
|
937
|
+
self.assertEqual(msg.value.value,
|
|
938
|
+
all_types.SerializeToString())
|
|
939
|
+
# Tests Is() method.
|
|
940
|
+
self.assertTrue(msg.value.Is(all_descriptor))
|
|
941
|
+
self.assertFalse(msg.value.Is(msg_descriptor))
|
|
942
|
+
# Unpacks Any.
|
|
943
|
+
unpacked_message = unittest_pb2.TestAllTypes()
|
|
944
|
+
self.assertTrue(msg.value.Unpack(unpacked_message))
|
|
945
|
+
self.assertEqual(all_types, unpacked_message)
|
|
946
|
+
# Unpacks to different type.
|
|
947
|
+
self.assertFalse(msg.value.Unpack(msg))
|
|
948
|
+
# Only Any messages have Pack method.
|
|
949
|
+
try:
|
|
950
|
+
msg.Pack(all_types)
|
|
951
|
+
except AttributeError:
|
|
952
|
+
pass
|
|
953
|
+
else:
|
|
954
|
+
raise AttributeError('%s should not have Pack method.' %
|
|
955
|
+
msg_descriptor.full_name)
|
|
956
|
+
|
|
957
|
+
def testUnpackWithNoSlashInTypeUrl(self):
|
|
958
|
+
msg = any_test_pb2.TestAny()
|
|
959
|
+
all_types = unittest_pb2.TestAllTypes()
|
|
960
|
+
all_descriptor = all_types.DESCRIPTOR
|
|
961
|
+
msg.value.Pack(all_types)
|
|
962
|
+
# Reset type_url to part of type_url after '/'
|
|
963
|
+
msg.value.type_url = msg.value.TypeName()
|
|
964
|
+
self.assertFalse(msg.value.Is(all_descriptor))
|
|
965
|
+
unpacked_message = unittest_pb2.TestAllTypes()
|
|
966
|
+
self.assertFalse(msg.value.Unpack(unpacked_message))
|
|
967
|
+
|
|
968
|
+
def testMessageName(self):
|
|
969
|
+
# Creates and sets message.
|
|
970
|
+
submessage = any_test_pb2.TestAny()
|
|
971
|
+
submessage.int_value = 12345
|
|
972
|
+
msg = any_pb2.Any()
|
|
973
|
+
msg.Pack(submessage)
|
|
974
|
+
self.assertEqual(msg.TypeName(), 'google.protobuf_ml.internal.TestAny')
|
|
975
|
+
|
|
976
|
+
def testPackWithCustomTypeUrl(self):
|
|
977
|
+
submessage = any_test_pb2.TestAny()
|
|
978
|
+
submessage.int_value = 12345
|
|
979
|
+
msg = any_pb2.Any()
|
|
980
|
+
# Pack with a custom type URL prefix.
|
|
981
|
+
msg.Pack(submessage, 'type.myservice.com')
|
|
982
|
+
self.assertEqual(msg.type_url,
|
|
983
|
+
'type.myservice.com/%s' % submessage.DESCRIPTOR.full_name)
|
|
984
|
+
# Pack with a custom type URL prefix ending with '/'.
|
|
985
|
+
msg.Pack(submessage, 'type.myservice.com/')
|
|
986
|
+
self.assertEqual(msg.type_url,
|
|
987
|
+
'type.myservice.com/%s' % submessage.DESCRIPTOR.full_name)
|
|
988
|
+
# Pack with an empty type URL prefix.
|
|
989
|
+
msg.Pack(submessage, '')
|
|
990
|
+
self.assertEqual(msg.type_url,
|
|
991
|
+
'/%s' % submessage.DESCRIPTOR.full_name)
|
|
992
|
+
# Test unpacking the type.
|
|
993
|
+
unpacked_message = any_test_pb2.TestAny()
|
|
994
|
+
self.assertTrue(msg.Unpack(unpacked_message))
|
|
995
|
+
self.assertEqual(submessage, unpacked_message)
|
|
996
|
+
|
|
997
|
+
def testPackDeterministic(self):
|
|
998
|
+
submessage = any_test_pb2.TestAny()
|
|
999
|
+
for i in range(10):
|
|
1000
|
+
submessage.map_value[str(i)] = i * 2
|
|
1001
|
+
msg = any_pb2.Any()
|
|
1002
|
+
msg.Pack(submessage, deterministic=True)
|
|
1003
|
+
serialized = msg.SerializeToString(deterministic=True)
|
|
1004
|
+
golden = (b'\n4type.googleapis.com/google.protobuf_ml.internal.TestAny\x12F'
|
|
1005
|
+
b'\x1a\x05\n\x010\x10\x00\x1a\x05\n\x011\x10\x02\x1a\x05\n\x01'
|
|
1006
|
+
b'2\x10\x04\x1a\x05\n\x013\x10\x06\x1a\x05\n\x014\x10\x08\x1a'
|
|
1007
|
+
b'\x05\n\x015\x10\n\x1a\x05\n\x016\x10\x0c\x1a\x05\n\x017\x10'
|
|
1008
|
+
b'\x0e\x1a\x05\n\x018\x10\x10\x1a\x05\n\x019\x10\x12')
|
|
1009
|
+
self.assertEqual(golden, serialized)
|
|
1010
|
+
|
|
1011
|
+
|
|
1012
|
+
if __name__ == '__main__':
|
|
1013
|
+
unittest.main()
|