From 3bfbd83c05aa5736ec25fa23f641ed6748fdb8c5 Mon Sep 17 00:00:00 2001 From: Inso <insomniak.fr@gmail.com> Date: Sat, 31 Oct 2015 08:48:16 +0100 Subject: [PATCH] New tests for account, community, wallet --- src/cutecoin/tests/core/account.py | 40 ++++++ src/cutecoin/tests/core/community.py | 40 ++++++ src/cutecoin/tests/core/wallet.py | 40 ++++++ .../main_window/test_main_window_dialogs.py | 3 +- src/cutecoin/tests/stubs/__init__.py | 0 src/cutecoin/tests/stubs/core/__init__.py | 0 src/cutecoin/tests/stubs/core/net/__init__.py | 1 - src/cutecoin/tests/stubs/core/net/network.py | 97 --------------- .../tests/stubs/core/registry/__init__.py | 2 - .../tests/stubs/core/registry/identities.py | 29 ----- .../tests/stubs/core/registry/identity.py | 114 ------------------ 11 files changed, 121 insertions(+), 245 deletions(-) create mode 100644 src/cutecoin/tests/core/account.py create mode 100644 src/cutecoin/tests/core/community.py create mode 100644 src/cutecoin/tests/core/wallet.py delete mode 100644 src/cutecoin/tests/stubs/__init__.py delete mode 100644 src/cutecoin/tests/stubs/core/__init__.py delete mode 100644 src/cutecoin/tests/stubs/core/net/__init__.py delete mode 100644 src/cutecoin/tests/stubs/core/net/network.py delete mode 100644 src/cutecoin/tests/stubs/core/registry/__init__.py delete mode 100644 src/cutecoin/tests/stubs/core/registry/identities.py delete mode 100644 src/cutecoin/tests/stubs/core/registry/identity.py diff --git a/src/cutecoin/tests/core/account.py b/src/cutecoin/tests/core/account.py new file mode 100644 index 00000000..4fbbf67e --- /dev/null +++ b/src/cutecoin/tests/core/account.py @@ -0,0 +1,40 @@ +import sys +import unittest +import asyncio +import quamash +import logging +from PyQt5.QtCore import QLocale +from cutecoin.core.registry.identities import IdentitiesRegistry +from cutecoin.core import Account +from cutecoin.tests import get_application + + +class TestAccount(unittest.TestCase): + def setUp(self): + self.qapplication = get_application() + QLocale.setDefault(QLocale("en_GB")) + self.lp = quamash.QEventLoop(self.qapplication) + asyncio.set_event_loop(self.lp) + self.identities_registry = IdentitiesRegistry() + + def tearDown(self): + try: + self.lp.close() + finally: + asyncio.set_event_loop(None) + + def test_load_save_account(self): + account = Account("test_salt", "HnFcSms8jzwngtVomTTnzudZx7SHUQY8sVE1y8yBmULk", + "test_uid", [], [], [], self.identities_registry) + json_data = account.jsonify() + account_from_json = Account.load(json_data, self.identities_registry) + + self.assertEqual(account.name, account_from_json.name) + self.assertEqual(account.pubkey, account_from_json.pubkey) + self.assertEqual(len(account.communities), len(account_from_json.communities)) + self.assertEqual(len(account.wallets), len(account.wallets)) + +if __name__ == '__main__': + logging.basicConfig(stream=sys.stderr) + logging.getLogger().setLevel(logging.DEBUG) + unittest.main() diff --git a/src/cutecoin/tests/core/community.py b/src/cutecoin/tests/core/community.py new file mode 100644 index 00000000..97ff383e --- /dev/null +++ b/src/cutecoin/tests/core/community.py @@ -0,0 +1,40 @@ +import sys +import unittest +import asyncio +import quamash +import logging +from PyQt5.QtCore import QLocale +from cutecoin.core.registry.identities import IdentitiesRegistry +from cutecoin.core.net.api.bma.access import BmaAccess +from cutecoin.core.net.network import Network +from cutecoin.core import Community +from cutecoin.tests import get_application + + +class TestCommunity(unittest.TestCase): + def setUp(self): + self.qapplication = get_application() + QLocale.setDefault(QLocale("en_GB")) + self.lp = quamash.QEventLoop(self.qapplication) + asyncio.set_event_loop(self.lp) + + def tearDown(self): + try: + self.lp.close() + finally: + asyncio.set_event_loop(None) + + def test_load_save_community(self): + network = Network("test_currency", []) + bma_access = BmaAccess([], network) + community = Community("test_currency", network, bma_access) + + json_data = community.jsonify() + community_from_json = Community.load(json_data) + self.assertEqual(community.name, community_from_json.name) + self.assertEqual(len(community.network._nodes), len(community_from_json.network._nodes)) + +if __name__ == '__main__': + logging.basicConfig(stream=sys.stderr) + logging.getLogger().setLevel(logging.DEBUG) + unittest.main() diff --git a/src/cutecoin/tests/core/wallet.py b/src/cutecoin/tests/core/wallet.py new file mode 100644 index 00000000..05db39fe --- /dev/null +++ b/src/cutecoin/tests/core/wallet.py @@ -0,0 +1,40 @@ +import sys +import unittest +import asyncio +import quamash +import logging +from PyQt5.QtCore import QLocale +from cutecoin.core.registry.identities import IdentitiesRegistry +from cutecoin.core import Wallet +from cutecoin.tests import get_application + + +class TestWallet(unittest.TestCase): + def setUp(self): + self.qapplication = get_application() + QLocale.setDefault(QLocale("en_GB")) + self.lp = quamash.QEventLoop(self.qapplication) + asyncio.set_event_loop(self.lp) + self.identities_registry = IdentitiesRegistry({}) + + def tearDown(self): + try: + self.lp.close() + finally: + asyncio.set_event_loop(None) + + def test_load_save_wallet(self): + wallet = Wallet(0, "7Aqw6Efa9EzE7gtsc8SveLLrM7gm6NEGoywSv4FJx6pZ", + "Wallet 1", self.identities_registry) + + json_data = wallet.jsonify() + wallet_from_json = Wallet.load(json_data, self.identities_registry) + self.assertEqual(wallet.walletid, wallet_from_json.walletid) + self.assertEqual(wallet.pubkey, wallet_from_json.pubkey) + self.assertEqual(wallet.name, wallet_from_json.name) + self.assertEqual(wallet._identities_registry, wallet_from_json._identities_registry) + +if __name__ == '__main__': + logging.basicConfig(stream=sys.stderr) + logging.getLogger().setLevel(logging.DEBUG) + unittest.main() diff --git a/src/cutecoin/tests/gui/main_window/test_main_window_dialogs.py b/src/cutecoin/tests/gui/main_window/test_main_window_dialogs.py index 29051bf6..a3f55041 100644 --- a/src/cutecoin/tests/gui/main_window/test_main_window_dialogs.py +++ b/src/cutecoin/tests/gui/main_window/test_main_window_dialogs.py @@ -7,8 +7,7 @@ from PyQt5.QtNetwork import QNetworkAccessManager from cutecoin.gui.mainwindow import MainWindow from cutecoin.core.app import Application from cutecoin.tests import get_application - -from cutecoin.tests.stubs.core.registry import IdentitiesRegistry +from cutecoin.core.registry.identities import IdentitiesRegistry # Qapplication cause a core dumped when re-run in setup # set it as global var diff --git a/src/cutecoin/tests/stubs/__init__.py b/src/cutecoin/tests/stubs/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/src/cutecoin/tests/stubs/core/__init__.py b/src/cutecoin/tests/stubs/core/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/src/cutecoin/tests/stubs/core/net/__init__.py b/src/cutecoin/tests/stubs/core/net/__init__.py deleted file mode 100644 index 8779e25f..00000000 --- a/src/cutecoin/tests/stubs/core/net/__init__.py +++ /dev/null @@ -1 +0,0 @@ -from .network import Network \ No newline at end of file diff --git a/src/cutecoin/tests/stubs/core/net/network.py b/src/cutecoin/tests/stubs/core/net/network.py deleted file mode 100644 index d37d8adf..00000000 --- a/src/cutecoin/tests/stubs/core/net/network.py +++ /dev/null @@ -1,97 +0,0 @@ -""" -Created on 24 févr. 2015 - -@author: inso -""" -import asyncio - -from PyQt5.QtCore import pyqtSignal, pyqtSlot, QObject - - -class Network(QObject): - """ - A network is managing nodes polling and crawling of a - given community. - """ - nodes_changed = pyqtSignal() - new_block_mined = pyqtSignal(int) - - def __init__(self, currency, nodes): - """ - Constructor of a network - - :param str currency: The currency name of the community - :param list nodes: The root nodes of the network - """ - super().__init__() - self.currency = currency - - @classmethod - def create(cls, node): - nodes = [node] - network = cls(node.currency, nodes) - return network - - def merge_with_json(self, json_data): - pass - - @classmethod - def from_json(cls, currency, json_data): - nodes = [] - network = cls(currency, nodes) - return network - - def jsonify(self): - data = [] - return data - - @property - def quality(self): - return 0.33 - - def stop_coroutines(self): - pass - - def continue_crawling(self): - return False - - @property - def synced_nodes(self): - return self.nodes - - @property - def online_nodes(self): - return self.nodes - - @property - def nodes(self): - """ - Get all knew nodes. - """ - return self._nodes - - @property - def root_nodes(self): - return self._root_nodes - - @property - def latest_block(self): - return 20000 - - def add_node(self, node): - pass - - def add_root_node(self, node): - pass - - def remove_root_node(self, index):pass - - def is_root_node(self, node): - return True - - def root_node_index(self, index): - return self.nodes[0] - - @asyncio.coroutine - def discover_network(self): - pass diff --git a/src/cutecoin/tests/stubs/core/registry/__init__.py b/src/cutecoin/tests/stubs/core/registry/__init__.py deleted file mode 100644 index 4ab046a1..00000000 --- a/src/cutecoin/tests/stubs/core/registry/__init__.py +++ /dev/null @@ -1,2 +0,0 @@ -from .identities import IdentitiesRegistry -from .identity import Identity \ No newline at end of file diff --git a/src/cutecoin/tests/stubs/core/registry/identities.py b/src/cutecoin/tests/stubs/core/registry/identities.py deleted file mode 100644 index 1a2a90dd..00000000 --- a/src/cutecoin/tests/stubs/core/registry/identities.py +++ /dev/null @@ -1,29 +0,0 @@ -from PyQt5.QtCore import QObject, pyqtSlot, pyqtSignal, QTimer -from ucoinpy.api import bma -from .identity import Identity - -import asyncio - - -class IdentitiesRegistry: - def __init__(self, instances={}): - pass - - def load_json(self, json_data): - pass - - def jsonify(self): - return {'registry': []} - - def lookup(self, pubkey, community): - identity = Identity.empty(pubkey) - return identity - - @asyncio.coroutine - def future_find(self, pubkey, community): - identity = Identity.empty(pubkey) - yield from asyncio.sleep(1) - return identity - - def from_metadata(self, metadata): - return Identity() diff --git a/src/cutecoin/tests/stubs/core/registry/identity.py b/src/cutecoin/tests/stubs/core/registry/identity.py deleted file mode 100644 index 9c3faee1..00000000 --- a/src/cutecoin/tests/stubs/core/registry/identity.py +++ /dev/null @@ -1,114 +0,0 @@ -""" -Created on 11 févr. 2014 - -@author: inso -""" - -import logging -import time -import asyncio - -from ucoinpy.documents.certification import SelfCertification -from cutecoin.tools.exceptions import Error, NoPeerAvailable,\ - MembershipNotFoundError -from ucoinpy.api import bma -from ucoinpy.api.bma import PROTOCOL_VERSION -from PyQt5.QtCore import QObject, pyqtSignal - - -class Identity(QObject): - """ - A person with a uid and a pubkey - """ - FOUND = 1 - NOT_FOUND = 0 - - inner_data_changed = pyqtSignal(str) - - def __init__(self, uid, pubkey, status): - """ - Initializing a person object. - - :param str uid: The person uid, also known as its uid on the network - :param str pubkey: The person pubkey - :param int status: The local status of the identity - """ - super().__init__() - assert(status in (Identity.FOUND, Identity.NOT_FOUND)) - self.uid = uid - self.pubkey = pubkey - self.status = status - - @classmethod - def empty(cls, pubkey): - return cls("", pubkey, Identity.NOT_FOUND) - - @classmethod - def from_metadata(cls, metadata): - return cls(metadata["text"], metadata["id"], Identity.NOT_FOUND) - - @classmethod - def from_json(cls, json_data): - """ - Create a person from json data - - :param dict json_data: The person as a dict in json format - :return: A new person if pubkey wasn't known, else a new person instance. - """ - pubkey = json_data['pubkey'] - uid = json_data['uid'] - status = json_data['status'] - - return cls(uid, pubkey, status) - - @asyncio.coroutine - def selfcert(self, community): - yield from asyncio.sleep(1) - return None - - def get_join_date(self, community): - return time.time() + 100000000 - - def get_expiration_date(self, community): - return time.time() + 1000000000 - - def membership(self, community): - raise MembershipNotFoundError() - - def published_uid(self, community): - return False - - def is_member(self, community): - return False - - def certifiers_of(self, community): - return list() - - def unique_valid_certifiers_of(self, community): - return list() - - def certified_by(self, community): - return list() - - def unique_valid_certified_by(self, community): - return list() - - def membership_expiration_time(self, community): - current_time = time.time() - return current_time+1000000 - - def jsonify(self): - """ - Get the community as dict in json format. - :return: The community as a dict in json format - """ - data = {'uid': self.uid, - 'pubkey': self.pubkey, - 'status': self.status} - return data - - def __str__(self): - status_str = ("NOT_FOUND", "FOUND") - return "{0} - {1} - {2}".format(self.uid, - self.pubkey, - status_str[self.status]) \ No newline at end of file -- GitLab