diff --git a/ci/travis/test.sh b/ci/travis/test.sh index 2ef4409d195aeceeaf55fceaf6a4846d1ec71ab5..fff3ec06f80b27955f527aaa30e008c28736358c 100755 --- a/ci/travis/test.sh +++ b/ci/travis/test.sh @@ -14,9 +14,9 @@ cd $HOME/build/duniter/sakia pyenv shell $PYENV_PYTHON_VERSION if [ $TRAVIS_OS_NAME == "linux" ] then - coverage run --source=sakia.core,sakia.gui,sakia.models setup.py test + coverage run --source=sakia.core,sakia.gui,sakia.models pytest else - python setup.py test + pytest fi diff --git a/src/sakia/app.py b/src/sakia/app.py index d4fa548bb44390e326c2b38ee6b256dff0b1db1f..5797ead42b9786034e6be6562e3c01751dd8019c 100644 --- a/src/sakia/app.py +++ b/src/sakia/app.py @@ -80,7 +80,7 @@ class Application(QObject): app.load_profile(app_data.default) app.start_coroutines() app.documents_service = DocumentsService.instanciate(app) - #app.switch_language() + app.switch_language() return app def load_profile(self, profile_name): diff --git a/src/sakia/money/quantitative.py b/src/sakia/money/quantitative.py index 62315be2b5f249550dde4eb0b066fc67a8a5bd97..1f016128201c9a666015acbf365c608469b183eb 100644 --- a/src/sakia/money/quantitative.py +++ b/src/sakia/money/quantitative.py @@ -57,7 +57,21 @@ class Quantitative(BaseReferential): @staticmethod def to_si(value, digits): - prefixes = ['', 'k', 'M', 'G', 'Tera', 'Peta', 'Exa', 'Zeta', 'Yotta'] + unicodes = { + '0': ord('\u2070'), + '1': ord('\u00B9'), + '2': ord('\u00B2'), + '3': ord('\u00B3'), + } + for n in range(4, 10): + unicodes[str(n)] = ord('\u2070') + n + + exponent = 0 + scientific_value = value + while scientific_value > 1000: + exponent += 3 + scientific_value /= 1000 + if value < 0: value = -value multiplier = -1 @@ -65,20 +79,18 @@ class Quantitative(BaseReferential): multiplier = 1 scientific_value = value - prefix_index = 0 - prefix = "" + exponent = 0 while scientific_value > 1000: - prefix_index += 1 + exponent += 3 scientific_value /= 1000 - if prefix_index < len(prefixes): - prefix = prefixes[prefix_index] + if exponent > 1: localized_value = QLocale().toString(float(scientific_value * multiplier), 'f', digits) else: localized_value = QLocale().toString(float(value * multiplier), 'f', 0) - return localized_value, prefix + return localized_value, "x10" + "".join([chr(unicodes[e]) for e in str(exponent)]) def localized(self, units=False, international_system=False): value = self.value() @@ -93,7 +105,7 @@ class Quantitative(BaseReferential): Quantitative._REF_STR_) \ .format(localized_value, prefix, - shortened(self.currency) if units else "") + " " if prefix else "" + shortened(self.currency) if units else "") else: return localized_value diff --git a/src/sakia/tests/conftest.py b/src/sakia/tests/conftest.py index 917c07443a4c651608a2ab9c4fef776c30f5b79a..da9bd951bfc237e652f99b93141a6d6faf3c128c 100644 --- a/src/sakia/tests/conftest.py +++ b/src/sakia/tests/conftest.py @@ -74,27 +74,27 @@ def application(event_loop, meta_repo, sakia_options, app_data, user_parameters) @pytest.fixture def fake_server(event_loop): - return event_loop.run_until_complete(mirage.Node.start(None, "testcurrency", "12356", "123456", event_loop)) + return event_loop.run_until_complete(mirage.Node.start(None, "test_currency", "12356", "123456", event_loop)) @pytest.fixture def alice(): - return mirage.User.create("testcurrency", "alice", "alicesalt", "alicepassword", BlockUID.empty()) + return mirage.User.create("test_currency", "alice", "alicesalt", "alicepassword", BlockUID.empty()) @pytest.fixture def bob(): - return mirage.User.create("testcurrency", "bob", "bobsalt", "bobpassword", BlockUID.empty()) + return mirage.User.create("test_currency", "bob", "bobsalt", "bobpassword", BlockUID.empty()) @pytest.fixture def wrong_bob_uid(): - return mirage.User.create("testcurrency", "wrong_bob", "bobsalt", "bobpassword", BlockUID.empty()) + return mirage.User.create("test_currency", "wrong_bob", "bobsalt", "bobpassword", BlockUID.empty()) @pytest.fixture def wrong_bob_pubkey(): - return mirage.User.create("testcurrency", "bob", "wrongbobsalt", "bobpassword", BlockUID.empty()) + return mirage.User.create("test_currency", "bob", "wrongbobsalt", "bobpassword", BlockUID.empty()) @pytest.fixture @@ -123,7 +123,7 @@ def application_with_one_connection(application, simple_fake_server, bob): 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] - connection = Connection(currency="testcurrency", + connection = Connection(currency="test_currency", pubkey=bob.key.pubkey, salt=bob.salt, uid=bob.uid, scrypt_N=4096, scrypt_r=4, scrypt_p=2, @@ -182,6 +182,8 @@ def application_with_one_connection(application, simple_fake_server, bob): state=Node.ONLINE, software="duniter", version="0.40.2")) + application.switch_language() + return application diff --git a/src/sakia/tests/unit/core/graph/__init__.py b/src/sakia/tests/unit/core/graph/__init__.py deleted file mode 100644 index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000 diff --git a/src/sakia/tests/unit/core/graph/test_base_graph.py b/src/sakia/tests/unit/core/graph/test_base_graph.py deleted file mode 100644 index 193d715c6b8f5fd10e93953f7a9de77823d8f112..0000000000000000000000000000000000000000 --- a/src/sakia/tests/unit/core/graph/test_base_graph.py +++ /dev/null @@ -1,196 +0,0 @@ -import unittest - -from PyQt5.QtCore import QLocale -from asynctest.mock import Mock, CoroutineMock, patch -from sakia.core.graph import BaseGraph - -from sakia.data.graphs.constants import EdgeStatus, NodeStatus -from sakia.tests import QuamashTest - - -class TestBaseGraph(unittest.TestCase, QuamashTest): - def setUp(self): - self.setUpQuamash() - QLocale.setDefault(QLocale("en_GB")) - - self.account_identity = Mock(specs='core.registry.Identity') - self.account_identity.pubkey = "HnFcSms8jzwngtVomTTnzudZx7SHUQY8sVE1y8yBmULk" - self.account_identity.uid = "account_identity" - self.account_identity.is_member = CoroutineMock(spec='core.registry.Identity.is_member', return_value=True) - - self.first_identity = Mock(specs='core.registry.Identity') - self.first_identity.pubkey = "7Aqw6Efa9EzE7gtsc8SveLLrM7gm6NEGoywSv4FJx6pZ" - self.first_identity.uid = "first_identity" - self.first_identity.is_member = CoroutineMock(spec='core.registry.Identity.is_member', return_value=True) - - self.second_identity = Mock(specs='core.registry.Identity') - self.second_identity.pubkey = "FADxcH5LmXGmGFgdixSes6nWnC4Vb4pRUBYT81zQRhjn" - self.second_identity.uid = "second_uid" - self.second_identity.is_member = CoroutineMock(spec='core.registry.Identity.is_member', return_value=False) - - def tearDown(self): - self.tearDownQuamash() - - @patch('sakia.core.Community') - @patch('time.time', Mock(return_value=50000)) - def test_arc_status(self, community): - community.parameters = CoroutineMock(return_value = {'sigValidity': 1000}) - app = Mock() - - base_graph = BaseGraph(app, community) - - async def exec_test(): - self.assertEquals((await base_graph.arc_status(48000)), EdgeStatus.WEAK) - self.assertEquals((await base_graph.arc_status(49500)), EdgeStatus.STRONG) - self.assertEquals((await base_graph.arc_status(49200)), EdgeStatus.WEAK) - - self.lp.run_until_complete(exec_test()) - - @patch('sakia.core.Application') - @patch('sakia.core.Community') - def test_node_status_member(self, app, community): - community.parameters = CoroutineMock(return_value = {'sigValidity': 1000}) - - base_graph = BaseGraph(app, community) - certifier = Mock(specs='core.registry.Identity') - certifier.pubkey = "7Aqw6Efa9EzE7gtsc8SveLLrM7gm6NEGoywSv4FJx6pZ" - certifier.uid = "first_identity" - certifier.is_member = CoroutineMock(spec='core.registry.Identity.is_member', return_value=False) - - account_identity = Mock(specs='core.registry.Identity') - account_identity.pubkey = "FADxcH5LmXGmGFgdixSes6nWnC4Vb4pRUBYT81zQRhjn" - account_identity.uid = "second_uid" - account_identity.is_member = CoroutineMock(spec='core.registry.Identity.is_member', return_value=True) - - async def exec_test(): - self.assertEquals((await base_graph.node_status(certifier, account_identity)), NodeStatus.OUT) - self.assertEquals((await base_graph.node_status(account_identity, account_identity)), NodeStatus.HIGHLIGHTED) - - self.lp.run_until_complete(exec_test()) - - @patch('sakia.core.Application') - @patch('sakia.core.Community') - def test_confirmation_text_expert_enabled(self, app, community): - community.network.confirmations = Mock(return_value=2) - app.preferences = {'expert_mode': True} - - base_graph = BaseGraph(app, community) - - self.assertEquals(base_graph.confirmation_text(200), "2/6") - - @patch('sakia.core.Application') - @patch('sakia.core.Community') - def test_confirmation_text_expert_disabled(self, app, community): - community.network.confirmations = Mock(return_value=2) - app.preferences = {'expert_mode': False} - - base_graph = BaseGraph(app, community) - - self.assertEquals(base_graph.confirmation_text(200), "33 %") - - @patch('sakia.core.Community') - @patch('sakia.core.Application') - @patch('time.time', Mock(return_value=50000)) - def test_add_identitiers(self, app, community): - community.parameters = CoroutineMock(return_value = {'sigValidity': 1000}) - community.network.confirmations = Mock(side_effect=lambda n: 4 if 996 else None) - app.preferences = {'expert_mode': True} - - base_graph = BaseGraph(app, community) - - certifications = [ - { - 'identity': self.first_identity, - 'cert_time': 49100, - 'block_number': 900 - }, - { - 'identity': self.second_identity, - 'cert_time': 49800, - 'block_number': 996 - } - ] - async def exec_test(): - await base_graph.add_certifier_list(certifications, self.account_identity, self.account_identity) - self.assertEqual(len(base_graph.nx_graph.nodes()), 3) - self.assertEqual(len(base_graph.nx_graph.edges()), 2) - nodes = base_graph.nx_graph.nodes(data=True) - edges = base_graph.nx_graph.edges(data=True) - - first_node = [n for n in nodes if n[0] == self.first_identity.pubkey][0] - self.assertEqual(first_node[1]['status'], NodeStatus.NEUTRAL) - self.assertEqual(first_node[1]['text'], certifications[0]['identity'].uid) - self.assertEqual(first_node[1]['tooltip'], certifications[0]['identity'].pubkey) - - second_node = [n for n in nodes if n[0] == self.second_identity.pubkey][0] - self.assertEqual(second_node[1]['status'], NodeStatus.OUT) - self.assertEqual(second_node[1]['text'], certifications[1]['identity'].uid) - self.assertEqual(second_node[1]['tooltip'], certifications[1]['identity'].pubkey) - - arc_from_first = [e for e in edges if e[0] == self.first_identity.pubkey][0] - self.assertEqual(arc_from_first[1], self.account_identity.pubkey) - self.assertEqual(arc_from_first[2]['status'], EdgeStatus.WEAK) - self.assertEqual(arc_from_first[2]['cert_time'], certifications[0]['cert_time']) - - arc_from_second = [e for e in edges if e[0] == self.second_identity.pubkey][0] - self.assertEqual(arc_from_second[1], self.account_identity.pubkey) - self.assertEqual(arc_from_second[2]['status'], EdgeStatus.STRONG) - self.assertEqual(arc_from_second[2]['cert_time'], certifications[1]['cert_time']) - - self.lp.run_until_complete(exec_test()) - - @patch('sakia.core.Community') - @patch('sakia.core.Application') - @patch('time.time', Mock(return_value=50000)) - def test_add_certified(self, app, community): - community.parameters = CoroutineMock(return_value = {'sigValidity': 1000}) - community.network.confirmations = Mock(side_effect=lambda n: 4 if 996 else None) - app.preferences = {'expert_mode': True} - - base_graph = BaseGraph(app, community) - - certifications = [ - { - 'identity': self.first_identity, - 'cert_time': 49100, - 'block_number': 900 - }, - { - 'identity': self.second_identity, - 'cert_time': 49800, - 'block_number': 996 - } - ] - async def exec_test(): - await base_graph.add_certified_list(certifications, self.account_identity, self.account_identity) - self.assertEqual(len(base_graph.nx_graph.nodes()), 3) - self.assertEqual(len(base_graph.nx_graph.edges()), 2) - nodes = base_graph.nx_graph.nodes(data=True) - first_node = [n for n in nodes if n[0] == self.first_identity.pubkey][0] - self.assertEqual(first_node[1]['status'], NodeStatus.NEUTRAL) - self.assertEqual(first_node[1]['text'], certifications[0]['identity'].uid) - self.assertEqual(first_node[1]['tooltip'], certifications[0]['identity'].pubkey) - - second_node = [n for n in nodes if n[0] == self.second_identity.pubkey][0] - self.assertEqual(second_node[1]['status'], NodeStatus.OUT) - self.assertEqual(second_node[1]['text'], certifications[1]['identity'].uid) - self.assertEqual(second_node[1]['tooltip'], certifications[1]['identity'].pubkey) - - self.lp.run_until_complete(exec_test()) - - @patch('sakia.core.Community') - @patch('sakia.core.Application') - @patch('time.time', Mock(return_value=50000)) - def test_add_identity(self, app, community): - app.preferences = {'expert_mode': True} - - base_graph = BaseGraph(app, community) - - base_graph.add_identity(self.account_identity, NodeStatus.HIGHLIGHTED) - self.assertEqual(len(base_graph.nx_graph.nodes()), 1) - self.assertEqual(len(base_graph.nx_graph.edges()), 0) - nodes = base_graph.nx_graph.nodes(data=True) - account_node = [n for n in nodes if n[0] == self.account_identity.pubkey][0] - self.assertEqual(account_node[1]['status'], NodeStatus.HIGHLIGHTED) - self.assertEqual(account_node[1]['text'], self.account_identity.uid) - self.assertEqual(account_node[1]['tooltip'], self.account_identity.pubkey) diff --git a/src/sakia/tests/unit/core/graph/test_explorer_graph.py b/src/sakia/tests/unit/core/graph/test_explorer_graph.py deleted file mode 100644 index 5f1f5e5481f329466c0c04fd017f02ff382b3bad..0000000000000000000000000000000000000000 --- a/src/sakia/tests/unit/core/graph/test_explorer_graph.py +++ /dev/null @@ -1,211 +0,0 @@ -import asyncio -import unittest - -from PyQt5.QtCore import QLocale -from asynctest.mock import Mock, CoroutineMock, patch - -from sakia.data.graphs import ExplorerGraph -from sakia.tests import QuamashTest - - -class TestExplorerGraph(unittest.TestCase, QuamashTest): - def setUp(self): - self.setUpQuamash() - QLocale.setDefault(QLocale("en_GB")) - - ## Graph to test : - ## - E - ## A - B - C - D - ## - ## Path : Between A and C - - self.idA = Mock(specs='core.registry.Identity') - self.idA.pubkey = "HnFcSms8jzwngtVomTTnzudZx7SHUQY8sVE1y8yBmULk" - self.idA.uid = "A" - self.idA.is_member = CoroutineMock(spec='core.registry.Identity.is_member', return_value=True) - - self.idB = Mock(specs='core.registry.Identity') - self.idB.pubkey = "7Aqw6Efa9EzE7gtsc8SveLLrM7gm6NEGoywSv4FJx6pZ" - self.idB.uid = "B" - self.idB.is_member = CoroutineMock(spec='core.registry.Identity.is_member', return_value=True) - - self.idC = Mock(specs='core.registry.Identity') - self.idC.pubkey = "FADxcH5LmXGmGFgdixSes6nWnC4Vb4pRUBYT81zQRhjn" - self.idC.uid = "C" - self.idC.is_member = CoroutineMock(spec='core.registry.Identity.is_member', return_value=False) - - self.idD = Mock(specs='core.registry.Identity') - self.idD.pubkey = "6R11KGpG6w5Z6JfiwaPf3k4BCMY4dwhjCdmjGpvn7Gz5" - self.idD.uid = "D" - self.idD.is_member = CoroutineMock(spec='core.registry.Identity.is_member', return_value=True) - - self.idE = Mock(specs='core.registry.Identity') - self.idE.pubkey = "CZVDEsM6pPNxhAvXApGM8MJ6ExBZVpc8PNVyDZ7hKxLu" - self.idE.uid = "E" - self.idE.is_member = CoroutineMock(spec='core.registry.Identity.is_member', return_value=False) - - self.idA.unique_valid_certified_by = CoroutineMock(spec='core.registry.Identity.certified_by', - return_value=[ - { - 'cert_time': 49800, - 'identity': self.idB, - 'block_number': 996 - } - ]) - self.idA.unique_valid_certifiers_of = CoroutineMock(spec='core.registry.Identity.certifiers_of', - return_value=[]) - - self.idB.unique_valid_certified_by = CoroutineMock(spec='core.registry.Identity.certified_by', - return_value=[ - { - 'cert_time': 49100, - 'identity': self.idC, - 'block_number': 990 - } - ]) - - self.idB.unique_valid_certifiers_of = CoroutineMock(spec='core.registry.Identity.certifiers_of', - return_value=[ - { - 'cert_time': 49800, - 'identity': self.idA, - 'block_number': 996 - } - ]) - - self.idC.unique_valid_certified_by = CoroutineMock(spec='core.registry.Identity.certified_by', - return_value=[ - { - 'cert_time': 49100, - 'identity': self.idD, - 'block_number': 990 - }, - { - 'cert_time': 49110, - 'identity': self.idE, - 'block_number': 990 - } - ]) - - self.idC.unique_valid_certifiers_of = CoroutineMock(spec='core.registry.Identity.certifiers_of', - return_value=[ - { - 'cert_time': 49100, - 'identity': self.idB, - 'block_number': 990 - } - ]) - - self.idD.unique_valid_certified_by = CoroutineMock(spec='core.registry.Identity.certified_by', - return_value=[ - ]) - self.idD.unique_valid_certifiers_of = CoroutineMock(spec='core.registry.Identity.certifiers_of', - return_value=[ - { - 'cert_time': 49100, - 'identity': self.idC, - 'block_number': 990 - }]) - - self.idE.unique_valid_certified_by = CoroutineMock(spec='core.registry.Identity.certified_by', - return_value=[ - ]) - self.idE.unique_valid_certifiers_of = CoroutineMock(spec='core.registry.Identity.certifiers_of', - return_value=[ - { - 'cert_time': 49100, - 'identity': self.idC, - 'block_number': 990 - }]) - - def tearDown(self): - self.tearDownQuamash() - - @patch('sakia.core.Community') - @patch('sakia.core.Application') - @patch('time.time', Mock(return_value=50000)) - def test_explore_full_from_center(self, app, community): - community.parameters = CoroutineMock(return_value = {'sigValidity': 1000}) - community.network.confirmations = Mock(side_effect=lambda n: 4 if 996 else None) - community.nb_members = CoroutineMock(return_value = 3) - app.preferences = {'expert_mode': True} - - explorer_graph = ExplorerGraph(app, community) - - async def exec_test(): - await explorer_graph._explore(self.idB, 5) - self.assertEqual(len(explorer_graph.nx_graph.nodes()), 5) - self.assertEqual(len(explorer_graph.nx_graph.edges()), 4) - - self.lp.run_until_complete(exec_test()) - - @patch('sakia.core.Community') - @patch('sakia.core.Application') - @patch('time.time', Mock(return_value=50000)) - def test_explore_full_from_extremity(self, app, community): - community.parameters = CoroutineMock(return_value = {'sigValidity': 1000}) - community.network.confirmations = Mock(side_effect=lambda n: 4 if 996 else None) - community.nb_members = CoroutineMock(return_value = 3) - app.preferences = {'expert_mode': True} - - explorer_graph = ExplorerGraph(app, community) - - async def exec_test(): - await explorer_graph._explore(self.idA, 5) - self.assertEqual(len(explorer_graph.nx_graph.nodes()), 5) - self.assertEqual(len(explorer_graph.nx_graph.edges()), 4) - - self.lp.run_until_complete(exec_test()) - - @patch('sakia.core.Community') - @patch('sakia.core.Application') - @patch('time.time', Mock(return_value=50000)) - def test_explore_partial(self, app, community): - community.parameters = CoroutineMock(return_value = {'sigValidity': 1000}) - community.network.confirmations = Mock(side_effect=lambda n: 4 if 996 else None) - community.nb_members = CoroutineMock(return_value = 3) - app.preferences = {'expert_mode': True} - - explorer_graph = ExplorerGraph(app, community) - - async def exec_test(): - await explorer_graph._explore(self.idB, 1) - self.assertEqual(len(explorer_graph.nx_graph.nodes()), 3) - self.assertEqual(len(explorer_graph.nx_graph.edges()), 2) - - self.lp.run_until_complete(exec_test()) - - @patch('sakia.core.Community') - @patch('sakia.core.Application') - @patch('time.time', Mock(return_value=50000)) - def test_start_stop_exploration(self, app, community): - async def explore_mock(id, steps): - await asyncio.sleep(0.1) - await asyncio.sleep(0.1) - await asyncio.sleep(0.1) - - explorer_graph = ExplorerGraph(app, community) - explorer_graph._explore = explore_mock - - async def exec_test(): - self.assertEqual(explorer_graph.exploration_task, None) - explorer_graph.start_exploration(self.idA, 1) - self.assertNotEqual(explorer_graph.exploration_task, None) - task = explorer_graph.exploration_task - explorer_graph.start_exploration(self.idA, 1) - self.assertEqual(task, explorer_graph.exploration_task) - explorer_graph.start_exploration(self.idB, 1) - await asyncio.sleep(0) - self.assertTrue(task.cancelled()) - self.assertNotEqual(task, explorer_graph.exploration_task) - task2 = explorer_graph.exploration_task - explorer_graph.start_exploration(self.idB, 2) - await asyncio.sleep(0) - self.assertTrue(task2.cancelled()) - task3 = explorer_graph.exploration_task - explorer_graph.stop_exploration() - await asyncio.sleep(0) - self.assertTrue(task2.cancelled()) - - - self.lp.run_until_complete(exec_test()) diff --git a/src/sakia/tests/unit/core/graph/test_wot_graph.py b/src/sakia/tests/unit/core/graph/test_wot_graph.py deleted file mode 100644 index 4ce689393cd5e11498d6a4d21224d01efe3449ae..0000000000000000000000000000000000000000 --- a/src/sakia/tests/unit/core/graph/test_wot_graph.py +++ /dev/null @@ -1,157 +0,0 @@ -import unittest - -from PyQt5.QtCore import QLocale -from asynctest.mock import Mock, CoroutineMock, patch -from sakia.core.graph import WoTGraph - -from sakia.tests import QuamashTest - - -class TestWotGraph(unittest.TestCase, QuamashTest): - def setUp(self): - self.setUpQuamash() - QLocale.setDefault(QLocale("en_GB")) - - ## Graph to test : - ## - ## A - B - C - ## - ## Path : Between A and C - - self.account_identity = Mock(specs='core.registry.Identity') - self.account_identity.pubkey = "HnFcSms8jzwngtVomTTnzudZx7SHUQY8sVE1y8yBmULk" - self.account_identity.uid = "A" - self.account_identity.is_member = CoroutineMock(spec='core.registry.Identity.is_member', return_value=True) - - self.idB = Mock(specs='core.registry.Identity') - self.idB.pubkey = "7Aqw6Efa9EzE7gtsc8SveLLrM7gm6NEGoywSv4FJx6pZ" - self.idB.uid = "B" - self.idB.is_member = CoroutineMock(spec='core.registry.Identity.is_member', return_value=True) - - self.idC = Mock(specs='core.registry.Identity') - self.idC.pubkey = "FADxcH5LmXGmGFgdixSes6nWnC4Vb4pRUBYT81zQRhjn" - self.idC.uid = "C" - self.idC.is_member = CoroutineMock(spec='core.registry.Identity.is_member', return_value=False) - - self.account_identity.unique_valid_certified_by = CoroutineMock(spec='core.registry.Identity.certified_by', - return_value=[ - { - 'cert_time': 49800, - 'identity': self.idB, - 'block_number': 996 - } - ]) - self.account_identity.unique_valid_certifiers_of = CoroutineMock(spec='core.registry.Identity.certifiers_of', - return_value=[]) - - self.idC.unique_valid_certified_by = CoroutineMock(spec='core.registry.Identity.certifierd_by', - return_value=[]) - - self.idC.unique_valid_certifiers_of = CoroutineMock(spec='core.registry.Identity.certifiers_of', - return_value=[ - { - 'cert_time': 49100, - 'identity': self.idB, - 'block_number': 990 - } - ]) - - self.idB.unique_valid_certified_by = CoroutineMock(spec='core.registry.Identity.certified_by', - return_value=[ - { - 'cert_time': 49100, - 'identity': self.idC, - 'block_number': 996 - } - ]) - - self.idB.unique_valid_certifiers_of = CoroutineMock(spec='core.registry.Identity.certifiers_of', - return_value=[ - { - 'cert_time': 49800, - 'identity': self.account_identity, - 'block_number': 996 - } - ]) - - def tearDown(self): - self.tearDownQuamash() - - @patch('sakia.core.Community') - @patch('sakia.core.Application') - @patch('time.time', Mock(return_value=50000)) - def test_explore_to_find_member(self, app, community): - community.parameters = CoroutineMock(return_value = {'sigValidity': 1000}) - community.network.confirmations = Mock(side_effect=lambda n: 4 if 996 else None) - app.preferences = {'expert_mode': True} - - wot_graph = WoTGraph(app, community) - - async def exec_test(): - result = await wot_graph.explore_to_find_member(self.account_identity, self.idC) - self.assertTrue(result) - self.assertEqual(len(wot_graph.nx_graph.nodes()), 3) - self.assertEqual(len(wot_graph.nx_graph.edges()), 2) - - self.lp.run_until_complete(exec_test()) - - @patch('sakia.core.Application') - @patch('sakia.core.Community') - @patch('time.time', Mock(return_value=50000)) - def test_explore_to_find_unknown(self, app, community): - community.parameters = CoroutineMock(return_value = {'sigValidity': 1000}) - community.network.confirmations = Mock(side_effect=lambda n: 4 if 996 else None) - app.preferences = {'expert_mode': True} - - wot_graph = WoTGraph(app, community) - - identity_unknown = Mock(specs='core.registry.Identity') - identity_unknown.pubkey = "8Fi1VSTbjkXguwThF4v2ZxC5whK7pwG2vcGTkPUPjPGU" - identity_unknown.uid = "unkwn" - - async def exec_test(): - result = await wot_graph.explore_to_find_member(self.account_identity, identity_unknown) - self.assertFalse(result) - self.assertEqual(len(wot_graph.nx_graph.nodes()), 3) - self.assertEqual(len(wot_graph.nx_graph.edges()), 2) - - self.lp.run_until_complete(exec_test()) - - @patch('sakia.core.Community') - @patch('sakia.core.Application') - @patch('time.time', Mock(return_value=50000)) - def test_shortest_path(self, app, community): - community.parameters = CoroutineMock(return_value = {'sigValidity': 1000}) - community.network.confirmations = Mock(side_effect=lambda n: 4 if 996 else None) - app.preferences = {'expert_mode': True} - - wot_graph = WoTGraph(app, community) - - async def exec_test(): - result = await wot_graph.explore_to_find_member(self.account_identity, self.idC) - self.assertTrue(result) - self.assertEqual(len(wot_graph.nx_graph.nodes()), 3) - self.assertEqual(len(wot_graph.nx_graph.edges()), 2) - path = await wot_graph.get_shortest_path_to_identity(self.account_identity, self.idC) - self.assertEqual(path[0], self.account_identity.pubkey,) - self.assertEqual(path[1], self.idB.pubkey) - self.assertEqual(path[2], self.idC.pubkey) - - self.lp.run_until_complete(exec_test()) - - @patch('sakia.core.Community') - @patch('sakia.core.Application') - @patch('time.time', Mock(return_value=50000)) - def test_initialize(self, app, community): - community.parameters = CoroutineMock(return_value = {'sigValidity': 1000}) - community.network.confirmations = Mock(side_effect=lambda n: 4 if 996 else None) - app.preferences = {'expert_mode': True} - - wot_graph = WoTGraph(app, community) - - async def exec_test(): - await wot_graph.initialize(self.account_identity, self.account_identity) - self.assertEqual(len(wot_graph.nx_graph.nodes()), 2) - self.assertEqual(len(wot_graph.nx_graph.edges()), 1) - - self.lp.run_until_complete(exec_test()) diff --git a/src/sakia/tests/unit/core/money/test_quantitative.py b/src/sakia/tests/unit/core/money/test_quantitative.py index b0b00bed60cd21548eaf2cfcafd8dee365031259..c9aecf8b26fb4b97009a03e852a872b97a73600e 100644 --- a/src/sakia/tests/unit/core/money/test_quantitative.py +++ b/src/sakia/tests/unit/core/money/test_quantitative.py @@ -1,146 +1,77 @@ -import unittest +from sakia.money import Quantitative -from PyQt5.QtCore import QLocale -from asynctest.mock import patch, PropertyMock -from sakia.money import Quantitative -from sakia.tests import QuamashTest - - -class TestQuantitative(unittest.TestCase, QuamashTest): - def setUp(self): - self.setUpQuamash() - QLocale.setDefault(QLocale("en_GB")) - - def tearDown(self): - self.tearDownQuamash() - - @patch('sakia.core.Community') - @patch('sakia.core.Application') - def test_units(self, app, community): - type(community).short_currency = PropertyMock(return_value="TC") - referential = Quantitative(0, community, app, None) - self.assertEqual(referential.units, "TC") - - @patch('sakia.core.Community') - @patch('sakia.core.Application') - def test_diff_units(self, app, community): - type(community).short_currency = PropertyMock(return_value="TC") - referential = Quantitative(0, community, app, None) - self.assertEqual(referential.units, "TC") - - @patch('sakia.core.Community') - @patch('sakia.core.Application') - def test_value(self, app, community): - referential = Quantitative(101010110, community, app, None) - async def exec_test(): - value = await referential.value() - self.assertEqual(value, 101010110) - self.lp.run_until_complete(exec_test()) - - @patch('sakia.core.Community') - @patch('sakia.core.Application') - def test_differential(self, app, community): - referential = Quantitative(110, community, app, None) - async def exec_test(): - value = await referential.value() - self.assertEqual(value, 110) - self.lp.run_until_complete(exec_test()) - - @patch('sakia.core.Community') - @patch('sakia.core.Application') - def test_localized_no_si(self, app, community): - type(community).short_currency = PropertyMock(return_value="TC") - referential = Quantitative(101010110, community, app, None) - async def exec_test(): - value = await referential.localized(units=True) - self.assertEqual(value, "101,010,110 TC") - self.lp.run_until_complete(exec_test()) - - @patch('sakia.core.Community') - @patch('sakia.core.Application') - def test_localized_with_si(self, app, community): - type(community).short_currency = PropertyMock(return_value="TC") - app.preferences = { - 'digits_after_comma': 6 - } - referential = Quantitative(101010110, community, app, None) - async def exec_test(): - value = await referential.localized(units=True, international_system=True) - self.assertEqual(value, "101.010110 MTC") - self.lp.run_until_complete(exec_test()) - - @patch('sakia.core.Community') - @patch('sakia.core.Application') - def test_localized_no_units_no_si(self, app, community): - type(community).short_currency = PropertyMock(return_value="TC") - app.preferences = { - 'digits_after_comma': 6 - } - referential = Quantitative(101010110, community, app, None) - async def exec_test(): - value = await referential.localized(units=False, international_system=False) - self.assertEqual(value, "101,010,110") - self.lp.run_until_complete(exec_test()) - - @patch('sakia.core.Community') - @patch('sakia.core.Application') - def test_localized_no_units_with_si(self, app, community): - type(community).short_currency = PropertyMock(return_value="TC") - app.preferences = { - 'digits_after_comma': 6 - } - referential = Quantitative(101010110, community, app, None) - async def exec_test(): - value = await referential.localized(units=False, international_system=True) - self.assertEqual(value, "101.010110 M") - self.lp.run_until_complete(exec_test()) - - @patch('sakia.core.Community') - @patch('sakia.core.Application') - def test_diff_localized_no_si(self, app, community): - type(community).short_currency = PropertyMock(return_value="TC") - referential = Quantitative(101010110, community, app, None) - async def exec_test(): - value = await referential.diff_localized(units=True) - self.assertEqual(value, "101,010,110 TC") - self.lp.run_until_complete(exec_test()) - - @patch('sakia.core.Community') - @patch('sakia.core.Application') - def test_diff_localized_with_si(self, app, community): - type(community).short_currency = PropertyMock(return_value="TC") - app.preferences = { - 'digits_after_comma': 6 - } - referential = Quantitative(101010110, community, app, None) - async def exec_test(): - value = await referential.diff_localized(units=True, international_system=True) - self.assertEqual(value, "101.010110 MTC") - self.lp.run_until_complete(exec_test()) - - @patch('sakia.core.Community') - @patch('sakia.core.Application') - def test_diff_localized_no_units_no_si(self, app, community): - type(community).short_currency = PropertyMock(return_value="TC") - app.preferences = { - 'digits_after_comma': 6 - } - referential = Quantitative(101010110, community, app, None) - async def exec_test(): - value = await referential.diff_localized(units=False, international_system=False) - self.assertEqual(value, "101,010,110") - self.lp.run_until_complete(exec_test()) - - @patch('sakia.core.Community') - @patch('sakia.core.Application') - def test_diff_localized_no_units_with_si(self, app, community): - type(community).short_currency = PropertyMock(return_value="TC") - app.preferences = { - 'digits_after_comma': 6 - } - referential = Quantitative(101010110, community, app, None) - async def exec_test(): - value = await referential.diff_localized(units=False, international_system=True) - self.assertEqual(value, "101.010110 M") - self.lp.run_until_complete(exec_test()) +def test_units(application_with_one_connection, bob): + referential = Quantitative(0, bob.currency, application_with_one_connection, None) + assert referential.units == "TC" + + +def test_diff_units(application_with_one_connection, bob): + referential = Quantitative(0, bob.currency, application_with_one_connection, None) + assert referential.units == "TC" + + +def test_value(application_with_one_connection, bob): + referential = Quantitative(101010110, bob.currency, application_with_one_connection, None) + value = referential.value() + assert value == 101010110 + + +def test_differential(application_with_one_connection, bob): + referential = Quantitative(110, bob.currency, application_with_one_connection, None) + value = referential.value() + assert value == 110 + + +def test_localized_no_si(application_with_one_connection, bob): + referential = Quantitative(101010110, bob.currency, application_with_one_connection, None) + value = referential.localized(units=True) + assert value == "101,010,110 TC" + + +def test_localized_with_si(application_with_one_connection, bob): + application_with_one_connection.parameters.digits_after_comma = 6 + referential = Quantitative(101010110, bob.currency, application_with_one_connection, None) + value = referential.localized(units=True, international_system=True) + assert value == "101.010110 x10â¶ TC" + + +def test_localized_no_units_no_si(application_with_one_connection, bob): + application_with_one_connection.parameters.digits_after_comma = 6 + referential = Quantitative(101010110, bob.currency, application_with_one_connection, None) + value = referential.localized(units=False, international_system=False) + assert value == "101,010,110" + + +def test_localized_no_units_with_si(application_with_one_connection, bob): + application_with_one_connection.parameters.digits_after_comma = 6 + referential = Quantitative(101010110, bob.currency, application_with_one_connection, None) + value = referential.localized(units=False, international_system=True) + assert value == "101.010110 x10â¶" + + +def test_diff_localized_no_si(application_with_one_connection, bob): + referential = Quantitative(101010110, bob.currency, application_with_one_connection, None) + value = referential.diff_localized(units=True) + assert value == "101,010,110 TC" + + +def test_diff_localized_with_si(application_with_one_connection, bob): + application_with_one_connection.parameters.digits_after_comma = 6 + referential = Quantitative(101010110, bob.currency, application_with_one_connection, None) + value = referential.diff_localized(units=True, international_system=True) + assert value == "101.010110 x10â¶ TC" + + +def test_diff_localized_no_units_no_si(application_with_one_connection, bob): + application_with_one_connection.parameters.digits_after_comma = 6 + referential = Quantitative(101010110, bob.currency, application_with_one_connection, None) + value = referential.diff_localized(units=False, international_system=False) + assert value == "101,010,110" + + +def test_diff_localized_no_units_with_si(application_with_one_connection, bob): + application_with_one_connection.parameters.digits_after_comma = 6 + referential = Quantitative(101010110, bob.currency, application_with_one_connection, None) + value = referential.diff_localized(units=False, international_system=True) + assert value == "101.010110 M"