|
1
|
|
|
# pylint: disable=redefined-outer-name |
|
|
|
|
|
|
2
|
|
|
|
|
3
|
|
|
import json |
|
4
|
|
|
import logging |
|
5
|
|
|
|
|
6
|
|
|
import pytest |
|
7
|
|
|
from testing.postgresql import Postgresql |
|
|
|
|
|
|
8
|
|
|
from sqlalchemy.exc import OperationalError |
|
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
from memegen.app import create_app |
|
11
|
|
|
from memegen.extensions import db as _db |
|
12
|
|
|
from memegen.settings import get_config |
|
13
|
|
|
|
|
14
|
|
|
from memegen.test.conftest import pytest_configure # pylint: disable=unused-import |
|
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
def load(response, as_json=True, key=None): |
|
18
|
|
|
"""Convert a response's binary data (JSON) to a dictionary.""" |
|
19
|
|
|
text = response.data.decode('utf-8') |
|
20
|
|
|
if not as_json: |
|
21
|
|
|
return text |
|
22
|
|
|
if text: |
|
23
|
|
|
data = json.loads(text) |
|
24
|
|
|
if key: |
|
25
|
|
|
data = data[key] |
|
26
|
|
|
else: |
|
27
|
|
|
data = None |
|
28
|
|
|
logging.debug("Response: %r", data) |
|
29
|
|
|
return data |
|
30
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
@pytest.yield_fixture(scope='session') |
|
33
|
|
|
def app(): |
|
|
|
|
|
|
34
|
|
|
app = create_app(get_config('test')) |
|
35
|
|
|
yield app |
|
36
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
@pytest.yield_fixture(scope='session') |
|
39
|
|
|
def postgres(): |
|
|
|
|
|
|
40
|
|
|
try: |
|
41
|
|
|
import psycopg2 |
|
|
|
|
|
|
42
|
|
|
except ImportError: |
|
43
|
|
|
yield None # PostgreSQL database adapter is unavailable on this system |
|
44
|
|
|
else: |
|
45
|
|
|
try: |
|
46
|
|
|
with Postgresql() as pg: |
|
|
|
|
|
|
47
|
|
|
yield pg |
|
48
|
|
|
except FileNotFoundError: |
|
49
|
|
|
yield None # PostgreSQL is unavailable on this system |
|
50
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
@pytest.yield_fixture(scope='module') |
|
53
|
|
|
def db_engine(app, postgres): |
|
|
|
|
|
|
54
|
|
|
if postgres: |
|
55
|
|
|
app.config['SQLALCHEMY_DATABASE_URI'] = postgres.url() |
|
56
|
|
|
_db.app = app |
|
57
|
|
|
|
|
58
|
|
|
with app.app_context(): |
|
59
|
|
|
_db.create_all() |
|
60
|
|
|
|
|
61
|
|
|
yield _db |
|
62
|
|
|
|
|
63
|
|
|
# http://stackoverflow.com/a/6810165/1255482 |
|
64
|
|
|
_db.session.close() # pylint: disable=no-member |
|
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
try: |
|
67
|
|
|
_db.drop_all() |
|
68
|
|
|
except OperationalError: |
|
69
|
|
|
pass # allow tests to be killed cleanly |
|
70
|
|
|
else: |
|
71
|
|
|
yield None |
|
72
|
|
|
|
|
73
|
|
|
|
|
74
|
|
|
@pytest.yield_fixture(scope='function') |
|
75
|
|
|
def db(db_engine): |
|
|
|
|
|
|
76
|
|
|
yield db_engine |
|
77
|
|
|
# Do a rollback after each test in case bad stuff happened |
|
78
|
|
|
if db_engine: |
|
79
|
|
|
db_engine.session.rollback() |
|
80
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
@pytest.yield_fixture |
|
83
|
|
|
def client(app, db): # pylint: disable=unused-argument |
|
|
|
|
|
|
84
|
|
|
yield app.test_client() |
|
85
|
|
|
|
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.