Skip to content

Commit 7a3c782

Browse files
authored
Add unit tests to transaction.py (#406)
* Add unit tests to transaction.py * fix qa * remove redundant lines
1 parent 93f9362 commit 7a3c782

File tree

2 files changed

+47
-10
lines changed

2 files changed

+47
-10
lines changed

pycardano/transaction.py

+2-9
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ def normalize(self) -> Asset:
8888
for k, v in list(self.items()):
8989
if v == 0:
9090
self.pop(k)
91+
9192
return self
9293

9394
def union(self, other: Asset) -> Asset:
@@ -425,11 +426,6 @@ def __post_init__(self):
425426

426427
def validate(self):
427428
super().validate()
428-
if isinstance(self.amount, int) and self.amount < 0:
429-
raise InvalidDataException(
430-
f"Transaction output cannot have negative amount of ADA or "
431-
f"native asset: \n {self.amount}"
432-
)
433429
if isinstance(self.amount, Value) and (
434430
self.amount.coin < 0
435431
or self.amount.multi_asset.count(lambda p, n, v: v < 0) > 0
@@ -441,10 +437,7 @@ def validate(self):
441437

442438
@property
443439
def lovelace(self) -> int:
444-
if isinstance(self.amount, int):
445-
return self.amount
446-
else:
447-
return self.amount.coin
440+
return self.amount.coin
448441

449442
def to_primitive(self) -> Primitive:
450443
if self.datum or self.script or self.post_alonzo:

test/pycardano/test_transaction.py

+45-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ def test_transaction_input():
3333
check_two_way_cbor(tx_in)
3434

3535

36+
def test_hashable_transaction_input():
37+
my_inputs = {}
38+
tx_id_hex1 = "732bfd67e66be8e8288349fcaaa2294973ef6271cc189a239bb431275401b8e5"
39+
tx_id_hex2 = "732bfd67e66be8e8288349fcaaa2294973ef6271cc189a239bb431275401b8e5"
40+
tx_in1 = TransactionInput(TransactionId(bytes.fromhex(tx_id_hex1)), 0)
41+
tx_in2 = TransactionInput(TransactionId(bytes.fromhex(tx_id_hex2)), 0)
42+
assert tx_in1 == tx_in2
43+
my_inputs[tx_in1] = 1
44+
45+
3646
def test_transaction_output():
3747
addr = Address.decode(
3848
"addr_test1vrm9x2zsux7va6w892g38tvchnzahvcd9tykqf3ygnmwtaqyfg52x"
@@ -133,7 +143,7 @@ def test_invalid_transaction_output():
133143
addr = Address.decode(
134144
"addr_test1vrm9x2zsux7va6w892g38tvchnzahvcd9tykqf3ygnmwtaqyfg52x"
135145
)
136-
output = TransactionOutput(addr, -100000000000)
146+
output = TransactionOutput(addr, -1)
137147
with pytest.raises(InvalidDataException):
138148
output.to_cbor_hex()
139149

@@ -266,15 +276,26 @@ def test_multi_asset_addition():
266276
}
267277
)
268278

279+
result = a.union(b)
280+
269281
assert a + b == MultiAsset.from_primitive(
270282
{
271283
b"1" * SCRIPT_HASH_SIZE: {b"Token1": 11, b"Token2": 22},
272284
b"2" * SCRIPT_HASH_SIZE: {b"Token1": 1, b"Token2": 2},
273285
}
274286
)
287+
288+
assert result == MultiAsset.from_primitive(
289+
{
290+
b"1" * SCRIPT_HASH_SIZE: {b"Token1": 11, b"Token2": 22},
291+
b"2" * SCRIPT_HASH_SIZE: {b"Token1": 1, b"Token2": 2},
292+
}
293+
)
294+
275295
assert a == MultiAsset.from_primitive(
276296
{b"1" * SCRIPT_HASH_SIZE: {b"Token1": 1, b"Token2": 2}}
277297
)
298+
278299
assert b == MultiAsset.from_primitive(
279300
{
280301
b"1" * SCRIPT_HASH_SIZE: {b"Token1": 10, b"Token2": 20},
@@ -305,6 +326,7 @@ def test_multi_asset_subtraction():
305326
assert a == MultiAsset.from_primitive(
306327
{b"1" * SCRIPT_HASH_SIZE: {b"Token1": 1, b"Token2": 2}}
307328
)
329+
308330
assert b == MultiAsset.from_primitive(
309331
{
310332
b"1" * SCRIPT_HASH_SIZE: {b"Token1": 10, b"Token2": 20},
@@ -329,6 +351,10 @@ def test_asset_comparison():
329351

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

354+
result = a.union(b)
355+
356+
assert result == Asset.from_primitive({b"Token1": 2, b"Token2": 5})
357+
332358
assert a == a
333359

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

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

370+
assert not a == "abc"
371+
344372
with pytest.raises(TypeCheckError):
345373
a <= 1
346374

@@ -381,6 +409,8 @@ def test_multi_asset_comparison():
381409
assert not a <= d
382410
assert not d <= a
383411

412+
assert not a == "abc"
413+
384414
with pytest.raises(TypeCheckError):
385415
a <= 1
386416

@@ -414,6 +444,8 @@ def test_values():
414444
assert b <= c
415445
assert not c <= b
416446

447+
assert not a == "abc"
448+
417449
assert b - a == Value.from_primitive(
418450
[10, {b"1" * SCRIPT_HASH_SIZE: {b"Token1": 10, b"Token2": 20}}]
419451
)
@@ -452,6 +484,18 @@ def test_values():
452484
]
453485
)
454486

487+
result = a.union(b)
488+
489+
assert result == Value.from_primitive(
490+
[12, {b"1" * SCRIPT_HASH_SIZE: {b"Token1": 12, b"Token2": 24}}]
491+
)
492+
493+
d = 10000000
494+
495+
f = Value(1)
496+
497+
assert f <= d
498+
455499

456500
def test_inline_datum_serdes():
457501
@dataclass

0 commit comments

Comments
 (0)