Completed
Pull Request — master (#194)
by Jace
03:26
created

postgres()   B

Complexity

Conditions 5

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 5
dl 0
loc 12
rs 8.5454
1
# pylint: disable=redefined-outer-name
0 ignored issues
show
Coding Style introduced by
This module should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
introduced by
Locally disabling redefined-outer-name (W0621)
Loading history...
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
0 ignored issues
show
introduced by
Locally disabling unused-import (W0611)
Loading history...
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():
0 ignored issues
show
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
34
    app = create_app(get_config('test'))
35
    yield app
36
37
38
@pytest.yield_fixture(scope='session')
39
def postgres():
0 ignored issues
show
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
40
    try:
41
        import psycopg2
0 ignored issues
show
Unused Code introduced by
The variable psycopg2 seems to be unused.
Loading history...
42
    except ImportError:
43
        yield None  # PostgreSQL database adapter is unavailable on this system
44
    else:
45
        try:
46
            with Postgresql() as pg:
0 ignored issues
show
Coding Style Naming introduced by
The name pg does not conform to the variable naming conventions ([a-z_][a-z0-9_]{2,30}$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
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):
0 ignored issues
show
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
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
0 ignored issues
show
introduced by
Locally disabling no-member (E1101)
Loading history...
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):
0 ignored issues
show
Coding Style Naming introduced by
The name db does not conform to the function naming conventions ([a-z_][a-z0-9_]{2,30}$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
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
0 ignored issues
show
introduced by
Locally disabling unused-argument (W0613)
Loading history...
Coding Style Naming introduced by
The name db does not conform to the argument naming conventions ([a-z_][a-z0-9_]{2,30}$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
84
    yield app.test_client()
85