|
16 | 16 |
|
17 | 17 | from __future__ import annotations |
18 | 18 |
|
| 19 | +import typing |
19 | 20 | import unittest |
20 | 21 |
|
21 | 22 | from apache.thrift.type.standard.thrift_types import TypeName, Void |
|
24 | 25 | from thrift.python.any.serializer import deserialize_primitive, serialize_primitive |
25 | 26 |
|
26 | 27 |
|
| 28 | +if typing.TYPE_CHECKING: |
| 29 | + from thrift.python.any.serializer import PrimitiveType |
| 30 | + |
| 31 | + |
27 | 32 | class SerializerTests(unittest.TestCase): |
| 33 | + def _test_round_trip( |
| 34 | + self, value: PrimitiveType, thrift_type: typing.Optional[Type] = None |
| 35 | + ) -> None: |
| 36 | + iobuf = serialize_primitive(value, thrift_type=thrift_type) |
| 37 | + decoded = deserialize_primitive(type(value), iobuf, thrift_type=thrift_type) |
| 38 | + if isinstance(value, float): |
| 39 | + self.assertAlmostEqual(value, float(decoded), places=3) |
| 40 | + else: |
| 41 | + self.assertEqual(value, decoded) |
| 42 | + |
| 43 | + def test_bool_round_trip(self) -> None: |
| 44 | + self._test_round_trip(True) |
| 45 | + |
28 | 46 | def test_int_round_trip(self) -> None: |
29 | | - i = 42 |
30 | | - iobuf = serialize_primitive(i) |
31 | | - decoded = deserialize_primitive(int, iobuf) |
32 | | - self.assertEqual(i, decoded) |
| 47 | + self._test_round_trip(42) |
33 | 48 |
|
34 | | - def test_int_round_trip_with_type_name(self) -> None: |
35 | | - i = 42 |
36 | | - for type_name in [ |
37 | | - TypeName(i16Type=Void.Unused), |
38 | | - TypeName(i32Type=Void.Unused), |
39 | | - TypeName(i64Type=Void.Unused), |
40 | | - ]: |
| 49 | + def test_float_round_trip(self) -> None: |
| 50 | + self._test_round_trip(123456.789) |
| 51 | + |
| 52 | + def test_str_round_trip(self) -> None: |
| 53 | + self._test_round_trip("thrift-python") |
| 54 | + |
| 55 | + def test_bytes_round_trip(self) -> None: |
| 56 | + self._test_round_trip(b"raw bytes") |
| 57 | + |
| 58 | + def _test_round_trip_with_type_names( |
| 59 | + self, value: PrimitiveType, type_names: typing.Sequence[TypeName] |
| 60 | + ) -> None: |
| 61 | + for type_name in type_names: |
41 | 62 | with self.subTest(type_name=type_name): |
42 | | - iobuf = serialize_primitive(i, thrift_type=Type(name=type_name)) |
43 | | - decoded = deserialize_primitive( |
44 | | - int, iobuf, thrift_type=Type(name=type_name) |
45 | | - ) |
46 | | - self.assertEqual(i, decoded) |
| 63 | + self._test_round_trip(value, thrift_type=Type(name=type_name)) |
47 | 64 |
|
48 | | - def test_float_round_trip(self) -> None: |
49 | | - f = 123456.789 |
50 | | - iobuf = serialize_primitive(f) |
51 | | - decoded = deserialize_primitive(float, iobuf) |
52 | | - self.assertAlmostEqual(f, decoded, delta=0.001) |
| 65 | + def test_int_round_trip_with_type_name(self) -> None: |
| 66 | + self._test_round_trip_with_type_names( |
| 67 | + 42, |
| 68 | + [ |
| 69 | + TypeName(byteType=Void.Unused), |
| 70 | + TypeName(i16Type=Void.Unused), |
| 71 | + TypeName(i32Type=Void.Unused), |
| 72 | + TypeName(i64Type=Void.Unused), |
| 73 | + ], |
| 74 | + ) |
53 | 75 |
|
54 | 76 | def test_float_round_trip_with_type_name(self) -> None: |
55 | | - f = 123456.789 |
56 | | - for type_name in [ |
57 | | - TypeName(floatType=Void.Unused), |
58 | | - TypeName(doubleType=Void.Unused), |
59 | | - ]: |
60 | | - with self.subTest(type_name=type_name): |
61 | | - iobuf = serialize_primitive(f, thrift_type=Type(name=type_name)) |
62 | | - decoded = deserialize_primitive( |
63 | | - float, iobuf, thrift_type=Type(name=type_name) |
64 | | - ) |
65 | | - self.assertAlmostEqual(f, decoded, delta=0.001) |
| 77 | + self._test_round_trip_with_type_names( |
| 78 | + 123456.789, |
| 79 | + [ |
| 80 | + TypeName(floatType=Void.Unused), |
| 81 | + TypeName(doubleType=Void.Unused), |
| 82 | + ], |
| 83 | + ) |
0 commit comments