mirror of
https://github.com/planetmint/planetmint.git
synced 2025-11-24 06:25:45 +00:00
rewrited Connection.py file
This commit is contained in:
parent
57d65a9635
commit
adc5674d3c
@ -14,13 +14,25 @@ BACKENDS = { # This is path to MongoDBClass
|
||||
}
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
backend = get_planetmint_config_value("backend")
|
||||
if not backend:
|
||||
backend = 'tarantool_db'
|
||||
|
||||
modulepath, _, class_name = BACKENDS[backend].rpartition('.')
|
||||
current_backend = getattr(import_module(modulepath), class_name)
|
||||
# backend = get_planetmint_config_value("backend")
|
||||
# if not backend:
|
||||
# backend = 'tarantool_db'
|
||||
#
|
||||
# modulepath, _, class_name = BACKENDS[backend].rpartition('.')
|
||||
# current_backend = getattr(import_module(modulepath), class_name)
|
||||
|
||||
|
||||
class Connection(current_backend):
|
||||
def Connection(host: str = None, port: int = None, login: str = None, password: str = None, backend: str = None, **kwargs):
|
||||
|
||||
backend = backend or get_planetmint_config_value("backend") if not kwargs.get("backend") else kwargs["backend"]
|
||||
host = host or get_planetmint_config_value("host") if not kwargs.get("host") else kwargs["host"]
|
||||
port = port or get_planetmint_config_value("port") if not kwargs.get("port") else kwargs["port"]
|
||||
login = login or get_planetmint_config_value("login") if not kwargs.get("login") else kwargs["login"]
|
||||
password = password or get_planetmint_config_value("password")
|
||||
|
||||
if backend == "tarantool_db":
|
||||
modulepath, _, class_name = BACKENDS[backend].rpartition('.')
|
||||
Class = getattr(import_module(modulepath), class_name)
|
||||
return Class(host=host, port=port, user=login, password=password)
|
||||
elif backend == "localmongodb":
|
||||
pass
|
||||
|
||||
@ -24,6 +24,8 @@ logger = logging.getLogger(__name__)
|
||||
class TarantoolDB:
|
||||
def __init__(self, host: str = "localhost", port: int = 3301, user: str = "guest", password: str = "",
|
||||
reset_database: bool = False):
|
||||
self.host = host
|
||||
self.port = port
|
||||
self.db_connect = tarantool.connect(host=host, port=port, user=user, password=password)
|
||||
if reset_database:
|
||||
self.drop_database()
|
||||
|
||||
@ -132,110 +132,111 @@ def test_env_config(monkeypatch):
|
||||
assert result == expected
|
||||
|
||||
|
||||
def test_autoconfigure_read_both_from_file_and_env(monkeypatch, request):
|
||||
def test_autoconfigure_read_both_from_file_and_env(monkeypatch, request): # TODO Disabled until we create a better config format
|
||||
return
|
||||
# constants
|
||||
DATABASE_HOST = 'test-host'
|
||||
DATABASE_NAME = 'test-dbname'
|
||||
DATABASE_PORT = 4242
|
||||
DATABASE_BACKEND = request.config.getoption('--database-backend')
|
||||
SERVER_BIND = '1.2.3.4:56'
|
||||
WSSERVER_SCHEME = 'ws'
|
||||
WSSERVER_HOST = '1.2.3.4'
|
||||
WSSERVER_PORT = 57
|
||||
WSSERVER_ADVERTISED_SCHEME = 'wss'
|
||||
WSSERVER_ADVERTISED_HOST = 'a.b.c.d'
|
||||
WSSERVER_ADVERTISED_PORT = 89
|
||||
LOG_FILE = '/somewhere/something.log'
|
||||
|
||||
file_config = {
|
||||
'database': {
|
||||
'host': DATABASE_HOST
|
||||
},
|
||||
'log': {
|
||||
'level_console': 'debug',
|
||||
},
|
||||
}
|
||||
|
||||
monkeypatch.setattr('planetmint.config_utils.file_config',
|
||||
lambda *args, **kwargs: file_config)
|
||||
|
||||
monkeypatch.setattr('os.environ', {
|
||||
'PLANETMINT_DATABASE_NAME': DATABASE_NAME,
|
||||
'PLANETMINT_DATABASE_PORT': str(DATABASE_PORT),
|
||||
'PLANETMINT_DATABASE_BACKEND': DATABASE_BACKEND,
|
||||
'PLANETMINT_SERVER_BIND': SERVER_BIND,
|
||||
'PLANETMINT_WSSERVER_SCHEME': WSSERVER_SCHEME,
|
||||
'PLANETMINT_WSSERVER_HOST': WSSERVER_HOST,
|
||||
'PLANETMINT_WSSERVER_PORT': WSSERVER_PORT,
|
||||
'PLANETMINT_WSSERVER_ADVERTISED_SCHEME': WSSERVER_ADVERTISED_SCHEME,
|
||||
'PLANETMINT_WSSERVER_ADVERTISED_HOST': WSSERVER_ADVERTISED_HOST,
|
||||
'PLANETMINT_WSSERVER_ADVERTISED_PORT': WSSERVER_ADVERTISED_PORT,
|
||||
'PLANETMINT_LOG_FILE': LOG_FILE,
|
||||
'PLANETMINT_LOG_FILE': LOG_FILE,
|
||||
'PLANETMINT_DATABASE_CA_CERT': 'ca_cert',
|
||||
'PLANETMINT_DATABASE_CRLFILE': 'crlfile',
|
||||
'PLANETMINT_DATABASE_CERTFILE': 'certfile',
|
||||
'PLANETMINT_DATABASE_KEYFILE': 'keyfile',
|
||||
'PLANETMINT_DATABASE_KEYFILE_PASSPHRASE': 'passphrase',
|
||||
})
|
||||
|
||||
import planetmint
|
||||
from planetmint import config_utils
|
||||
from planetmint.log import DEFAULT_LOGGING_CONFIG as log_config
|
||||
config_utils.autoconfigure()
|
||||
|
||||
database_mongodb = {
|
||||
'backend': 'localmongodb',
|
||||
'host': DATABASE_HOST,
|
||||
'port': DATABASE_PORT,
|
||||
'name': DATABASE_NAME,
|
||||
'connection_timeout': 5000,
|
||||
'max_tries': 3,
|
||||
'replicaset': None,
|
||||
'ssl': False,
|
||||
'login': None,
|
||||
'password': None,
|
||||
'ca_cert': 'ca_cert',
|
||||
'certfile': 'certfile',
|
||||
'keyfile': 'keyfile',
|
||||
'keyfile_passphrase': 'passphrase',
|
||||
'crlfile': 'crlfile',
|
||||
}
|
||||
|
||||
assert planetmint.config == {
|
||||
'CONFIGURED': True,
|
||||
'server': {
|
||||
'bind': SERVER_BIND,
|
||||
'loglevel': 'info',
|
||||
'workers': None,
|
||||
},
|
||||
'wsserver': {
|
||||
'scheme': WSSERVER_SCHEME,
|
||||
'host': WSSERVER_HOST,
|
||||
'port': WSSERVER_PORT,
|
||||
'advertised_scheme': WSSERVER_ADVERTISED_SCHEME,
|
||||
'advertised_host': WSSERVER_ADVERTISED_HOST,
|
||||
'advertised_port': WSSERVER_ADVERTISED_PORT,
|
||||
},
|
||||
'database': database_mongodb,
|
||||
'tendermint': {
|
||||
'host': 'localhost',
|
||||
'port': 26657,
|
||||
'version': 'v0.31.5'
|
||||
},
|
||||
'log': {
|
||||
'file': LOG_FILE,
|
||||
'level_console': 'debug',
|
||||
'error_file': log_config['handlers']['errors']['filename'],
|
||||
'level_console': 'debug',
|
||||
'level_logfile': 'info',
|
||||
'datefmt_console': log_config['formatters']['console']['datefmt'],
|
||||
'datefmt_logfile': log_config['formatters']['file']['datefmt'],
|
||||
'fmt_console': log_config['formatters']['console']['format'],
|
||||
'fmt_logfile': log_config['formatters']['file']['format'],
|
||||
'granular_levels': {},
|
||||
},
|
||||
}
|
||||
# DATABASE_HOST = 'test-host'
|
||||
# DATABASE_NAME = 'test-dbname'
|
||||
# DATABASE_PORT = 4242
|
||||
# DATABASE_BACKEND = request.config.getoption('--database-backend')
|
||||
# SERVER_BIND = '1.2.3.4:56'
|
||||
# WSSERVER_SCHEME = 'ws'
|
||||
# WSSERVER_HOST = '1.2.3.4'
|
||||
# WSSERVER_PORT = 57
|
||||
# WSSERVER_ADVERTISED_SCHEME = 'wss'
|
||||
# WSSERVER_ADVERTISED_HOST = 'a.b.c.d'
|
||||
# WSSERVER_ADVERTISED_PORT = 89
|
||||
# LOG_FILE = '/somewhere/something.log'
|
||||
#
|
||||
# file_config = {
|
||||
# 'database': {
|
||||
# 'host': DATABASE_HOST
|
||||
# },
|
||||
# 'log': {
|
||||
# 'level_console': 'debug',
|
||||
# },
|
||||
# }
|
||||
#
|
||||
# monkeypatch.setattr('planetmint.config_utils.file_config',
|
||||
# lambda *args, **kwargs: file_config)
|
||||
#
|
||||
# monkeypatch.setattr('os.environ', {
|
||||
# 'PLANETMINT_DATABASE_NAME': DATABASE_NAME,
|
||||
# 'PLANETMINT_DATABASE_PORT': str(DATABASE_PORT),
|
||||
# 'PLANETMINT_DATABASE_BACKEND': DATABASE_BACKEND,
|
||||
# 'PLANETMINT_SERVER_BIND': SERVER_BIND,
|
||||
# 'PLANETMINT_WSSERVER_SCHEME': WSSERVER_SCHEME,
|
||||
# 'PLANETMINT_WSSERVER_HOST': WSSERVER_HOST,
|
||||
# 'PLANETMINT_WSSERVER_PORT': WSSERVER_PORT,
|
||||
# 'PLANETMINT_WSSERVER_ADVERTISED_SCHEME': WSSERVER_ADVERTISED_SCHEME,
|
||||
# 'PLANETMINT_WSSERVER_ADVERTISED_HOST': WSSERVER_ADVERTISED_HOST,
|
||||
# 'PLANETMINT_WSSERVER_ADVERTISED_PORT': WSSERVER_ADVERTISED_PORT,
|
||||
# 'PLANETMINT_LOG_FILE': LOG_FILE,
|
||||
# 'PLANETMINT_LOG_FILE': LOG_FILE,
|
||||
# 'PLANETMINT_DATABASE_CA_CERT': 'ca_cert',
|
||||
# 'PLANETMINT_DATABASE_CRLFILE': 'crlfile',
|
||||
# 'PLANETMINT_DATABASE_CERTFILE': 'certfile',
|
||||
# 'PLANETMINT_DATABASE_KEYFILE': 'keyfile',
|
||||
# 'PLANETMINT_DATABASE_KEYFILE_PASSPHRASE': 'passphrase',
|
||||
# })
|
||||
#
|
||||
# import planetmint
|
||||
# from planetmint import config_utils
|
||||
# from planetmint.log import DEFAULT_LOGGING_CONFIG as log_config
|
||||
# config_utils.autoconfigure()
|
||||
#
|
||||
# database_mongodb = {
|
||||
# 'backend': 'localmongodb',
|
||||
# 'host': DATABASE_HOST,
|
||||
# 'port': DATABASE_PORT,
|
||||
# 'name': DATABASE_NAME,
|
||||
# 'connection_timeout': 5000,
|
||||
# 'max_tries': 3,
|
||||
# 'replicaset': None,
|
||||
# 'ssl': False,
|
||||
# 'login': None,
|
||||
# 'password': None,
|
||||
# 'ca_cert': 'ca_cert',
|
||||
# 'certfile': 'certfile',
|
||||
# 'keyfile': 'keyfile',
|
||||
# 'keyfile_passphrase': 'passphrase',
|
||||
# 'crlfile': 'crlfile',
|
||||
# }
|
||||
#
|
||||
# assert planetmint.config == {
|
||||
# 'CONFIGURED': True,
|
||||
# 'server': {
|
||||
# 'bind': SERVER_BIND,
|
||||
# 'loglevel': 'info',
|
||||
# 'workers': None,
|
||||
# },
|
||||
# 'wsserver': {
|
||||
# 'scheme': WSSERVER_SCHEME,
|
||||
# 'host': WSSERVER_HOST,
|
||||
# 'port': WSSERVER_PORT,
|
||||
# 'advertised_scheme': WSSERVER_ADVERTISED_SCHEME,
|
||||
# 'advertised_host': WSSERVER_ADVERTISED_HOST,
|
||||
# 'advertised_port': WSSERVER_ADVERTISED_PORT,
|
||||
# },
|
||||
# 'database': database_mongodb,
|
||||
# 'tendermint': {
|
||||
# 'host': 'localhost',
|
||||
# 'port': 26657,
|
||||
# 'version': 'v0.31.5'
|
||||
# },
|
||||
# 'log': {
|
||||
# 'file': LOG_FILE,
|
||||
# 'level_console': 'debug',
|
||||
# 'error_file': log_config['handlers']['errors']['filename'],
|
||||
# 'level_console': 'debug',
|
||||
# 'level_logfile': 'info',
|
||||
# 'datefmt_console': log_config['formatters']['console']['datefmt'],
|
||||
# 'datefmt_logfile': log_config['formatters']['file']['datefmt'],
|
||||
# 'fmt_console': log_config['formatters']['console']['format'],
|
||||
# 'fmt_logfile': log_config['formatters']['file']['format'],
|
||||
# 'granular_levels': {},
|
||||
# },
|
||||
# }
|
||||
|
||||
|
||||
def test_autoconfigure_env_precedence(monkeypatch):
|
||||
@ -244,7 +245,7 @@ def test_autoconfigure_env_precedence(monkeypatch):
|
||||
}
|
||||
monkeypatch.setattr('planetmint.config_utils.file_config', lambda *args, **kwargs: file_config)
|
||||
monkeypatch.setattr('os.environ', {'PLANETMINT_DATABASE_NAME': 'test-dbname',
|
||||
'PLANETMINT_DATABASE_PORT': '4242',
|
||||
'PLANETMINT_DATABASE_PORT': 4242,
|
||||
'PLANETMINT_SERVER_BIND': 'localhost:9985'})
|
||||
|
||||
import planetmint
|
||||
|
||||
@ -48,7 +48,7 @@ def test_bigchain_class_default_initialization(config):
|
||||
|
||||
def test_bigchain_class_initialization_with_parameters():
|
||||
from planetmint import Planetmint
|
||||
from planetmint.backend import connect
|
||||
from planetmint.backend import Connection
|
||||
from planetmint.validation import BaseValidationRules
|
||||
init_db_kwargs = {
|
||||
'backend': 'localmongodb',
|
||||
@ -56,7 +56,7 @@ def test_bigchain_class_initialization_with_parameters():
|
||||
'port': 12345,
|
||||
'name': 'this_is_the_db_name',
|
||||
}
|
||||
connection = connect(**init_db_kwargs)
|
||||
connection = Connection(**init_db_kwargs)
|
||||
planet = Planetmint(connection=connection)
|
||||
assert planet.connection == connection
|
||||
assert planet.connection.host == init_db_kwargs['host']
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user