def test_get_client_wrong_uri(): ## GIVEN a mongomock MongoClient ## WHEN checking if the connection works ## THEN assert that the connection is valid since it is a mock with pytest.raises(InvalidURI): get_client(uri="wierd uri")
def database_group(context, username, password, host, port, uri, database): db_config = {} db_config['host'] = host or context.obj.get('host') or 'localhost' db_config['port'] = port or context.obj.get('port') or 27017 db_config['uri'] = uri or context.obj.get('uri') db_config['username'] = username or context.obj.get('username') db_config['password'] = password or context.obj.get('password') if uri: LOG.info("Establishing connection with uri {}".format( db_config['uri'])) else: LOG.info("Establishing connection with host {}, on port {}".format( db_config['host'], db_config['port'])) mutacc_client = mongo_adapter.get_client(**db_config) if not mongo_adapter.check_connection(mutacc_client): LOG.warning("Connection could not be established") context.abort() context.obj['client'] = mutacc_client context.obj['adapter'] = MutaccAdapter(client=mutacc_client, db_name=context.obj['db_name'])
def database(uri: str = None, db_name: str = None) -> MongoAdapter: uri = uri or settings.uri db_name = db_name or settings.db_name try: client = get_client(uri=uri, ) except DB_Error: raise HTTPException(status_code=status.HTTP_503_SERVICE_UNAVAILABLE, detail="Could not connect to database") return MongoAdapter(client, db_name=db_name)
def cli(ctx, database, username, password, authdb, port, host, uri, verbose, config, test, genome_build): """loqusdb: manage a local variant count database.""" loglevel = "INFO" if verbose: loglevel = "DEBUG" coloredlogs.install(level=loglevel) LOG.info("Running loqusdb version %s", __version__) configs = {} if config: try: configs = yaml.safe_load(config) except yaml.YAMLError as err: LOG.warning(err) ctx.abort() uri = configs.get("uri") or uri if test: uri = "mongomock://" try: client = get_client( host=configs.get("host") or host, port=configs.get("port") or port, username=configs.get("username") or username, password=configs.get("password") or password, authdb=authdb or database or "loqusdb", uri=uri, ) except DB_Error as err: LOG.warning(err) ctx.abort() database = configs.get("db_name") or database if not database: database = "loqusdb" if uri: uri_info = uri_parser.parse_uri(uri) database = uri_info.get("database") adapter = MongoAdapter(client, db_name=database) genome_build = genome_build or configs.get("genome_build") or GRCH37 ctx.obj = {} ctx.obj["db"] = database if uri: ctx.obj["uri"] = uri else: ctx.obj["port"] = port ctx.obj["host"] = host ctx.obj["adapter"] = adapter ctx.obj["version"] = __version__ ctx.obj["genome_build"] = genome_build
def cli(ctx, database, username, password, authdb, port, host, uri, verbose, config, test, genome_build): """loqusdb: manage a local variant count database.""" loglevel = "INFO" if verbose: loglevel = "DEBUG" coloredlogs.install(level=loglevel) LOG.info("Running loqusdb version %s", __version__) configs = {} if config: try: configs = yaml.safe_load(config) except yaml.YAMLError as err: LOG.warning(err) ctx.abort() uri = configs.get('uri') or uri if test: uri = "mongomock://" try: client = get_client( host=configs.get('host') or host, port=configs.get('port') or port, username=configs.get('username') or username, password=configs.get('password') or password, authdb=authdb or database or 'loqusdb', uri=uri, ) except DB_Error as err: LOG.warning(err) ctx.abort() database = configs.get('db_name') or database if not database: database = 'loqusdb' if uri: uri_info = uri_parser.parse_uri(uri) database = uri_info.get('database') adapter = MongoAdapter(client, db_name=database) genome_build = genome_build or configs.get('genome_build') or GRCH37 ctx.obj = {} ctx.obj['db'] = database ctx.obj['user'] = username ctx.obj['password'] = password ctx.obj['port'] = port ctx.obj['host'] = host ctx.obj['adapter'] = adapter ctx.obj['version'] = __version__ ctx.obj['genome_build'] = genome_build
def test_get_connection(): ## GIVEN a mongomock URI uri = "mongomock://" ## WHEN getting a client mock_client = get_client(uri=uri) ## THEN assert that the client is a mock client assert isinstance(mock_client, mongomock.MongoClient) ## THEN assert that the connection is valid since it is a mock assert check_connection(mock_client)
# -*- coding: utf-8 -*- import os import logging from flask import Flask from mongo_adapter import (get_client, check_connection) from paneldb.adapter.mongo import PanelAdapter logging.basicConfig(level=logging.INFO) LOG = logging.getLogger(__name__) #configuration files are relative to the instance folder app = Flask(__name__, template_folder='server/templates', instance_relative_config=True) app.config.from_pyfile('paneldb_config.cfg') client = get_client(mongodb=app.config['MONGODB_DATABASE_NAME'], timeout=20) app.adapter = PanelAdapter(client=client, db_name=app.config['MONGODB_DATABASE_NAME']) import paneldb.server.views