4 |
4 |
|
5 |
5 |
import pytest
|
6 |
6 |
|
|
7 |
from src.config.configuration import test_configuration
|
|
8 |
from src.config.connection_provider import ConnectionProvider
|
7 |
9 |
from src.dao.certificate_repository import CertificateRepository
|
8 |
10 |
from src.dao.private_key_repository import PrivateKeyRepository
|
9 |
11 |
from src.db.init_queries import SCHEMA_SQL, DEFAULT_VALUES_SQL
|
... | ... | |
15 |
17 |
# scope="module" means that this fixture is run once per module
|
16 |
18 |
@pytest.fixture(scope="module")
|
17 |
19 |
def connection():
|
18 |
|
print("Creating a new SQLITE connection to the test DB")
|
19 |
|
test_db_file = "test.sqlite"
|
20 |
|
connection: Connection = sqlite3.connect(test_db_file)
|
21 |
|
|
22 |
|
# yield the created connection
|
23 |
|
yield connection
|
24 |
|
|
25 |
|
# after tests have finished delete the created db file
|
26 |
|
try:
|
27 |
|
print("Deleting the test DB")
|
28 |
|
os.unlink(test_db_file)
|
29 |
|
except FileNotFoundError:
|
30 |
|
print(f"Could not delete {test_db_file} file containing the test DB")
|
31 |
|
pass
|
32 |
|
|
33 |
|
|
34 |
|
@pytest.fixture(scope="module")
|
35 |
|
def cursor(connection):
|
36 |
|
cursor = connection.cursor()
|
37 |
|
|
38 |
|
# execute db initialisation script
|
39 |
|
cursor.executescript(SCHEMA_SQL)
|
40 |
|
|
41 |
|
# insert default values
|
42 |
|
cursor.executescript(DEFAULT_VALUES_SQL)
|
43 |
|
|
44 |
|
return cursor
|
|
20 |
return ConnectionProvider().connect(test_configuration())
|
45 |
21 |
|
46 |
22 |
|
47 |
23 |
# scope defaults to "function" which means that the fixture is run once per test (function)
|
48 |
24 |
@pytest.fixture
|
49 |
|
def certificate_repository(connection, cursor):
|
|
25 |
def certificate_repository(connection):
|
50 |
26 |
return CertificateRepository(connection)
|
51 |
27 |
|
52 |
28 |
|
53 |
29 |
@pytest.fixture
|
54 |
|
def private_key_repository(connection, cursor):
|
|
30 |
def private_key_repository(connection):
|
55 |
31 |
return PrivateKeyRepository(connection)
|
56 |
32 |
|
57 |
33 |
|
... | ... | |
70 |
46 |
return CertificateService(cryptography_service, certificate_repository)
|
71 |
47 |
|
72 |
48 |
|
73 |
|
# TODO improve this (lots of duplicated code) - some test cases need a DB connection that is not shared with other tests
|
74 |
49 |
@pytest.fixture
|
75 |
50 |
def connection_unique():
|
76 |
|
print("Creating a new unique SQLITE connection to the test DB")
|
77 |
|
test_db_file = "test_unique.sqlite"
|
78 |
|
connection: Connection = sqlite3.connect(test_db_file)
|
79 |
|
|
80 |
|
# yield the created connection
|
81 |
|
yield connection
|
82 |
|
|
83 |
|
# after tests have finished delete the created db file
|
84 |
|
try:
|
85 |
|
print("Deleting the unique test DB")
|
86 |
|
os.unlink(test_db_file)
|
87 |
|
except FileNotFoundError:
|
88 |
|
print(f"Could not delete {test_db_file} file containing the unique test DB")
|
89 |
|
pass
|
90 |
|
|
91 |
|
|
92 |
|
@pytest.fixture
|
93 |
|
def cursor_unique(connection_unique):
|
94 |
|
cursor = connection_unique.cursor()
|
95 |
|
|
96 |
|
# execute db initialisation script
|
97 |
|
cursor.executescript(SCHEMA_SQL)
|
98 |
|
|
99 |
|
# insert default values
|
100 |
|
cursor.executescript(DEFAULT_VALUES_SQL)
|
101 |
|
|
102 |
|
return cursor
|
|
51 |
return ConnectionProvider().connect(test_configuration())
|
103 |
52 |
|
104 |
53 |
|
105 |
54 |
# scope defaults to "function" which means that the fixture is run once per test (function)
|
106 |
55 |
@pytest.fixture
|
107 |
|
def certificate_repository_unique(connection_unique, cursor_unique):
|
|
56 |
def certificate_repository_unique(connection_unique):
|
108 |
57 |
return CertificateRepository(connection_unique)
|
109 |
58 |
|
110 |
59 |
|
111 |
60 |
@pytest.fixture
|
112 |
|
def private_key_repository_unique(connection_unique, cursor_unique):
|
|
61 |
def private_key_repository_unique(connection_unique):
|
113 |
62 |
return PrivateKeyRepository(connection_unique)
|
114 |
63 |
|
115 |
64 |
|
Re #8579 - Changed conftest.py of service and DAO tests in such way that test_configuration is now used