1 |
d3fa9147
|
Stanislav Král
|
import os
|
2 |
e39e138f
|
Stanislav Král
|
import sqlite3
|
3 |
|
|
from sqlite3 import Connection
|
4 |
|
|
|
5 |
|
|
import pytest
|
6 |
d3fa9147
|
Stanislav Král
|
|
7 |
|
|
from src.dao.certificate_repository import CertificateRepository
|
8 |
|
|
from src.dao.private_key_repository import PrivateKeyRepository
|
9 |
e39e138f
|
Stanislav Král
|
from src.db.init_queries import SCHEMA_SQL, DEFAULT_VALUES_SQL
|
10 |
d3fa9147
|
Stanislav Král
|
from src.services.certificate_service import CertificateService
|
11 |
|
|
from src.services.cryptography import CryptographyService
|
12 |
|
|
from src.services.key_service import KeyService
|
13 |
|
|
|
14 |
|
|
|
15 |
e39e138f
|
Stanislav Král
|
# scope="module" means that this fixture is run once per module
|
16 |
|
|
@pytest.fixture(scope="module")
|
17 |
d3fa9147
|
Stanislav Král
|
def connection():
|
18 |
e39e138f
|
Stanislav Král
|
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 |
d3fa9147
|
Stanislav Král
|
try:
|
27 |
e39e138f
|
Stanislav Král
|
print("Deleting the test DB")
|
28 |
|
|
os.unlink(test_db_file)
|
29 |
d3fa9147
|
Stanislav Král
|
except FileNotFoundError:
|
30 |
e39e138f
|
Stanislav Král
|
print(f"Could not delete {test_db_file} file containing the test DB")
|
31 |
d3fa9147
|
Stanislav Král
|
pass
|
32 |
|
|
|
33 |
|
|
|
34 |
e39e138f
|
Stanislav Král
|
@pytest.fixture(scope="module")
|
35 |
d3fa9147
|
Stanislav Král
|
def cursor(connection):
|
36 |
|
|
cursor = connection.cursor()
|
37 |
|
|
|
38 |
|
|
# execute db initialisation script
|
39 |
e39e138f
|
Stanislav Král
|
cursor.executescript(SCHEMA_SQL)
|
40 |
d3fa9147
|
Stanislav Král
|
|
41 |
|
|
# insert default values
|
42 |
e39e138f
|
Stanislav Král
|
cursor.executescript(DEFAULT_VALUES_SQL)
|
43 |
d3fa9147
|
Stanislav Král
|
|
44 |
|
|
return cursor
|
45 |
|
|
|
46 |
|
|
|
47 |
e39e138f
|
Stanislav Král
|
# scope defaults to "function" which means that the fixture is run once per test (function)
|
48 |
d3fa9147
|
Stanislav Král
|
@pytest.fixture
|
49 |
|
|
def certificate_repository(connection, cursor):
|
50 |
|
|
return CertificateRepository(connection, cursor)
|
51 |
|
|
|
52 |
|
|
|
53 |
|
|
@pytest.fixture
|
54 |
|
|
def private_key_repository(connection, cursor):
|
55 |
|
|
return PrivateKeyRepository(connection, cursor)
|
56 |
|
|
|
57 |
|
|
|
58 |
|
|
@pytest.fixture
|
59 |
|
|
def cryptography_service():
|
60 |
|
|
return CryptographyService()
|
61 |
|
|
|
62 |
|
|
|
63 |
|
|
@pytest.fixture
|
64 |
|
|
def private_key_service(private_key_repository, cryptography_service):
|
65 |
|
|
return KeyService(cryptography_service, private_key_repository)
|
66 |
|
|
|
67 |
|
|
|
68 |
|
|
@pytest.fixture
|
69 |
|
|
def certificate_service(certificate_repository, cryptography_service):
|
70 |
|
|
return CertificateService(cryptography_service, certificate_repository)
|