Scheduled maintenance on Saturday, 27 September 2025, from 07:00 AM to 4:00 PM GMT (09:00 AM to 6:00 PM CEST) - some services may be unavailable -

Skip to content
Snippets Groups Projects
conftest.py 3.31 KiB
Newer Older
  • Learn to ignore specific revisions
  • Lluis Gifre Renom's avatar
    Lluis Gifre Renom committed
    # Copyright 2021-2023 H2020 TeraFlow (https://www.teraflow-h2020.eu/)
    #
    # Licensed under the Apache License, Version 2.0 (the "License");
    # you may not use this file except in compliance with the License.
    # You may obtain a copy of the License at
    #
    #      http://www.apache.org/licenses/LICENSE-2.0
    #
    # Unless required by applicable law or agreed to in writing, software
    # distributed under the License is distributed on an "AS IS" BASIS,
    # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    # See the License for the specific language governing permissions and
    # limitations under the License.
    
    
    Lluis Gifre Renom's avatar
    Lluis Gifre Renom committed
    import os, pytest, sqlalchemy
    
    Lluis Gifre Renom's avatar
    Lluis Gifre Renom committed
    from _pytest.config import Config
    from _pytest.terminal import TerminalReporter
    
    Lluis Gifre Renom's avatar
    Lluis Gifre Renom committed
    from typing import Tuple
    
    Lluis Gifre Renom's avatar
    Lluis Gifre Renom committed
    from common.Constants import ServiceNameEnum
    from common.Settings import (
        ENVVAR_SUFIX_SERVICE_HOST, ENVVAR_SUFIX_SERVICE_PORT_GRPC, ENVVAR_SUFIX_SERVICE_PORT_HTTP, get_env_var_name,
        get_service_port_grpc, get_service_port_http)
    from common.message_broker.Factory import get_messagebroker_backend, BackendEnum as MessageBrokerBackendEnum
    from common.message_broker.MessageBroker import MessageBroker
    
    Lluis Gifre Renom's avatar
    Lluis Gifre Renom committed
    from common.method_wrappers.Decorator import MetricsPool
    
    Lluis Gifre Renom's avatar
    Lluis Gifre Renom committed
    from context.client.ContextClient import ContextClient
    from context.service.ContextService import ContextService
    
    Lluis Gifre Renom's avatar
    Lluis Gifre Renom committed
    from context.service.database.Engine import Engine
    
    Lluis Gifre Renom's avatar
    Lluis Gifre Renom committed
    from context.service.database.models._Base import rebuild_database
    
    LOCAL_HOST = '127.0.0.1'
    GRPC_PORT = 10000 + int(get_service_port_grpc(ServiceNameEnum.CONTEXT))   # avoid privileged ports
    HTTP_PORT = 10000 + int(get_service_port_http(ServiceNameEnum.CONTEXT))   # avoid privileged ports
    
    os.environ[get_env_var_name(ServiceNameEnum.CONTEXT, ENVVAR_SUFIX_SERVICE_HOST     )] = str(LOCAL_HOST)
    os.environ[get_env_var_name(ServiceNameEnum.CONTEXT, ENVVAR_SUFIX_SERVICE_PORT_GRPC)] = str(GRPC_PORT)
    os.environ[get_env_var_name(ServiceNameEnum.CONTEXT, ENVVAR_SUFIX_SERVICE_PORT_HTTP)] = str(HTTP_PORT)
    
    @pytest.fixture(scope='session')
    def context_db_mb(request) -> Tuple[sqlalchemy.engine.Engine, MessageBroker]:   # pylint: disable=unused-argument
        _db_engine = Engine.get_engine()
        Engine.drop_database(_db_engine)
        Engine.create_database(_db_engine)
        rebuild_database(_db_engine)
    
        _msg_broker = MessageBroker(get_messagebroker_backend(backend=MessageBrokerBackendEnum.INMEMORY))
        yield _db_engine, _msg_broker
        _msg_broker.terminate()
    
    
    Lluis Gifre Renom's avatar
    Lluis Gifre Renom committed
    RAW_METRICS : MetricsPool = None
    
    Lluis Gifre Renom's avatar
    Lluis Gifre Renom committed
    
    @pytest.fixture(scope='session')
    
    Lluis Gifre Renom's avatar
    Lluis Gifre Renom committed
    def context_service(
        context_db_mb : Tuple[sqlalchemy.engine.Engine, MessageBroker]  # pylint: disable=redefined-outer-name
    ):
    
    Lluis Gifre Renom's avatar
    Lluis Gifre Renom committed
        global RAW_METRICS # pylint: disable=global-statement
        _service = ContextService(context_db_mb[0], context_db_mb[1])
        RAW_METRICS = _service.context_servicer._get_metrics()
        _service.start()
        yield _service
        _service.stop()
    
    @pytest.fixture(scope='session')
    
    Lluis Gifre Renom's avatar
    Lluis Gifre Renom committed
    def context_client(context_service : ContextService): # pylint: disable=redefined-outer-name,unused-argument
    
    Lluis Gifre Renom's avatar
    Lluis Gifre Renom committed
        _client = ContextClient()
        yield _client
        _client.close()
    
    @pytest.hookimpl(hookwrapper=True)
    def pytest_terminal_summary(
        terminalreporter : TerminalReporter, exitstatus : int, config : Config  # pylint: disable=unused-argument
    ):
        yield
    
        print('')
        print('Performance Results:')
    
    Lluis Gifre Renom's avatar
    Lluis Gifre Renom committed
        print(RAW_METRICS.get_pretty_table().get_string())