forked from pytest-dev/plugincompat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_web.py
293 lines (231 loc) · 10.2 KB
/
test_web.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
from flask import json
import pytest
from web import PlugsStorage
class MemoryStorage(object):
"""
Mock class that simulates a PlugsStorage instance. This class simply
holds the values in memory, and is used by TestView as a mock to the real
storage class, allowing the view to be tested without a database.
"""
def __init__(self):
self._results = []
def add_test_result(self, result):
required = {'name', 'version', 'env', 'pytest', 'status'}
if not required.issubset(result):
raise TypeError('Invalid keys given: %s' % result.keys())
for index, existing_result in enumerate(self._results):
if (existing_result['name'] == result['name'] and
existing_result['version'] == result['version'] and
existing_result['env'] == result['env'] and
existing_result['pytest'] == result['pytest']):
self._results[index] = result
break
else:
self._results.append(result)
def get_all_results(self):
return self._results
def get_test_results(self, name, version):
result = []
for entry in self._results:
if entry['name'] == name and entry['version'] == version:
result.append(entry)
return result
def drop_all(self):
self._results[:] = []
@pytest.fixture(params=[PlugsStorage, MemoryStorage])
def storage(request):
"""
Initializes a Storage for execution in a test environment. This fixture
will instantiate the storage class given in the parameters. This way we
ensure both the real implementation and dummy implementation work in the
same way.
When initializing the real PlugsStorage(), it will use a test-specific
data-base to avoid any conflicts between tests and to avoid clashing with
real databases.
"""
if request.param is PlugsStorage:
db_name = 'testing-{}'.format(request.node.name)
plugs_storage = PlugsStorage(default_db_name=db_name)
plugs_storage.__TESTING__ = True
def finalizer():
plugs_storage.get_connection().drop_database(db_name)
request.addfinalizer(finalizer)
return plugs_storage
elif request.param is MemoryStorage:
memory_storage = MemoryStorage()
return memory_storage
else:
assert False
def make_result_data(**kwparams):
result = {
'name': 'mylib',
'version': '1.0',
'env': 'py27',
'pytest': '2.3',
'status': 'ok',
'output': 'all commands:\nok',
'description': 'a generic library',
}
result.update(kwparams)
return result
#noinspection PyShadowingNames
class TestPlugsStorage(object):
"""
Tests for PlugsStorage class
"""
def test_add_test_result(self, storage):
"""
:type storage: PlugsStorage
"""
assert list(storage.get_all_results()) == []
with pytest.raises(TypeError):
# missing "env" key
invalid_result = make_result_data()
del invalid_result['env']
storage.add_test_result(invalid_result)
result1 = make_result_data()
storage.add_test_result(result1)
assert storage.get_test_results('mylib', '1.0') == [result1]
result2 = make_result_data(env='py33', status='failed')
storage.add_test_result(result2)
assert storage.get_test_results('mylib', '1.0') == [result1, result2]
result3 = make_result_data(env='py33')
storage.add_test_result(result3)
assert storage.get_test_results('mylib', '1.0') == [result1, result3]
result4 = make_result_data(version='1.1', output='another output')
storage.add_test_result(result4)
assert storage.get_test_results('mylib', '1.0') == [result1, result3]
assert storage.get_test_results('mylib', '1.1') == [result4]
def test_invalid_lib(self, storage):
assert storage.get_test_results('foobar', '1.0') == []
def test_get_all_results(self, storage):
assert list(storage.get_all_results()) == []
result1 = make_result_data()
storage.add_test_result(result1)
assert list(storage.get_all_results()) == [result1]
result2 = make_result_data(version='1.1')
storage.add_test_result(result2)
assert list(storage.get_all_results()) == [result1, result2]
result3 = make_result_data(name='myotherlib')
storage.add_test_result(result3)
assert list(storage.get_all_results()) == [result1, result2, result3]
def test_drop_all(self, storage):
result1 = make_result_data()
result2 = make_result_data(version='1.1')
storage.add_test_result(result1)
storage.add_test_result(result2)
assert len(storage.get_all_results()) == 2
storage.drop_all()
assert len(storage.get_all_results()) == 0
@pytest.fixture
def patched_storage(monkeypatch):
import web
result = MemoryStorage()
monkeypatch.setattr(web, 'get_storage_for_view', lambda: result)
return result
@pytest.fixture
def client():
from web import app
result = app.test_client()
app.testing = True
return result
#noinspection PyShadowingNames
class TestView(object):
"""
Tests web views for pytest-plugs
"""
def post_result(self, client, result):
response = client.post('/', data=json.dumps(result),
content_type='application/json')
assert response.status_code == 200
def test_index_post(self, client, patched_storage):
result1 = make_result_data()
self.post_result(client, result1)
assert patched_storage.get_all_results() == [result1]
result2 = make_result_data(env='py33')
self.post_result(client, result2)
assert patched_storage.get_all_results() == [result1, result2]
result3 = make_result_data(name='myotherlib')
result4 = make_result_data(name='myotherlib', env='py33')
self.post_result(client, [result3, result4])
assert patched_storage.get_all_results() == [result1, result2, result3,
result4]
def test_index_get_json(self, client, patched_storage):
self.post_result(client, make_result_data())
self.post_result(client, make_result_data(env='py33'))
self.post_result(client, make_result_data(name='myotherlib'))
self.post_result(client,
make_result_data(name='myotherlib', env='py33'))
assert len(patched_storage.get_all_results()) == 4
response = client.get('/?json=1')
results = json.loads(response.data)['data']
assert set(x['name'] for x in results) == {'mylib', 'myotherlib'}
def test_get_render_namespace(self):
from web import get_namespace_for_rendering
# post results; only the latest lib versions should be rendered
all_results = [
make_result_data(),
make_result_data(env='py33', status='failed'),
make_result_data(name='myotherlib', version='1.8', pytest='2.4'),
make_result_data(env='py33', pytest='2.4'),
make_result_data(env='py33', pytest='2.4', version='0.6'),
make_result_data(env='py33', pytest='2.4', version='0.7'),
make_result_data(env='py33', pytest='2.4', version='0.8'),
make_result_data(name='myotherlib', version='2.0', pytest='2.4',
description='my other library',
output='output for myotherlib-2.0'),
]
bad_result = make_result_data(name='badlib')
del bad_result['output']
all_results.append(bad_result)
output_ok = 'all commands:\nok'
lib_data = {
('badlib-1.0', 'py27', '2.3'): (
'ok', '<no output available>', 'a generic library'),
('mylib-1.0', 'py27', '2.3'): (
'ok', output_ok, 'a generic library'),
('mylib-1.0', 'py33', '2.3'): (
'failed', output_ok, 'a generic library'),
('mylib-1.0', 'py33', '2.4'): (
'ok', output_ok, 'a generic library'),
('myotherlib-2.0', 'py27', '2.4'): (
'ok', 'output for myotherlib-2.0', 'my other library'),
}
statuses = {k: status for (k, (status, output, desc)) in
lib_data.items()}
outputs = {k: output for (k, (status, output, desc)) in
lib_data.items()}
descriptions = {k: desc for (k, (status, output, desc)) in
lib_data.items()}
assert get_namespace_for_rendering(all_results) == {
'python_versions': ['py27', 'py33'],
'lib_names': ['badlib-1.0', 'mylib-1.0', 'myotherlib-2.0'],
'pytest_versions': ['2.3', '2.4'],
'latest_pytest_ver': '2.4',
'statuses': statuses,
'outputs': outputs,
'descriptions': descriptions,
}
def test_get_with_empty_database(self, client, patched_storage):
assert len(patched_storage.get_all_results()) == 0
response = client.get('/')
assert response.data == 'Database is empty'
def test_get_output(self, client):
self.post_result(client, make_result_data())
response = client.get('/output/mylib-1.0?py=py27&pytest=2.3')
assert response.data == 'all commands:\nok'
assert response.content_type == 'text/plain'
def test_get_output_missing(self, client, patched_storage):
post_data = make_result_data()
del post_data['output']
patched_storage.add_test_result(post_data)
response = client.get('/output/mylib-1.0?py=py27&pytest=2.3')
assert response.data == '<no output available>'
assert response.content_type == 'text/plain'
def test_status_image_help(self, client):
response = client.get('/status/mylib-1.0')
assert 'Plugin Status Images' in response.data
def test_status_image(self, client):
self.post_result(client, make_result_data())
response = client.get('/status/mylib-1.0?py=py27&pytest=2.3')
assert response.content_type == 'image/png'