|
| 1 | +# Copyright 2015 Google Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""conftest.py is used to define common test fixtures for pytest.""" |
| 16 | + |
| 17 | +import bookshelf |
| 18 | +import config |
| 19 | +from gcloud.exceptions import ServiceUnavailable |
| 20 | +from oauth2client.client import HttpAccessTokenRefreshError |
| 21 | +import pytest |
| 22 | +from retrying import retry |
| 23 | + |
| 24 | + |
| 25 | +@pytest.yield_fixture(params=['datastore', 'cloudsql', 'mongodb']) |
| 26 | +def app(request): |
| 27 | + """This fixtures provides a Flask app instance configured for testing. |
| 28 | +
|
| 29 | + Because it's parametric, it will cause every test that uses this fixture |
| 30 | + to run three times: one time for each backend (datastore, cloudsql, and |
| 31 | + mongodb). |
| 32 | +
|
| 33 | + It also ensures the tests run within a request context, allowing |
| 34 | + any calls to flask.request, flask.current_app, etc. to work.""" |
| 35 | + app = bookshelf.create_app( |
| 36 | + config, |
| 37 | + testing=True, |
| 38 | + config_overrides={ |
| 39 | + 'DATA_BACKEND': request.param |
| 40 | + }) |
| 41 | + |
| 42 | + with app.test_request_context(): |
| 43 | + yield app |
| 44 | + |
| 45 | + |
| 46 | +@pytest.yield_fixture |
| 47 | +def model(monkeypatch, app): |
| 48 | + """This fixture provides a modified version of the app's model that tracks |
| 49 | + all created items and deletes them at the end of the test. |
| 50 | +
|
| 51 | + Any tests that directly or indirectly interact with the database should use |
| 52 | + this to ensure that resources are properly cleaned up. |
| 53 | +
|
| 54 | + Monkeypatch is provided by pytest and used to patch the model's create |
| 55 | + method. |
| 56 | +
|
| 57 | + The app fixture is needed to provide the configuration and context needed |
| 58 | + to get the proper model object. |
| 59 | + """ |
| 60 | + model = bookshelf.get_model() |
| 61 | + |
| 62 | + # Ensure no books exist before running. This typically helps if tests |
| 63 | + # somehow left the database in a bad state. |
| 64 | + delete_all_books(model) |
| 65 | + |
| 66 | + yield model |
| 67 | + |
| 68 | + # Delete all books that we created during tests. |
| 69 | + delete_all_books(model) |
| 70 | + |
| 71 | + |
| 72 | +# The backend data stores can sometimes be flaky. It's useful to retry this |
| 73 | +# a few times before giving up. |
| 74 | +@retry( |
| 75 | + stop_max_attempt_number=3, |
| 76 | + wait_exponential_multiplier=100, |
| 77 | + wait_exponential_max=2000) |
| 78 | +def delete_all_books(model): |
| 79 | + while True: |
| 80 | + books, _ = model.list(limit=50) |
| 81 | + if not books: |
| 82 | + break |
| 83 | + for book in books: |
| 84 | + model.delete(book['id']) |
| 85 | + |
| 86 | + |
| 87 | +def flaky_filter(info, *args): |
| 88 | + """Used by flaky to determine when to re-run a test case.""" |
| 89 | + _, e, _ = info |
| 90 | + return isinstance(e, (ServiceUnavailable, HttpAccessTokenRefreshError)) |
0 commit comments