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)
|
51
|
|
52
|
|
53
|
@pytest.fixture
|
54
|
def private_key_repository(connection, cursor):
|
55
|
return PrivateKeyRepository(connection)
|
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)
|
71
|
|
72
|
|
73
|
# TODO improve this (lots of duplicated code) - some test cases need a DB connection that is not shared with other tests
|
74
|
@pytest.fixture
|
75
|
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
|
103
|
|
104
|
|
105
|
# scope defaults to "function" which means that the fixture is run once per test (function)
|
106
|
@pytest.fixture
|
107
|
def certificate_repository_unique(connection_unique, cursor_unique):
|
108
|
return CertificateRepository(connection_unique)
|
109
|
|
110
|
|
111
|
@pytest.fixture
|
112
|
def private_key_repository_unique(connection_unique, cursor_unique):
|
113
|
return PrivateKeyRepository(connection_unique)
|
114
|
|
115
|
|
116
|
@pytest.fixture
|
117
|
def cryptography_service_unique():
|
118
|
return CryptographyService()
|
119
|
|
120
|
|
121
|
@pytest.fixture
|
122
|
def private_key_service_unique(private_key_repository_unique, cryptography_service_unique):
|
123
|
return KeyService(cryptography_service_unique, private_key_repository_unique)
|
124
|
|
125
|
|
126
|
@pytest.fixture
|
127
|
def certificate_service_unique(certificate_repository_unique, cryptography_service_unique):
|
128
|
return CertificateService(cryptography_service_unique, certificate_repository_unique)
|