From 07cab50b7b9a6d1301839328cae5695ac00f4a9b Mon Sep 17 00:00:00 2001 From: tacmota Date: Thu, 12 Dec 2024 16:51:28 -0800 Subject: [PATCH 1/3] Add unit tests to transaction.py --- pycardano/transaction.py | 14 +++----- test/pycardano/test_transaction.py | 57 +++++++++++++++++++++++++++++- 2 files changed, 61 insertions(+), 10 deletions(-) diff --git a/pycardano/transaction.py b/pycardano/transaction.py index 2c4d2909..a342fa29 100644 --- a/pycardano/transaction.py +++ b/pycardano/transaction.py @@ -43,6 +43,8 @@ from pycardano.types import typechecked from pycardano.witness import TransactionWitnessSet +import traceback + __all__ = [ "TransactionInput", "AssetName", @@ -88,6 +90,8 @@ def normalize(self) -> Asset: for k, v in list(self.items()): if v == 0: self.pop(k) + + # traceback.print_stack() return self def union(self, other: Asset) -> Asset: @@ -425,11 +429,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 @@ -441,10 +440,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: diff --git a/test/pycardano/test_transaction.py b/test/pycardano/test_transaction.py index 5ec6f51e..25b29bbe 100644 --- a/test/pycardano/test_transaction.py +++ b/test/pycardano/test_transaction.py @@ -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( @@ -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() @@ -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}, @@ -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}, @@ -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 @@ -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 @@ -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 @@ -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}}] ) @@ -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 From 9d33020041a0fef66276210a24ddae835d5b05e8 Mon Sep 17 00:00:00 2001 From: tacmota Date: Thu, 12 Dec 2024 23:06:43 -0800 Subject: [PATCH 2/3] fix qa --- pycardano/transaction.py | 2 -- test/pycardano/test_transaction.py | 19 ++++--------------- 2 files changed, 4 insertions(+), 17 deletions(-) diff --git a/pycardano/transaction.py b/pycardano/transaction.py index a342fa29..323b8ed0 100644 --- a/pycardano/transaction.py +++ b/pycardano/transaction.py @@ -43,8 +43,6 @@ from pycardano.types import typechecked from pycardano.witness import TransactionWitnessSet -import traceback - __all__ = [ "TransactionInput", "AssetName", diff --git a/test/pycardano/test_transaction.py b/test/pycardano/test_transaction.py index 25b29bbe..8428c456 100644 --- a/test/pycardano/test_transaction.py +++ b/test/pycardano/test_transaction.py @@ -32,16 +32,14 @@ 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 - ) + assert tx_in1 == tx_in2 my_inputs[tx_in1] = 1 @@ -355,11 +353,7 @@ def test_asset_comparison(): result = a.union(b) - assert result == Asset.from_primitive( - { - b"Token1": 2, b"Token2": 5 - } - ) + assert result == Asset.from_primitive({b"Token1": 2, b"Token2": 5}) assert a == a @@ -493,12 +487,7 @@ def test_values(): result = a.union(b) assert result == Value.from_primitive( - [ - 12, - { - b"1" * SCRIPT_HASH_SIZE: {b"Token1": 12, b"Token2": 24} - } - ] + [12, {b"1" * SCRIPT_HASH_SIZE: {b"Token1": 12, b"Token2": 24}}] ) d = 10000000 From 48d00b6ea1de583e01f0a3791121b96fee94981f Mon Sep 17 00:00:00 2001 From: tacmota Date: Thu, 12 Dec 2024 23:09:11 -0800 Subject: [PATCH 3/3] remove redundant lines --- pycardano/transaction.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pycardano/transaction.py b/pycardano/transaction.py index 323b8ed0..d95967ed 100644 --- a/pycardano/transaction.py +++ b/pycardano/transaction.py @@ -89,7 +89,6 @@ def normalize(self) -> Asset: if v == 0: self.pop(k) - # traceback.print_stack() return self def union(self, other: Asset) -> Asset: