This repository has been archived by the owner on Dec 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconftest.py
84 lines (66 loc) · 2 KB
/
conftest.py
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
"""
File to create all needed fixtures to set up a Flask client
"""
# pylint: disable=redefined-outer-name, unused-argument
import pytest
from sqlalchemy import create_engine
from sqlalchemy.engine import Connection
from dependency_injector import providers
from src.app import create_app
from src.config.container import Container
from src.config.database_handler import DatabaseHandler
from src.config.db import database_url_connection
from src.config.session_handler import SessionHandler
from src.tests.utils.api_client import ApiClient
container = Container()
@pytest.fixture(scope="session")
def connection():
"""
Manages database connection
"""
_engine = create_engine(url=database_url_connection({"database_name": "oipie_tests"}))
_connection = _engine.connect()
yield _connection
_connection.close()
@pytest.fixture(scope="session", autouse=True)
def initialise_db(connection: Connection):
"""
Migrates the database once before running tests
"""
DatabaseHandler(connection).create_database()
@pytest.fixture(autouse=True)
def transaction(connection: Connection):
"""
Wraps the test in a transaction
"""
_transaction = connection.begin()
yield
_transaction.rollback()
@pytest.fixture()
def session_handler(connection):
"""
Creates a session handler
"""
return SessionHandler(connection)
@pytest.fixture()
def create_test_app(connection: Connection):
"""
This method returns an instance of an app for testing purposes
"""
app_factory = create_app()
app_factory.container.connection.override(providers.Object(connection))
# other setup can go here
yield app_factory
# clean up / reset resources here
@pytest.fixture()
def client(create_test_app):
"""
This methods takes an app fixture and returns a Flask http client for testing purposes
"""
return create_test_app.test_client()
@pytest.fixture()
def api_client(client):
"""
Generates an API Client
"""
return ApiClient(client)