forked from GoogleCloudPlatform/getting-started-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconftest.py
More file actions
90 lines (71 loc) · 2.86 KB
/
conftest.py
File metadata and controls
90 lines (71 loc) · 2.86 KB
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
# Copyright 2015 Google Inc.
#
# 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
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# 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.
"""conftest.py is used to define common test fixtures for pytest."""
import bookshelf
import config
from gcloud.exceptions import ServiceUnavailable
from oauth2client.client import HttpAccessTokenRefreshError
import pytest
from retrying import retry
@pytest.yield_fixture(params=['datastore', 'cloudsql', 'mongodb'])
def app(request):
"""This fixtures provides a Flask app instance configured for testing.
Because it's parametric, it will cause every test that uses this fixture
to run three times: one time for each backend (datastore, cloudsql, and
mongodb).
It also ensures the tests run within a request context, allowing
any calls to flask.request, flask.current_app, etc. to work."""
app = bookshelf.create_app(
config,
testing=True,
config_overrides={
'DATA_BACKEND': request.param
})
with app.test_request_context():
yield app
@pytest.yield_fixture
def model(monkeypatch, app):
"""This fixture provides a modified version of the app's model that tracks
all created items and deletes them at the end of the test.
Any tests that directly or indirectly interact with the database should use
this to ensure that resources are properly cleaned up.
Monkeypatch is provided by pytest and used to patch the model's create
method.
The app fixture is needed to provide the configuration and context needed
to get the proper model object.
"""
model = bookshelf.get_model()
# Ensure no books exist before running. This typically helps if tests
# somehow left the database in a bad state.
delete_all_books(model)
yield model
# Delete all books that we created during tests.
delete_all_books(model)
# The backend data stores can sometimes be flaky. It's useful to retry this
# a few times before giving up.
@retry(
stop_max_attempt_number=3,
wait_exponential_multiplier=100,
wait_exponential_max=2000)
def delete_all_books(model):
while True:
books, _ = model.list(limit=50)
if not books:
break
for book in books:
model.delete(book['id'])
def flaky_filter(info, *args):
"""Used by flaky to determine when to re-run a test case."""
_, e, _ = info
return isinstance(e, (ServiceUnavailable, HttpAccessTokenRefreshError))