8
8
from pycardano import (
9
9
ALONZO_COINS_PER_UTXO_WORD ,
10
10
CardanoCliChainContext ,
11
+ CardanoCliError ,
11
12
CardanoCliNetwork ,
12
13
DatumHash ,
13
14
GenesisParameters ,
14
15
MultiAsset ,
15
16
PlutusV2Script ,
16
17
ProtocolParameters ,
18
+ PyCardanoException ,
17
19
RawPlutusData ,
20
+ TransactionFailedException ,
18
21
TransactionInput ,
19
22
)
20
23
483
486
}
484
487
485
488
486
- def override_run_command (cmd : List [str ]):
489
+ @pytest .fixture
490
+ def chain_context (genesis_file , config_file ):
491
+ """
492
+ Create a CardanoCliChainContext with a mock run_command method
493
+ Args:
494
+ genesis_file: The genesis file
495
+ config_file: The config file
496
+
497
+ Returns:
498
+ The CardanoCliChainContext
499
+ """
500
+
501
+ def override_run_command_older_version (cmd : List [str ]):
502
+ """
503
+ Override the run_command method of CardanoCliChainContext to return a mock result of older versions of cardano-cli.
504
+ Args:
505
+ cmd: The command to run
506
+
507
+ Returns:
508
+ The mock result
509
+ """
510
+ if "latest" in cmd :
511
+ raise CardanoCliError (
512
+ "Older versions of cardano-cli do not support the latest command"
513
+ )
514
+ if "tip" in cmd :
515
+ return json .dumps (QUERY_TIP_RESULT )
516
+ if "protocol-parameters" in cmd :
517
+ return json .dumps (QUERY_PROTOCOL_PARAMETERS_RESULT )
518
+ if "utxo" in cmd :
519
+ return json .dumps (QUERY_UTXO_RESULT )
520
+ if "txid" in cmd :
521
+ return "270be16fa17cdb3ef683bf2c28259c978d4b7088792074f177c8efda247e23f7"
522
+ if "version" in cmd :
523
+ return "cardano-cli 8.1.2 - linux-x86_64 - ghc-8.10\n git rev d2d90b48c5577b4412d5c9c9968b55f8ab4b9767"
524
+ else :
525
+ return None
526
+
527
+ with patch (
528
+ "pycardano.backend.cardano_cli.CardanoCliChainContext._run_command" ,
529
+ side_effect = override_run_command_older_version ,
530
+ ):
531
+ context = CardanoCliChainContext (
532
+ binary = Path ("cardano-cli" ),
533
+ socket = Path ("node.socket" ),
534
+ config_file = config_file ,
535
+ network = CardanoCliNetwork .PREPROD ,
536
+ )
537
+ context ._run_command = override_run_command_older_version
538
+ return context
539
+
540
+
541
+ @pytest .fixture
542
+ def chain_context_latest (genesis_file , config_file ):
487
543
"""
488
- Override the run_command method of CardanoCliChainContext to return a mock result
544
+ Create a CardanoCliChainContext with a mock run_command method
489
545
Args:
490
- cmd: The command to run
546
+ genesis_file: The genesis file
547
+ config_file: The config file
491
548
492
549
Returns:
493
- The mock result
550
+ The CardanoCliChainContext
494
551
"""
495
- if "tip" in cmd :
496
- return json .dumps (QUERY_TIP_RESULT )
497
- if "protocol-parameters" in cmd :
498
- return json .dumps (QUERY_PROTOCOL_PARAMETERS_RESULT )
499
- if "utxo" in cmd :
500
- return json .dumps (QUERY_UTXO_RESULT )
501
- if "txid" in cmd :
502
- return "270be16fa17cdb3ef683bf2c28259c978d4b7088792074f177c8efda247e23f7"
503
- if "version" in cmd :
504
- return "cardano-cli 8.1.2 - linux-x86_64 - ghc-8.10\n git rev d2d90b48c5577b4412d5c9c9968b55f8ab4b9767"
505
- else :
552
+
553
+ def override_run_command_latest (cmd : List [str ]):
554
+ """
555
+ Override the run_command method of CardanoCliChainContext to return a mock result of the latest versions of cardano-cli.
556
+ Args:
557
+ cmd: The command to run
558
+
559
+ Returns:
560
+ The mock result
561
+ """
562
+ if "tip" in cmd :
563
+ return json .dumps (QUERY_TIP_RESULT )
564
+ if "txid" in cmd :
565
+ return "270be16fa17cdb3ef683bf2c28259c978d4b7088792074f177c8efda247e23f7"
566
+ else :
567
+ return None
568
+
569
+ with patch (
570
+ "pycardano.backend.cardano_cli.CardanoCliChainContext._run_command" ,
571
+ side_effect = override_run_command_latest ,
572
+ ):
573
+ context = CardanoCliChainContext (
574
+ binary = Path ("cardano-cli" ),
575
+ socket = Path ("node.socket" ),
576
+ config_file = config_file ,
577
+ network = CardanoCliNetwork .PREPROD ,
578
+ )
579
+ context ._run_command = override_run_command_latest
580
+ return context
581
+
582
+
583
+ @pytest .fixture
584
+ def chain_context_tx_fail (genesis_file , config_file ):
585
+ """
586
+ Create a CardanoCliChainContext with a mock run_command method
587
+ Args:
588
+ genesis_file: The genesis file
589
+ config_file: The config file
590
+
591
+ Returns:
592
+ The CardanoCliChainContext
593
+ """
594
+
595
+ def override_run_command_fail (cmd : List [str ]):
596
+ if "transaction" in cmd :
597
+ raise CardanoCliError ("Intentionally raised error for testing purposes" )
598
+ if "tip" in cmd :
599
+ return json .dumps (QUERY_TIP_RESULT )
506
600
return None
507
601
602
+ with patch (
603
+ "pycardano.backend.cardano_cli.CardanoCliChainContext._run_command" ,
604
+ side_effect = override_run_command_fail ,
605
+ ):
606
+ context = CardanoCliChainContext (
607
+ binary = Path ("cardano-cli" ),
608
+ socket = Path ("node.socket" ),
609
+ config_file = config_file ,
610
+ network = CardanoCliNetwork .PREPROD ,
611
+ )
612
+ context ._run_command = override_run_command_fail
613
+ return context
614
+
508
615
509
616
@pytest .fixture
510
- def chain_context (genesis_file , config_file ):
617
+ def chain_context_tx_id_fail (genesis_file , config_file ):
511
618
"""
512
619
Create a CardanoCliChainContext with a mock run_command method
513
620
Args:
@@ -517,17 +624,25 @@ def chain_context(genesis_file, config_file):
517
624
Returns:
518
625
The CardanoCliChainContext
519
626
"""
627
+
628
+ def override_run_command_fail (cmd : List [str ]):
629
+ if "txid" in cmd :
630
+ raise CardanoCliError ("Intentionally raised error for testing purposes" )
631
+ if "tip" in cmd :
632
+ return json .dumps (QUERY_TIP_RESULT )
633
+ return None
634
+
520
635
with patch (
521
636
"pycardano.backend.cardano_cli.CardanoCliChainContext._run_command" ,
522
- side_effect = override_run_command ,
637
+ side_effect = override_run_command_fail ,
523
638
):
524
639
context = CardanoCliChainContext (
525
640
binary = Path ("cardano-cli" ),
526
641
socket = Path ("node.socket" ),
527
642
config_file = config_file ,
528
643
network = CardanoCliNetwork .PREPROD ,
529
644
)
530
- context ._run_command = override_run_command
645
+ context ._run_command = override_run_command_fail
531
646
return context
532
647
533
648
@@ -722,6 +837,14 @@ def test_utxo(self, chain_context):
722
837
"55fe36f482e21ff6ae2caf2e33c3565572b568852dccd3f317ddecb91463d780"
723
838
)
724
839
840
+ def test_submit_tx_bytes (self , chain_context ):
841
+ results = chain_context .submit_tx ("testcborhexfromtransaction" .encode ("utf-8" ))
842
+
843
+ assert (
844
+ results
845
+ == "270be16fa17cdb3ef683bf2c28259c978d4b7088792074f177c8efda247e23f7"
846
+ )
847
+
725
848
def test_submit_tx (self , chain_context ):
726
849
results = chain_context .submit_tx ("testcborhexfromtransaction" )
727
850
@@ -730,5 +853,23 @@ def test_submit_tx(self, chain_context):
730
853
== "270be16fa17cdb3ef683bf2c28259c978d4b7088792074f177c8efda247e23f7"
731
854
)
732
855
856
+ def test_submit_tx_latest (self , chain_context_latest ):
857
+ results = chain_context_latest .submit_tx ("testcborhexfromtransaction" )
858
+
859
+ assert (
860
+ results
861
+ == "270be16fa17cdb3ef683bf2c28259c978d4b7088792074f177c8efda247e23f7"
862
+ )
863
+
864
+ def test_submit_tx_fail (self , chain_context_tx_fail ):
865
+ with pytest .raises (TransactionFailedException ) as exc_info :
866
+ chain_context_tx_fail .submit_tx ("testcborhexfromtransaction" )
867
+ assert str (exc_info .value ) == "Failed to submit transaction"
868
+
869
+ def test_submit_tx_id_fail (self , chain_context_tx_id_fail ):
870
+ with pytest .raises (PyCardanoException ) as exc_info :
871
+ chain_context_tx_id_fail .submit_tx ("testcborhexfromtransaction" )
872
+ assert str (exc_info .value ).startswith ("Unable to get transaction id for" )
873
+
733
874
def test_epoch (self , chain_context ):
734
875
assert chain_context .epoch == 98
0 commit comments