Skip to content
Snippets Groups Projects
Commit 3bfbd83c authored by inso's avatar inso
Browse files

New tests for account, community, wallet

parent 69f61721
No related branches found
No related tags found
No related merge requests found
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()
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()
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()
...@@ -7,8 +7,7 @@ from PyQt5.QtNetwork import QNetworkAccessManager ...@@ -7,8 +7,7 @@ from PyQt5.QtNetwork import QNetworkAccessManager
from cutecoin.gui.mainwindow import MainWindow from cutecoin.gui.mainwindow import MainWindow
from cutecoin.core.app import Application from cutecoin.core.app import Application
from cutecoin.tests import get_application from cutecoin.tests import get_application
from cutecoin.core.registry.identities import IdentitiesRegistry
from cutecoin.tests.stubs.core.registry import IdentitiesRegistry
# Qapplication cause a core dumped when re-run in setup # Qapplication cause a core dumped when re-run in setup
# set it as global var # set it as global var
......
from .network import Network
\ No newline at end of file
"""
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
from .identities import IdentitiesRegistry
from .identity import Identity
\ No newline at end of file
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()
"""
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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment