Skip to content

Commit e48aee5

Browse files
committed
test: add save and load tests for Address and CBORSerializable classes
1 parent 971db5f commit e48aee5

File tree

2 files changed

+36
-26
lines changed

2 files changed

+36
-26
lines changed

test/pycardano/test_address.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import tempfile
2+
13
import pytest
24

35
from pycardano.address import Address, AddressType, PointerAddress
@@ -211,3 +213,13 @@ def test_from_primitive_invalid_type_addr():
211213

212214
with pytest.raises(DeserializeException):
213215
Address.from_primitive(value)
216+
217+
218+
def test_save_load_address():
219+
address_string = "addr_test1vr2p8st5t5cxqglyjky7vk98k7jtfhdpvhl4e97cezuhn0cqcexl7"
220+
address = Address.from_primitive(address_string)
221+
222+
with tempfile.NamedTemporaryFile() as f:
223+
address.save(f.name)
224+
loaded_address = Address.load(f.name)
225+
assert address == loaded_address

test/pycardano/test_serialization.py

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
CBORBase,
2727
Datum,
2828
MultiAsset,
29+
Primitive,
2930
RawPlutusData,
3031
Transaction,
3132
TransactionWitnessSet,
@@ -49,7 +50,6 @@
4950
NonEmptyOrderedSet,
5051
OrderedSet,
5152
RawCBOR,
52-
TextEnvelope,
5353
default_encoder,
5454
limit_primitive_type,
5555
)
@@ -991,39 +991,37 @@ class TestData(MapCBORSerializable):
991991
assert s_copy[0].value == 100
992992

993993

994-
def test_text_envelope():
994+
def test_save_load():
995995
@dataclass
996-
class Test1(ArrayCBORSerializable, TextEnvelope):
996+
class Test1(CBORSerializable):
997997
a: str
998998
b: Union[str, None] = None
999999

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"
10141003

1015-
test1 = Test1(a="a")
1004+
@property
1005+
def json_description(self) -> str:
1006+
return "Test Description"
10161007

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"))
10221015

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())
10251021

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()
10271025

10281026
with tempfile.NamedTemporaryFile() as f:
10291027
test1.save(f.name)

0 commit comments

Comments
 (0)