rookeries/tests/conftest.py

70 lines
2.0 KiB
Python

"""
Configurations for all tests.
:copyright: Copyright 2013-2017, Dorian Pula <dorian.pula@amber-penguin-software.ca>
:license: AGPL v3+
"""
import collections
import pytest
from sqlalchemy import orm
import rookeries
from rookeries import database as rookeries_db
from rookeries.sites import models as site_models
from tests import utils
TestSiteInfo = collections.namedtuple('TestSiteInfo', ['menu', 'landing_page'])
def pytest_addoption(parser):
help_message = "Hostname and port of server under test: localhost, 127.0.0.1, myremote-server.net..."
parser.addoption("--server-host", action="store", default="rookeries:5000", help=help_message)
@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():
app = rookeries.make_rookeries_app(quiet_log=True)
rookeries_db.db.init_app(app)
with app.app_context():
rookeries_db.db.create_all()
yield rookeries_db.db.engine
@pytest.fixture(scope="session")
def test_site(db_engine) -> TestSiteInfo:
"""
Generates a test site.
:param db_engine: The database engine to start a session from.
:return: A tuple with information about the test site.
"""
# TODO: Include way to generate and save the menu appropriately
menu = f'/blocks/{utils.generate_random_suffix()}'
landing_page = f'/pages/{utils.generate_random_suffix()}'
session = orm.sessionmaker(bind=db_engine, autocommit=True)()
session.begin()
site = site_models.Site()
session.add(site)
session.commit()
session.close()
return TestSiteInfo(menu=menu, landing_page=landing_page)