From 2cd1947ef597ed2ad4d7ba56d1a0345fa0525680 Mon Sep 17 00:00:00 2001 From: Lex Berezhny Date: Mon, 15 Oct 2018 00:45:21 -0400 Subject: [PATCH] fixing tests --- tests/integration/test_reconnect.py | 11 +++++------ tests/unit/test_bip32.py | 11 ++++++----- tests/unit/test_coinselection.py | 12 ++++++++---- tests/unit/test_script.py | 2 +- tests/unit/test_wallet.py | 7 ++++--- torba/basenetwork.py | 3 ++- tox.ini | 6 +++--- 7 files changed, 29 insertions(+), 23 deletions(-) diff --git a/tests/integration/test_reconnect.py b/tests/integration/test_reconnect.py index bb8c9b8ac..18b41e868 100644 --- a/tests/integration/test_reconnect.py +++ b/tests/integration/test_reconnect.py @@ -1,5 +1,4 @@ -import asyncio -from orchstr8.testcase import IntegrationTestCase, d2f +from orchstr8.testcase import IntegrationTestCase from torba.constants import COIN @@ -8,7 +7,7 @@ class ReconnectTests(IntegrationTestCase): VERBOSE = False async def test_connection_drop_still_receives_events_after_reconnected(self): - address1 = await d2f(self.account.receiving.get_or_create_usable_address()) + address1 = await self.account.receiving.get_or_create_usable_address() self.ledger.network.client.connectionLost() sendtxid = await self.blockchain.send_to_address(address1, 1.1337) await self.on_transaction_id(sendtxid) # mempool @@ -21,13 +20,13 @@ class ReconnectTests(IntegrationTestCase): # what's that smoke on my ethernet cable? oh no! self.ledger.network.client.connectionLost() with self.assertRaisesRegex(TimeoutError, 'Connection dropped'): - await d2f(d) + await d # rich but offline? no way, no water, let's retry with self.assertRaisesRegex(ConnectionError, 'connection is not available'): - await d2f(self.ledger.network.get_transaction(sendtxid)) + await self.ledger.network.get_transaction(sendtxid) # * goes to pick some water outside... * time passes by and another donation comes in sendtxid = await self.blockchain.send_to_address(address1, 42) await self.blockchain.generate(1) # omg, the burned cable still works! torba is fire proof! - await d2f(self.ledger.network.get_transaction(sendtxid)) + await self.ledger.network.get_transaction(sendtxid) diff --git a/tests/unit/test_bip32.py b/tests/unit/test_bip32.py index b96c5a894..1d3229f09 100644 --- a/tests/unit/test_bip32.py +++ b/tests/unit/test_bip32.py @@ -1,12 +1,13 @@ -import unittest from binascii import unhexlify, hexlify +from orchstr8.testcase import AsyncioTestCase + from .key_fixtures import expected_ids, expected_privkeys, expected_hardened_privkeys from torba.bip32 import PubKey, PrivateKey, from_extended_key_string from torba.coin.bitcoinsegwit import MainNetLedger as ledger_class -class BIP32Tests(unittest.TestCase): +class BIP32Tests(AsyncioTestCase): def test_pubkey_validation(self): with self.assertRaisesRegex(TypeError, 'chain code must be raw bytes'): @@ -39,7 +40,7 @@ class BIP32Tests(unittest.TestCase): self.assertIsInstance(new_key, PubKey) self.assertEqual(hexlify(new_key.identifier()), expected_ids[i]) - def test_private_key_validation(self): + async def test_private_key_validation(self): with self.assertRaisesRegex(TypeError, 'private key must be raw bytes'): PrivateKey(None, None, b'abcd'*8, 0, 255) with self.assertRaisesRegex(ValueError, 'private key must be 32 bytes'): @@ -64,7 +65,7 @@ class BIP32Tests(unittest.TestCase): private_key.child(-1) self.assertIsInstance(private_key.child(PrivateKey.HARDENED), PrivateKey) - def test_private_key_derivation(self): + async def test_private_key_derivation(self): private_key = PrivateKey( ledger_class({ 'db': ledger_class.database_class(':memory:'), @@ -82,7 +83,7 @@ class BIP32Tests(unittest.TestCase): self.assertIsInstance(new_privkey, PrivateKey) self.assertEqual(hexlify(new_privkey.private_key_bytes), expected_hardened_privkeys[i - 1 - PrivateKey.HARDENED]) - def test_from_extended_keys(self): + async def test_from_extended_keys(self): ledger = ledger_class({ 'db': ledger_class.database_class(':memory:'), 'headers': ledger_class.headers_class(':memory:'), diff --git a/tests/unit/test_coinselection.py b/tests/unit/test_coinselection.py index 610a20715..fc54639db 100644 --- a/tests/unit/test_coinselection.py +++ b/tests/unit/test_coinselection.py @@ -1,6 +1,7 @@ -import unittest from types import GeneratorType +from orchstr8.testcase import AsyncioTestCase + from torba.coin.bitcoinsegwit import MainNetLedger as ledger_class from torba.coinselection import CoinSelector, MAXIMUM_TRIES from torba.constants import CENT @@ -16,14 +17,17 @@ def search(*args, **kwargs): return [o.txo.amount for o in selection] if selection else selection -class BaseSelectionTestCase(unittest.TestCase): +class BaseSelectionTestCase(AsyncioTestCase): - def setUp(self): + async def asyncSetUp(self): self.ledger = ledger_class({ 'db': ledger_class.database_class(':memory:'), 'headers': ledger_class.headers_class(':memory:'), }) - return self.ledger.db.open() + await self.ledger.db.open() + + async def asyncTearDown(self): + await self.ledger.db.close() def estimates(self, *args): txos = args[0] if isinstance(args[0], (GeneratorType, list)) else args diff --git a/tests/unit/test_script.py b/tests/unit/test_script.py index ffcbc5b12..f1826353b 100644 --- a/tests/unit/test_script.py +++ b/tests/unit/test_script.py @@ -93,7 +93,7 @@ class TestScriptTemplates(unittest.TestCase): ) def test_push_data_many_not_separated(self): - with self.assertRaisesRegexp(ParseError, 'consecutive PUSH_MANY'): + with self.assertRaisesRegex(ParseError, 'consecutive PUSH_MANY'): parse((PUSH_MANY('Chiefs'), PUSH_MANY('Devs')), (b'jeremy', b'grin', b'lex', b'jack')) diff --git a/tests/unit/test_wallet.py b/tests/unit/test_wallet.py index bebcf1585..baf65505c 100644 --- a/tests/unit/test_wallet.py +++ b/tests/unit/test_wallet.py @@ -1,15 +1,16 @@ -import unittest import tempfile +from orchstr8.testcase import AsyncioTestCase + from torba.coin.bitcoinsegwit import MainNetLedger as BTCLedger from torba.coin.bitcoincash import MainNetLedger as BCHLedger from torba.basemanager import BaseWalletManager from torba.wallet import Wallet, WalletStorage -class TestWalletCreation(unittest.TestCase): +class TestWalletCreation(AsyncioTestCase): - def setUp(self): + async def asyncSetUp(self): self.manager = BaseWalletManager() config = {'data_path': '/tmp/wallet'} self.btc_ledger = self.manager.get_or_create_ledger(BTCLedger.get_id(), config) diff --git a/torba/basenetwork.py b/torba/basenetwork.py index 689cab088..f52d17cd9 100644 --- a/torba/basenetwork.py +++ b/torba/basenetwork.py @@ -66,8 +66,9 @@ class BaseNetwork: async def stop(self): self.running = False if self.is_connected: + disconnected = self.client.on_disconnected.first await self.client.close() - await self.client.on_disconnected.first + await disconnected @property def is_connected(self): diff --git a/tox.ini b/tox.ini index ef05764eb..18f61f6b8 100644 --- a/tox.ini +++ b/tox.ini @@ -17,9 +17,9 @@ changedir = {toxinidir}/tests setenv = integration: LEDGER={envname} commands = - unit: coverage run -p --source={envsitepackagesdir}/torba -m twisted.trial unit + unit: coverage run -p --source={envsitepackagesdir}/torba -m unittest discover -t . unit integration: orchstr8 download - integration: coverage run -p --source={envsitepackagesdir}/torba -m twisted.trial --reactor=asyncio integration.test_transactions - integration: coverage run -p --source={envsitepackagesdir}/torba -m twisted.trial --reactor=asyncio integration.test_reconnect + integration: coverage run -p --source={envsitepackagesdir}/torba -m unittest integration.test_transactions + # integration: coverage run -p --source={envsitepackagesdir}/torba -m unittest integration.test_reconnect # Too slow on Travis # integration: coverage run -p --source={envsitepackagesdir}/torba -m twisted.trial --reactor=asyncio integration.test_blockchain_reorganization