tests: improving tests of twin cli
from tests import unittest
-from tests.utils import add_new_blocks, add_new_transactions
+from tests.utils import add_new_blocks, add_new_transactions, run_server, request_server
from twisted.internet.task import Clock
from hathor.transaction import Transaction
from hathor.cli.twin_tx import create_parser, execute
+from hathor.cli.tx_generator import create_parser as create_parser_tx, execute as execute_tx
+from hathor.cli.mining import create_parser as create_parser_mining, execute as execute_mining
+import urllib.parse
import time
import ast
@@ -12,6 +15,21 @@ from io import StringIO
from contextlib import redirect_stdout
+def prepare_transactions(host):
+ # Unlock wallet to start mining
+ request_server('wallet/unlock', 'POST', data={'passphrase': '123'})
+
+ # Mining
+ parser_mining = create_parser_mining()
+ args = parser_mining.parse_args([urllib.parse.urljoin(host, 'mining'), '--count', '2'])
+ execute_mining(args)
+
+ # Generating txs
+ parser_tx = create_parser_tx()
+ args = parser_tx.parse_args([host, '--count', '4'])
+ execute_tx(args)
+
+
class TwinTxTest(unittest.TestCase):
def setUp(self):
super().setUp()
@@ -51,9 +69,18 @@ class TwinTxTest(unittest.TestCase):
meta = self.tx.get_metadata()
self.assertEqual(meta.twins, set([twin_tx.hash]))
- def test_twin_different_weight(self):
- # Twin different weight
- params = ['--raw_tx', self.tx.get_struct().hex(), '--weight', '4']
+ def test_twin_different(self):
+ server = run_server()
+
+ host = 'http://localhost:8085'
+ prepare_transactions(host)
+
+ response = request_server('transaction', 'GET', data={b'count': 4, b'type': 'tx'})
+ tx = response['transactions'][-1]
+ print(tx)
+
+ # Twin different weight and parents
+ params = ['--url', host, '--hash', tx['hash'], '--parents', '--weight', '14']
args = self.parser.parse_args(params)
f = StringIO()
@@ -66,18 +93,16 @@ class TwinTxTest(unittest.TestCase):
output.pop()
twin_tx = Transaction.create_from_struct(bytes.fromhex(output[0]))
- # Parents are the same but in different order
- self.assertEqual(twin_tx.parents[0], self.tx.parents[1])
- self.assertEqual(twin_tx.parents[1], self.tx.parents[0])
+ # Parents are differents
+ self.assertNotEqual(twin_tx.parents[0], tx['parents'][0])
+ self.assertNotEqual(twin_tx.parents[0], tx['parents'][1])
+ self.assertNotEqual(twin_tx.parents[1], tx['parents'][0])
+ self.assertNotEqual(twin_tx.parents[1], tx['parents'][1])
- self.assertNotEqual(twin_tx.weight, self.tx.weight)
- self.assertEqual(twin_tx.weight, 4.0)
-
- self.manager.propagate_tx(twin_tx)
+ self.assertNotEqual(twin_tx.weight, tx['weight'])
+ self.assertEqual(twin_tx.weight, 14.0)
- # Validate they are twins
- meta = self.tx.get_metadata()
- self.assertEqual(meta.twins, set([twin_tx.hash]))
+ server.terminate()
def test_twin_human(self):
# Twin in human form
