replaced TarantoolDB with TarantoolDBConnection

Signed-off-by: Lorenz Herzberger <lorenzherzberger@gmail.com>
This commit is contained in:
Lorenz Herzberger 2022-05-24 18:17:13 +02:00
parent aadabbfa8b
commit 6348110d83
No known key found for this signature in database
GPG Key ID: FA5EE906EB55316A
17 changed files with 173 additions and 173 deletions

View File

@ -13,4 +13,4 @@ configuration or the ``PLANETMINT_DATABASE_BACKEND`` environment variable.
# Include the backend interfaces
from planetmint.backend import schema, query # noqa
from planetmint.backend.connection import Connection
from planetmint.backend.connection import connect, Connection

View File

@ -11,14 +11,14 @@ from planetmint.transactions.common.exceptions import ConfigurationError
BACKENDS = { # This is path to MongoDBClass
'tarantool_db': 'planetmint.backend.tarantool.connection.TarantoolDB',
'tarantool_db': 'planetmint.backend.tarantool.connection.TarantoolDBConnection',
'localmongodb': 'planetmint.backend.localmongodb.connection.LocalMongoDBConnection'
}
logger = logging.getLogger(__name__)
def Connection(host: str = None, port: int = None, login: str = None, password: str = None, backend: str = None,
def connect(host: str = None, port: int = None, login: str = None, password: str = None, backend: str = None,
**kwargs):
backend = backend
if not backend and kwargs and kwargs.get("backend"):
@ -75,3 +75,80 @@ def _kwargs_parser(key, kwargs):
if kwargs.get(key):
return kwargs[key]
return None
class Connection:
"""Connection class interface.
All backend implementations should provide a connection class that inherits
from and implements this class.
"""
def __init__(self, host=None, port=None, dbname=None,
connection_timeout=None, max_tries=None,
**kwargs):
"""Create a new :class:`~.Connection` instance.
Args:
host (str): the host to connect to.
port (int): the port to connect to.
dbname (str): the name of the database to use.
connection_timeout (int, optional): the milliseconds to wait
until timing out the database connection attempt.
Defaults to 5000ms.
max_tries (int, optional): how many tries before giving up,
if 0 then try forever. Defaults to 3.
**kwargs: arbitrary keyword arguments provided by the
configuration's ``database`` settings
"""
dbconf = Config().get()['database']
self.host = host or dbconf['host']
self.port = port or dbconf['port']
self.dbname = dbname or dbconf['name']
self.connection_timeout = connection_timeout if connection_timeout is not None \
else dbconf['connection_timeout']
self.max_tries = max_tries if max_tries is not None else dbconf['max_tries']
self.max_tries_counter = range(self.max_tries) if self.max_tries != 0 else repeat(0)
self._conn = None
@property
def conn(self):
if self._conn is None:
self.connect()
return self._conn
def run(self, query):
"""Run a query.
Args:
query: the query to run
Raises:
:exc:`~DuplicateKeyError`: If the query fails because of a
duplicate key constraint.
:exc:`~OperationFailure`: If the query fails for any other
reason.
:exc:`~ConnectionError`: If the connection to the database
fails.
"""
raise NotImplementedError()
def connect(self):
"""Try to connect to the database.
Raises:
:exc:`~ConnectionError`: If the connection to the database
fails.
"""
attempt = 0
for i in self.max_tries_counter:
attempt += 1
try:
self._conn = self._connect()
except ConnectionError as exc:
logger.warning('Attempt %s/%s. Connection to %s:%s failed after %sms.',
attempt, self.max_tries if self.max_tries != 0 else '',
self.host, self.port, self.connection_timeout)
if attempt == self.max_tries:
logger.critical('Cannot connect to the Database. Giving up.')
raise ConnectionError() from exc
else:
break

View File

@ -13,88 +13,10 @@ from planetmint.backend.exceptions import (DuplicateKeyError,
ConnectionError)
from planetmint.transactions.common.exceptions import ConfigurationError
from planetmint.utils import Lazy
from planetmint.backend.connection import Connection
logger = logging.getLogger(__name__)
class Connection:
"""Connection class interface.
All backend implementations should provide a connection class that inherits
from and implements this class.
"""
def __init__(self, host=None, port=None, dbname=None,
connection_timeout=None, max_tries=None,
**kwargs):
"""Create a new :class:`~.Connection` instance.
Args:
host (str): the host to connect to.
port (int): the port to connect to.
dbname (str): the name of the database to use.
connection_timeout (int, optional): the milliseconds to wait
until timing out the database connection attempt.
Defaults to 5000ms.
max_tries (int, optional): how many tries before giving up,
if 0 then try forever. Defaults to 3.
**kwargs: arbitrary keyword arguments provided by the
configuration's ``database`` settings
"""
dbconf = Config().get()['database']
self.host = host or dbconf['host']
self.port = port or dbconf['port']
self.dbname = dbname or dbconf['name']
self.connection_timeout = connection_timeout if connection_timeout is not None \
else dbconf['connection_timeout']
self.max_tries = max_tries if max_tries is not None else dbconf['max_tries']
self.max_tries_counter = range(self.max_tries) if self.max_tries != 0 else repeat(0)
self._conn = None
@property
def conn(self):
if self._conn is None:
self.connect()
return self._conn
def run(self, query):
"""Run a query.
Args:
query: the query to run
Raises:
:exc:`~DuplicateKeyError`: If the query fails because of a
duplicate key constraint.
:exc:`~OperationFailure`: If the query fails for any other
reason.
:exc:`~ConnectionError`: If the connection to the database
fails.
"""
raise NotImplementedError()
def connect(self):
"""Try to connect to the database.
Raises:
:exc:`~ConnectionError`: If the connection to the database
fails.
"""
attempt = 0
for i in self.max_tries_counter:
attempt += 1
try:
self._conn = self._connect()
except ConnectionError as exc:
logger.warning('Attempt %s/%s. Connection to %s:%s failed after %sms.',
attempt, self.max_tries if self.max_tries != 0 else '',
self.host, self.port, self.connection_timeout)
if attempt == self.max_tries:
logger.critical('Cannot connect to the Database. Giving up.')
raise ConnectionError() from exc
else:
break
class LocalMongoDBConnection(Connection):
def __init__(self, replicaset=None, ssl=None, login=None, password=None,

View File

@ -9,7 +9,7 @@ from functools import singledispatch
import logging
from planetmint.config import Config
from planetmint.backend.connection import Connection
from planetmint.backend.connection import connect
from planetmint.transactions.common.exceptions import ValidationError
from planetmint.transactions.common.utils import (
validate_all_values_for_key_in_obj, validate_all_values_for_key_in_list)
@ -79,7 +79,7 @@ def init_database(connection=None, dbname=None):
configuration.
"""
connection = connection or Connection()
connection = connection or connect()
dbname = dbname or Config().get()['database']['name']
create_database(connection, dbname)

View File

@ -8,11 +8,12 @@ import tarantool
from planetmint.config import Config
from planetmint.transactions.common.exceptions import ConfigurationError
from planetmint.backend.connection import Connection
logger = logging.getLogger(__name__)
class TarantoolDB:
class TarantoolDBConnection(Connection):
def __init__(self, host: str = "localhost", port: int = 3303, user: str = None, password: str = None, **kwargs):
try:
self.host = host

View File

@ -10,14 +10,14 @@ from operator import itemgetter
from planetmint.backend import query
from planetmint.backend.utils import module_dispatch_registrar
from planetmint.backend.tarantool.connection import TarantoolDB
from planetmint.backend.tarantool.connection import TarantoolDBConnection
from planetmint.backend.tarantool.transaction.tools import TransactionCompose, TransactionDecompose
from json import dumps, loads
register_query = module_dispatch_registrar(query)
@register_query(TarantoolDB)
@register_query(TarantoolDBConnection)
def _group_transaction_by_ids(connection, txids: list):
txspace = connection.space("transactions")
inxspace = connection.space("inputs")
@ -53,7 +53,7 @@ def _group_transaction_by_ids(connection, txids: list):
return _transactions
@register_query(TarantoolDB)
@register_query(TarantoolDBConnection)
def store_transactions(connection, signed_transactions: list):
txspace = connection.space("transactions")
inxspace = connection.space("inputs")
@ -86,26 +86,26 @@ def store_transactions(connection, signed_transactions: list):
assetsxspace.insert(txtuples["asset"])
@register_query(TarantoolDB)
@register_query(TarantoolDBConnection)
def get_transaction(connection, transaction_id: str):
_transactions = _group_transaction_by_ids(txids=[transaction_id], connection=connection)
return next(iter(_transactions), None)
@register_query(TarantoolDB)
@register_query(TarantoolDBConnection)
def get_transactions(connection, transactions_ids: list):
_transactions = _group_transaction_by_ids(txids=transactions_ids, connection=connection)
return _transactions
@register_query(TarantoolDB)
@register_query(TarantoolDBConnection)
def store_metadatas(connection, metadata: list):
space = connection.space("meta_data")
for meta in metadata:
space.insert((meta["id"], meta["data"] if not "metadata" in meta else meta["metadata"]))
@register_query(TarantoolDB)
@register_query(TarantoolDBConnection)
def get_metadata(connection, transaction_ids: list):
_returned_data = []
space = connection.space("meta_data")
@ -116,7 +116,7 @@ def get_metadata(connection, transaction_ids: list):
return _returned_data if len(_returned_data) > 0 else None
@register_query(TarantoolDB)
@register_query(TarantoolDBConnection)
def store_asset(connection, asset):
space = connection.space("assets")
convert = lambda obj: obj if isinstance(obj, tuple) else (obj, obj["id"], obj["id"])
@ -126,7 +126,7 @@ def store_asset(connection, asset):
print("DUPLICATE ERROR")
@register_query(TarantoolDB)
@register_query(TarantoolDBConnection)
def store_assets(connection, assets: list):
space = connection.space("assets")
convert = lambda obj: obj if isinstance(obj, tuple) else (obj, obj["id"], obj["id"])
@ -137,7 +137,7 @@ def store_assets(connection, assets: list):
print(f"EXCEPTION : {ex} ")
@register_query(TarantoolDB)
@register_query(TarantoolDBConnection)
def get_asset(connection, asset_id: str):
space = connection.space("assets")
_data = space.select(asset_id, index="txid_search")
@ -145,7 +145,7 @@ def get_asset(connection, asset_id: str):
return _data[0][0] if len(_data) > 0 else []
@register_query(TarantoolDB)
@register_query(TarantoolDBConnection)
def get_assets(connection, assets_ids: list) -> list:
_returned_data = []
for _id in list(set(assets_ids)):
@ -154,7 +154,7 @@ def get_assets(connection, assets_ids: list) -> list:
return sorted(_returned_data, key=lambda k: k["id"], reverse=False)
@register_query(TarantoolDB)
@register_query(TarantoolDBConnection)
def get_spent(connection, fullfil_transaction_id: str, fullfil_output_index: str):
space = connection.space("inputs")
_inputs = space.select([fullfil_transaction_id, str(fullfil_output_index)], index="spent_search")
@ -163,7 +163,7 @@ def get_spent(connection, fullfil_transaction_id: str, fullfil_output_index: str
return _transactions
@register_query(TarantoolDB)
@register_query(TarantoolDBConnection)
def get_latest_block(connection): # TODO Here is used DESCENDING OPERATOR
space = connection.space("blocks")
_all_blocks = space.select()
@ -183,7 +183,7 @@ def get_latest_block(connection): # TODO Here is used DESCENDING OPERATOR
return block
@register_query(TarantoolDB)
@register_query(TarantoolDBConnection)
def store_block(connection, block: dict):
space = connection.space("blocks")
block_unique_id = token_hex(8)
@ -195,7 +195,7 @@ def store_block(connection, block: dict):
space.insert((txid, block_unique_id))
@register_query(TarantoolDB)
@register_query(TarantoolDBConnection)
def get_txids_filtered(connection, asset_id: str, operation: str = None,
last_tx: any = None): # TODO here is used 'OR' operator
actions = {
@ -256,7 +256,7 @@ def _remove_text_score(asset):
return asset
@register_query(TarantoolDB)
@register_query(TarantoolDBConnection)
def get_owned_ids(connection, owner: str):
space = connection.space("keys")
_keys = space.select(owner, index="keys_search")
@ -267,7 +267,7 @@ def get_owned_ids(connection, owner: str):
return _transactions
@register_query(TarantoolDB)
@register_query(TarantoolDBConnection)
def get_spending_transactions(connection, inputs):
_transactions = []
@ -280,7 +280,7 @@ def get_spending_transactions(connection, inputs):
return _transactions
@register_query(TarantoolDB)
@register_query(TarantoolDBConnection)
def get_block(connection, block_id=[]):
space = connection.space("blocks")
_block = space.select(block_id, index="block_search", limit=1)
@ -295,7 +295,7 @@ def get_block(connection, block_id=[]):
return {"app_hash": _block[0], "height": _block[1], "transactions": [_tx[0] for _tx in _txblock]}
@register_query(TarantoolDB)
@register_query(TarantoolDBConnection)
def get_block_with_transaction(connection, txid: str):
space = connection.space("blocks_tx")
_all_blocks_tx = space.select(txid, index="id_search")
@ -307,7 +307,7 @@ def get_block_with_transaction(connection, txid: str):
return [{"height": _height[1]} for _height in _block.data]
@register_query(TarantoolDB)
@register_query(TarantoolDBConnection)
def delete_transactions(connection, txn_ids: list):
tx_space = connection.space("transactions")
for _id in txn_ids:
@ -335,7 +335,7 @@ def delete_transactions(connection, txn_ids: list):
assets_space.delete(_id, index="txid_search")
@register_query(TarantoolDB)
@register_query(TarantoolDBConnection)
def store_unspent_outputs(connection, *unspent_outputs: list):
space = connection.space('utxos')
result = []
@ -345,7 +345,7 @@ def store_unspent_outputs(connection, *unspent_outputs: list):
result.append(output.data)
return result
@register_query(TarantoolDB)
@register_query(TarantoolDBConnection)
def delete_unspent_outputs(connection, *unspent_outputs: list):
space = connection.space('utxos')
result = []
@ -356,14 +356,14 @@ def delete_unspent_outputs(connection, *unspent_outputs: list):
return result
@register_query(TarantoolDB)
@register_query(TarantoolDBConnection)
def get_unspent_outputs(connection, query=None): # for now we don't have implementation for 'query'.
space = connection.space('utxos')
_utxos = space.select([]).data
return [loads(utx[2]) for utx in _utxos]
@register_query(TarantoolDB)
@register_query(TarantoolDBConnection)
def store_pre_commit_state(connection, state: dict):
space = connection.space("pre_commits")
_precommit = space.select(state["height"], index="height_search", limit=1)
@ -375,7 +375,7 @@ def store_pre_commit_state(connection, state: dict):
limit=1)
@register_query(TarantoolDB)
@register_query(TarantoolDBConnection)
def get_pre_commit_state(connection):
space = connection.space("pre_commits")
_commit = space.select([], index="id_search").data
@ -385,7 +385,7 @@ def get_pre_commit_state(connection):
return {"height": _commit[1], "transactions": _commit[2]}
@register_query(TarantoolDB)
@register_query(TarantoolDBConnection)
def store_validator_set(conn, validators_update: dict):
space = conn.space("validators")
_validator = space.select(validators_update["height"], index="height_search", limit=1)
@ -397,7 +397,7 @@ def store_validator_set(conn, validators_update: dict):
limit=1)
@register_query(TarantoolDB)
@register_query(TarantoolDBConnection)
def delete_validator_set(connection, height: int):
space = connection.space("validators")
_validators = space.select(height, index="height_search")
@ -405,7 +405,7 @@ def delete_validator_set(connection, height: int):
space.delete(_valid[0])
@register_query(TarantoolDB)
@register_query(TarantoolDBConnection)
def store_election(connection, election_id: str, height: int, is_concluded: bool):
space = connection.space("elections")
space.upsert((election_id, height, is_concluded),
@ -415,7 +415,7 @@ def store_election(connection, election_id: str, height: int, is_concluded: bool
limit=1)
@register_query(TarantoolDB)
@register_query(TarantoolDBConnection)
def store_elections(connection, elections: list):
space = connection.space("elections")
for election in elections:
@ -424,7 +424,7 @@ def store_elections(connection, elections: list):
election["is_concluded"]))
@register_query(TarantoolDB)
@register_query(TarantoolDBConnection)
def delete_elections(connection, height: int):
space = connection.space("elections")
_elections = space.select(height, index="height_search")
@ -432,7 +432,7 @@ def delete_elections(connection, height: int):
space.delete(_elec[0])
@register_query(TarantoolDB)
@register_query(TarantoolDBConnection)
def get_validator_set(connection, height: int = None):
space = connection.space("validators")
_validators = space.select()
@ -446,7 +446,7 @@ def get_validator_set(connection, height: int = None):
return next(iter(sorted(_validators, key=lambda k: k["height"], reverse=True)), None)
@register_query(TarantoolDB)
@register_query(TarantoolDBConnection)
def get_election(connection, election_id: str):
space = connection.space("elections")
_elections = space.select(election_id, index="id_search")
@ -457,7 +457,7 @@ def get_election(connection, election_id: str):
return {"election_id": _election[0], "height": _election[1], "is_concluded": _election[2]}
@register_query(TarantoolDB)
@register_query(TarantoolDBConnection)
def get_asset_tokens_for_public_key(connection, asset_id: str, public_key: str):
space = connection.space("keys")
# _keys = space.select([public_key], index="keys_search")
@ -469,7 +469,7 @@ def get_asset_tokens_for_public_key(connection, asset_id: str, public_key: str):
return _grouped_transactions
@register_query(TarantoolDB)
@register_query(TarantoolDBConnection)
def store_abci_chain(connection, height: int, chain_id: str, is_synced: bool = True):
space = connection.space("abci_chains")
space.upsert((height, is_synced, chain_id),
@ -479,7 +479,7 @@ def store_abci_chain(connection, height: int, chain_id: str, is_synced: bool = T
limit=1)
@register_query(TarantoolDB)
@register_query(TarantoolDBConnection)
def delete_abci_chain(connection, height: int):
space = connection.space("abci_chains")
_chains = space.select(height, index="height_search")
@ -487,7 +487,7 @@ def delete_abci_chain(connection, height: int):
space.delete(_chain[2])
@register_query(TarantoolDB)
@register_query(TarantoolDBConnection)
def get_latest_abci_chain(connection):
space = connection.space("abci_chains")
_all_chains = space.select().data

View File

@ -2,22 +2,22 @@ import warnings
from planetmint.backend.utils import module_dispatch_registrar
from planetmint import backend
from planetmint.backend.tarantool.connection import TarantoolDB
from planetmint.backend.tarantool.connection import TarantoolDBConnection
register_schema = module_dispatch_registrar(backend.schema)
@register_schema(TarantoolDB)
@register_schema(TarantoolDBConnection)
def drop_database(connection, not_used=None):
connection.drop_database()
@register_schema(TarantoolDB)
@register_schema(TarantoolDBConnection)
def create_database(connection, not_used=None):
connection.init_database()
@register_schema(TarantoolDB)
@register_schema(TarantoolDBConnection)
def create_tables(connection, not_used=None):
"""
This function is not necessary for using backend tarantool.

View File

@ -13,7 +13,7 @@ import argparse
import copy
import json
import sys
from planetmint.backend.tarantool.connection import TarantoolDB
from planetmint.backend.tarantool.connection import TarantoolDBConnection
from planetmint.core import rollback
from planetmint.migrations.chain_migration_election import ChainMigrationElection
@ -265,9 +265,9 @@ def run_drop(args):
if response != 'y':
return
from planetmint.backend.connection import Connection
from planetmint.backend.connection import connect
from planetmint.backend import schema
conn = Connection()
conn = connect()
try:
schema.drop_database(conn)
except DatabaseDoesNotExist:

View File

@ -1,5 +1,5 @@
import pytest
from planetmint.backend.connection import Connection
from planetmint.backend.connection import connect
#
@ -27,5 +27,5 @@ from planetmint.backend.connection import Connection
@pytest.fixture
def db_conn():
conn = Connection()
conn = connect()
return conn

View File

@ -3,7 +3,7 @@
# SPDX-License-Identifier: (Apache-2.0 AND CC-BY-4.0)
# Code is Apache-2.0 and docs are CC-BY-4.0
from planetmint.backend.tarantool.connection import TarantoolDB
from planetmint.backend.tarantool.connection import TarantoolDBConnection
def _check_spaces_by_list(conn, space_names):

View File

@ -8,9 +8,9 @@ import pytest
def test_get_connection_raises_a_configuration_error(monkeypatch):
from planetmint.transactions.common.exceptions import ConfigurationError
from planetmint.backend.connection import Connection
from planetmint.backend.connection import connect
with pytest.raises(ConfigurationError):
Connection('msaccess', 'localhost', '1337', 'mydb')
connect('msaccess', 'localhost', '1337', 'mydb')
with pytest.raises(ConfigurationError):
# We need to force a misconfiguration here
@ -18,4 +18,4 @@ def test_get_connection_raises_a_configuration_error(monkeypatch):
{'catsandra':
'planetmint.backend.meowmeow.Catsandra'})
Connection('catsandra', 'localhost', '1337', 'mydb')
connect('catsandra', 'localhost', '1337', 'mydb')

View File

@ -94,9 +94,9 @@ def test__run_init(mocker):
init_db_mock = mocker.patch(
'planetmint.backend.tarantool.connection.TarantoolDB.init_database')
from planetmint.backend.connection import Connection
from planetmint.backend.connection import connect
conn = Connection()
conn = connect()
conn.init_database()
init_db_mock.assert_called_once_with()

View File

@ -18,8 +18,8 @@ import codecs
from collections import namedtuple
from logging import getLogger
from logging.config import dictConfig
from planetmint.backend.connection import Connection
from planetmint.backend.tarantool.connection import TarantoolDB
from planetmint.backend.connection import connect
from planetmint.backend.tarantool.connection import TarantoolDBConnection
import pytest
# from pymongo import MongoClient
@ -123,10 +123,10 @@ def _configure_planetmint(request):
@pytest.fixture(scope='session')
def _setup_database(_configure_planetmint): # TODO Here is located setup database
from planetmint.backend.tarantool.connection import TarantoolDB
from planetmint.backend.tarantool.connection import TarantoolDBConnection
print('Deleting `{}` database')
db_conn = Connection()
db_conn = connect()
db_conn.drop_database()
db_conn.init_database()
print('Finished deleting ``')
@ -134,17 +134,17 @@ def _setup_database(_configure_planetmint): # TODO Here is located setup databa
yield
print('Initializing test db')
db_conn2 = Connection()
db_conn2 = connect()
db_conn2.drop_database()
print('Finishing init database')
@pytest.fixture
def _bdb(_setup_database):
from planetmint.backend import Connection
from planetmint.backend import connect
from planetmint.transactions.common.memoize import to_dict, from_dict
from planetmint.models import Transaction
conn = Connection()
conn = connect()
yield
to_dict.cache_clear()
@ -383,8 +383,8 @@ def db_name(db_config):
@pytest.fixture
def db_conn():
from planetmint.backend import Connection
return Connection()
from planetmint.backend import connect
return connect()
@pytest.fixture
@ -533,7 +533,7 @@ def unspent_outputs(unspent_output_0, unspent_output_1, unspent_output_2):
@pytest.fixture
def tarantool_client(db_context): # TODO Here add TarantoolConnectionClass
return TarantoolDB(host=db_context.host, port=db_context.port)
return TarantoolDBConnection(host=db_context.host, port=db_context.port)
# @pytest.fixture

View File

@ -47,13 +47,13 @@ class TestBigchainApi(object):
def test_double_inclusion(self, b, alice):
from planetmint.backend.exceptions import OperationError
from tarantool.error import DatabaseError
from planetmint.backend.tarantool.connection import TarantoolDB
from planetmint.backend.tarantool.connection import TarantoolDBConnection
tx = Create.generate([alice.public_key], [([alice.public_key], 1)])
tx = tx.sign([alice.private_key])
b.store_bulk_transactions([tx])
if isinstance(b.connection, TarantoolDB):
if isinstance(b.connection, TarantoolDBConnection):
with pytest.raises(DatabaseError):
b.store_bulk_transactions([tx])
else:
@ -61,9 +61,9 @@ class TestBigchainApi(object):
b.store_bulk_transactions([tx])
def test_text_search(self, b, alice):
from planetmint.backend.tarantool.connection import TarantoolDB
from planetmint.backend.tarantool.connection import TarantoolDBConnection
if isinstance(b.connection, TarantoolDB):
if isinstance(b.connection, TarantoolDBConnection):
warnings.warn(" :::::: This function is used only with :::::: ")
return

View File

@ -93,7 +93,7 @@ def test_filter_unspent_outputs(b, user_pk, user_sk):
def test_outputs_query_key_order(b, user_pk, user_sk, user2_pk, user2_sk):
from planetmint import backend
from planetmint.backend.connection import Connection
from planetmint.backend.connection import connect
from planetmint.backend import query
tx1 = Create.generate([user_pk],
@ -119,7 +119,7 @@ def test_outputs_query_key_order(b, user_pk, user_sk, user2_pk, user2_sk):
# clean the transaction, metdata and asset collection
# conn = connect()
connection = Connection()
connection = connect()
# conn.run(conn.collection('transactions').delete_many({}))
# conn.run(conn.collection('metadata').delete_many({}))
# conn.run(conn.collection('assets').delete_many({}))

View File

@ -28,10 +28,10 @@ from planetmint.lib import Block
def test_asset_is_separated_from_transaciton(b):
import copy
from planetmint.transactions.common.crypto import generate_key_pair
from planetmint.backend.tarantool.connection import TarantoolDB
from planetmint.backend.tarantool.connection import TarantoolDBConnection
if isinstance(b.connection, TarantoolDB):
if isinstance(b.connection, TarantoolDBConnection):
pytest.skip("This specific function is skipped because, assets are stored differently if using Tarantool")
@ -172,14 +172,14 @@ def test_update_utxoset(b, signed_create_tx, signed_transfer_tx, db_conn):
@pytest.mark.bdb
def test_store_transaction(mocker, b, signed_create_tx,
signed_transfer_tx, db_context):
from planetmint.backend.tarantool.connection import TarantoolDB
from planetmint.backend.tarantool.connection import TarantoolDBConnection
mocked_store_asset = mocker.patch('planetmint.backend.query.store_assets')
mocked_store_metadata = mocker.patch(
'planetmint.backend.query.store_metadatas')
mocked_store_transaction = mocker.patch(
'planetmint.backend.query.store_transactions')
b.store_bulk_transactions([signed_create_tx])
if not isinstance(b.connection, TarantoolDB):
if not isinstance(b.connection, TarantoolDBConnection):
mongo_client = MongoClient(host=db_context.host, port=db_context.port)
utxoset = mongo_client[db_context.name]['utxos']
assert utxoset.count_documents({}) == 1
@ -209,7 +209,7 @@ def test_store_transaction(mocker, b, signed_create_tx,
mocked_store_metadata.reset_mock()
mocked_store_transaction.reset_mock()
b.store_bulk_transactions([signed_transfer_tx])
if not isinstance(b.connection, TarantoolDB):
if not isinstance(b.connection, TarantoolDBConnection):
assert utxoset.count_documents({}) == 1
utxo = utxoset.find_one()
assert utxo['transaction_id'] == signed_transfer_tx.id
@ -219,7 +219,7 @@ def test_store_transaction(mocker, b, signed_create_tx,
b.connection,
[{'id': signed_transfer_tx.id, 'metadata': signed_transfer_tx.metadata}],
)
if not isinstance(b.connection, TarantoolDB):
if not isinstance(b.connection, TarantoolDBConnection):
mocked_store_transaction.assert_called_once_with(
b.connection,
[{k: v for k, v in signed_transfer_tx.to_dict().items()
@ -230,7 +230,7 @@ def test_store_transaction(mocker, b, signed_create_tx,
@pytest.mark.bdb
def test_store_bulk_transaction(mocker, b, signed_create_tx,
signed_transfer_tx, db_context):
from planetmint.backend.tarantool.connection import TarantoolDB
from planetmint.backend.tarantool.connection import TarantoolDBConnection
mocked_store_assets = mocker.patch(
'planetmint.backend.query.store_assets')
mocked_store_metadata = mocker.patch(
@ -238,14 +238,14 @@ def test_store_bulk_transaction(mocker, b, signed_create_tx,
mocked_store_transactions = mocker.patch(
'planetmint.backend.query.store_transactions')
b.store_bulk_transactions((signed_create_tx,))
if not isinstance(b.connection, TarantoolDB):
if not isinstance(b.connection, TarantoolDBConnection):
mongo_client = MongoClient(host=db_context.host, port=db_context.port)
utxoset = mongo_client[db_context.name]['utxos']
assert utxoset.count_documents({}) == 1
utxo = utxoset.find_one()
assert utxo['transaction_id'] == signed_create_tx.id
assert utxo['output_index'] == 0
if isinstance(b.connection, TarantoolDB):
if isinstance(b.connection, TarantoolDBConnection):
mocked_store_assets.assert_called_once_with(
b.connection, # signed_create_tx.asset['data'] this was before
[(signed_create_tx.asset, signed_create_tx.id, signed_create_tx.id)],
@ -268,7 +268,7 @@ def test_store_bulk_transaction(mocker, b, signed_create_tx,
mocked_store_metadata.reset_mock()
mocked_store_transactions.reset_mock()
b.store_bulk_transactions((signed_transfer_tx,))
if not isinstance(b.connection, TarantoolDB):
if not isinstance(b.connection, TarantoolDBConnection):
assert utxoset.count_documents({}) == 1
utxo = utxoset.find_one()
assert utxo['transaction_id'] == signed_transfer_tx.id
@ -279,7 +279,7 @@ def test_store_bulk_transaction(mocker, b, signed_create_tx,
[{'id': signed_transfer_tx.id,
'metadata': signed_transfer_tx.metadata}],
)
if not isinstance(b.connection, TarantoolDB):
if not isinstance(b.connection, TarantoolDBConnection):
mocked_store_transactions.assert_called_once_with(
b.connection,
[{k: v for k, v in signed_transfer_tx.to_dict().items()
@ -304,10 +304,10 @@ def test_delete_zero_unspent_outputs(b, utxoset):
@pytest.mark.bdb
def test_delete_one_unspent_outputs(b, utxoset):
from planetmint.backend.tarantool.connection import TarantoolDB
from planetmint.backend.tarantool.connection import TarantoolDBConnection
unspent_outputs, utxo_collection = utxoset
delete_res = b.delete_unspent_outputs(unspent_outputs[0])
if not isinstance(b.connection, TarantoolDB):
if not isinstance(b.connection, TarantoolDBConnection):
assert len(list(delete_res)) == 1
assert utxo_collection.count_documents(
{'$or': [
@ -328,10 +328,10 @@ def test_delete_one_unspent_outputs(b, utxoset):
@pytest.mark.bdb
def test_delete_many_unspent_outputs(b, utxoset):
from planetmint.backend.tarantool.connection import TarantoolDB
from planetmint.backend.tarantool.connection import TarantoolDBConnection
unspent_outputs, utxo_collection = utxoset
delete_res = b.delete_unspent_outputs(*unspent_outputs[::2])
if not isinstance(b.connection, TarantoolDB):
if not isinstance(b.connection, TarantoolDBConnection):
assert len(list(delete_res)) == 2
assert utxo_collection.count_documents(
{'$or': [
@ -360,9 +360,9 @@ def test_store_zero_unspent_output(b, utxo_collection):
@pytest.mark.bdb
def test_store_one_unspent_output(b, unspent_output_1, utxo_collection):
from planetmint.backend.tarantool.connection import TarantoolDB
from planetmint.backend.tarantool.connection import TarantoolDBConnection
res = b.store_unspent_outputs(unspent_output_1)
if not isinstance(b.connection, TarantoolDB):
if not isinstance(b.connection, TarantoolDBConnection):
assert res.acknowledged
assert len(list(res)) == 1
assert utxo_collection.count_documents(
@ -378,9 +378,9 @@ def test_store_one_unspent_output(b, unspent_output_1, utxo_collection):
@pytest.mark.bdb
def test_store_many_unspent_outputs(b, unspent_outputs, utxo_collection):
from planetmint.backend.tarantool.connection import TarantoolDB
from planetmint.backend.tarantool.connection import TarantoolDBConnection
res = b.store_unspent_outputs(*unspent_outputs)
if not isinstance(b.connection, TarantoolDB):
if not isinstance(b.connection, TarantoolDBConnection):
assert res.acknowledged
assert len(list(res)) == 3
assert utxo_collection.count_documents(

View File

@ -65,7 +65,7 @@ def test_bigchain_class_default_initialization(config):
def test_bigchain_class_initialization_with_parameters():
from planetmint import Planetmint
from planetmint.backend import Connection
from planetmint.backend import connect
from planetmint.validation import BaseValidationRules
init_db_kwargs = {
'backend': 'localmongodb',
@ -73,7 +73,7 @@ def test_bigchain_class_initialization_with_parameters():
'port': 12345,
'name': 'this_is_the_db_name',
}
connection = Connection(**init_db_kwargs)
connection = connect(**init_db_kwargs)
planet = Planetmint(connection=connection)
assert planet.connection == connection
assert planet.connection.host == init_db_kwargs['host']