1
|
import os
|
2
|
import sqlite3
|
3
|
from sqlite3 import Connection
|
4
|
|
5
|
import pytest
|
6
|
|
7
|
from src.dao.certificate_repository import CertificateRepository
|
8
|
from src.dao.private_key_repository import PrivateKeyRepository
|
9
|
from src.db.init_queries import SCHEMA_SQL, DEFAULT_VALUES_SQL
|
10
|
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
|
# scope="module" means that this fixture is run once per module
|
16
|
@pytest.fixture(scope="module")
|
17
|
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
|
45
|
|
46
|
|
47
|
# scope defaults to "function" which means that the fixture is run once per test (function)
|
48
|
@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)
|