From f6a52e24e465a515f6bace45e5449f9834c5b75e Mon Sep 17 00:00:00 2001 From: inso <insomniak.fr@gmaiL.com> Date: Mon, 3 Apr 2017 07:23:43 +0200 Subject: [PATCH] aiohttp 2.0 --- src/sakia/app.py | 2 +- src/sakia/data/connectors/bma.py | 6 +- src/sakia/data/connectors/node.py | 28 +++---- src/sakia/data/processors/identities.py | 2 +- .../gui/dialogs/connection_cfg/controller.py | 5 +- src/sakia/gui/dialogs/revocation/model.py | 5 +- src/sakia/gui/sub/search_user/controller.py | 4 +- src/sakia/gui/sub/search_user/model.py | 3 +- tests/conftest.py | 82 +++++++++++-------- tests/functional/test_certification_dialog.py | 6 +- .../functional/test_connection_cfg_dialog.py | 18 ++-- tests/functional/test_contacts_dialog.py | 17 ++-- tests/functional/test_transfer_dialog.py | 6 +- tests/technical/test_blockchain_service.py | 16 ++-- tests/technical/test_documents_service.py | 10 +-- tests/technical/test_identities_service.py | 38 ++++----- tests/technical/test_sources_service.py | 54 ++++++------ tests/technical/test_transactions_service.py | 50 +++++------ tests/unit/money/test_relative_zsum.py | 2 +- 19 files changed, 181 insertions(+), 173 deletions(-) diff --git a/src/sakia/app.py b/src/sakia/app.py index 0c753b25..a29efd7d 100644 --- a/src/sakia/app.py +++ b/src/sakia/app.py @@ -211,7 +211,7 @@ class Application(QObject): logging.debug("Found version : {0}".format(latest_version)) logging.debug("Current version : {0}".format(__version__)) self.available_version = version - except (aiohttp.errors.ClientError, aiohttp.errors.TimeoutError) as e: + except (aiohttp.ClientError, asyncio.TimeoutError) as e: self._logger.debug("Could not connect to github : {0}".format(str(e))) def save_parameters(self, parameters): diff --git a/src/sakia/data/connectors/bma.py b/src/sakia/data/connectors/bma.py index e6721bb8..a5c6bef9 100644 --- a/src/sakia/data/connectors/bma.py +++ b/src/sakia/data/connectors/bma.py @@ -1,6 +1,6 @@ import logging import aiohttp -from aiohttp.errors import ClientError, ServerDisconnectedError +from aiohttp import ClientError from duniterpy.api import bma, errors from duniterpy.documents import BMAEndpoint, SecuredBMAEndpoint from sakia.errors import NoPeerAvailable @@ -242,8 +242,8 @@ class BmaConnector: tries += 1 else: raise - except (ClientError, ServerDisconnectedError, gaierror, - asyncio.TimeoutError, ValueError, jsonschema.ValidationError) as e: + except (ClientError, gaierror, asyncio.TimeoutError, + ValueError, jsonschema.ValidationError) as e: self._logger.debug(str(e)) tries += 1 raise NoPeerAvailable("", len(endpoints)) diff --git a/src/sakia/data/connectors/node.py b/src/sakia/data/connectors/node.py index 039b6428..b191be7e 100644 --- a/src/sakia/data/connectors/node.py +++ b/src/sakia/data/connectors/node.py @@ -7,8 +7,7 @@ from socket import gaierror import aiohttp import jsonschema from PyQt5.QtCore import QObject, pyqtSignal -from aiohttp.errors import ClientError, DisconnectedError -from aiohttp.errors import WSServerHandshakeError, ClientResponseError +from aiohttp import ClientError from duniterpy.api import bma, errors from duniterpy.documents import BlockUID, MalformedDocumentError, BMAEndpoint @@ -98,7 +97,7 @@ class NodeConnector(QObject): conn_handler = next(endpoint.conn_handler(self.session, proxy=proxy)) data = await request(conn_handler, **req_args) return data - except (ClientError, gaierror, TimeoutError, ConnectionRefusedError, DisconnectedError, ValueError) as e: + except (ClientError, gaierror, TimeoutError, ConnectionRefusedError, ValueError) as e: self._logger.debug("{0} : {1}".format(str(e), self.node.pubkey[:5])) self.change_state_and_emit(Node.OFFLINE) except jsonschema.ValidationError as e: @@ -114,7 +113,6 @@ class NodeConnector(QObject): for ws in self._ws_tasks.values(): if ws: ws.cancel() - await asyncio.sleep(0) closed = False while not closed: for ws in self._ws_tasks.values(): @@ -156,20 +154,19 @@ class NodeConnector(QObject): self._logger.debug("Connected successfully to block ws : {0}" .format(self.node.pubkey[:5])) async for msg in ws: - if msg.tp == aiohttp.MsgType.text: + if msg.tp == aiohttp.WSMsgType.TEXT: self._logger.debug("Received a block : {0}".format(self.node.pubkey[:5])) block_data = bma.parse_text(msg.data, bma.ws.WS_BLOCk_SCHEMA) await self.refresh_block(block_data) - elif msg.tp == aiohttp.MsgType.closed: + elif msg.tp == aiohttp.WSMsgType.CLOSED: break - elif msg.tp == aiohttp.MsgType.error: + elif msg.tp == aiohttp.WSMsgType.ERROR: break - except (WSServerHandshakeError, - ClientResponseError, ValueError) as e: + except (aiohttp.WSServerHandshakeError, ValueError) as e: self._logger.debug("Websocket block {0} : {1} - {2}" .format(type(e).__name__, str(e), self.node.pubkey[:5])) await self.request_current_block() - except (ClientError, gaierror, TimeoutError, DisconnectedError) as e: + except (ClientError, gaierror, TimeoutError) as e: self._logger.debug("{0} : {1}".format(str(e), self.node.pubkey[:5])) self.change_state_and_emit(Node.OFFLINE) except jsonschema.ValidationError as e: @@ -283,20 +280,19 @@ class NodeConnector(QObject): self._connected['peer'] = True self._logger.debug("Connected successfully to peer ws : {0}".format(self.node.pubkey[:5])) async for msg in ws: - if msg.tp == aiohttp.MsgType.text: + if msg.tp == aiohttp.WSMsgType.TEXT: self._logger.debug("Received a peer : {0}".format(self.node.pubkey[:5])) peer_data = bma.parse_text(msg.data, bma.ws.WS_PEER_SCHEMA) self.refresh_peer_data(peer_data) - elif msg.tp == aiohttp.MsgType.closed: + elif msg.tp == aiohttp.WSMsgType.CLOSED: break - elif msg.tp == aiohttp.MsgType.error: + elif msg.tp == aiohttp.WSMsgType.ERROR: break - except (WSServerHandshakeError, - ClientResponseError, ValueError) as e: + except (aiohttp.WSServerHandshakeError, ValueError) as e: self._logger.debug("Websocket peer {0} : {1} - {2}" .format(type(e).__name__, str(e), self.node.pubkey[:5])) await self.request_peers() - except (ClientError, gaierror, TimeoutError, DisconnectedError) as e: + except (ClientError, gaierror, TimeoutError) as e: self._logger.debug("{0} : {1}".format(str(e), self.node.pubkey[:5])) self.change_state_and_emit(Node.OFFLINE) except jsonschema.ValidationError as e: diff --git a/src/sakia/data/processors/identities.py b/src/sakia/data/processors/identities.py index e2166973..0db94626 100644 --- a/src/sakia/data/processors/identities.py +++ b/src/sakia/data/processors/identities.py @@ -9,7 +9,7 @@ from duniterpy.api import bma, errors from duniterpy.key import SigningKey from duniterpy.documents import BlockUID, block_uid from duniterpy.documents import Identity as IdentityDoc -from aiohttp.errors import ClientError +from aiohttp import ClientError from sakia.errors import NoPeerAvailable diff --git a/src/sakia/gui/dialogs/connection_cfg/controller.py b/src/sakia/gui/dialogs/connection_cfg/controller.py index d1476b07..30815bac 100644 --- a/src/sakia/gui/dialogs/connection_cfg/controller.py +++ b/src/sakia/gui/dialogs/connection_cfg/controller.py @@ -2,7 +2,8 @@ import asyncio import logging from PyQt5.QtCore import QObject -from aiohttp.errors import DisconnectedError, ClientError, TimeoutError +from aiohttp import ClientError +from asyncio import TimeoutError from duniterpy.api.errors import DuniterError from duniterpy.documents import MalformedDocumentError @@ -118,7 +119,7 @@ class ConnectionConfigController(QObject): self.view.button_connect.setEnabled(False) self.view.button_register.setEnabled(False) await self.model.create_connection() - except (DisconnectedError, ClientError, MalformedDocumentError, ValueError, TimeoutError) as e: + except (ClientError, MalformedDocumentError, ValueError, TimeoutError) as e: self._logger.debug(str(e)) self.view.display_info(self.tr("Could not connect. Check hostname, ip address or port : <br/>" + str(e))) diff --git a/src/sakia/gui/dialogs/revocation/model.py b/src/sakia/gui/dialogs/revocation/model.py index f3a316e3..a3f64118 100644 --- a/src/sakia/gui/dialogs/revocation/model.py +++ b/src/sakia/gui/dialogs/revocation/model.py @@ -4,8 +4,7 @@ from sakia.data.connectors import NodeConnector from asyncio import TimeoutError from socket import gaierror import jsonschema -from aiohttp.errors import ClientError, DisconnectedError -from aiohttp.errors import ClientResponseError +from aiohttp import ClientError from PyQt5.QtCore import QObject import logging @@ -58,7 +57,7 @@ class RevocationModel(QObject): except errors.DuniterError as e: return False, e.message except (jsonschema.ValidationError, ClientError, gaierror, - TimeoutError, ConnectionRefusedError, DisconnectedError, ValueError) as e: + TimeoutError, ConnectionRefusedError, ValueError) as e: return False, str(e) finally: node_connector.session.close() diff --git a/src/sakia/gui/sub/search_user/controller.py b/src/sakia/gui/sub/search_user/controller.py index 027804e8..666769b8 100644 --- a/src/sakia/gui/sub/search_user/controller.py +++ b/src/sakia/gui/sub/search_user/controller.py @@ -57,6 +57,6 @@ class SearchUserController(QObject): """ Select node in graph when item is selected in combobox """ - self.model.select_identity(index) - self.identity_selected.emit(self.model.identity()) + if self.model.select_identity(index): + self.identity_selected.emit(self.model.identity()) diff --git a/src/sakia/gui/sub/search_user/model.py b/src/sakia/gui/sub/search_user/model.py index 7b912bb5..8839788c 100644 --- a/src/sakia/gui/sub/search_user/model.py +++ b/src/sakia/gui/sub/search_user/model.py @@ -68,4 +68,5 @@ class SearchUserModel(QObject): if index < 0 or index >= len(self._nodes): self._current_identity = None return False - self._current_identity = self._nodes[index] \ No newline at end of file + self._current_identity = self._nodes[index] + return True \ No newline at end of file diff --git a/tests/conftest.py b/tests/conftest.py index 181b21d8..4a82164b 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -10,6 +10,7 @@ sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 's from sakia.constants import ROOT_SERVERS from duniterpy.documents import BlockUID +from duniterpy.key import ScryptParams from sakia.app import Application from sakia.options import SakiaOptions from sakia.data.files import * @@ -85,6 +86,11 @@ def application(event_loop, meta_repo, sakia_options, app_data, user_parameters, return app +@pytest.fixture() +def simple_forge(): + return mirage.BlockForge.start("test_currency", "12356", "123456", ScryptParams(2 ** 12, 16, 1)) + + @pytest.fixture def fake_server(application, event_loop): server = event_loop.run_until_complete(mirage.Node.start(None, "test_currency", "12356", "123456", event_loop)) @@ -114,10 +120,10 @@ def bob(): @pytest.fixture -def john(simple_fake_server): +def john(simple_blockchain_forge): # John is not written in the blockchain return mirage.User.create("test_currency", "john", "johnsalt", "johnpassword", - simple_fake_server.forge.blocks[-1].blockUID) + simple_blockchain_forge.blocks[-1].blockUID) @pytest.fixture @@ -131,35 +137,41 @@ def wrong_bob_pubkey(): @pytest.fixture -def simple_fake_server(fake_server, alice, bob): - fake_server.forge.push(alice.identity()) - fake_server.forge.push(bob.identity()) - fake_server.forge.push(alice.join(BlockUID.empty())) - fake_server.forge.push(bob.join(BlockUID.empty())) - fake_server.forge.push(alice.certify(bob, BlockUID.empty())) - fake_server.forge.push(bob.certify(alice, BlockUID.empty())) - fake_server.forge.forge_block() - fake_server.forge.set_member(alice.key.pubkey, True) - fake_server.forge.set_member(bob.key.pubkey, True) +def simple_blockchain_forge(simple_forge, alice, bob): + simple_forge.push(alice.identity()) + simple_forge.push(bob.identity()) + simple_forge.push(alice.join(BlockUID.empty())) + simple_forge.push(bob.join(BlockUID.empty())) + simple_forge.push(alice.certify(bob, BlockUID.empty())) + simple_forge.push(bob.certify(alice, BlockUID.empty())) + simple_forge.forge_block() + simple_forge.set_member(alice.key.pubkey, True) + simple_forge.set_member(bob.key.pubkey, True) for i in range(0, 10): new_user = mirage.User.create("test_currency", "user{0}".format(i), "salt{0}".format(i), "password{0}".format(i), - fake_server.forge.blocks[-1].blockUID) - fake_server.forge.push(new_user.identity()) - fake_server.forge.push(new_user.join(fake_server.forge.blocks[-1].blockUID)) - fake_server.forge.forge_block() - fake_server.forge.set_member(new_user.key.pubkey, True) - fake_server.forge.generate_dividend() - fake_server.forge.forge_block() + simple_forge.blocks[-1].blockUID) + simple_forge.push(new_user.identity()) + simple_forge.push(new_user.join(simple_forge.blocks[-1].blockUID)) + simple_forge.forge_block() + simple_forge.set_member(new_user.key.pubkey, True) + simple_forge.generate_dividend() + simple_forge.forge_block() + return simple_forge + + +@pytest.fixture +def fake_server_with_blockchain(fake_server, simple_blockchain_forge): + fake_server.forge = simple_blockchain_forge return fake_server @pytest.fixture -def application_with_one_connection(application, simple_fake_server, bob): - current_block = simple_fake_server.forge.blocks[-1] - last_ud_block = [b for b in simple_fake_server.forge.blocks if b.ud][-1] - previous_ud_block = [b for b in simple_fake_server.forge.blocks if b.ud][-2] - origin_block = simple_fake_server.forge.blocks[0] +def application_with_one_connection(application, simple_blockchain_forge, bob): + current_block = simple_blockchain_forge.blocks[-1] + last_ud_block = [b for b in simple_blockchain_forge.blocks if b.ud][-1] + previous_ud_block = [b for b in simple_blockchain_forge.blocks if b.ud][-2] + origin_block = simple_blockchain_forge.blocks[0] connection = Connection(currency="test_currency", pubkey=bob.key.pubkey, uid=bob.uid, scrypt_N=mirage.User.SCRYPT_PARAMS.N, @@ -173,43 +185,43 @@ def application_with_one_connection(application, simple_fake_server, bob): blockchain = Blockchain(parameters=blockchain_parameters, current_buid=current_block.blockUID, current_members_count=current_block.members_count, - current_mass=simple_fake_server.forge.monetary_mass(current_block.number), + current_mass=simple_blockchain_forge.monetary_mass(current_block.number), median_time=current_block.mediantime, last_members_count=previous_ud_block.members_count, last_ud=last_ud_block.ud, last_ud_base=last_ud_block.unit_base, last_ud_time=last_ud_block.mediantime, - previous_mass=simple_fake_server.forge.monetary_mass(previous_ud_block.number), + previous_mass=simple_blockchain_forge.monetary_mass(previous_ud_block.number), previous_members_count=previous_ud_block.members_count, previous_ud=previous_ud_block.ud, previous_ud_base=previous_ud_block.unit_base, previous_ud_time=previous_ud_block.mediantime, - currency=simple_fake_server.forge.currency) + currency=simple_blockchain_forge.currency) application.db.blockchains_repo.insert(blockchain) - for s in simple_fake_server.forge.user_identities[bob.key.pubkey].sources: - application.db.sources_repo.insert(Source(currency=simple_fake_server.forge.currency, + for s in simple_blockchain_forge.user_identities[bob.key.pubkey].sources: + application.db.sources_repo.insert(Source(currency=simple_blockchain_forge.currency, pubkey=bob.key.pubkey, identifier=s.origin_id, noffset=s.index, type=s.source, amount=s.amount, base=s.base)) - bob_blockstamp = simple_fake_server.forge.user_identities[bob.key.pubkey].blockstamp - bob_user_identity = simple_fake_server.forge.user_identities[bob.key.pubkey] + bob_blockstamp = simple_blockchain_forge.user_identities[bob.key.pubkey].blockstamp + bob_user_identity = simple_blockchain_forge.user_identities[bob.key.pubkey] bob_ms = bob_user_identity.memberships[-1] - bob_identity = Identity(currency=simple_fake_server.forge.currency, + bob_identity = Identity(currency=simple_blockchain_forge.currency, pubkey=bob.key.pubkey, uid=bob.uid, blockstamp=bob_blockstamp, signature=bob_user_identity.signature, - timestamp=simple_fake_server.forge.blocks[bob_blockstamp.number].mediantime, + timestamp=simple_blockchain_forge.blocks[bob_blockstamp.number].mediantime, written=True, revoked_on=0, member=bob_user_identity.member, membership_buid=bob_ms.blockstamp, - membership_timestamp=simple_fake_server.forge.blocks[bob_ms.blockstamp.number].mediantime, + membership_timestamp=simple_blockchain_forge.blocks[bob_ms.blockstamp.number].mediantime, membership_type=bob_ms.type, - membership_written_on=simple_fake_server.forge.blocks[bob_ms.written_on].number) + membership_written_on=simple_blockchain_forge.blocks[bob_ms.written_on].number) application.db.identities_repo.insert(bob_identity) application.switch_language() diff --git a/tests/functional/test_certification_dialog.py b/tests/functional/test_certification_dialog.py index 75e1a3de..3dde6823 100644 --- a/tests/functional/test_certification_dialog.py +++ b/tests/functional/test_certification_dialog.py @@ -9,7 +9,7 @@ from ..helpers import click_on_top_message_box @pytest.mark.asyncio -async def test_certification_init_community(application_with_one_connection, fake_server, bob, alice): +async def test_certification_init_community(application_with_one_connection, fake_server_with_blockchain, bob, alice): certification_dialog = CertificationController.create(None, application_with_one_connection) def close_dialog(): @@ -42,9 +42,9 @@ async def test_certification_init_community(application_with_one_connection, fak await asyncio.sleep(0.1) click_on_top_message_box() await asyncio.sleep(0.2) - assert isinstance(fake_server.forge.pool[0], Certification) + assert isinstance(fake_server_with_blockchain.forge.pool[0], Certification) application_with_one_connection.loop.call_later(10, close_dialog) asyncio.ensure_future(exec_test()) await certification_dialog.async_exec() - await fake_server.close() + await fake_server_with_blockchain.close() diff --git a/tests/functional/test_connection_cfg_dialog.py b/tests/functional/test_connection_cfg_dialog.py index 09e3b610..792397f7 100644 --- a/tests/functional/test_connection_cfg_dialog.py +++ b/tests/functional/test_connection_cfg_dialog.py @@ -5,7 +5,7 @@ from PyQt5.QtCore import Qt from PyQt5.QtTest import QTest from sakia.data.processors import ConnectionsProcessor from sakia.gui.dialogs.connection_cfg import ConnectionConfigController -from ..helpers import click_on_top_message_box +from tests.helpers import click_on_top_message_box def assert_key_parameters_behaviour(connection_config_dialog, user): @@ -64,7 +64,7 @@ async def test_register_empty_blockchain(application, fake_server, bob): @pytest.mark.asyncio -async def test_connect(application, simple_fake_server, bob): +async def test_connect(application, fake_server_with_blockchain, bob): connection_config_dialog = ConnectionConfigController.create_connection(None, application) def close_dialog(): @@ -89,11 +89,11 @@ async def test_connect(application, simple_fake_server, bob): application.loop.call_later(10, close_dialog) asyncio.ensure_future(exec_test()) await connection_config_dialog.async_exec() - await simple_fake_server.close() + await fake_server_with_blockchain.close() @pytest.mark.asyncio -async def test_connect_wrong_uid(application, simple_fake_server, wrong_bob_uid, bob): +async def test_connect_wrong_uid(application, fake_server_with_blockchain, wrong_bob_uid, bob): connection_config_dialog = ConnectionConfigController.create_connection(None, application) def close_dialog(): @@ -115,11 +115,11 @@ Yours : {0}, the network : {1}""".format(wrong_bob_uid.uid, bob.uid) application.loop.call_later(10, close_dialog) asyncio.ensure_future(exec_test()) await connection_config_dialog.async_exec() - await simple_fake_server.close() + await fake_server_with_blockchain.close() @pytest.mark.asyncio -async def test_connect_wrong_pubkey(application, simple_fake_server, wrong_bob_pubkey, bob): +async def test_connect_wrong_pubkey(application, fake_server_with_blockchain, wrong_bob_pubkey, bob): connection_config_dialog = ConnectionConfigController.create_connection(None, application) def close_dialog(): @@ -141,12 +141,12 @@ Yours : {0}, the network : {1}""".format(wrong_bob_pubkey.pubkey, bob.pubkey) application.loop.call_later(10, close_dialog) asyncio.ensure_future(exec_test()) await connection_config_dialog.async_exec() - await simple_fake_server.close() + await fake_server_with_blockchain.close() @pytest.mark.asyncio -async def test_connect_pubkey_wrong_uid(application, simple_fake_server, wrong_bob_uid, bob): +async def test_connect_pubkey_wrong_uid(application, fake_server_with_blockchain, wrong_bob_uid, bob): connection_config_dialog = ConnectionConfigController.create_connection(None, application) def close_dialog(): @@ -169,4 +169,4 @@ Yours : {0}, the network : {1}""".format(wrong_bob_uid.uid, bob.uid) application.loop.call_later(10, close_dialog) asyncio.ensure_future(exec_test()) await connection_config_dialog.async_exec() - await simple_fake_server.close() + await fake_server_with_blockchain.close() diff --git a/tests/functional/test_contacts_dialog.py b/tests/functional/test_contacts_dialog.py index 204628b7..272783d0 100644 --- a/tests/functional/test_contacts_dialog.py +++ b/tests/functional/test_contacts_dialog.py @@ -5,11 +5,10 @@ from PyQt5.QtCore import QLocale, Qt, QEvent, QModelIndex from PyQt5.QtTest import QTest from PyQt5.QtWidgets import QDialogButtonBox from sakia.gui.dialogs.contact.controller import ContactController -from ..helpers import click_on_top_message_box @pytest.mark.asyncio -async def test_add_contact(application_with_one_connection, fake_server): +async def test_add_contact(application_with_one_connection, fake_server_with_blockchain): contact_dialog = ContactController.create(None, application_with_one_connection) def close_dialog(): @@ -26,11 +25,11 @@ async def test_add_contact(application_with_one_connection, fake_server): application_with_one_connection.loop.call_later(10, close_dialog) asyncio.ensure_future(exec_test()) await contact_dialog.async_exec() - await fake_server.close() + await fake_server_with_blockchain.close() @pytest.mark.asyncio -async def test_edit_contact(application_with_one_connection, fake_server): +async def test_edit_contact(application_with_one_connection, fake_server_with_blockchain): contacts_repo = application_with_one_connection.db.contacts_repo contacts_repo.insert(Contact(currency="test_currency", pubkey="7Aqw6Efa9EzE7gtsc8SveLLrM7gm6NEGoywSv4FJx6pZ", @@ -55,11 +54,11 @@ async def test_edit_contact(application_with_one_connection, fake_server): application_with_one_connection.loop.call_later(10, close_dialog) asyncio.ensure_future(exec_test()) await contact_dialog.async_exec() - await fake_server.close() + await fake_server_with_blockchain.close() @pytest.mark.asyncio -async def test_remove_contact(application_with_one_connection, fake_server): +async def test_remove_contact(application_with_one_connection, fake_server_with_blockchain): contacts_repo = application_with_one_connection.db.contacts_repo contacts_repo.insert(Contact(currency="test_currency", pubkey="7Aqw6Efa9EzE7gtsc8SveLLrM7gm6NEGoywSv4FJx6pZ", @@ -83,11 +82,11 @@ async def test_remove_contact(application_with_one_connection, fake_server): application_with_one_connection.loop.call_later(10, close_dialog) asyncio.ensure_future(exec_test()) await contact_dialog.async_exec() - await fake_server.close() + await fake_server_with_blockchain.close() @pytest.mark.asyncio -async def test_clear_selection(application_with_one_connection, fake_server): +async def test_clear_selection(application_with_one_connection, fake_server_with_blockchain): contacts_repo = application_with_one_connection.db.contacts_repo contacts_repo.insert(Contact(currency="test_currency", pubkey="7Aqw6Efa9EzE7gtsc8SveLLrM7gm6NEGoywSv4FJx6pZ", @@ -117,4 +116,4 @@ async def test_clear_selection(application_with_one_connection, fake_server): application_with_one_connection.loop.call_later(10, close_dialog) asyncio.ensure_future(exec_test()) await contact_dialog.async_exec() - await fake_server.close() + await fake_server_with_blockchain.close() diff --git a/tests/functional/test_transfer_dialog.py b/tests/functional/test_transfer_dialog.py index f89e4024..d6f5a6de 100644 --- a/tests/functional/test_transfer_dialog.py +++ b/tests/functional/test_transfer_dialog.py @@ -8,7 +8,7 @@ from duniterpy.documents import Transaction @pytest.mark.asyncio -async def test_transfer(application_with_one_connection, simple_fake_server, bob, alice): +async def test_transfer(application_with_one_connection, fake_server_with_blockchain, bob, alice): transfer_dialog = TransferController.create(None, application_with_one_connection) def close_dialog(): @@ -27,9 +27,9 @@ async def test_transfer(application_with_one_connection, simple_fake_server, bob assert transfer_dialog.view.button_box.button(QDialogButtonBox.Ok).isEnabled() QTest.mouseClick(transfer_dialog.view.button_box.button(QDialogButtonBox.Ok), Qt.LeftButton) await asyncio.sleep(0.2) - assert isinstance(simple_fake_server.forge.pool[0], Transaction) + assert isinstance(fake_server_with_blockchain.forge.pool[0], Transaction) application_with_one_connection.loop.call_later(10, close_dialog) asyncio.ensure_future(exec_test()) await transfer_dialog.async_exec() - await simple_fake_server.close() + await fake_server_with_blockchain.close() diff --git a/tests/technical/test_blockchain_service.py b/tests/technical/test_blockchain_service.py index 22216985..ba8a872a 100644 --- a/tests/technical/test_blockchain_service.py +++ b/tests/technical/test_blockchain_service.py @@ -4,16 +4,16 @@ import pytest @pytest.mark.asyncio -async def test_new_block_with_ud(application_with_one_connection, fake_server): +async def test_new_block_with_ud(application_with_one_connection, fake_server_with_blockchain): previous_ud = application_with_one_connection.blockchain_service.previous_ud() - fake_server.forge.forge_block() - fake_server.forge.generate_dividend() - fake_server.forge.forge_block() - fake_server.forge.generate_dividend() - fake_server.forge.forge_block() - new_blocks = fake_server.forge.blocks[-3:] + fake_server_with_blockchain.forge.forge_block() + fake_server_with_blockchain.forge.generate_dividend() + fake_server_with_blockchain.forge.forge_block() + fake_server_with_blockchain.forge.generate_dividend() + fake_server_with_blockchain.forge.forge_block() + new_blocks = fake_server_with_blockchain.forge.blocks[-3:] application_with_one_connection.blockchain_service.handle_new_blocks( new_blocks) previous_ud_after_parse = application_with_one_connection.blockchain_service.previous_ud() assert previous_ud_after_parse > previous_ud - await fake_server.close() \ No newline at end of file + await fake_server_with_blockchain.close() \ No newline at end of file diff --git a/tests/technical/test_documents_service.py b/tests/technical/test_documents_service.py index 67e0ca44..1ea541d6 100644 --- a/tests/technical/test_documents_service.py +++ b/tests/technical/test_documents_service.py @@ -3,12 +3,12 @@ from sakia.data.entities import Transaction @pytest.mark.asyncio -async def test_send_more_than_40_sources(application_with_one_connection, fake_server, bob, alice): +async def test_send_more_than_40_sources(application_with_one_connection, fake_server_with_blockchain, bob, alice): for i in range(0, 60): - fake_server.forge.generate_dividend() - fake_server.forge.forge_block() + fake_server_with_blockchain.forge.generate_dividend() + fake_server_with_blockchain.forge.forge_block() - new_blocks = fake_server.forge.blocks[-60:] + new_blocks = fake_server_with_blockchain.forge.blocks[-60:] changed_tx, new_tx, new_ud = await application_with_one_connection.transactions_service.handle_new_blocks(new_blocks) await application_with_one_connection.sources_service.refresh_sources_of_pubkey(bob.key.pubkey, new_tx, new_ud, None) @@ -27,4 +27,4 @@ async def test_send_more_than_40_sources(application_with_one_connection, fake_s amount_after_send = application_with_one_connection.sources_service.amount(bob.key.pubkey) assert amount_after_send == 0 - await fake_server.close() + await fake_server_with_blockchain.close() diff --git a/tests/technical/test_identities_service.py b/tests/technical/test_identities_service.py index f450785a..f9842c6b 100644 --- a/tests/technical/test_identities_service.py +++ b/tests/technical/test_identities_service.py @@ -5,11 +5,11 @@ import pytest @pytest.mark.asyncio -async def test_new_block_with_certs(application_with_one_connection, fake_server, bob, alice): +async def test_new_block_with_certs(application_with_one_connection, fake_server_with_blockchain, bob, alice): certs_before_send = application_with_one_connection.identities_service.certifications_sent( bob.key.pubkey) - alice_user_identity = fake_server.forge.user_identities[bob.key.pubkey] - alice_identity = Identity(currency=fake_server.forge.currency, + alice_user_identity = fake_server_with_blockchain.forge.user_identities[bob.key.pubkey] + alice_identity = Identity(currency=fake_server_with_blockchain.forge.currency, pubkey=alice.key.pubkey, uid=alice.uid, blockstamp=alice_user_identity.blockstamp, @@ -22,23 +22,23 @@ async def test_new_block_with_certs(application_with_one_connection, fake_server bob.key.pubkey) assert len(certs_after_send) == len(certs_before_send) + 1 assert certs_after_send[0].written_on == -1 - assert isinstance(fake_server.forge.pool[0], Certification) - fake_server.forge.forge_block() - fake_server.forge.forge_block() - fake_server.forge.forge_block() - new_blocks = fake_server.forge.blocks[-3:] + assert isinstance(fake_server_with_blockchain.forge.pool[0], Certification) + fake_server_with_blockchain.forge.forge_block() + fake_server_with_blockchain.forge.forge_block() + fake_server_with_blockchain.forge.forge_block() + new_blocks = fake_server_with_blockchain.forge.blocks[-3:] await application_with_one_connection.identities_service.handle_new_blocks( new_blocks) certs_after_parse = application_with_one_connection.identities_service.certifications_sent( bob.key.pubkey) assert len(certs_after_parse) == len(certs_after_send) - assert certs_after_parse[0].written_on == fake_server.forge.blocks[-3].number - await fake_server.close() + assert certs_after_parse[0].written_on == fake_server_with_blockchain.forge.blocks[-3].number + await fake_server_with_blockchain.close() @pytest.mark.asyncio -async def test_new_block_with_idty(application_with_one_connection, john, simple_fake_server): - john_identity = Identity(currency=simple_fake_server.forge.currency, +async def test_new_block_with_idty(application_with_one_connection, john, fake_server_with_blockchain): + john_identity = Identity(currency=fake_server_with_blockchain.forge.currency, pubkey=john.key.pubkey, uid=john.uid, blockstamp=john.blockstamp, @@ -53,14 +53,14 @@ async def test_new_block_with_idty(application_with_one_connection, john, simple john_found = application_with_one_connection.db.identities_repo.get_one(pubkey=john_identity.pubkey) assert john_found.written is False - simple_fake_server.forge.forge_block() - simple_fake_server.forge.push(john.identity()) - simple_fake_server.forge.push(john.join(BlockUID.empty())) - simple_fake_server.forge.forge_block() - simple_fake_server.forge.forge_block() - new_blocks = simple_fake_server.forge.blocks[-3:] + fake_server_with_blockchain.forge.forge_block() + fake_server_with_blockchain.forge.push(john.identity()) + fake_server_with_blockchain.forge.push(john.join(BlockUID.empty())) + fake_server_with_blockchain.forge.forge_block() + fake_server_with_blockchain.forge.forge_block() + new_blocks = fake_server_with_blockchain.forge.blocks[-3:] await application_with_one_connection.identities_service.handle_new_blocks( new_blocks) john_found = application_with_one_connection.db.identities_repo.get_one(pubkey=john_identity.pubkey) assert john_found.written is True - await simple_fake_server.close() + await fake_server_with_blockchain.close() diff --git a/tests/technical/test_sources_service.py b/tests/technical/test_sources_service.py index bb65798c..85175606 100644 --- a/tests/technical/test_sources_service.py +++ b/tests/technical/test_sources_service.py @@ -4,58 +4,58 @@ from sakia.data.processors import TransactionsProcessor @pytest.mark.asyncio -async def test_receive_source(application_with_one_connection, fake_server, bob, alice): +async def test_receive_source(application_with_one_connection, fake_server_with_blockchain, bob, alice): amount = application_with_one_connection.sources_service.amount(bob.key.pubkey) - fake_server.forge.push(alice.send_money(150, fake_server.forge.user_identities[alice.key.pubkey].sources, bob, - fake_server.forge.blocks[-1].blockUID, "Test receive")) - fake_server.forge.forge_block() - fake_server.forge.forge_block() - fake_server.forge.forge_block() - new_blocks = fake_server.forge.blocks[-3:] + fake_server_with_blockchain.forge.push(alice.send_money(150, fake_server_with_blockchain.forge.user_identities[alice.key.pubkey].sources, bob, + fake_server_with_blockchain.forge.blocks[-1].blockUID, "Test receive")) + fake_server_with_blockchain.forge.forge_block() + fake_server_with_blockchain.forge.forge_block() + fake_server_with_blockchain.forge.forge_block() + new_blocks = fake_server_with_blockchain.forge.blocks[-3:] changed_tx, new_tx, new_ud = await application_with_one_connection.transactions_service.handle_new_blocks(new_blocks) await application_with_one_connection.sources_service.refresh_sources(new_tx, new_ud) assert amount + 150 == application_with_one_connection.sources_service.amount(bob.key.pubkey) - await fake_server.close() + await fake_server_with_blockchain.close() @pytest.mark.asyncio -async def test_send_source(application_with_one_connection, fake_server, bob, alice): +async def test_send_source(application_with_one_connection, fake_server_with_blockchain, bob, alice): amount = application_with_one_connection.sources_service.amount(bob.key.pubkey) - fake_server.forge.push(bob.send_money(150, fake_server.forge.user_identities[bob.key.pubkey].sources, alice, - fake_server.forge.blocks[-1].blockUID, "Test receive")) - fake_server.forge.forge_block() - fake_server.forge.forge_block() - fake_server.forge.forge_block() - new_blocks = fake_server.forge.blocks[-3:] + fake_server_with_blockchain.forge.push(bob.send_money(150, fake_server_with_blockchain.forge.user_identities[bob.key.pubkey].sources, alice, + fake_server_with_blockchain.forge.blocks[-1].blockUID, "Test receive")) + fake_server_with_blockchain.forge.forge_block() + fake_server_with_blockchain.forge.forge_block() + fake_server_with_blockchain.forge.forge_block() + new_blocks = fake_server_with_blockchain.forge.blocks[-3:] changed_tx, new_tx, new_ud = await application_with_one_connection.transactions_service.handle_new_blocks(new_blocks) await application_with_one_connection.sources_service.refresh_sources(new_tx, new_ud) assert amount - 150 == application_with_one_connection.sources_service.amount(bob.key.pubkey) - await fake_server.close() + await fake_server_with_blockchain.close() @pytest.mark.asyncio -async def test_destruction(application_with_one_connection, fake_server, bob, alice): +async def test_destruction(application_with_one_connection, fake_server_with_blockchain, bob, alice): amount = application_with_one_connection.sources_service.amount(bob.key.pubkey) - fake_server.forge.push(bob.send_money(amount - 80, fake_server.forge.user_identities[bob.key.pubkey].sources, alice, - fake_server.forge.blocks[-1].blockUID, "Test receive")) - fake_server.forge.forge_block() - fake_server.forge.forge_block() - fake_server.forge.forge_block() - new_blocks = fake_server.forge.blocks[-3:] + fake_server_with_blockchain.forge.push(bob.send_money(amount - 80, fake_server_with_blockchain.forge.user_identities[bob.key.pubkey].sources, alice, + fake_server_with_blockchain.forge.blocks[-1].blockUID, "Test receive")) + fake_server_with_blockchain.forge.forge_block() + fake_server_with_blockchain.forge.forge_block() + fake_server_with_blockchain.forge.forge_block() + new_blocks = fake_server_with_blockchain.forge.blocks[-3:] changed_tx, new_tx, new_ud = await application_with_one_connection.transactions_service.handle_new_blocks(new_blocks) await application_with_one_connection.sources_service.refresh_sources(new_tx, new_ud) assert 0 == application_with_one_connection.sources_service.amount(bob.key.pubkey) tx_after_parse = application_with_one_connection.transactions_service.transfers(bob.key.pubkey) assert tx_after_parse[-1].comment == "Too low balance" - await fake_server.close() + await fake_server_with_blockchain.close() @pytest.mark.asyncio -async def test_send_tx_then_cancel(application_with_one_connection, fake_server, bob, alice): +async def test_send_tx_then_cancel(application_with_one_connection, fake_server_with_blockchain, bob, alice): tx_before_send = application_with_one_connection.transactions_service.transfers(bob.key.pubkey) sources_before_send = application_with_one_connection.sources_service.amount(bob.key.pubkey) bob_connection = application_with_one_connection.db.connections_repo.get_one(pubkey=bob.key.pubkey) - fake_server.reject_next_post = True + fake_server_with_blockchain.reject_next_post = True await application_with_one_connection.documents_service.send_money(bob_connection, bob.salt, bob.password, @@ -78,4 +78,4 @@ async def test_send_tx_then_cancel(application_with_one_connection, fake_server, assert len(tx_before_send) + 1 == len(tx_after_cancel) assert sources_before_send == sources_after_cancel - await fake_server.close() \ No newline at end of file + await fake_server_with_blockchain.close() \ No newline at end of file diff --git a/tests/technical/test_transactions_service.py b/tests/technical/test_transactions_service.py index 2d45e077..12281a5e 100644 --- a/tests/technical/test_transactions_service.py +++ b/tests/technical/test_transactions_service.py @@ -3,7 +3,7 @@ from sakia.data.entities import Transaction @pytest.mark.asyncio -async def test_send_tx_then_validate(application_with_one_connection, fake_server, bob, alice): +async def test_send_tx_then_validate(application_with_one_connection, fake_server_with_blockchain, bob, alice): tx_before_send = application_with_one_connection.transactions_service.transfers(bob.key.pubkey) bob_connection = application_with_one_connection.db.connections_repo.get_one(pubkey=bob.key.pubkey) await application_with_one_connection.documents_service.send_money(bob_connection, @@ -14,45 +14,45 @@ async def test_send_tx_then_validate(application_with_one_connection, fake_serve assert len(tx_before_send) + 1 == len(tx_after_send) assert tx_after_send[-1].state is Transaction.AWAITING assert tx_after_send[-1].written_block == 0 - fake_server.forge.forge_block() - fake_server.forge.forge_block() - fake_server.forge.forge_block() - new_blocks = fake_server.forge.blocks[-3:] + fake_server_with_blockchain.forge.forge_block() + fake_server_with_blockchain.forge.forge_block() + fake_server_with_blockchain.forge.forge_block() + new_blocks = fake_server_with_blockchain.forge.blocks[-3:] await application_with_one_connection.transactions_service.handle_new_blocks(new_blocks) tx_after_parse = application_with_one_connection.transactions_service.transfers(bob.key.pubkey) assert tx_after_parse[-1].state is Transaction.VALIDATED - assert tx_after_parse[-1].written_block == fake_server.forge.blocks[-3].number - await fake_server.close() + assert tx_after_parse[-1].written_block == fake_server_with_blockchain.forge.blocks[-3].number + await fake_server_with_blockchain.close() @pytest.mark.asyncio -async def test_receive_tx(application_with_one_connection, fake_server, bob, alice): +async def test_receive_tx(application_with_one_connection, fake_server_with_blockchain, bob, alice): tx_before_send = application_with_one_connection.transactions_service.transfers(bob.key.pubkey) - fake_server.forge.push(alice.send_money(10, fake_server.forge.user_identities[alice.key.pubkey].sources, bob, - fake_server.forge.blocks[-1].blockUID, "Test receive")) - fake_server.forge.forge_block() - fake_server.forge.forge_block() - fake_server.forge.forge_block() - new_blocks = fake_server.forge.blocks[-3:] + fake_server_with_blockchain.forge.push(alice.send_money(10, fake_server_with_blockchain.forge.user_identities[alice.key.pubkey].sources, bob, + fake_server_with_blockchain.forge.blocks[-1].blockUID, "Test receive")) + fake_server_with_blockchain.forge.forge_block() + fake_server_with_blockchain.forge.forge_block() + fake_server_with_blockchain.forge.forge_block() + new_blocks = fake_server_with_blockchain.forge.blocks[-3:] await application_with_one_connection.transactions_service.handle_new_blocks(new_blocks) tx_after_parse = application_with_one_connection.transactions_service.transfers(bob.key.pubkey) assert tx_after_parse[-1].state is Transaction.VALIDATED assert len(tx_before_send) + 1 == len(tx_after_parse) - await fake_server.close() + await fake_server_with_blockchain.close() @pytest.mark.asyncio -async def test_issue_dividend(application_with_one_connection, fake_server, bob): +async def test_issue_dividend(application_with_one_connection, fake_server_with_blockchain, bob): dividends_before_send = application_with_one_connection.transactions_service.dividends(bob.key.pubkey) - fake_server.forge.forge_block() - fake_server.forge.generate_dividend() - fake_server.forge.forge_block() - fake_server.forge.forge_block() - fake_server.forge.generate_dividend() - fake_server.forge.forge_block() - fake_server.forge.forge_block() - new_blocks = fake_server.forge.blocks[-5:] + fake_server_with_blockchain.forge.forge_block() + fake_server_with_blockchain.forge.generate_dividend() + fake_server_with_blockchain.forge.forge_block() + fake_server_with_blockchain.forge.forge_block() + fake_server_with_blockchain.forge.generate_dividend() + fake_server_with_blockchain.forge.forge_block() + fake_server_with_blockchain.forge.forge_block() + new_blocks = fake_server_with_blockchain.forge.blocks[-5:] await application_with_one_connection.transactions_service.handle_new_blocks(new_blocks) dividends_after_parse = application_with_one_connection.transactions_service.dividends(bob.key.pubkey) assert len(dividends_before_send) + 2 == len(dividends_after_parse) - await fake_server.close() + await fake_server_with_blockchain.close() diff --git a/tests/unit/money/test_relative_zsum.py b/tests/unit/money/test_relative_zsum.py index 7aad08cf..e9f11b1f 100644 --- a/tests/unit/money/test_relative_zsum.py +++ b/tests/unit/money/test_relative_zsum.py @@ -14,7 +14,7 @@ def test_differential(application_with_one_connection, bob): assert value == approx(-3.521619496) -def test_localized_no_si(application_with_one_connection, fake_server, bob): +def test_localized_no_si(application_with_one_connection, bob): referential = RelativeZSum(110, bob.currency, application_with_one_connection, None) value = referential.localized(units=True) assert value == "-3.53 R0 UD" -- GitLab