Skip to content

Commit 8686209

Browse files
committed
test: add cardano-cli tests for latest
1 parent 62267f3 commit 8686209

File tree

1 file changed

+67
-4
lines changed

1 file changed

+67
-4
lines changed

test/pycardano/backend/test_cardano_cli.py

Lines changed: 67 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
ProtocolParameters,
1717
RawPlutusData,
1818
TransactionInput,
19+
CardanoCliError,
1920
)
2021

2122
QUERY_TIP_RESULT = {
@@ -483,15 +484,19 @@
483484
}
484485

485486

486-
def override_run_command(cmd: List[str]):
487+
def override_run_command_older_version(cmd: List[str]):
487488
"""
488-
Override the run_command method of CardanoCliChainContext to return a mock result
489+
Override the run_command method of CardanoCliChainContext to return a mock result of older versions of cardano-cli.
489490
Args:
490491
cmd: The command to run
491492
492493
Returns:
493494
The mock result
494495
"""
496+
if "latest" in cmd:
497+
raise CardanoCliError(
498+
"Older versions of cardano-cli do not support the latest command"
499+
)
495500
if "tip" in cmd:
496501
return json.dumps(QUERY_TIP_RESULT)
497502
if "protocol-parameters" in cmd:
@@ -506,6 +511,23 @@ def override_run_command(cmd: List[str]):
506511
return None
507512

508513

514+
def override_run_command_latest(cmd: List[str]):
515+
"""
516+
Override the run_command method of CardanoCliChainContext to return a mock result of the latest versions of cardano-cli.
517+
Args:
518+
cmd: The command to run
519+
520+
Returns:
521+
The mock result
522+
"""
523+
if "tip" in cmd:
524+
return json.dumps(QUERY_TIP_RESULT)
525+
if "txid" in cmd:
526+
return "270be16fa17cdb3ef683bf2c28259c978d4b7088792074f177c8efda247e23f7"
527+
else:
528+
return None
529+
530+
509531
@pytest.fixture
510532
def chain_context(genesis_file, config_file):
511533
"""
@@ -519,15 +541,40 @@ def chain_context(genesis_file, config_file):
519541
"""
520542
with patch(
521543
"pycardano.backend.cardano_cli.CardanoCliChainContext._run_command",
522-
side_effect=override_run_command,
544+
side_effect=override_run_command_older_version,
545+
):
546+
context = CardanoCliChainContext(
547+
binary=Path("cardano-cli"),
548+
socket=Path("node.socket"),
549+
config_file=config_file,
550+
network=CardanoCliNetwork.PREPROD,
551+
)
552+
context._run_command = override_run_command_older_version
553+
return context
554+
555+
556+
@pytest.fixture
557+
def chain_context_latest(genesis_file, config_file):
558+
"""
559+
Create a CardanoCliChainContext with a mock run_command method
560+
Args:
561+
genesis_file: The genesis file
562+
config_file: The config file
563+
564+
Returns:
565+
The CardanoCliChainContext
566+
"""
567+
with patch(
568+
"pycardano.backend.cardano_cli.CardanoCliChainContext._run_command",
569+
side_effect=override_run_command_latest,
523570
):
524571
context = CardanoCliChainContext(
525572
binary=Path("cardano-cli"),
526573
socket=Path("node.socket"),
527574
config_file=config_file,
528575
network=CardanoCliNetwork.PREPROD,
529576
)
530-
context._run_command = override_run_command
577+
context._run_command = override_run_command_latest
531578
return context
532579

533580

@@ -722,6 +769,14 @@ def test_utxo(self, chain_context):
722769
"55fe36f482e21ff6ae2caf2e33c3565572b568852dccd3f317ddecb91463d780"
723770
)
724771

772+
def test_submit_tx_bytes(self, chain_context):
773+
results = chain_context.submit_tx("testcborhexfromtransaction".encode("utf-8"))
774+
775+
assert (
776+
results
777+
== "270be16fa17cdb3ef683bf2c28259c978d4b7088792074f177c8efda247e23f7"
778+
)
779+
725780
def test_submit_tx(self, chain_context):
726781
results = chain_context.submit_tx("testcborhexfromtransaction")
727782

@@ -730,5 +785,13 @@ def test_submit_tx(self, chain_context):
730785
== "270be16fa17cdb3ef683bf2c28259c978d4b7088792074f177c8efda247e23f7"
731786
)
732787

788+
def test_submit_tx_latest(self, chain_context_latest):
789+
results = chain_context_latest.submit_tx("testcborhexfromtransaction")
790+
791+
assert (
792+
results
793+
== "270be16fa17cdb3ef683bf2c28259c978d4b7088792074f177c8efda247e23f7"
794+
)
795+
733796
def test_epoch(self, chain_context):
734797
assert chain_context.epoch == 98

0 commit comments

Comments
 (0)