diff --git a/planetmint/backend/connection.py b/planetmint/backend/connection.py index 35cb6ed..400d9f3 100644 --- a/planetmint/backend/connection.py +++ b/planetmint/backend/connection.py @@ -6,6 +6,7 @@ import logging from importlib import import_module from planetmint.config import Config +from planetmint.common.exceptions import ConfigurationError BACKENDS = { # This is path to MongoDBClass 'tarantool_db': 'planetmint.backend.tarantool.connection.TarantoolDB', @@ -30,40 +31,43 @@ def Connection(host: str = None, port: int = None, login: str = None, password: port = port or Config().get()['database']['port'] if not kwargs.get("port") else kwargs["port"] login = login or Config().get()["database"]["login"] if not kwargs.get("login") else kwargs["login"] password = password or Config().get()["database"]["password"] - if backend == "tarantool_db": - modulepath, _, class_name = BACKENDS[backend].rpartition('.') - Class = getattr(import_module(modulepath), class_name) - print("LOGIN " + str(login)) - print("PASSWORD " + str(password)) - return Class(host=host, port=port, user=login, password=password) - elif backend == "localmongodb": - modulepath, _, class_name = BACKENDS[backend].rpartition('.') - Class = getattr(import_module(modulepath), class_name) - print(Config().get()) - dbname = _kwargs_parser(key="name", kwargs=kwargs) or Config().get()['database']['name'] - replicaset = _kwargs_parser(key="replicaset", kwargs=kwargs) or Config().get()['database']['replicaset'] - ssl = _kwargs_parser(key="ssl", kwargs=kwargs) or Config().get()['database']['ssl'] - login = login or Config().get()['database']['login'] if _kwargs_parser(key="login", - kwargs=kwargs) is None else _kwargs_parser( - key="login", kwargs=kwargs) - password = password or Config().get()['database']['password'] if _kwargs_parser(key="password", - kwargs=kwargs) is None else _kwargs_parser( - key="password", kwargs=kwargs) - ca_cert = _kwargs_parser(key="ca_cert", kwargs=kwargs) or Config().get()['database']['ca_cert'] - certfile = _kwargs_parser(key="certfile", kwargs=kwargs) or Config().get()['database']['certfile'] - keyfile = _kwargs_parser(key="keyfile", kwargs=kwargs) or Config().get()['database']['keyfile'] - keyfile_passphrase = _kwargs_parser(key="keyfile_passphrase", kwargs=kwargs) or Config().get()['database'][ - 'keyfile_passphrase'] - crlfile = _kwargs_parser(key="crlfile", kwargs=kwargs) or Config().get()['database']['crlfile'] - max_tries = _kwargs_parser(key="max_tries", kwargs=kwargs) - connection_timeout = _kwargs_parser(key="connection_timeout", kwargs=kwargs) - - return Class(host=host, port=port, dbname=dbname, - max_tries=max_tries, connection_timeout=connection_timeout, - replicaset=replicaset, ssl=ssl, login=login, password=password, - ca_cert=ca_cert, certfile=certfile, keyfile=keyfile, - keyfile_passphrase=keyfile_passphrase, crlfile=crlfile) + try: + if backend == "tarantool_db": + modulepath, _, class_name = BACKENDS[backend].rpartition('.') + Class = getattr(import_module(modulepath), class_name) + print("LOGIN " + str(login)) + print("PASSWORD " + str(password)) + return Class(host=host, port=port, user=login, password=password) + elif backend == "localmongodb": + modulepath, _, class_name = BACKENDS[backend].rpartition('.') + Class = getattr(import_module(modulepath), class_name) + print(Config().get()) + dbname = _kwargs_parser(key="name", kwargs=kwargs) or Config().get()['database']['name'] + replicaset = _kwargs_parser(key="replicaset", kwargs=kwargs) or Config().get()['database']['replicaset'] + ssl = _kwargs_parser(key="ssl", kwargs=kwargs) or Config().get()['database']['ssl'] + login = login or Config().get()['database']['login'] if _kwargs_parser(key="login", + kwargs=kwargs) is None else _kwargs_parser( + key="login", kwargs=kwargs) + password = password or Config().get()['database']['password'] if _kwargs_parser(key="password", + kwargs=kwargs) is None else _kwargs_parser( + key="password", kwargs=kwargs) + ca_cert = _kwargs_parser(key="ca_cert", kwargs=kwargs) or Config().get()['database']['ca_cert'] + certfile = _kwargs_parser(key="certfile", kwargs=kwargs) or Config().get()['database']['certfile'] + keyfile = _kwargs_parser(key="keyfile", kwargs=kwargs) or Config().get()['database']['keyfile'] + keyfile_passphrase = _kwargs_parser(key="keyfile_passphrase", kwargs=kwargs) or Config().get()['database'][ + 'keyfile_passphrase'] + crlfile = _kwargs_parser(key="crlfile", kwargs=kwargs) or Config().get()['database']['crlfile'] + max_tries = _kwargs_parser(key="max_tries", kwargs=kwargs) + connection_timeout = _kwargs_parser(key="connection_timeout", kwargs=kwargs) + return Class(host=host, port=port, dbname=dbname, + max_tries=max_tries, connection_timeout=connection_timeout, + replicaset=replicaset, ssl=ssl, login=login, password=password, + ca_cert=ca_cert, certfile=certfile, keyfile=keyfile, + keyfile_passphrase=keyfile_passphrase, crlfile=crlfile) + except: + logger.info('Exception in _connect(): {}') + raise ConfigurationError def _kwargs_parser(key, kwargs): if kwargs.get(key): diff --git a/planetmint/backend/tarantool/connection.py b/planetmint/backend/tarantool/connection.py index d17a6f8..aac2c1d 100644 --- a/planetmint/backend/tarantool/connection.py +++ b/planetmint/backend/tarantool/connection.py @@ -7,6 +7,7 @@ import logging import tarantool from planetmint.config import Config +from planetmint.common.exceptions import ConfigurationError logger = logging.getLogger(__name__) @@ -14,22 +15,29 @@ logger = logging.getLogger(__name__) class TarantoolDB: def __init__(self, host: str = "localhost", port: int = 3303, user: str = None, password: str = None, reset_database: bool = False): - self.host = host - self.port = port - # TODO add user support later on - print(f"host : {host}") - print(f"port : {port}") - # self.db_connect = tarantool.connect(host=host, port=port, user=user, password=password) - self.db_connect = tarantool.connect(host=self.host, port=self.port) - self.init_path = Config().get()["database"]["init_config"]["absolute_path"] - self.drop_path = Config().get()["database"]["drop_config"]["absolute_path"] - if reset_database: - self.drop_database() - self.init_database() - self._reconnect() - self.SPACE_NAMES = ["abci_chains", "assets", "blocks", "blocks_tx", - "elections", "meta_data", "pre_commits", "validators", - "transactions", "inputs", "outputs", "keys"] + try: + self.host = host + self.port = port + # TODO add user support later on + print(f"host : {host}") + print(f"port : {port}") + # self.db_connect = tarantool.connect(host=host, port=port, user=user, password=password) + #TODO : raise configuraiton error if the connection cannot be established + self.db_connect = tarantool.connect(host=self.host, port=self.port) + print( f"connection : {self.db_connect}") + self.init_path = Config().get()["database"]["init_config"]["absolute_path"] + self.drop_path = Config().get()["database"]["drop_config"]["absolute_path"] + if reset_database: + self.drop_database() + self.init_database() + self._reconnect() + self.SPACE_NAMES = ["abci_chains", "assets", "blocks", "blocks_tx", + "elections", "meta_data", "pre_commits", "validators", + "transactions", "inputs", "outputs", "keys"] + except: + logger.info('Exception in _connect(): {}') + raise ConfigurationError + def _reconnect(self): self.db_connect = tarantool.connect(host=self.host, port=self.port) diff --git a/tests/conftest.py b/tests/conftest.py index 5edccc3..dc35da2 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -113,16 +113,23 @@ def _configure_planetmint(request): @pytest.fixture(scope='session') def _setup_database(_configure_planetmint): # TODO Here is located setup database - # from planetmint.backend.connection_tarantool import init_tarantool, drop_tarantool - # print('Initializing test db') - # init_tarantool() - # print('Finishing init database') + from planetmint.backend.tarantool.connection import TarantoolDB + + + print('Deleting `{}` database') + db_conn = TarantoolDB("localhost", 3303) + db_conn.drop_database() + db_conn.init_database() + print('Finished deleting ``') yield - # print('Deleting `{}` database') - # drop_tarantool() - # print('Finished deleting ``') + + print('Initializing test db') + db_conn2 = TarantoolDB("localhost", 3303) + db_conn2.drop_database() + print('Finishing init database') + @pytest.fixture @@ -130,8 +137,7 @@ def _bdb(_setup_database ): from planetmint.backend import Connection from planetmint.common.memoize import to_dict, from_dict from planetmint.models import Transaction - #conn = Connection( backend='tarantool_db', port=3301, host='localhost', login='guest') - #conn = Connection() + conn = Connection() yield to_dict.cache_clear() @@ -510,14 +516,14 @@ def unspent_outputs(unspent_output_0, unspent_output_1, unspent_output_2): return unspent_output_0, unspent_output_1, unspent_output_2 -@pytest.fixture -def mongo_client(db_context): # TODO Here add TarantoolConnectionClass - return None # MongoClient(host=db_context.host, port=db_context.port) - - -@pytest.fixture -def utxo_collection(db_context, mongo_client): - return mongo_client[db_context.name].utxos +#@pytest.fixture +#def mongo_client(db_context): # TODO Here add TarantoolConnectionClass +# return None # MongoClient(host=db_context.host, port=db_context.port) +# +# +#@pytest.fixture +#def utxo_collection(db_context, mongo_client): +# return mongo_client[db_context.name].utxos @pytest.fixture @@ -529,12 +535,12 @@ def dummy_unspent_outputs(): ] -@pytest.fixture -def utxoset(dummy_unspent_outputs, utxo_collection): - res = utxo_collection.insert_many(copy.deepcopy(dummy_unspent_outputs)) - assert res.acknowledged - assert len(res.inserted_ids) == 3 - return dummy_unspent_outputs, utxo_collection +#@pytest.fixture +#def utxoset(dummy_unspent_outputs, utxo_collection): +# res = utxo_collection.insert_many(copy.deepcopy(dummy_unspent_outputs)) +# assert res.acknowledged +# assert len(res.inserted_ids) == 3 +# return dummy_unspent_outputs, utxo_collection @pytest.fixture