|
21 | 21 | MyStruct_V2,
|
22 | 22 | MyUnion_V1,
|
23 | 23 | MyUnion_V2,
|
| 24 | + New, |
| 25 | + Old, |
24 | 26 | )
|
25 | 27 | from thrift.python.protocol import Protocol
|
26 | 28 |
|
27 | 29 | from thrift.python.serializer import deserialize, serialize
|
28 |
| -from thrift.python.types import StructOrUnion |
| 30 | +from thrift.python.types import isset, StructOrUnion |
| 31 | + |
| 32 | + |
| 33 | +@parameterized_class( |
| 34 | + ("protocol"), |
| 35 | + [(Protocol.BINARY,), (Protocol.COMPACT,), (Protocol.JSON,)], |
| 36 | +) |
| 37 | +class TestEvolution(unittest.TestCase): |
| 38 | + def test_evolution(self) -> None: |
| 39 | + params = {field_name: field_name for field_name, _ in Old} |
| 40 | + old_struct = Old(**params) |
| 41 | + serialized = serialize(old_struct, protocol=self.protocol) |
| 42 | + new_struct = deserialize(New, serialized, protocol=self.protocol) |
| 43 | + |
| 44 | + self.assertEqual( |
| 45 | + old_struct.unqualified_to_unqualified, new_struct.unqualified_to_unqualified |
| 46 | + ) |
| 47 | + self.assertEqual( |
| 48 | + old_struct.unqualified_to_optional, new_struct.unqualified_to_optional |
| 49 | + ) |
| 50 | + self.assertEqual( |
| 51 | + old_struct.unqualified_to_required, new_struct.unqualified_to_required |
| 52 | + ) |
| 53 | + |
| 54 | + self.assertEqual( |
| 55 | + old_struct.optional_to_unqualified, new_struct.optional_to_unqualified |
| 56 | + ) |
| 57 | + self.assertEqual( |
| 58 | + old_struct.optional_to_optional, new_struct.optional_to_optional |
| 59 | + ) |
| 60 | + self.assertEqual( |
| 61 | + old_struct.optional_to_required, new_struct.optional_to_required |
| 62 | + ) |
| 63 | + |
| 64 | + self.assertEqual( |
| 65 | + old_struct.required_to_unqualified, new_struct.required_to_unqualified |
| 66 | + ) |
| 67 | + self.assertEqual( |
| 68 | + old_struct.required_to_optional, new_struct.required_to_optional |
| 69 | + ) |
| 70 | + self.assertEqual( |
| 71 | + old_struct.required_to_required, new_struct.required_to_required |
| 72 | + ) |
| 73 | + |
| 74 | + # Protocol.JSON is SimpleJSON |
| 75 | + if self.protocol is Protocol.JSON: |
| 76 | + self.assertEqual(new_struct.unqualified_new, "") |
| 77 | + self.assertEqual(new_struct.required_new, "") |
| 78 | + |
| 79 | + self.assertFalse(isset(new_struct)["unqualified_new"]) |
| 80 | + self.assertIsNone(new_struct.optional_new) |
| 81 | + self.assertIsNotNone(new_struct.required_new) |
| 82 | + else: |
| 83 | + self.assertEqual(new_struct.unqualified_new, old_struct.unqualified_old) |
| 84 | + self.assertEqual(new_struct.optional_new, old_struct.optional_old) |
| 85 | + self.assertEqual(new_struct.required_new, old_struct.required_old) |
| 86 | + |
| 87 | + self.assertEqual(new_struct.unqualified_added, "") |
| 88 | + self.assertEqual(new_struct.required_added, "") |
| 89 | + |
| 90 | + self.assertFalse(isset(new_struct)["unqualified_added"]) |
| 91 | + self.assertIsNone(new_struct.optional_added) |
| 92 | + self.assertIsNotNone(new_struct.required_added) |
29 | 93 |
|
30 | 94 |
|
31 | 95 | @parameterized_class(
|
|
0 commit comments