1 |
163f57d0
|
David Friesecký
|
import pytest
|
2 |
|
|
import sqlite3
|
3 |
|
|
from sqlite3 import Connection, Cursor
|
4 |
|
|
|
5 |
|
|
from src.dao.certificate_repository import CertificateRepository
|
6 |
|
|
from src.dao.private_key_repository import PrivateKeyRepository
|
7 |
|
|
from src.db.init_queries import SCHEMA_SQL, DEFAULT_VALUES_SQL
|
8 |
|
|
|
9 |
|
|
|
10 |
|
|
# scope="module" means that this fixture is run once per module
|
11 |
|
|
@pytest.fixture(scope="module")
|
12 |
|
|
def connection():
|
13 |
|
|
print("Creating a new SQLITE connection to the test DB")
|
14 |
|
|
connection: Connection = sqlite3.connect(':memory:')
|
15 |
|
|
|
16 |
|
|
# yield the created connection
|
17 |
|
|
yield connection
|
18 |
|
|
|
19 |
|
|
|
20 |
6654bf8b
|
David Friesecký
|
@pytest.fixture(scope="module")
|
21 |
163f57d0
|
David Friesecký
|
def cursor(connection):
|
22 |
|
|
cursor = connection.cursor()
|
23 |
|
|
|
24 |
|
|
# execute db initialisation script
|
25 |
|
|
cursor.executescript(SCHEMA_SQL)
|
26 |
|
|
|
27 |
|
|
# insert default values
|
28 |
|
|
cursor.executescript(DEFAULT_VALUES_SQL)
|
29 |
|
|
|
30 |
|
|
return cursor
|
31 |
|
|
|
32 |
6654bf8b
|
David Friesecký
|
@pytest.fixture
|
33 |
|
|
def connection_unique():
|
34 |
|
|
print("Creating a new SQLITE connection to the test DB")
|
35 |
|
|
connection: Connection = sqlite3.connect(':memory:')
|
36 |
|
|
|
37 |
|
|
# yield the created connection
|
38 |
|
|
yield connection
|
39 |
|
|
|
40 |
|
|
|
41 |
|
|
@pytest.fixture
|
42 |
|
|
def cursor_unique(connection):
|
43 |
|
|
cursor = connection.cursor()
|
44 |
|
|
|
45 |
|
|
# execute db initialisation script
|
46 |
|
|
cursor.executescript(SCHEMA_SQL)
|
47 |
|
|
|
48 |
|
|
# insert default values
|
49 |
|
|
cursor.executescript(DEFAULT_VALUES_SQL)
|
50 |
|
|
|
51 |
|
|
return cursor
|
52 |
|
|
|
53 |
163f57d0
|
David Friesecký
|
@pytest.fixture
|
54 |
|
|
def certificate_repository(connection, cursor):
|
55 |
|
|
return CertificateRepository(connection, cursor)
|
56 |
|
|
|
57 |
|
|
@pytest.fixture
|
58 |
6654bf8b
|
David Friesecký
|
def private_key_repository(connection_unique, cursor_unique):
|
59 |
|
|
return PrivateKeyRepository(connection_unique, cursor_unique)
|