Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions test/pycardano/test_address.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import tempfile

import pytest
Expand Down Expand Up @@ -219,7 +220,14 @@ def test_save_load_address():
address_string = "addr_test1vr2p8st5t5cxqglyjky7vk98k7jtfhdpvhl4e97cezuhn0cqcexl7"
address = Address.from_primitive(address_string)

with tempfile.NamedTemporaryFile() as f:
address.save(f.name)
loaded_address = Address.load(f.name)
# On Windows, NamedTemporaryFile keeps the file locked while open, so
# save() cannot open it for writing. Use delete=False and close the handle
# first, then clean up manually afterward.
with tempfile.NamedTemporaryFile(delete=False) as f:
tmp_path = f.name
try:
address.save(tmp_path)
loaded_address = Address.load(tmp_path)
assert address == loaded_address
finally:
os.unlink(tmp_path)
40 changes: 29 additions & 11 deletions test/pycardano/test_key.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import os
import pathlib
import tempfile

Expand Down Expand Up @@ -216,25 +217,42 @@ def test_stake_pool_key_load():


def test_key_save():
with tempfile.NamedTemporaryFile() as f:
SK.save(f.name)
sk = PaymentSigningKey.load(f.name)
# On Windows, NamedTemporaryFile keeps the file locked while open.
# Use delete=False and close the handle first, then clean up manually.
with tempfile.NamedTemporaryFile(delete=False) as f:
tmp_path = f.name
try:
SK.save(tmp_path)
sk = PaymentSigningKey.load(tmp_path)
assert SK == sk
finally:
os.unlink(tmp_path)


def test_key_save_invalid_address():
with tempfile.NamedTemporaryFile() as f:
SK.save(f.name)
with tempfile.NamedTemporaryFile(delete=False) as f:
tmp_path = f.name
try:
SK.save(tmp_path)
with pytest.raises(IOError):
VK.save(f.name)
VK.save(tmp_path)
finally:
os.unlink(tmp_path)


def test_stake_pool_key_save():
with tempfile.NamedTemporaryFile() as skf, tempfile.NamedTemporaryFile() as vkf:
SPSK.save(skf.name)
sk = StakePoolSigningKey.load(skf.name)
SPVK.save(vkf.name)
vk = StakePoolSigningKey.load(vkf.name)
with tempfile.NamedTemporaryFile(delete=False) as skf:
sk_path = skf.name
with tempfile.NamedTemporaryFile(delete=False) as vkf:
vk_path = vkf.name
try:
SPSK.save(sk_path)
sk = StakePoolSigningKey.load(sk_path)
SPVK.save(vk_path)
vk = StakePoolSigningKey.load(vk_path)
finally:
os.unlink(sk_path)
os.unlink(vk_path)
assert SPSK == sk
assert SPVK == vk

Expand Down
13 changes: 9 additions & 4 deletions test/pycardano/test_serialization.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import os
import tempfile
from collections import defaultdict, deque
from copy import deepcopy
Expand Down Expand Up @@ -1047,13 +1048,17 @@ def to_shallow_primitive(self) -> Union[Primitive, CBORSerializable]:
assert test1_json["description"] == "Test Description"
assert test1_json["cborHex"] == test1.to_cbor_hex()

with tempfile.NamedTemporaryFile() as f:
test1.save(f.name)
loaded = Test1.load(f.name)
with tempfile.NamedTemporaryFile(delete=False) as f:
tmp_path = f.name
try:
test1.save(tmp_path)
loaded = Test1.load(tmp_path)
assert test1 == loaded

with pytest.raises(IOError):
test1.save(f.name)
test1.save(tmp_path)
finally:
os.unlink(tmp_path)


def test_ordered_set_as_key_in_dict():
Expand Down
11 changes: 8 additions & 3 deletions test/pycardano/test_transaction.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import tempfile
from dataclasses import dataclass
from fractions import Fraction
Expand Down Expand Up @@ -265,10 +266,14 @@ def test_transaction_save_load():
)
tx = Transaction.from_cbor(tx_cbor)

with tempfile.NamedTemporaryFile() as f:
tx.save(f.name)
loaded_tx = Transaction.load(f.name)
with tempfile.NamedTemporaryFile(delete=False) as f:
tmp_path = f.name
try:
tx.save(tmp_path)
loaded_tx = Transaction.load(tmp_path)
assert tx == loaded_tx
finally:
os.unlink(tmp_path)


def test_multi_asset():
Expand Down
11 changes: 8 additions & 3 deletions test/pycardano/test_witness.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import os
import tempfile

from pycardano import (
Expand All @@ -19,12 +20,16 @@ def test_witness_save_load():
signature=sk.sign(b"test"),
)

with tempfile.NamedTemporaryFile() as f:
witness.save(f.name)
loaded_witness = VerificationKeyWitness.load(f.name)
with tempfile.NamedTemporaryFile(delete=False) as f:
tmp_path = f.name
try:
witness.save(tmp_path)
loaded_witness = VerificationKeyWitness.load(tmp_path)
assert witness == loaded_witness

assert witness != vk
finally:
os.unlink(tmp_path)


def test_redeemer_decode():
Expand Down