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

New Entity and Repo "Identitiy" + tests

parent ebc5b300
No related branches found
No related tags found
No related merge requests found
from .identity import Identity
\ No newline at end of file
import attr
from duniterpy.documents import block_uid
@attr.s(slots=True)
class Identity:
currency = attr.ib(convert=str)
pubkey = attr.ib(convert=str)
uid = attr.ib(convert=str)
signature = attr.ib(convert=str)
blockstamp = attr.ib(convert=block_uid)
timestamp = attr.ib(convert=int)
member = attr.ib(validator=attr.validators.instance_of(bool))
membership_buid = attr.ib(convert=block_uid)
membership_timestamp = attr.ib(convert=int)
def astuple(self):
return (self.currency, self.pubkey, self.uid, self.signature, self.blockstamp, self.timestamp,
self.member, self.membership_buid, self.membership_timestamp)
\ No newline at end of file
from .identities import IdentitiesRepo
\ No newline at end of file
import sqlite3
from ..entities import Identity
class IdentitiesRepo:
def __init__(self, conn):
"""
:param sqlite3.Connection conn: the cursor
"""
self._conn = conn
def prepare(self):
"""
Prepares the database if the table is missing
"""
with self._conn:
self._conn.execute("create table if not exists identities("
"CURRENCY varchar(30), "
"PUBKEY varchar(50),"
"UID varchar(255),"
"SIGNATURE varchar(100),"
"BLOCKSTAMP varchar(100),"
"TS int,"
"MEMBER boolean,"
"MS_BUID varchar(100),"
"MS_TIMESTAMP int,"
"PRIMARY KEY (CURRENCY, PUBKEY)"
")"
)
def insert(self, identity):
"""
Commit an identity to the database
:param sakia.data.entities.Identity identity: the identity to commit
"""
with self._conn:
self._conn.execute("INSERT INTO identities VALUES (?,?,?,?,?,?,?,?,?)", identity.astuple())
def update(self, identity):
"""
Update an existing identity in the database
:param sakia.data.entities.Identity identity: the identity to update
"""
with self._conn:
self.conn.execute("UPDATE identities SET "
"UID=?, "
"SIGNATURE=?, "
"BLOCKSTAMP=?,"
"TS=?,"
"MEMBER=?,"
"MS_BUID=?,"
"MS_TIMESTAMP=?"
"WHERE CURRENCY=? AND PUBKEY=?", identity.astuple()[2:] + (identity.currency,
identity.pubkey)
)
def get_one(self, currency, pubkey):
"""
Get an existing identity in the database
:param str currency:
:param str pubkey:
:rtype: sakia.data.entities.Identity
"""
with self._conn:
c = self._conn.execute("SELECT * FROM identities WHERE currency=? AND pubkey=?", (currency, pubkey))
data = c.fetchone()
if data:
return Identity(*data)
def drop(self, currency, pubkey):
"""
Drop an existing identity from the database
:param str currency:
:param str pubkey:
"""
with self._conn:
self._conn.execute("DELETE FROM identities WHERE currency=? AND pubkey=?", (currency, pubkey))
\ No newline at end of file
from sakia.data.repositories import IdentitiesRepo
from sakia.data.entities import Identity
from duniterpy.documents import BlockUID
import unittest
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)
def tearDown(self):
self.con.close()
def test_add_get_identity(self):
identities_repo = IdentitiesRepo(self.con)
identities_repo.prepare()
identities_repo.insert(Identity("testcurrency", "7Aqw6Efa9EzE7gtsc8SveLLrM7gm6NEGoywSv4FJx6pZ",
"john",
"H41/8OGV2W4CLKbE35kk5t1HJQsb3jEM0/QGLUf80CwJvGZf3HvVCcNtHPUFoUBKEDQO9mPK3KJkqOoxHpqHCw==",
"20-7518C700E78B56CC21FB1DDC6CBAB24E0FACC9A798F5ED8736EA007F38617D67",
1473108382,
False,
None,
0))
identity = identities_repo.get_one("testcurrency", "7Aqw6Efa9EzE7gtsc8SveLLrM7gm6NEGoywSv4FJx6pZ")
self.assertEqual(identity.currency, "testcurrency")
self.assertEqual(identity.pubkey, "7Aqw6Efa9EzE7gtsc8SveLLrM7gm6NEGoywSv4FJx6pZ")
self.assertEqual(identity.uid, "john")
self.assertEqual(identity.blockstamp.number, 20)
self.assertEqual(identity.blockstamp.sha_hash, "7518C700E78B56CC21FB1DDC6CBAB24E0FACC9A798F5ED8736EA007F38617D67")
self.assertEqual(identity.timestamp, 1473108382)
self.assertEqual(identity.signature, "H41/8OGV2W4CLKbE35kk5t1HJQsb3jEM0/QGLUf80CwJvGZf3HvVCcNtHPUFoUBKEDQO9mPK3KJkqOoxHpqHCw==")
self.assertEqual(identity.member, False)
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")
self.assertIsNone(identity)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment