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

Add key entity

parent 4a855be8
Branches
Tags
No related merge requests found
...@@ -3,3 +3,4 @@ from .blockchain import Blockchain, BlockchainParameters ...@@ -3,3 +3,4 @@ from .blockchain import Blockchain, BlockchainParameters
from .certification import Certification from .certification import Certification
from .transaction import Transaction from .transaction import Transaction
from .node import Node from .node import Node
from .connection import Connection
import attr
@attr.s()
class Connection:
"""
A connection represents a connection to a currency's network
It is defined by the currency name, and the key informations
used to connect to it
"""
currency = attr.ib(convert=str)
pubkey = attr.ib(convert=str)
salt = attr.ib(convert=str)
...@@ -4,3 +4,4 @@ from .meta import MetaDatabase ...@@ -4,3 +4,4 @@ from .meta import MetaDatabase
from .certifications import CertificationsRepo from .certifications import CertificationsRepo
from .transactions import TransactionsRepo from .transactions import TransactionsRepo
from .nodes import NodesRepo from .nodes import NodesRepo
from .connections import ConnectionsRepo
import attr
from ..entities import Connection
@attr.s(frozen=True)
class ConnectionsRepo:
"""
The repository for Connections entities.
"""
_conn = attr.ib() # :type sqlite3.Connection
_primary_connections = (Connection.currency, Connection.pubkey)
def insert(self, connection):
"""
Commit a connection to the database
:param sakia.data.entities.Connection connection: the connection to commit
"""
with self._conn:
connection_tuple = attr.astuple(connection)
values = ",".join(['?'] * len(connection_tuple))
self._conn.execute("INSERT INTO connections VALUES ({0})".format(values), connection_tuple)
def get_one(self, **search):
"""
Get an existing connection in the database
:param dict search: the criterions of the lookup
:rtype: sakia.data.entities.Connection
"""
with self._conn:
filters = []
values = []
for k, v in search.items():
filters.append("{k}=?".format(k=k))
values.append(v)
request = "SELECT * FROM connections WHERE {filters}".format(filters=" AND ".join(filters))
c = self._conn.execute(request, tuple(values))
data = c.fetchone()
if data:
return Connection(*data)
def get_all(self, **search):
"""
Get all existing connection in the database corresponding to the search
:param dict search: the criterions of the lookup
:rtype: sakia.data.entities.Connection
"""
with self._conn:
filters = []
values = []
for k, v in search.items():
value = v
filters.append("{connection} = ?".format(connection=k))
values.append(value)
request = "SELECT * FROM connections WHERE {filters}".format(filters=" AND ".join(filters))
c = self._conn.execute(request, tuple(values))
datas = c.fetchall()
if datas:
return [Connection(*data) for data in datas]
return []
def drop(self, connection):
"""
Drop an existing connection from the database
:param sakia.data.entities.Connection connection: the connection to update
"""
with self._conn:
where_fields = attr.astuple(connection, filter=attr.filters.include(*ConnectionsRepo._primary_connections))
self._conn.execute("""DELETE FROM connections
WHERE
currency=? AND
pubkey=?""", where_fields)
...@@ -92,3 +92,11 @@ CREATE TABLE IF NOT EXISTS nodes( ...@@ -92,3 +92,11 @@ CREATE TABLE IF NOT EXISTS nodes(
root BOOLEAN, root BOOLEAN,
PRIMARY KEY (currency, pubkey) PRIMARY KEY (currency, pubkey)
); );
-- Keys TABLE
CREATE TABLE IF NOT EXISTS connections(
currency VARCHAR(30),
pubkey VARCHAR(50),
salt VARCHAR(50),
PRIMARY KEY (currency, pubkey)
);
from sakia.data.repositories import ConnectionsRepo, MetaDatabase
from sakia.data.entities import Connection
from duniterpy.documents import BlockUID
import unittest
import sqlite3
class TestConnectionsRepo(unittest.TestCase):
def setUp(self):
self.meta_repo = MetaDatabase.create(":memory:")
self.meta_repo.prepare()
self.meta_repo.upgrade_database()
def tearDown(self):
pass
def test_add_get_drop_connection(self):
connections_repo = ConnectionsRepo(self.meta_repo.conn)
connections_repo.insert(Connection("testcurrency",
"7Aqw6Efa9EzE7gtsc8SveLLrM7gm6NEGoywSv4FJx6pZ",
"somesalt"))
connection = connections_repo.get_one(currency="testcurrency",
pubkey="7Aqw6Efa9EzE7gtsc8SveLLrM7gm6NEGoywSv4FJx6pZ",
salt="somesalt")
self.assertEqual(connection.currency, "testcurrency")
self.assertEqual(connection.pubkey, "7Aqw6Efa9EzE7gtsc8SveLLrM7gm6NEGoywSv4FJx6pZ")
self.assertEqual(connection.salt, "somesalt")
connections_repo.drop(connection)
connection = connections_repo.get_one(currency="testcurrency",
pubkey="7Aqw6Efa9EzE7gtsc8SveLLrM7gm6NEGoywSv4FJx6pZ",
salt="somesalt")
self.assertIsNone(connection)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment