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