Skip to content

Commit 564aed7

Browse files
committed
test: add tests for network magic and DRep serialization and TextEnvelope save and load
1 parent 0cf1cd7 commit 564aed7

File tree

4 files changed

+156
-0
lines changed

4 files changed

+156
-0
lines changed

test/pycardano/backend/test_cardano_cli.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
TransactionInput,
2222
CardanoCliError,
2323
)
24+
from pycardano.backend.cardano_cli import network_magic
2425

2526
QUERY_TIP_RESULT = {
2627
"block": 1460093,
@@ -647,6 +648,10 @@ def override_run_command_fail(cmd: List[str]):
647648
return context
648649

649650

651+
def test_network_magic():
652+
assert network_magic(1) == ["--testnet-magic", "1"]
653+
654+
650655
class TestCardanoCliChainContext:
651656
def test_protocol_param(self, chain_context):
652657
assert (

test/pycardano/test_certificate.py

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
from pycardano.certificate import (
88
Anchor,
99
AuthCommitteeHotCertificate,
10+
DRep,
1011
DRepCredential,
12+
DRepKind,
1113
PoolRegistration,
1214
PoolRetirement,
1315
ResignCommitteeColdCertificate,
@@ -22,8 +24,10 @@
2224
from pycardano.hash import ( # plutus_script_hash,
2325
POOL_KEY_HASH_SIZE,
2426
SCRIPT_HASH_SIZE,
27+
VERIFICATION_KEY_HASH_SIZE,
2528
PoolKeyHash,
2629
ScriptHash,
30+
VerificationKeyHash,
2731
)
2832

2933
TEST_ADDR = Address.from_primitive(
@@ -197,6 +201,104 @@ def test_drep_credential():
197201
assert DRepCredential.from_cbor(drep_credential_cbor_hex) == drep_credential
198202

199203

204+
@pytest.mark.parametrize(
205+
"input_values,expected_kind,expected_credential,expected_exception,case_id",
206+
[
207+
# Happy path: VERIFICATION_KEY_HASH
208+
(
209+
[DRepKind.VERIFICATION_KEY_HASH.value, b"1" * VERIFICATION_KEY_HASH_SIZE],
210+
DRepKind.VERIFICATION_KEY_HASH,
211+
VerificationKeyHash(b"1" * VERIFICATION_KEY_HASH_SIZE),
212+
None,
213+
"verification_key_hash",
214+
),
215+
# Happy path: SCRIPT_HASH
216+
(
217+
[DRepKind.SCRIPT_HASH.value, b"1" * SCRIPT_HASH_SIZE],
218+
DRepKind.SCRIPT_HASH,
219+
ScriptHash(b"1" * SCRIPT_HASH_SIZE),
220+
None,
221+
"script_hash",
222+
),
223+
# Happy path: ALWAYS_ABSTAIN
224+
(
225+
[DRepKind.ALWAYS_ABSTAIN.value],
226+
DRepKind.ALWAYS_ABSTAIN,
227+
None,
228+
None,
229+
"always_abstain",
230+
),
231+
# Happy path: ALWAYS_NO_CONFIDENCE
232+
(
233+
[DRepKind.ALWAYS_NO_CONFIDENCE.value],
234+
DRepKind.ALWAYS_NO_CONFIDENCE,
235+
None,
236+
None,
237+
"always_no_confidence",
238+
),
239+
# Error: invalid DRepKind value (not in enum)
240+
([99], None, None, DeserializeException, "invalid_drep_kind"),
241+
# Error: valid kind but missing credential for VERIFICATION_KEY_HASH
242+
(
243+
[DRepKind.VERIFICATION_KEY_HASH.value],
244+
None,
245+
None,
246+
IndexError,
247+
"missing_credential_verification_key_hash",
248+
),
249+
# Error: valid kind but missing credential for SCRIPT_HASH
250+
(
251+
[DRepKind.SCRIPT_HASH.value],
252+
None,
253+
None,
254+
IndexError,
255+
"missing_credential_script_hash",
256+
),
257+
# Error: valid kind but extra credential for ALWAYS_ABSTAIN
258+
(
259+
[DRepKind.ALWAYS_ABSTAIN.value, b"extra"],
260+
DRepKind.ALWAYS_ABSTAIN,
261+
None,
262+
None,
263+
"abstain_with_extra",
264+
),
265+
# Error: valid kind but extra credential for ALWAYS_NO_CONFIDENCE
266+
(
267+
[DRepKind.ALWAYS_NO_CONFIDENCE.value, b"extra"],
268+
DRepKind.ALWAYS_NO_CONFIDENCE,
269+
None,
270+
None,
271+
"no_confidence_with_extra",
272+
),
273+
# Error: input is empty
274+
([], None, None, IndexError, "empty_input"),
275+
# Error: input is not a list or tuple
276+
("notalist", None, None, DeserializeException, "input_not_list"),
277+
],
278+
ids=lambda p: p if isinstance(p, str) else None,
279+
)
280+
def test_drep_from_primitive(
281+
input_values, expected_kind, expected_credential, expected_exception, case_id
282+
):
283+
284+
# Arrange
285+
# (All input values are provided via test parameters)
286+
287+
# Act / Assert
288+
if expected_exception:
289+
with pytest.raises(expected_exception):
290+
DRep.from_primitive(input_values)
291+
else:
292+
result = DRep.from_primitive(input_values)
293+
# Assert
294+
assert result.kind == expected_kind
295+
if expected_credential is not None:
296+
assert isinstance(result.credential, type(expected_credential))
297+
assert result.credential.payload == expected_credential.payload
298+
else:
299+
assert result.credential is None
300+
301+
200302
def test_unreg_drep_certificate():
201303
staking_key = StakeSigningKey.from_cbor(
202304
"5820ff3a330df8859e4e5f42a97fcaee73f6a00d0cf864f4bca902bd106d423f02c0"

test/pycardano/test_transaction.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import tempfile
12
from dataclasses import dataclass
23
from fractions import Fraction
34
from test.pycardano.util import check_two_way_cbor
@@ -243,6 +244,32 @@ def test_transaction():
243244
assert expected_tx_id == signed_tx.id
244245

245246

247+
def test_transaction_save_load():
248+
tx_cbor = (
249+
"84a70081825820b35a4ba9ef3ce21adcd6879d08553642224304704d206c74d3ffb3e6eed3ca28000d80018182581d60cc"
250+
"30497f4ff962f4c1dca54cceefe39f86f1d7179668009f8eb71e598200a1581cec8b7d1dd0b124e8333d3fa8d818f6eac0"
251+
"68231a287554e9ceae490ea24f5365636f6e6454657374746f6b656e1a009896804954657374746f6b656e1a0098968002"
252+
"1a000493e00e8009a1581cec8b7d1dd0b124e8333d3fa8d818f6eac068231a287554e9ceae490ea24f5365636f6e645465"
253+
"7374746f6b656e1a009896804954657374746f6b656e1a00989680075820592a2df0e091566969b3044626faa8023dabe6"
254+
"f39c78f33bed9e105e55159221a200828258206443a101bdb948366fc87369336224595d36d8b0eee5602cba8b81a024e5"
255+
"84735840846f408dee3b101fda0f0f7ca89e18b724b7ca6266eb29775d3967d6920cae7457accb91def9b77571e15dd2ed"
256+
"e38b12cf92496ce7382fa19eb90ab7f73e49008258205797dc2cc919dfec0bb849551ebdf30d96e5cbe0f33f734a87fe82"
257+
"6db30f7ef95840bdc771aa7b8c86a8ffcbe1b7a479c68503c8aa0ffde8059443055bf3e54b92f4fca5e0b9ca5bb11ab23b"
258+
"1390bb9ffce414fa398fc0b17f4dc76fe9f7e2c99c09018182018482051a075bcd1582041a075bcd0c8200581c9139e5c0"
259+
"a42f0f2389634c3dd18dc621f5594c5ba825d9a8883c66278200581c835600a2be276a18a4bebf0225d728f090f724f4c0"
260+
"acd591d066fa6ff5d90103a100a11902d1a16b7b706f6c6963795f69647da16d7b706f6c6963795f6e616d657da66b6465"
261+
"736372697074696f6e6a3c6f7074696f6e616c3e65696d6167656a3c72657175697265643e686c6f636174696f6ea36761"
262+
"7277656176656a3c6f7074696f6e616c3e6568747470736a3c6f7074696f6e616c3e64697066736a3c7265717569726564"
263+
"3e646e616d656a3c72657175697265643e667368613235366a3c72657175697265643e64747970656a3c72657175697265643e"
264+
)
265+
tx = Transaction.from_cbor(tx_cbor)
266+
267+
with tempfile.NamedTemporaryFile() as f:
268+
tx.save(f.name)
269+
loaded_tx = Transaction.load(f.name)
270+
assert tx == loaded_tx
271+
272+
246273
def test_multi_asset():
247274
serialized_value = [
248275
100,

test/pycardano/test_witness.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import tempfile
2+
3+
from pycardano import (
4+
PaymentKeyPair,
5+
PaymentSigningKey,
6+
PaymentVerificationKey,
7+
VerificationKeyWitness,
8+
)
9+
10+
11+
def test_witness_save_load():
12+
sk = PaymentSigningKey.generate()
13+
vk = PaymentVerificationKey.from_signing_key(sk)
14+
witness = VerificationKeyWitness(
15+
vkey=vk,
16+
signature=sk.sign(b"test"),
17+
)
18+
19+
with tempfile.NamedTemporaryFile() as f:
20+
witness.save(f.name)
21+
loaded_witness = VerificationKeyWitness.load(f.name)
22+
assert witness == loaded_witness

0 commit comments

Comments
 (0)