Debian Codegraph @

lang/py3/avro/tests/test_protocol.py

raw
1 #!/usr/bin/env python3
2 # -*- mode: python -*-
3 # -*- coding: utf-8 -*-
4
5 # Licensed to the Apache Software Foundation (ASF) under one
6 # or more contributor license agreements. See the NOTICE file
7 # distributed with this work for additional information
8 # regarding copyright ownership. The ASF licenses this file
9 # to you under the Apache License, Version 2.0 (the
10 # "License"); you may not use this file except in compliance
11 # with the License. You may obtain a copy of the License at
12 #
13 # http://www.apache.org/licenses/LICENSE-2.0
14 #
15 # Unless required by applicable law or agreed to in writing, software
16 # distributed under the License is distributed on an "AS IS" BASIS,
17 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 # See the License for the specific language governing permissions and
19 # limitations under the License.
20 """
21 Test the protocol parsing logic.
22 """
23
24 import logging
25 import traceback
26 import unittest
27
28 from avro import protocol
29
30
31 # ------------------------------------------------------------------------------
32
33
34 class ExampleProtocol(object):
35 def __init__(
36 self,
37 protocol_string,
38 valid=True,
39 ):
40 self._protocol_string = protocol_string
41 self._valid = valid
42
43 @property
44 def protocol_string(self):
45 return self._protocol_string
46
47 @property
48 def valid(self):
49 return self._valid
50
51
52 # ------------------------------------------------------------------------------
53 # Protocol test cases:
54
55 HELLO_WORLD = ExampleProtocol("""
56 {
57 "namespace": "com.acme",
58 "protocol": "HelloWorld",
59 "types": [
60 {
61 "name": "Greeting",
62 "type": "record",
63 "fields": [{"name": "message", "type": "string"}]
64 },
65 {
66 "name": "Curse",
67 "type": "error",
68 "fields": [{"name": "message", "type": "string"}]
69 }
70 ],
71 "messages": {
72 "hello": {
73 "request": [{"name": "greeting", "type": "Greeting" }],
74 "response": "Greeting",
75 "errors": ["Curse"]
76 }
77 }
78 }
79 """)
80
81 EXAMPLES = [
82 HELLO_WORLD,
83
84 ExampleProtocol(
85 """
86 {
87 "namespace": "org.apache.avro.test",
88 "protocol": "Simple",
89 "types": [
90 {"name": "Kind", "type": "enum", "symbols": ["FOO","BAR","BAZ"]},
91 {"name": "MD5", "type": "fixed", "size": 16},
92 {"name": "TestRecord", "type": "record",
93 "fields": [
94 {"name": "name", "type": "string", "order": "ignore"},
95 {"name": "kind", "type": "Kind", "order": "descending"},
96 {"name": "hash", "type": "MD5"}
97 ]
98 },
99 {"name": "TestError", "type": "error",
100 "fields": [
101 {"name": "message", "type": "string"}
102 ]
103 }
104 ],
105 "messages": {
106 "hello": {
107 "request": [{"name": "greeting", "type": "string"}],
108 "response": "string"
109 },
110 "echo": {
111 "request": [{"name": "record", "type": "TestRecord"}],
112 "response": "TestRecord"
113 },
114 "add": {
115 "request": [{"name": "arg1", "type": "int"},
116 {"name": "arg2", "type": "int"}],
117 "response": "int"
118 },
119 "echoBytes": {
120 "request": [{"name": "data", "type": "bytes"}],
121 "response": "bytes"
122 },
123 "error": {
124 "request": [],
125 "response": "null",
126 "errors": ["TestError"]
127 }
128 }
129 }
130 """),
131
132 ExampleProtocol(
133 """
134 {
135 "namespace": "org.apache.avro.test.namespace",
136 "protocol": "TestNamespace",
137 "types": [
138 {"name": "org.apache.avro.test.util.MD5", "type": "fixed", "size": 16},
139 {
140 "name": "TestRecord",
141 "type": "record",
142 "fields": [{"name": "hash", "type": "org.apache.avro.test.util.MD5"}]
143 },
144 {
145 "name": "TestError",
146 "namespace": "org.apache.avro.test.errors",
147 "type": "error",
148 "fields": [{"name": "message", "type": "string"}]
149 }
150 ],
151 "messages": {
152 "echo": {
153 "request": [{"name": "record", "type": "TestRecord"}],
154 "response": "TestRecord"
155 },
156 "error": {
157 "request": [],
158 "response": "null",
159 "errors": ["org.apache.avro.test.errors.TestError"]
160 }
161 }
162 }
163 """),
164
165 ExampleProtocol(
166 """
167 {
168 "namespace": "org.apache.avro.test.namespace",
169 "protocol": "TestImplicitNamespace",
170 "types": [
171 {"name": "org.apache.avro.test.util.MD5", "type": "fixed", "size": 16},
172 {
173 "name": "ReferencedRecord",
174 "type": "record",
175 "fields": [{"name": "foo", "type": "string"}]
176 },
177 {
178 "name": "TestRecord",
179 "type": "record",
180 "fields": [
181 {"name": "hash", "type": "org.apache.avro.test.util.MD5"},
182 {"name": "unqalified", "type": "ReferencedRecord"}
183 ]
184 },
185 {
186 "name": "TestError",
187 "type": "error",
188 "fields": [{"name": "message", "type": "string"}]
189 }
190 ],
191 "messages": {
192 "echo": {
193 "request": [
194 {"name": "qualified", "type": "org.apache.avro.test.namespace.TestRecord"}
195 ],
196 "response": "TestRecord"
197 },
198 "error": {
199 "request": [],
200 "response": "null",
201 "errors": ["org.apache.avro.test.namespace.TestError"]
202 }
203 }
204 }
205 """),
206
207 ExampleProtocol(
208 """
209 {
210 "namespace": "org.apache.avro.test.namespace",
211 "protocol": "TestNamespaceTwo",
212 "types": [
213 {"name": "org.apache.avro.test.util.MD5", "type": "fixed", "size": 16},
214 {
215 "name": "ReferencedRecord",
216 "namespace": "org.apache.avro.other.namespace",
217 "type": "record",
218 "fields": [{"name": "foo", "type": "string"}]
219 },
220 {
221 "name": "TestRecord",
222 "type": "record",
223 "fields": [
224 {"name": "hash", "type": "org.apache.avro.test.util.MD5"},
225 {
226 "name": "qualified",
227 "type": "org.apache.avro.other.namespace.ReferencedRecord"
228 }
229 ]
230 },
231 {
232 "name": "TestError",
233 "type": "error",
234 "fields": [{"name": "message", "type": "string"}]
235 }
236 ],
237 "messages": {
238 "echo": {
239 "request": [
240 {
241 "name": "qualified",
242 "type": "org.apache.avro.test.namespace.TestRecord"
243 }
244 ],
245 "response": "TestRecord"
246 },
247 "error": {
248 "request": [],
249 "response": "null",
250 "errors": ["org.apache.avro.test.namespace.TestError"]
251 }
252 }
253 }
254 """),
255
256 ExampleProtocol(
257 """
258 {
259 "namespace": "org.apache.avro.test.namespace",
260 "protocol": "TestValidRepeatedName",
261 "types": [
262 {"name": "org.apache.avro.test.util.MD5", "type": "fixed", "size": 16},
263 {
264 "name": "ReferencedRecord",
265 "namespace": "org.apache.avro.other.namespace",
266 "type": "record",
267 "fields": [{"name": "foo", "type": "string"}]
268 },
269 {
270 "name": "ReferencedRecord",
271 "type": "record",
272 "fields": [{"name": "bar", "type": "double"}]
273 },
274 {
275 "name": "TestError",
276 "type": "error",
277 "fields": [{"name": "message", "type": "string"}]
278 }
279 ],
280 "messages": {
281 "echo": {
282 "request": [{"name": "qualified", "type": "ReferencedRecord"}],
283 "response": "org.apache.avro.other.namespace.ReferencedRecord"
284 },
285 "error": {
286 "request": [],
287 "response": "null",
288 "errors": ["org.apache.avro.test.namespace.TestError"]
289 }
290 }
291 }
292 """),
293
294 ExampleProtocol(
295 """
296 {
297 "namespace": "org.apache.avro.test.namespace",
298 "protocol": "TestInvalidRepeatedName",
299 "types": [
300 {"name": "org.apache.avro.test.util.MD5", "type": "fixed", "size": 16},
301 {
302 "name": "ReferencedRecord",
303 "type": "record",
304 "fields": [{"name": "foo", "type": "string"}]
305 },
306 {
307 "name": "ReferencedRecord",
308 "type": "record",
309 "fields": [{"name": "bar", "type": "double"}]
310 },
311 {
312 "name": "TestError",
313 "type": "error",
314 "fields": [{"name": "message", "type": "string"}]
315 }
316 ],
317 "messages": {
318 "echo": {
319 "request": [{"name": "qualified", "type": "ReferencedRecord"}],
320 "response": "org.apache.avro.other.namespace.ReferencedRecord"
321 },
322 "error": {
323 "request": [],
324 "response": "null",
325 "errors": ["org.apache.avro.test.namespace.TestError"]
326 }
327 }
328 }
329 """,
330 valid=False),
331
332 ExampleProtocol(
333 """
334 {
335 "namespace": "org.apache.avro.test",
336 "protocol": "BulkData",
337 "types": [],
338 "messages": {
339 "read": {
340 "request": [],
341 "response": "bytes"
342 },
343 "write": {
344 "request": [ {"name": "data", "type": "bytes"} ],
345 "response": "null"
346 }
347 }
348 }
349 """),
350
351 ExampleProtocol(
352 """
353 {
354 "protocol": "API",
355 "namespace": "xyz.api",
356 "types": [
357 {
358 "type": "enum",
359 "name": "Symbology",
360 "namespace": "xyz.api.product",
361 "symbols": [ "OPRA", "CUSIP", "ISIN", "SEDOL" ]
362 },
363 {
364 "type": "record",
365 "name": "Symbol",
366 "namespace": "xyz.api.product",
367 "fields": [ {
368 "name": "symbology",
369 "type": "xyz.api.product.Symbology"
370 }, {
371 "name": "symbol",
372 "type": "string"
373 } ]
374 },
375 {
376 "type": "record",
377 "name": "MultiSymbol",
378 "namespace": "xyz.api.product",
379 "fields": [{
380 "name": "symbols",
381 "type": {
382 "type": "map",
383 "values": "xyz.api.product.Symbol"
384 }
385 }]
386 }
387 ],
388 "messages": {}
389 }
390 """),
391 ]
392 # End of EXAMPLES
393
394
395 VALID_EXAMPLES = [e for e in EXAMPLES if e.valid]
396
397
398 # ------------------------------------------------------------------------------
399
400
401 class TestProtocol(unittest.TestCase):
402
403 def testParse(self):
404 correct = 0
405 for iexample, example in enumerate(EXAMPLES):
406 logging.debug(
407 'Parsing protocol #%d:\n%s',
408 iexample, example.protocol_string)
409 try:
410 parsed = protocol.Parse(example.protocol_string)
411 if example.valid:
412 correct += 1
413 else:
414 self.fail(
415 'Invalid protocol was parsed:\n%s' % example.protocol_string)
416 except Exception as exn:
417 if example.valid:
418 self.fail(
419 'Valid protocol failed to parse: %s\n%s'
420 % (example.protocol_string, traceback.format_exc()))
421 else:
422 if logging.getLogger().getEffectiveLevel() <= 5:
423 logging.debug('Expected error:\n%s', traceback.format_exc())
424 else:
425 logging.debug('Expected error: %r', exn)
426 correct += 1
427
428 self.assertEqual(
429 correct,
430 len(EXAMPLES),
431 'Parse behavior correct on %d out of %d protocols.'
432 % (correct, len(EXAMPLES)))
433
434 def testInnerNamespaceSet(self):
435 proto = protocol.Parse(HELLO_WORLD.protocol_string)
436 self.assertEqual(proto.namespace, 'com.acme')
437 greeting_type = proto.type_map['com.acme.Greeting']
438 self.assertEqual(greeting_type.namespace, 'com.acme')
439
440 def testInnerNamespaceNotRendered(self):
441 proto = protocol.Parse(HELLO_WORLD.protocol_string)
442 self.assertEqual('com.acme.Greeting', proto.types[0].fullname)
443 self.assertEqual('Greeting', proto.types[0].name)
444 # but there shouldn't be 'namespace' rendered to json on the inner type
445 self.assertFalse('namespace' in proto.to_json()['types'][0])
446
447 def testValidCastToStringAfterParse(self):
448 """
449 Test that the string generated by an Avro Protocol object is,
450 in fact, a valid Avro protocol.
451 """
452 num_correct = 0
453 for example in VALID_EXAMPLES:
454 proto = protocol.Parse(example.protocol_string)
455 try:
456 protocol.Parse(str(proto))
457 logging.debug(
458 'Successfully reparsed protocol:\n%s',
459 example.protocol_string)
460 num_correct += 1
461 except:
462 logging.debug(
463 'Failed to reparse protocol:\n%s',
464 example.protocol_string)
465
466 fail_msg = (
467 'Cast to string success on %d out of %d protocols'
468 % (num_correct, len(VALID_EXAMPLES)))
469 self.assertEqual(num_correct, len(VALID_EXAMPLES), fail_msg)
470
471 def testEquivalenceAfterRoundTrip(self):
472 """
473 1. Given a string, parse it to get Avro protocol "original".
474 2. Serialize "original" to a string and parse that string
475 to generate Avro protocol "round trip".
476 3. Ensure "original" and "round trip" protocols are equivalent.
477 """
478 num_correct = 0
479 for example in VALID_EXAMPLES:
480 original_protocol = protocol.Parse(example.protocol_string)
481 round_trip_protocol = protocol.Parse(str(original_protocol))
482
483 if original_protocol == round_trip_protocol:
484 num_correct += 1
485 logging.debug(
486 'Successful round-trip for protocol:\n%s',
487 example.protocol_string)
488 else:
489 self.fail(
490 'Round-trip failure for protocol:\n%s\nOriginal protocol:\n%s'
491 % (example.protocol_string, str(original_protocol)))
492
493 self.assertEqual(
494 num_correct,
495 len(VALID_EXAMPLES),
496 'Round trip success on %d out of %d protocols.'
497 % (num_correct, len(VALID_EXAMPLES)))
498
499
500 # ------------------------------------------------------------------------------
501
502
503 if __name__ == '__main__':
504 raise Exception('Use run_tests.py')