56 lines
1.7 KiB
Python
56 lines
1.7 KiB
Python
"""
|
|
Extra configurations to allow for parameterized tests.
|
|
|
|
:copyright: Copyright 2013-2016, Dorian Pula <dorian.pula@amber-penguin-software.ca>
|
|
:license: AGPL v3+
|
|
"""
|
|
|
|
import sys
|
|
import traceback
|
|
|
|
import pytest
|
|
import sqlalchemy
|
|
from sqlalchemy import exc as sql_error
|
|
|
|
from rookeries import database as rookeries_db
|
|
|
|
|
|
def pytest_addoption(parser):
|
|
parser.addoption("--server-host", action="store", default="api:5000",
|
|
help="Hostname and port of server under test: localhost, 127.0.0.1, myremote-server.net...")
|
|
parser.addoption("--db-connection",
|
|
action="store", default=rookeries_db.DEFAULT_DB_CONNECTION, help="DB connection info")
|
|
|
|
|
|
@pytest.fixture
|
|
def api_base_uri(request):
|
|
"""
|
|
The base URI of the API server to build request to API. The base URI is the format http://hostname:port, provided
|
|
by the server hostname and server port pytest parameters.
|
|
|
|
:param request: The request for the test.
|
|
:return: The base URI of the API server.
|
|
"""
|
|
server_host = request.config.getoption("--server-host")
|
|
return f"http://{server_host}"
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
def db_engine(request):
|
|
try:
|
|
db_connection_uri = request.config.getoption('--db-connection')
|
|
_db_engine = sqlalchemy.create_engine(db_connection_uri, echo=True)
|
|
rookeries_db.ModelBase.metadata.create_all(_db_engine)
|
|
|
|
yield _db_engine
|
|
|
|
except sql_error.OperationalError as err:
|
|
print(f'Unable to connect to the database because of "{err}" \n Stack: \n')
|
|
traceback.print_tb(sys.exc_info()[2])
|
|
raise err
|
|
|
|
except Exception as err:
|
|
print(f'Unable to run server test because of "{err}" \n Stack: \n')
|
|
traceback.print_tb(sys.exc_info()[2])
|
|
raise err
|