|
| 1 | +import json |
| 2 | +import tempfile |
1 | 3 | from collections import defaultdict, deque
|
2 | 4 | from copy import deepcopy
|
3 | 5 | from dataclasses import dataclass, field
|
|
30 | 32 | VerificationKey,
|
31 | 33 | VerificationKeyWitness,
|
32 | 34 | )
|
33 |
| -from pycardano.exception import DeserializeException, SerializeException |
| 35 | +from pycardano.exception import ( |
| 36 | + DeserializeException, |
| 37 | + SerializeException, |
| 38 | + InvalidKeyTypeException, |
| 39 | +) |
34 | 40 | from pycardano.plutus import PlutusData, PlutusV1Script, PlutusV2Script
|
35 | 41 | from pycardano.serialization import (
|
36 | 42 | ArrayCBORSerializable,
|
|
45 | 51 | RawCBOR,
|
46 | 52 | default_encoder,
|
47 | 53 | limit_primitive_type,
|
| 54 | + TextEnvelope, |
48 | 55 | )
|
49 | 56 |
|
50 | 57 |
|
@@ -982,3 +989,46 @@ class TestData(MapCBORSerializable):
|
982 | 989 | s_copy[0].value = 100
|
983 | 990 | assert s[0].value == 1
|
984 | 991 | assert s_copy[0].value == 100
|
| 992 | + |
| 993 | + |
| 994 | +def test_text_envelope(): |
| 995 | + @dataclass |
| 996 | + class Test1(ArrayCBORSerializable, TextEnvelope): |
| 997 | + a: str |
| 998 | + b: Union[str, None] = None |
| 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) |
| 1014 | + |
| 1015 | + test1 = Test1(a="a") |
| 1016 | + |
| 1017 | + wrong_type = { |
| 1018 | + "type": "Test2", |
| 1019 | + "description": "A test class for TextEnvelope serialization", |
| 1020 | + "cborHex": "826161f6", |
| 1021 | + } |
| 1022 | + |
| 1023 | + with pytest.raises(InvalidKeyTypeException): |
| 1024 | + invalid_test1 = Test1.from_json(json.dumps(wrong_type), validate_type=True) |
| 1025 | + |
| 1026 | + assert test1.payload == b"\x82aa\xf6" |
| 1027 | + |
| 1028 | + with tempfile.NamedTemporaryFile() as f: |
| 1029 | + test1.save(f.name) |
| 1030 | + loaded = Test1.load(f.name) |
| 1031 | + assert test1 == loaded |
| 1032 | + |
| 1033 | + with pytest.raises(IOError): |
| 1034 | + test1.save(f.name) |
0 commit comments