Skip to content

Commit 82dd140

Browse files
committed
test: add tests for TextEnvelope and witness
1 parent 564aed7 commit 82dd140

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

test/pycardano/test_serialization.py

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import json
2+
import tempfile
13
from collections import defaultdict, deque
24
from copy import deepcopy
35
from dataclasses import dataclass, field
@@ -30,7 +32,11 @@
3032
VerificationKey,
3133
VerificationKeyWitness,
3234
)
33-
from pycardano.exception import DeserializeException, SerializeException
35+
from pycardano.exception import (
36+
DeserializeException,
37+
SerializeException,
38+
InvalidKeyTypeException,
39+
)
3440
from pycardano.plutus import PlutusData, PlutusV1Script, PlutusV2Script
3541
from pycardano.serialization import (
3642
ArrayCBORSerializable,
@@ -45,6 +51,7 @@
4551
RawCBOR,
4652
default_encoder,
4753
limit_primitive_type,
54+
TextEnvelope,
4855
)
4956

5057

@@ -982,3 +989,46 @@ class TestData(MapCBORSerializable):
982989
s_copy[0].value = 100
983990
assert s[0].value == 1
984991
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)

test/pycardano/test_witness.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import tempfile
22

33
from pycardano import (
4-
PaymentKeyPair,
54
PaymentSigningKey,
65
PaymentVerificationKey,
76
VerificationKeyWitness,
@@ -20,3 +19,5 @@ def test_witness_save_load():
2019
witness.save(f.name)
2120
loaded_witness = VerificationKeyWitness.load(f.name)
2221
assert witness == loaded_witness
22+
23+
assert witness != vk

0 commit comments

Comments
 (0)