Skip to content

Commit

Permalink
Add unit tests to transaction.py (#406)
Browse files Browse the repository at this point in the history
* Add unit tests to transaction.py

* fix qa

* remove redundant lines
  • Loading branch information
tacmota authored Dec 13, 2024
1 parent 93f9362 commit 7a3c782
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 10 deletions.
11 changes: 2 additions & 9 deletions pycardano/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ def normalize(self) -> Asset:
for k, v in list(self.items()):
if v == 0:
self.pop(k)

return self

def union(self, other: Asset) -> Asset:
Expand Down Expand Up @@ -425,11 +426,6 @@ def __post_init__(self):

def validate(self):
super().validate()
if isinstance(self.amount, int) and self.amount < 0:
raise InvalidDataException(
f"Transaction output cannot have negative amount of ADA or "
f"native asset: \n {self.amount}"
)
if isinstance(self.amount, Value) and (
self.amount.coin < 0
or self.amount.multi_asset.count(lambda p, n, v: v < 0) > 0
Expand All @@ -441,10 +437,7 @@ def validate(self):

@property
def lovelace(self) -> int:
if isinstance(self.amount, int):
return self.amount
else:
return self.amount.coin
return self.amount.coin

def to_primitive(self) -> Primitive:
if self.datum or self.script or self.post_alonzo:
Expand Down
46 changes: 45 additions & 1 deletion test/pycardano/test_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ def test_transaction_input():
check_two_way_cbor(tx_in)


def test_hashable_transaction_input():
my_inputs = {}
tx_id_hex1 = "732bfd67e66be8e8288349fcaaa2294973ef6271cc189a239bb431275401b8e5"
tx_id_hex2 = "732bfd67e66be8e8288349fcaaa2294973ef6271cc189a239bb431275401b8e5"
tx_in1 = TransactionInput(TransactionId(bytes.fromhex(tx_id_hex1)), 0)
tx_in2 = TransactionInput(TransactionId(bytes.fromhex(tx_id_hex2)), 0)
assert tx_in1 == tx_in2
my_inputs[tx_in1] = 1


def test_transaction_output():
addr = Address.decode(
"addr_test1vrm9x2zsux7va6w892g38tvchnzahvcd9tykqf3ygnmwtaqyfg52x"
Expand Down Expand Up @@ -133,7 +143,7 @@ def test_invalid_transaction_output():
addr = Address.decode(
"addr_test1vrm9x2zsux7va6w892g38tvchnzahvcd9tykqf3ygnmwtaqyfg52x"
)
output = TransactionOutput(addr, -100000000000)
output = TransactionOutput(addr, -1)
with pytest.raises(InvalidDataException):
output.to_cbor_hex()

Expand Down Expand Up @@ -266,15 +276,26 @@ def test_multi_asset_addition():
}
)

result = a.union(b)

assert a + b == MultiAsset.from_primitive(
{
b"1" * SCRIPT_HASH_SIZE: {b"Token1": 11, b"Token2": 22},
b"2" * SCRIPT_HASH_SIZE: {b"Token1": 1, b"Token2": 2},
}
)

assert result == MultiAsset.from_primitive(
{
b"1" * SCRIPT_HASH_SIZE: {b"Token1": 11, b"Token2": 22},
b"2" * SCRIPT_HASH_SIZE: {b"Token1": 1, b"Token2": 2},
}
)

assert a == MultiAsset.from_primitive(
{b"1" * SCRIPT_HASH_SIZE: {b"Token1": 1, b"Token2": 2}}
)

assert b == MultiAsset.from_primitive(
{
b"1" * SCRIPT_HASH_SIZE: {b"Token1": 10, b"Token2": 20},
Expand Down Expand Up @@ -305,6 +326,7 @@ def test_multi_asset_subtraction():
assert a == MultiAsset.from_primitive(
{b"1" * SCRIPT_HASH_SIZE: {b"Token1": 1, b"Token2": 2}}
)

assert b == MultiAsset.from_primitive(
{
b"1" * SCRIPT_HASH_SIZE: {b"Token1": 10, b"Token2": 20},
Expand All @@ -329,6 +351,10 @@ def test_asset_comparison():

d = Asset.from_primitive({b"Token3": 1, b"Token4": 2})

result = a.union(b)

assert result == Asset.from_primitive({b"Token1": 2, b"Token2": 5})

assert a == a

assert a <= b
Expand All @@ -341,6 +367,8 @@ def test_asset_comparison():

assert not any([a == d, a <= d, d <= a])

assert not a == "abc"

with pytest.raises(TypeCheckError):
a <= 1

Expand Down Expand Up @@ -381,6 +409,8 @@ def test_multi_asset_comparison():
assert not a <= d
assert not d <= a

assert not a == "abc"

with pytest.raises(TypeCheckError):
a <= 1

Expand Down Expand Up @@ -414,6 +444,8 @@ def test_values():
assert b <= c
assert not c <= b

assert not a == "abc"

assert b - a == Value.from_primitive(
[10, {b"1" * SCRIPT_HASH_SIZE: {b"Token1": 10, b"Token2": 20}}]
)
Expand Down Expand Up @@ -452,6 +484,18 @@ def test_values():
]
)

result = a.union(b)

assert result == Value.from_primitive(
[12, {b"1" * SCRIPT_HASH_SIZE: {b"Token1": 12, b"Token2": 24}}]
)

d = 10000000

f = Value(1)

assert f <= d


def test_inline_datum_serdes():
@dataclass
Expand Down

0 comments on commit 7a3c782

Please sign in to comment.