Show More
@@ -1,180 +1,185 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | |
|
3 | 3 | # Copyright 2010 - 2017 RhodeCode GmbH and the AppEnlight project authors |
|
4 | 4 | # |
|
5 | 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
|
6 | 6 | # you may not use this file except in compliance with the License. |
|
7 | 7 | # You may obtain a copy of the License at |
|
8 | 8 | # |
|
9 | 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
|
10 | 10 | # |
|
11 | 11 | # Unless required by applicable law or agreed to in writing, software |
|
12 | 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
|
13 | 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
14 | 14 | # See the License for the specific language governing permissions and |
|
15 | 15 | # limitations under the License. |
|
16 | 16 | |
|
17 | 17 | import mock |
|
18 | 18 | import os |
|
19 | 19 | import pytest |
|
20 | 20 | import transaction |
|
21 | 21 | from datetime import datetime |
|
22 | 22 | |
|
23 | 23 | from alembic.config import Config |
|
24 | 24 | from alembic import command |
|
25 | 25 | from collections import OrderedDict |
|
26 | 26 | from zope.sqlalchemy import mark_changed |
|
27 | 27 | from pyramid.paster import get_appsettings |
|
28 | 28 | from pyramid import testing |
|
29 | 29 | |
|
30 | 30 | from appenlight.models import Base, DBSession |
|
31 | 31 | |
|
32 | 32 | |
|
33 | 33 | @pytest.fixture |
|
34 | 34 | def base_app(request): |
|
35 | 35 | from appenlight import main |
|
36 | 36 | import transaction |
|
37 | 37 | current_dir = os.path.dirname(os.path.abspath(__file__)) |
|
38 | 38 | path = os.path.join(current_dir, '../../../../', |
|
39 | 39 | os.environ.get("APPENLIGHT_INI", 'testing.ini')) |
|
40 | 40 | # appsettings from ini |
|
41 | 41 | app_settings = get_appsettings(path, name="appenlight") |
|
42 | 42 | app = main({}, **app_settings) |
|
43 | 43 | app_request = testing.DummyRequest(base_url='https://appenlight.com') |
|
44 | 44 | app_request.tm = transaction.manager |
|
45 | 45 | app_request.add_flash_to_headers = mock.Mock() |
|
46 | 46 | testing.setUp(registry=app.registry, request=app_request) |
|
47 | 47 | |
|
48 | 48 | def teardown(): |
|
49 | 49 | testing.tearDown() |
|
50 | 50 | |
|
51 | 51 | request.addfinalizer(teardown) |
|
52 | 52 | |
|
53 | 53 | return app |
|
54 | 54 | |
|
55 | 55 | |
|
56 | 56 | @pytest.fixture |
|
57 | 57 | def with_migrations(request, base_app): |
|
58 | 58 | settings = base_app.registry.settings |
|
59 | 59 | alembic_cfg = Config() |
|
60 | 60 | alembic_cfg.set_main_option("script_location", |
|
61 | 61 | "ziggurat_foundations:migrations") |
|
62 | 62 | alembic_cfg.set_main_option("sqlalchemy.url", settings["sqlalchemy.url"]) |
|
63 | 63 | command.upgrade(alembic_cfg, "head") |
|
64 | 64 | alembic_cfg = Config() |
|
65 | 65 | alembic_cfg.set_main_option("script_location", "appenlight:migrations") |
|
66 | 66 | alembic_cfg.set_main_option("sqlalchemy.url", settings["sqlalchemy.url"]) |
|
67 | 67 | command.upgrade(alembic_cfg, "head") |
|
68 | 68 | |
|
69 | 69 | for plugin_name, config in base_app.registry.appenlight_plugins.items(): |
|
70 | 70 | if config['sqlalchemy_migrations']: |
|
71 | 71 | alembic_cfg = Config() |
|
72 | 72 | alembic_cfg.set_main_option("script_location", |
|
73 | 73 | config['sqlalchemy_migrations']) |
|
74 | 74 | alembic_cfg.set_main_option( |
|
75 | 75 | "sqlalchemy.url", |
|
76 | 76 | base_app.registry.settings["sqlalchemy.url"]) |
|
77 | 77 | command.upgrade(alembic_cfg, "head") |
|
78 | 78 | |
|
79 | 79 | |
|
80 | 80 | @pytest.fixture |
|
81 | 81 | def default_data(base_app): |
|
82 | 82 | from appenlight.models.services.config import ConfigService |
|
83 | 83 | from appenlight.lib import get_callable |
|
84 | 84 | transaction.begin() |
|
85 | 85 | ConfigService.setup_default_values() |
|
86 | 86 | for plugin_name, config in base_app.registry.appenlight_plugins.items(): |
|
87 | 87 | if config['default_values_setter']: |
|
88 | 88 | get_callable(config['default_values_setter'])() |
|
89 | 89 | transaction.commit() |
|
90 | 90 | |
|
91 | 91 | |
|
92 | 92 | @pytest.fixture |
|
93 | def clean_tables(): | |
|
93 | def clean_tables(request): | |
|
94 | def fin(): | |
|
94 | 95 | tables = Base.metadata.tables.keys() |
|
95 | 96 | transaction.begin() |
|
96 | 97 | for t in tables: |
|
97 | 98 | if not t.startswith('alembic_'): |
|
98 | 99 | DBSession.execute('truncate %s cascade' % t) |
|
99 | 100 | session = DBSession() |
|
100 | 101 | mark_changed(session) |
|
101 | 102 | transaction.commit() |
|
102 | 103 | |
|
104 | request.addfinalizer(fin) | |
|
105 | ||
|
103 | 106 | |
|
104 | 107 | @pytest.fixture |
|
105 | 108 | def default_user(): |
|
106 | 109 | from appenlight.models.user import User |
|
107 | 110 | from appenlight.models.auth_token import AuthToken |
|
108 | 111 | transaction.begin() |
|
112 | session = DBSession() | |
|
109 | 113 | user = User(id=1, |
|
110 | 114 | user_name='testuser', |
|
111 | 115 | status=1, |
|
112 | 116 | email='foo@barbaz99.com') |
|
113 |
|
|
|
117 | session.add(user) | |
|
114 | 118 | token = AuthToken(token='1234') |
|
115 | 119 | user.auth_tokens.append(token) |
|
116 | DBSession.flush() | |
|
120 | session.execute("SELECT nextval('users_id_seq')") | |
|
117 | 121 | transaction.commit() |
|
118 | 122 | return user |
|
119 | 123 | |
|
120 | 124 | |
|
121 | 125 | @pytest.fixture |
|
122 | 126 | def default_application(default_user): |
|
123 | 127 | from appenlight.models.application import Application |
|
124 | 128 | |
|
125 | 129 | transaction.begin() |
|
130 | session = DBSession() | |
|
126 | 131 | application = Application( |
|
127 | 132 | resource_id=1, resource_name='testapp', api_key='xxxx') |
|
128 |
|
|
|
133 | session.add(application) | |
|
129 | 134 | default_user.resources.append(application) |
|
130 | DBSession.flush() | |
|
135 | session.execute("SELECT nextval('resources_resource_id_seq')") | |
|
131 | 136 | transaction.commit() |
|
132 | 137 | return application |
|
133 | 138 | |
|
134 | 139 | |
|
135 | 140 | @pytest.fixture |
|
136 | 141 | def report_type_matrix(): |
|
137 | 142 | from appenlight.models.report import REPORT_TYPE_MATRIX |
|
138 | 143 | return REPORT_TYPE_MATRIX |
|
139 | 144 | |
|
140 | 145 | |
|
141 | 146 | @pytest.fixture |
|
142 | 147 | def chart_series(): |
|
143 | 148 | series = [] |
|
144 | 149 | |
|
145 | 150 | for x in range(1, 7): |
|
146 | 151 | tmp_list = [('key', 'X'), ('0_1', x)] |
|
147 | 152 | if x % 2 == 0: |
|
148 | 153 | tmp_list.append(('0_2', x)) |
|
149 | 154 | if x % 3 == 0: |
|
150 | 155 | tmp_list.append(('0_3', x)) |
|
151 | 156 | |
|
152 | 157 | series.append( |
|
153 | 158 | OrderedDict(tmp_list) |
|
154 | 159 | ) |
|
155 | 160 | return series |
|
156 | 161 | |
|
157 | 162 | |
|
158 | 163 | @pytest.fixture |
|
159 | 164 | def log_schema(): |
|
160 | 165 | from appenlight.validators import LogListSchema |
|
161 | 166 | schema = LogListSchema().bind(utcnow=datetime.utcnow()) |
|
162 | 167 | return schema |
|
163 | 168 | |
|
164 | 169 | @pytest.fixture |
|
165 | 170 | def general_metrics_schema(): |
|
166 | 171 | from appenlight.validators import GeneralMetricsListSchema |
|
167 | 172 | schema = GeneralMetricsListSchema().bind(utcnow=datetime.utcnow()) |
|
168 | 173 | return schema |
|
169 | 174 | |
|
170 | 175 | @pytest.fixture |
|
171 | 176 | def request_metrics_schema(): |
|
172 | 177 | from appenlight.validators import MetricsListSchema |
|
173 | 178 | schema = MetricsListSchema().bind(utcnow=datetime.utcnow()) |
|
174 | 179 | return schema |
|
175 | 180 | |
|
176 | 181 | @pytest.fixture |
|
177 | 182 | def report_05_schema(): |
|
178 | 183 | from appenlight.validators import ReportListSchema_0_5 |
|
179 | 184 | schema = ReportListSchema_0_5().bind(utcnow=datetime.utcnow()) |
|
180 | 185 | return schema |
General Comments 0
You need to be logged in to leave comments.
Login now