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

Enhance repositories

parent 480cf225
No related branches found
No related tags found
No related merge requests found
......@@ -10,6 +10,7 @@ class Identity:
blockstamp = attr.ib(convert=block_uid)
signature = attr.ib(convert=str)
timestamp = attr.ib(convert=int)
revoked = attr.ib(validator=attr.validators.instance_of(bool))
member = attr.ib(validator=attr.validators.instance_of(bool))
membership_buid = attr.ib(convert=block_uid)
membership_timestamp = attr.ib(convert=int)
......@@ -14,7 +14,7 @@ class IdentitiesRepo:
:param sakia.data.entities.Identity identity: the identity to commit
"""
with self._conn:
self._conn.execute("INSERT INTO identities VALUES (?,?,?,?,?,?,?,?,?)", attr.astuple(identity))
self._conn.execute("INSERT INTO identities VALUES (?,?,?,?,?,?,?,?,?,?)", attr.astuple(identity))
def update(self, identity):
"""
......@@ -22,31 +22,66 @@ class IdentitiesRepo:
:param sakia.data.entities.Identity identity: the identity to update
"""
with self._conn:
self.conn.execute("UPDATE identities SET "
"UID=?, "
"BLOCKSTAMP=?,"
"SIGNATURE=?, "
"TS=?,"
"MEMBER=?,"
"MS_BUID=?,"
"MS_TIMESTAMP=?"
"WHERE CURRENCY=? AND PUBKEY=?", attr.astuple(identity)[2:] + (identity.currency,
identity.pubkey)
self._conn.execute("UPDATE identities SET "
"signature=?, "
"ts=?,"
"revoked=?"
"member=?,"
"ms_buid=?,"
"ms_timestamp=?"
"WHERE "
"currency=? AND "
"pubkey=? AND "
"uid=? AND "
"blockstamp=?", attr.astuple(identity)[4:] + (identity.currency,
identity.pubkey,
identity.uid,
identity.blockstamp)
)
def get_one(self, currency, pubkey):
def get_one(self, **search):
"""
Get an existing identity in the database
:param str currency:
:param str pubkey:
:param dict search: the criterions of the lookup
:rtype: sakia.data.entities.Identity
"""
with self._conn:
c = self._conn.execute("SELECT * FROM identities WHERE currency=? AND pubkey=?", (currency, pubkey))
filters = []
values = []
for k, v in search.items():
filters.append("{k}=?".format(k=k))
values.append(v)
request = "SELECT * FROM identities WHERE "
request += " AND ".join(filters)
c = self._conn.execute(request, tuple(values))
data = c.fetchone()
if data:
return Identity(*data)
def get_all(self, **search):
"""
Get all existing identity in the database corresponding to the search
:param dict search: the criterions of the lookup
:rtype: sakia.data.entities.Identity
"""
with self._conn:
filters = []
values = []
for k, v in search.items():
filters.append("{k}=?".format(k=k))
values.append(v)
request = "SELECT * FROM identities WHERE "
request += " AND ".join(filters)
c = self._conn.execute(request, tuple(values))
datas = c.fetchall()
if datas:
return [Identity(*data) for data in datas]
return []
def drop(self, currency, pubkey):
"""
Drop an existing identity from the database
......
......@@ -44,16 +44,17 @@ class MetaDatabase:
"""
with self._conn:
self._conn.execute("create table if not exists identities("
"CURRENCY varchar(30), "
"PUBKEY varchar(50),"
"UID varchar(255),"
"BLOCKSTAMP varchar(100),"
"SIGNATURE varchar(100),"
"TS int,"
"MEMBER boolean,"
"MS_BUID varchar(100),"
"MS_TIMESTAMP int,"
"PRIMARY KEY (CURRENCY, PUBKEY, UID, BLOCKSTAMP)"
"currency varchar(30), "
"pubkey varchar(50),"
"uid varchar(255),"
"blockstamp varchar(100),"
"signature varchar(100),"
"ts int,"
"revoked boolean,"
"member boolean,"
"ms_buid varchar(100),"
"ms_timestamp int,"
"PRIMARY KEY (currency, pubkey, uid, blockstamp)"
")"
)
......
......@@ -15,7 +15,7 @@ class TestIdentitiesRepo(unittest.TestCase):
def tearDown(self):
self.con.close()
def test_add_get_identity(self):
def test_add_get_drop_identity(self):
meta_repo = MetaDatabase(self.con)
meta_repo.prepare()
meta_repo.upgrade_database()
......@@ -28,7 +28,12 @@ class TestIdentitiesRepo(unittest.TestCase):
False,
None,
0))
identity = identities_repo.get_one("testcurrency", "7Aqw6Efa9EzE7gtsc8SveLLrM7gm6NEGoywSv4FJx6pZ")
identity = identities_repo.get_one(currency="testcurrency",
pubkey="7Aqw6Efa9EzE7gtsc8SveLLrM7gm6NEGoywSv4FJx6pZ",
uid="john",
blockstamp=BlockUID(20,
"7518C700E78B56CC21FB1DDC6CBAB24E0FACC9A798F5ED8736EA007F38617D67")
)
self.assertEqual(identity.currency, "testcurrency")
self.assertEqual(identity.pubkey, "7Aqw6Efa9EzE7gtsc8SveLLrM7gm6NEGoywSv4FJx6pZ")
self.assertEqual(identity.uid, "john")
......@@ -40,9 +45,60 @@ class TestIdentitiesRepo(unittest.TestCase):
self.assertEqual(identity.membership_buid, BlockUID.empty())
self.assertEqual(identity.membership_timestamp, 0)
identities_repo.drop("testcurrency", "7Aqw6Efa9EzE7gtsc8SveLLrM7gm6NEGoywSv4FJx6pZ")
identity = identities_repo.get_one("testcurrency", "7Aqw6Efa9EzE7gtsc8SveLLrM7gm6NEGoywSv4FJx6pZ")
identity = identities_repo.get_one(currency="testcurrency",
pubkey="7Aqw6Efa9EzE7gtsc8SveLLrM7gm6NEGoywSv4FJx6pZ",
uid="john",
blockstamp=BlockUID(20,
"7518C700E78B56CC21FB1DDC6CBAB24E0FACC9A798F5ED8736EA007F38617D67")
)
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.insert(Identity("testcurrency", "7Aqw6Efa9EzE7gtsc8SveLLrM7gm6NEGoywSv4FJx6pZ",
"john",
"20-7518C700E78B56CC21FB1DDC6CBAB24E0FACC9A798F5ED8736EA007F38617D67",
"H41/8OGV2W4CLKbE35kk5t1HJQsb3jEM0/QGLUf80CwJvGZf3HvVCcNtHPUFoUBKEDQO9mPK3KJkqOoxHpqHCw==",
1473108382,
False,
None,
0))
identities_repo.insert(Identity("testcurrency", "FADxcH5LmXGmGFgdixSes6nWnC4Vb4pRUBYT81zQRhjn",
"doe",
"101-BAD49448A1AD73C978CEDCB8F137D20A5715EBAA739DAEF76B1E28EE67B2C00C",
"H41/8OGV2W4CLKbE35kk5t1HJQsb3jEM0/QGLUf80CwJvGZf3HvVCcNtHPUFoUBKEDQO9mPK3KJkqOoxHpqHCw==",
1455433535,
False,
None,
0))
identities = identities_repo.get_all(currency="testcurrency")
self.assertIn("testcurrency", [i.currency for i in identities])
self.assertIn("john", [i.uid for i in identities])
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)
identity = Identity("testcurrency", "7Aqw6Efa9EzE7gtsc8SveLLrM7gm6NEGoywSv4FJx6pZ",
"john",
"20-7518C700E78B56CC21FB1DDC6CBAB24E0FACC9A798F5ED8736EA007F38617D67",
"H41/8OGV2W4CLKbE35kk5t1HJQsb3jEM0/QGLUf80CwJvGZf3HvVCcNtHPUFoUBKEDQO9mPK3KJkqOoxHpqHCw==",
1473108382,
False,
None,
0)
identities_repo.insert(identity)
identity.member = True
identities_repo.update(identity)
identity2 = identities_repo.get_one(currency="testcurrency",
pubkey="7Aqw6Efa9EzE7gtsc8SveLLrM7gm6NEGoywSv4FJx6pZ")
self.assertTrue(identity2.member)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment