conftest.py
196 lines
| 5.7 KiB
| text/x-python
|
PythonLexer
r0 | # -*- coding: utf-8 -*- | |||
r112 | # Copyright 2010 - 2017 RhodeCode GmbH and the AppEnlight project authors | |||
r0 | # | |||
r112 | # 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 | ||||
r0 | # | |||
r112 | # http://www.apache.org/licenses/LICENSE-2.0 | |||
r0 | # | |||
r112 | # 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. | ||||
r0 | ||||
import mock | ||||
import os | ||||
import pytest | ||||
import transaction | ||||
from datetime import datetime | ||||
from alembic.config import Config | ||||
from alembic import command | ||||
from collections import OrderedDict | ||||
from zope.sqlalchemy import mark_changed | ||||
from pyramid.paster import get_appsettings | ||||
from pyramid import testing | ||||
@pytest.fixture | ||||
r145 | def base_app(request, mocker): | |||
# disable email sending | ||||
r153 | mocker.patch("pyramid_mailer.mailer_factory_from_settings", mocker.Mock()) | |||
r145 | ||||
r0 | from appenlight import main | |||
import transaction | ||||
r153 | ||||
r0 | current_dir = os.path.dirname(os.path.abspath(__file__)) | |||
r153 | path = os.path.join( | |||
current_dir, "../../../../", os.environ.get("APPENLIGHT_INI", "testing.ini") | ||||
) | ||||
r0 | # appsettings from ini | |||
app_settings = get_appsettings(path, name="appenlight") | ||||
app = main({}, **app_settings) | ||||
r153 | app_request = testing.DummyRequest(base_url="https://appenlight.com") | |||
r0 | app_request.tm = transaction.manager | |||
app_request.add_flash_to_headers = mock.Mock() | ||||
testing.setUp(registry=app.registry, request=app_request) | ||||
def teardown(): | ||||
testing.tearDown() | ||||
request.addfinalizer(teardown) | ||||
return app | ||||
@pytest.fixture | ||||
def with_migrations(request, base_app): | ||||
settings = base_app.registry.settings | ||||
alembic_cfg = Config() | ||||
r153 | alembic_cfg.set_main_option("script_location", "ziggurat_foundations:migrations") | |||
r0 | alembic_cfg.set_main_option("sqlalchemy.url", settings["sqlalchemy.url"]) | |||
command.upgrade(alembic_cfg, "head") | ||||
alembic_cfg = Config() | ||||
alembic_cfg.set_main_option("script_location", "appenlight:migrations") | ||||
alembic_cfg.set_main_option("sqlalchemy.url", settings["sqlalchemy.url"]) | ||||
command.upgrade(alembic_cfg, "head") | ||||
for plugin_name, config in base_app.registry.appenlight_plugins.items(): | ||||
r153 | if config["sqlalchemy_migrations"]: | |||
r0 | alembic_cfg = Config() | |||
alembic_cfg.set_main_option( | ||||
r153 | "script_location", config["sqlalchemy_migrations"] | |||
) | ||||
alembic_cfg.set_main_option( | ||||
"sqlalchemy.url", base_app.registry.settings["sqlalchemy.url"] | ||||
) | ||||
r0 | command.upgrade(alembic_cfg, "head") | |||
@pytest.fixture | ||||
def default_data(base_app): | ||||
from appenlight.models.services.config import ConfigService | ||||
from appenlight.lib import get_callable | ||||
r153 | ||||
r0 | transaction.begin() | |||
ConfigService.setup_default_values() | ||||
for plugin_name, config in base_app.registry.appenlight_plugins.items(): | ||||
r153 | if config["default_values_setter"]: | |||
get_callable(config["default_values_setter"])() | ||||
r0 | transaction.commit() | |||
@pytest.fixture | ||||
r142 | def clean_tables(request): | |||
r145 | from appenlight.models import Base, DBSession | |||
r142 | def fin(): | |||
tables = Base.metadata.tables.keys() | ||||
transaction.begin() | ||||
for t in tables: | ||||
r153 | if not t.startswith("alembic_"): | |||
DBSession.execute("truncate %s cascade" % t) | ||||
r142 | session = DBSession() | |||
mark_changed(session) | ||||
transaction.commit() | ||||
request.addfinalizer(fin) | ||||
r0 | ||||
@pytest.fixture | ||||
def default_user(): | ||||
r145 | from appenlight.models import DBSession | |||
r0 | from appenlight.models.user import User | |||
from appenlight.models.auth_token import AuthToken | ||||
r153 | ||||
r0 | transaction.begin() | |||
r142 | session = DBSession() | |||
r153 | user = User(id=1, user_name="testuser", status=1, email="foo@barbaz99.com") | |||
r142 | session.add(user) | |||
r153 | token = AuthToken(token="1234") | |||
r0 | user.auth_tokens.append(token) | |||
r142 | session.execute("SELECT nextval('users_id_seq')") | |||
r0 | transaction.commit() | |||
return user | ||||
@pytest.fixture | ||||
def default_application(default_user): | ||||
r145 | from appenlight.models import DBSession | |||
r0 | from appenlight.models.application import Application | |||
transaction.begin() | ||||
r142 | session = DBSession() | |||
r153 | application = Application(resource_id=1, resource_name="testapp", api_key="xxxx") | |||
r142 | session.add(application) | |||
r0 | default_user.resources.append(application) | |||
r142 | session.execute("SELECT nextval('resources_resource_id_seq')") | |||
r0 | transaction.commit() | |||
return application | ||||
@pytest.fixture | ||||
def report_type_matrix(): | ||||
from appenlight.models.report import REPORT_TYPE_MATRIX | ||||
r153 | ||||
r0 | return REPORT_TYPE_MATRIX | |||
@pytest.fixture | ||||
def chart_series(): | ||||
series = [] | ||||
for x in range(1, 7): | ||||
r153 | tmp_list = [("key", "X"), ("0_1", x)] | |||
r0 | if x % 2 == 0: | |||
r153 | tmp_list.append(("0_2", x)) | |||
r0 | if x % 3 == 0: | |||
r153 | tmp_list.append(("0_3", x)) | |||
r0 | ||||
r153 | series.append(OrderedDict(tmp_list)) | |||
r0 | return series | |||
@pytest.fixture | ||||
def log_schema(): | ||||
from appenlight.validators import LogListSchema | ||||
r153 | ||||
r0 | schema = LogListSchema().bind(utcnow=datetime.utcnow()) | |||
return schema | ||||
r153 | ||||
r0 | @pytest.fixture | |||
def general_metrics_schema(): | ||||
from appenlight.validators import GeneralMetricsListSchema | ||||
r153 | ||||
r0 | schema = GeneralMetricsListSchema().bind(utcnow=datetime.utcnow()) | |||
return schema | ||||
r153 | ||||
r0 | @pytest.fixture | |||
def request_metrics_schema(): | ||||
from appenlight.validators import MetricsListSchema | ||||
r153 | ||||
r0 | schema = MetricsListSchema().bind(utcnow=datetime.utcnow()) | |||
return schema | ||||
r153 | ||||
r0 | @pytest.fixture | |||
def report_05_schema(): | ||||
from appenlight.validators import ReportListSchema_0_5 | ||||
r153 | ||||
r0 | schema = ReportListSchema_0_5().bind(utcnow=datetime.utcnow()) | |||
return schema | ||||