Skip to content

Add unit tests to transaction.py #406

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 5 additions & 9 deletions pycardano/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
from pycardano.types import typechecked
from pycardano.witness import TransactionWitnessSet

import traceback

__all__ = [
"TransactionInput",
"AssetName",
Expand Down Expand Up @@ -88,6 +90,8 @@
for k, v in list(self.items()):
if v == 0:
self.pop(k)

# traceback.print_stack()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please remove this leftover comment and the corresponding import :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! Just removed.

return self

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

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 +440,7 @@

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

Check warning on line 443 in pycardano/transaction.py

View check run for this annotation

Codecov / codecov/patch

pycardano/transaction.py#L443

Added line #L443 was not covered by tests

def to_primitive(self) -> Primitive:
if self.datum or self.script or self.post_alonzo:
Expand Down
57 changes: 56 additions & 1 deletion test/pycardano/test_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ 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(
Expand Down Expand Up @@ -133,7 +145,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 +278,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 +328,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 +353,14 @@ 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 +373,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 +415,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 +450,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 +490,23 @@ 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
Loading