Completed
Pull Request — master (#176)
by Josh
03:37
created

db_engine()   A

Complexity

Conditions 3

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 3
dl 0
loc 17
rs 9.4285
1
# pylint: disable=redefined-outer-name
2
3
import json
4
import logging
5
6
import pytest
7
from testing.postgresql import Postgresql
0 ignored issues
show
Configuration introduced by
The import testing.postgresql could not be resolved.

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
8
from sqlalchemy.exc import OperationalError
0 ignored issues
show
Configuration introduced by
The import sqlalchemy.exc could not be resolved.

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
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(postgres):
34
    app = create_app(get_config('test'))
35
    app.config['SQLALCHEMY_DATABASE_URI'] = postgres.url()
36
    yield app
37
38
39
@pytest.yield_fixture(scope='session')
40
def postgres():
41
    with Postgresql() as pg:
42
        yield pg
43
44
45
@pytest.yield_fixture(scope='module')
46
def db_engine(app):
47
    _db.app = app
48
49
    with app.app_context():
50
        _db.create_all()
51
52
    yield _db
53
54
    # http://stackoverflow.com/a/6810165/1255482
55
    _db.session.close()  # pylint: disable=no-member
56
57
    try:
58
        _db.drop_all()
59
    except OperationalError:
60
        # Allow tests to be killed cleanly
61
        pass
62
63
64
@pytest.yield_fixture(scope='function')
65
def db(db_engine):
66
    yield db_engine
67
    # Do a rollback after each test in case bad stuff happened
68
    db_engine.session.rollback()
69
70
71
@pytest.yield_fixture
72
def client(app, db):  # pylint: disable=unused-argument
73
    yield app.test_client()
74