adjusted connect config error handling and test case for new signature

Signed-off-by: Lorenz Herzberger <lorenzherzberger@gmail.com>
This commit is contained in:
Lorenz Herzberger 2022-06-22 10:13:02 +02:00
parent fb0b3121bf
commit 124b8dd227
No known key found for this signature in database
GPG Key ID: FA5EE906EB55316A
2 changed files with 14 additions and 13 deletions

View File

@ -12,7 +12,7 @@ from planetmint.config import Config
from planetmint.backend.exceptions import ConnectionError
from planetmint.transactions.common.exceptions import ConfigurationError
BACKENDS = { # This is path to MongoDBClass
BACKENDS = {
'tarantool_db': 'planetmint.backend.tarantool.connection.TarantoolDBConnection',
'localmongodb': 'planetmint.backend.localmongodb.connection.LocalMongoDBConnection'
}
@ -22,14 +22,18 @@ logger = logging.getLogger(__name__)
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"):
backend = kwargs["backend"]
try:
backend = backend
if not backend and kwargs and kwargs.get("backend"):
backend = kwargs["backend"]
if backend and backend != Config().get()["database"]["backend"]:
Config().init_config(backend)
else:
backend = Config().get()["database"]["backend"]
if backend and backend != Config().get()["database"]["backend"]:
Config().init_config(backend)
else:
backend = Config().get()["database"]["backend"]
except KeyError:
logger.info("Backend {} not supported".format(backend))
raise ConfigurationError
host = host or Config().get()["database"]["host"] if not kwargs.get("host") else kwargs["host"]
port = port or Config().get()['database']['port'] if not kwargs.get("port") else kwargs["port"]
@ -72,9 +76,6 @@ def connect(host: str = None, port: int = None, login: str = None, password: str
except tarantool.error.NetworkError as network_err:
print(f"Host {host}:{port} can't be reached.\n{network_err}")
raise network_err
except:
logger.info('Exception in _connect(): {}')
raise ConfigurationError
def _kwargs_parser(key, kwargs):

View File

@ -10,7 +10,7 @@ def test_get_connection_raises_a_configuration_error(monkeypatch):
from planetmint.transactions.common.exceptions import ConfigurationError
from planetmint.backend.connection import connect
with pytest.raises(ConfigurationError):
connect('msaccess', 'localhost', '1337', 'mydb')
connect('localhost', '1337', 'mydb', 'password', 'msaccess')
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'})
connect('catsandra', 'localhost', '1337', 'mydb')
connect('localhost', '1337', 'mydb', 'password', 'catsandra')