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)
|
71 |
2a90f4fd
|
Stanislav Král
|
|
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, cursor_unique)
|
109 |
|
|
|
110 |
|
|
|
111 |
|
|
@pytest.fixture
|
112 |
|
|
def private_key_repository_unique(connection_unique, cursor_unique):
|
113 |
|
|
return PrivateKeyRepository(connection_unique, cursor_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)
|