diff --git a/src/sakia/data/repositories/meta.py b/src/sakia/data/repositories/meta.py index 319a03fcb303cced285f0eba4f319bbe050cb0ed..fff55f9dee70a45f1b369f74348459328b61d937 100644 --- a/src/sakia/data/repositories/meta.py +++ b/src/sakia/data/repositories/meta.py @@ -1,14 +1,27 @@ import attr import os +import sqlite3 +from duniterpy.documents import BlockUID @attr.s(frozen=True) class MetaDatabase: """The repository for Identities entities. """ - _conn = attr.ib() # :type sqlite3.Connection + @property + def conn(self): + return self._conn + + @classmethod + def create(cls, dbpath): + sqlite3.register_adapter(BlockUID, str) + sqlite3.register_adapter(bool, int) + sqlite3.register_converter("BOOLEAN", lambda v: bool(int(v))) + con = sqlite3.connect(dbpath, detect_types=sqlite3.PARSE_DECLTYPES) + return MetaDatabase(con) + def prepare(self): """ Prepares the database if the table is missing diff --git a/src/sakia/data/repositories/nodes.py b/src/sakia/data/repositories/nodes.py index d8770584041cc1452cae1369035c89059e59eae3..e7b2a06d18f95177b18a8a882d6eb8867568f90d 100644 --- a/src/sakia/data/repositories/nodes.py +++ b/src/sakia/data/repositories/nodes.py @@ -17,6 +17,8 @@ class NodesRepo: """ with self._conn: node_tuple = attr.astuple(node, tuple_factory=list) + node_tuple[2] = "\n".join([str(n) for n in node_tuple[2]]) + node_tuple[11] = "\n".join([str(n) for n in node_tuple[11]]) values = ",".join(['?'] * len(node_tuple)) self._conn.execute("INSERT INTO nodes VALUES ({0})".format(values), node_tuple) @@ -28,6 +30,8 @@ class NodesRepo: with self._conn: updated_fields = attr.astuple(node, tuple_factory=list, filter=attr.filters.exclude(*NodesRepo._primary_keys)) + updated_fields[0] = "\n".join([str(n) for n in updated_fields[0]]) + updated_fields[9] = "\n".join([str(n) for n in updated_fields[9]]) where_fields = attr.astuple(node, tuple_factory=list, filter=attr.filters.include(*NodesRepo._primary_keys)) self._conn.execute("""UPDATE nodes SET diff --git a/src/sakia/tests/unit/data/test_blockchains_repo.py b/src/sakia/tests/unit/data/test_blockchains_repo.py index 8f831fd06dce814c07b387ab5d9e4577aff5ef11..43c285098dfc3ceb45d3cfbf3a035887b653239f 100644 --- a/src/sakia/tests/unit/data/test_blockchains_repo.py +++ b/src/sakia/tests/unit/data/test_blockchains_repo.py @@ -9,19 +9,15 @@ from sakia.data.repositories import BlockchainsRepo, MetaDatabase class TestBlockchainsRepo(unittest.TestCase): def setUp(self): - sqlite3.register_adapter(BlockUID, str) - sqlite3.register_adapter(bool, int) - sqlite3.register_converter("BOOLEAN", lambda v: bool(int(v))) - self.con = sqlite3.connect(":memory:", detect_types=sqlite3.PARSE_DECLTYPES) + self.meta_repo = MetaDatabase.create(":memory:") + self.meta_repo.prepare() + self.meta_repo.upgrade_database() def tearDown(self): - self.con.close() + pass def test_add_get_drop_blockchain(self): - meta_repo = MetaDatabase(self.con) - meta_repo.prepare() - meta_repo.upgrade_database() - blockchains_repo = BlockchainsRepo(self.con) + blockchains_repo = BlockchainsRepo(self.meta_repo.conn) blockchains_repo.insert(Blockchain( BlockchainParameters( 0.1, @@ -78,10 +74,7 @@ class TestBlockchainsRepo(unittest.TestCase): self.assertIsNone(blockchain) def test_add_get_multiple_blockchain(self): - meta_repo = MetaDatabase(self.con) - meta_repo.prepare() - meta_repo.upgrade_database() - blockchains_repo = BlockchainsRepo(self.con) + blockchains_repo = BlockchainsRepo(self.meta_repo.conn) blockchains_repo.insert(Blockchain( BlockchainParameters( 0.1, @@ -148,10 +141,7 @@ class TestBlockchainsRepo(unittest.TestCase): self.assertEquals(20, blockchains[1].nb_members) def test_add_update_blockchain(self): - meta_repo = MetaDatabase(self.con) - meta_repo.prepare() - meta_repo.upgrade_database() - blockchains_repo = BlockchainsRepo(self.con) + blockchains_repo = BlockchainsRepo(self.meta_repo.conn) blockchain = Blockchain( BlockchainParameters( 0.1, diff --git a/src/sakia/tests/unit/data/test_certifications_repo.py b/src/sakia/tests/unit/data/test_certifications_repo.py index 7254f29d260aeee063aaba0f6727a7b5d5b63061..b818c6048997e53554926644b98c9c38f6083099 100644 --- a/src/sakia/tests/unit/data/test_certifications_repo.py +++ b/src/sakia/tests/unit/data/test_certifications_repo.py @@ -7,19 +7,15 @@ import sqlite3 class TestCertificationsRepo(unittest.TestCase): def setUp(self): - sqlite3.register_adapter(BlockUID, str) - sqlite3.register_adapter(bool, int) - sqlite3.register_converter("BOOLEAN", lambda v: bool(int(v))) - self.con = sqlite3.connect(":memory:", detect_types=sqlite3.PARSE_DECLTYPES) + self.meta_repo = MetaDatabase.create(":memory:") + self.meta_repo.prepare() + self.meta_repo.upgrade_database() def tearDown(self): - self.con.close() + pass - def test_add_get_drop_certification(self): - meta_repo = MetaDatabase(self.con) - meta_repo.prepare() - meta_repo.upgrade_database() - certifications_repo = CertificationsRepo(self.con) + def test_add_get_drop_blockchain(self): + certifications_repo = CertificationsRepo(self.meta_repo.conn) certifications_repo.insert(Certification("testcurrency", "7Aqw6Efa9EzE7gtsc8SveLLrM7gm6NEGoywSv4FJx6pZ", "FADxcH5LmXGmGFgdixSes6nWnC4Vb4pRUBYT81zQRhjn", @@ -51,10 +47,7 @@ class TestCertificationsRepo(unittest.TestCase): self.assertIsNone(certification) def test_add_get_multiple_certification(self): - meta_repo = MetaDatabase(self.con) - meta_repo.prepare() - meta_repo.upgrade_database() - certifications_repo = CertificationsRepo(self.con) + certifications_repo = CertificationsRepo(self.meta_repo.conn) certifications_repo.insert(Certification("testcurrency", "7Aqw6Efa9EzE7gtsc8SveLLrM7gm6NEGoywSv4FJx6pZ", "FADxcH5LmXGmGFgdixSes6nWnC4Vb4pRUBYT81zQRhjn", @@ -77,10 +70,7 @@ class TestCertificationsRepo(unittest.TestCase): self.assertIn("7Aqw6Efa9EzE7gtsc8SveLLrM7gm6NEGoywSv4FJx6pZ", [i.certified for i in certifications]) def test_add_update_certification(self): - meta_repo = MetaDatabase(self.con) - meta_repo.prepare() - meta_repo.upgrade_database() - certifications_repo = CertificationsRepo(self.con) + certifications_repo = CertificationsRepo(self.meta_repo.conn) certification = Certification("testcurrency", "7Aqw6Efa9EzE7gtsc8SveLLrM7gm6NEGoywSv4FJx6pZ", "FADxcH5LmXGmGFgdixSes6nWnC4Vb4pRUBYT81zQRhjn", diff --git a/src/sakia/tests/unit/data/test_communities_repo.py b/src/sakia/tests/unit/data/test_communities_repo.py deleted file mode 100644 index fb99e2adff8ca0a727f79bb2da244d35ca35138a..0000000000000000000000000000000000000000 --- a/src/sakia/tests/unit/data/test_communities_repo.py +++ /dev/null @@ -1,132 +0,0 @@ -import sqlite3 -import unittest - -from duniterpy.documents import BlockUID - -from sakia.data.entities import Community -from sakia.data.repositories import CommunitiesRepo, MetaDatabase - - -class TestCommunitiesRepo(unittest.TestCase): - def setUp(self): - sqlite3.register_adapter(BlockUID, str) - sqlite3.register_adapter(bool, int) - sqlite3.register_converter("BOOLEAN", lambda v: bool(int(v))) - self.con = sqlite3.connect(":memory:", detect_types=sqlite3.PARSE_DECLTYPES) - - def tearDown(self): - self.con.close() - - def test_add_get_drop_community(self): - meta_repo = MetaDatabase(self.con) - meta_repo.prepare() - meta_repo.upgrade_database() - communities_repo = CommunitiesRepo(self.con) - communities_repo.insert(Community( - 0.1, - 86400, - 100000, - 10800, - 40, - 2629800, - 31557600, - 1, - 0.9, - 604800, - 5, - 12, - 300, - 25, - 10, - 0.66, - "testcurrency" - )) - community = communities_repo.get_one(currency="testcurrency") - self.assertEqual(community.currency, "testcurrency") - self.assertEqual(community.c, 0.1) - self.assertEqual(community.dt, 86400) - - communities_repo.drop(community) - community = communities_repo.get_one(currency="testcurrency") - self.assertIsNone(community) - - def test_add_get_multiple_community(self): - meta_repo = MetaDatabase(self.con) - meta_repo.prepare() - meta_repo.upgrade_database() - communities_repo = CommunitiesRepo(self.con) - communities_repo.insert(Community( - 0.1, - 86400, - 100000, - 10800, - 40, - 2629800, - 31557600, - 1, - 0.9, - 604800, - 5, - 12, - 300, - 25, - 10, - 0.66, - "testcurrency" - ) - ) - communities_repo.insert(Community( - 0.1, - 86400 * 365, - 100000, - 10800, - 40, - 2629800, - 31557600, - 1, - 0.9, - 604800, - 5, - 12, - 300, - 25, - 10, - 0.66, - "testcurrency2" - ) - ) - communities = communities_repo.get_all(currency="testcurrency") - self.assertIn("testcurrency", [i.currency for i in communities]) - self.assertIn("testcurrency2", [i.currency for i in communities]) - self.assertIn(86400, [i.dt for i in communities]) - self.assertIn(86400 * 365, [i.dt for i in communities]) - - def test_add_update_community(self): - meta_repo = MetaDatabase(self.con) - meta_repo.prepare() - meta_repo.upgrade_database() - communities_repo = CommunitiesRepo(self.con) - community = Community( - 0.1, - 86400, - 100000, - 10800, - 40, - 2629800, - 31557600, - 1, - 0.9, - 604800, - 5, - 12, - 300, - 25, - 10, - 0.66, - "testcurrency" - ) - communities_repo.insert(community) - community.c = 0.0922 - communities_repo.update(community) - community2 = communities_repo.get_one(currency="testcurrency") - self.assertEquals(0.0922, community2.c) diff --git a/src/sakia/tests/unit/data/test_identies_repo.py b/src/sakia/tests/unit/data/test_identies_repo.py index a4abbd16943cd0f83e482c228b823d29e1395b9f..94ab7d58b5f0a6a837b04d56262c601423ec5fb7 100644 --- a/src/sakia/tests/unit/data/test_identies_repo.py +++ b/src/sakia/tests/unit/data/test_identies_repo.py @@ -7,19 +7,16 @@ import sqlite3 class TestIdentitiesRepo(unittest.TestCase): def setUp(self): - sqlite3.register_adapter(BlockUID, str) - sqlite3.register_adapter(bool, int) - sqlite3.register_converter("BOOLEAN", lambda v: bool(int(v))) - self.con = sqlite3.connect(":memory:", detect_types=sqlite3.PARSE_DECLTYPES) + self.meta_repo = MetaDatabase.create(":memory:") + self.meta_repo.prepare() + self.meta_repo.upgrade_database() def tearDown(self): - self.con.close() + pass + def test_add_get_drop_identity(self): - meta_repo = MetaDatabase(self.con) - meta_repo.prepare() - meta_repo.upgrade_database() - identities_repo = IdentitiesRepo(self.con) + identities_repo = IdentitiesRepo(self.meta_repo.conn) identities_repo.insert(Identity("testcurrency", "7Aqw6Efa9EzE7gtsc8SveLLrM7gm6NEGoywSv4FJx6pZ", "john", "20-7518C700E78B56CC21FB1DDC6CBAB24E0FACC9A798F5ED8736EA007F38617D67", @@ -59,10 +56,7 @@ class TestIdentitiesRepo(unittest.TestCase): self.assertIsNone(identity) def test_add_get_multiple_identity(self): - meta_repo = MetaDatabase(self.con) - meta_repo.prepare() - meta_repo.upgrade_database() - identities_repo = IdentitiesRepo(self.con) + identities_repo = IdentitiesRepo(self.meta_repo.conn) identities_repo.insert(Identity("testcurrency", "7Aqw6Efa9EzE7gtsc8SveLLrM7gm6NEGoywSv4FJx6pZ", "john", "20-7518C700E78B56CC21FB1DDC6CBAB24E0FACC9A798F5ED8736EA007F38617D67", @@ -93,10 +87,7 @@ class TestIdentitiesRepo(unittest.TestCase): self.assertIn("doe", [i.uid for i in identities]) def test_add_update_identity(self): - meta_repo = MetaDatabase(self.con) - meta_repo.prepare() - meta_repo.upgrade_database() - identities_repo = IdentitiesRepo(self.con) + identities_repo = IdentitiesRepo(self.meta_repo.conn) identity = Identity("testcurrency", "7Aqw6Efa9EzE7gtsc8SveLLrM7gm6NEGoywSv4FJx6pZ", "john", "20-7518C700E78B56CC21FB1DDC6CBAB24E0FACC9A798F5ED8736EA007F38617D67", diff --git a/src/sakia/tests/unit/data/test_nodes_repo.py b/src/sakia/tests/unit/data/test_nodes_repo.py index 23dda28ae7b90e0f00b8d91a1624f6bd5dcd6fdc..0875dbea7efc25b6635ca4594c14eb9adb8ad7e5 100644 --- a/src/sakia/tests/unit/data/test_nodes_repo.py +++ b/src/sakia/tests/unit/data/test_nodes_repo.py @@ -7,21 +7,15 @@ import sqlite3 class TestNodesRepo(unittest.TestCase): def setUp(self): - sqlite3.register_adapter(BlockUID, str) - sqlite3.register_adapter(bool, int) - sqlite3.register_adapter(list, lambda ls: '\n'.join([str(v) for v in ls])) - sqlite3.register_adapter(tuple, lambda ls: '\n'.join([str(v) for v in ls])) - sqlite3.register_converter("BOOLEAN", lambda v: bool(int(v))) - self.con = sqlite3.connect(":memory:", detect_types=sqlite3.PARSE_DECLTYPES) + self.meta_repo = MetaDatabase.create(":memory:") + self.meta_repo.prepare() + self.meta_repo.upgrade_database() def tearDown(self): - self.con.close() + pass def test_add_get_drop_node(self): - meta_repo = MetaDatabase(self.con) - meta_repo.prepare() - meta_repo.upgrade_database() - nodes_repo = NodesRepo(self.con) + nodes_repo = NodesRepo(self.meta_repo.conn) inserted = Node("testcurrency", "7Aqw6Efa9EzE7gtsc8SveLLrM7gm6NEGoywSv4FJx6pZ", """BASIC_MERKLED_API test-net.duniter.fr 13.222.11.22 9201 @@ -55,10 +49,7 @@ UNKNOWNAPI some useless information""", self.assertIsNone(node) def test_add_get_multiple_node(self): - meta_repo = MetaDatabase(self.con) - meta_repo.prepare() - meta_repo.upgrade_database() - nodes_repo = NodesRepo(self.con) + nodes_repo = NodesRepo(self.meta_repo.conn) nodes_repo.insert(Node("testcurrency", "7Aqw6Efa9EzE7gtsc8SveLLrM7gm6NEGoywSv4FJx6pZ", """BASIC_MERKLED_API test-net.duniter.fr 13.222.11.22 9201 @@ -87,10 +78,7 @@ UNKNOWNAPI some useless information""", self.assertIn("FADxcH5LmXGmGFgdixSes6nWnC4Vb4pRUBYT81zQRhjn", [n.pubkey for n in nodes]) def test_add_update_node(self): - meta_repo = MetaDatabase(self.con) - meta_repo.prepare() - meta_repo.upgrade_database() - nodes_repo = NodesRepo(self.con) + nodes_repo = NodesRepo(self.meta_repo.conn) node = Node("testcurrency", "7Aqw6Efa9EzE7gtsc8SveLLrM7gm6NEGoywSv4FJx6pZ", """BASIC_MERKLED_API test-net.duniter.fr 13.222.11.22 9201