|
26 | 26 | CBORBase, |
27 | 27 | Datum, |
28 | 28 | MultiAsset, |
| 29 | + Primitive, |
29 | 30 | RawPlutusData, |
30 | 31 | Transaction, |
31 | 32 | TransactionWitnessSet, |
|
49 | 50 | NonEmptyOrderedSet, |
50 | 51 | OrderedSet, |
51 | 52 | RawCBOR, |
52 | | - TextEnvelope, |
53 | 53 | default_encoder, |
54 | 54 | limit_primitive_type, |
55 | 55 | ) |
@@ -991,39 +991,37 @@ class TestData(MapCBORSerializable): |
991 | 991 | assert s_copy[0].value == 100 |
992 | 992 |
|
993 | 993 |
|
994 | | -def test_text_envelope(): |
| 994 | +def test_save_load(): |
995 | 995 | @dataclass |
996 | | - class Test1(ArrayCBORSerializable, TextEnvelope): |
| 996 | + class Test1(CBORSerializable): |
997 | 997 | a: str |
998 | 998 | b: Union[str, None] = None |
999 | 999 |
|
1000 | | - KEY_TYPE = "Test1" |
1001 | | - DESCRIPTION = "A test class for TextEnvelope serialization" |
1002 | | - |
1003 | | - def __init__( |
1004 | | - self, |
1005 | | - a: str, |
1006 | | - b: Union[str, None] = None, |
1007 | | - payload: Optional[bytes] = None, |
1008 | | - key_type: Optional[str] = None, |
1009 | | - description: Optional[str] = None, |
1010 | | - ): |
1011 | | - self.a = a |
1012 | | - self.b = b |
1013 | | - TextEnvelope.__init__(self, payload, key_type, description) |
| 1000 | + @property |
| 1001 | + def json_type(self) -> str: |
| 1002 | + return "Test Type" |
1014 | 1003 |
|
1015 | | - test1 = Test1(a="a") |
| 1004 | + @property |
| 1005 | + def json_description(self) -> str: |
| 1006 | + return "Test Description" |
1016 | 1007 |
|
1017 | | - wrong_type = { |
1018 | | - "type": "Test2", |
1019 | | - "description": "A test class for TextEnvelope serialization", |
1020 | | - "cborHex": "826161f6", |
1021 | | - } |
| 1008 | + @classmethod |
| 1009 | + def from_primitive( |
| 1010 | + cls: Type[CBORSerializable], value: Any, type_args: Optional[tuple] = None |
| 1011 | + ) -> CBORSerializable: |
| 1012 | + if not isinstance(value, dict): |
| 1013 | + raise DeserializeException(f"Expected dict, got {type(value)}") |
| 1014 | + return Test1(a=value["a"], b=value.get("b")) |
1022 | 1015 |
|
1023 | | - with pytest.raises(InvalidKeyTypeException): |
1024 | | - invalid_test1 = Test1.from_json(json.dumps(wrong_type), validate_type=True) |
| 1016 | + def to_shallow_primitive(self) -> Union[Primitive, CBORSerializable]: |
| 1017 | + return {"a": self.a, "b": self.b} |
| 1018 | + |
| 1019 | + test1 = Test1(a="a") |
| 1020 | + test1_json = json.loads(test1.to_json()) |
1025 | 1021 |
|
1026 | | - assert test1.payload == b"\x82aa\xf6" |
| 1022 | + assert test1_json["type"] == "Test Type" |
| 1023 | + assert test1_json["description"] == "Test Description" |
| 1024 | + assert test1_json["cborHex"] == test1.to_cbor_hex() |
1027 | 1025 |
|
1028 | 1026 | with tempfile.NamedTemporaryFile() as f: |
1029 | 1027 | test1.save(f.name) |
|
0 commit comments