|
1 | | -import asyncio |
2 | 1 | import logging |
3 | | -import multiprocessing |
4 | 2 |
|
5 | | -import pytest |
6 | 3 | from dls_normsql.constants import ClassTypes |
7 | 4 | from dls_normsql.databases import Databases |
8 | 5 |
|
9 | 6 | from soakdb3_api.databases.constants import BodyFieldnames, HeadFieldnames, Tablenames |
10 | 7 | from soakdb3_api.databases.database_definition import DatabaseDefinition |
| 8 | +from tests.base_tester import BaseTester2 |
11 | 9 |
|
12 | 10 | logger = logging.getLogger(__name__) |
13 | 11 |
|
@@ -53,57 +51,7 @@ def test(self, constants, logging_setup, output_directory): |
53 | 51 |
|
54 | 52 |
|
55 | 53 | # ---------------------------------------------------------------------------------------- |
56 | | -class TestDatabaseSqliteBackupRestore: |
57 | | - def test(self, constants, logging_setup, output_directory): |
58 | | - """ |
59 | | - Tests the sqlite implementation of XchemBeDatabase. |
60 | | - """ |
61 | | - |
62 | | - database_specification = { |
63 | | - "type": "dls_normsql.aiosqlite", |
64 | | - "filename": "%s/soakdb3.sqlite" % (output_directory), |
65 | | - } |
66 | | - |
67 | | - # Test direct SQL access to the database. |
68 | | - DatabaseTesterBackupRestore().main( |
69 | | - constants, |
70 | | - database_specification, |
71 | | - output_directory, |
72 | | - ) |
73 | | - |
74 | | - |
75 | | -# ---------------------------------------------------------------------------------------- |
76 | | -class _BaseTester: |
77 | | - """ |
78 | | - Provide asyncio loop and error checking over *Tester classes. |
79 | | - """ |
80 | | - |
81 | | - def main(self, constants, specification, output_directory): |
82 | | - """ |
83 | | - This is the main program which calls the test using asyncio. |
84 | | - """ |
85 | | - |
86 | | - multiprocessing.current_process().name = "main" |
87 | | - |
88 | | - failure_message = None |
89 | | - try: |
90 | | - # Run main test in asyncio event loop. |
91 | | - asyncio.run( |
92 | | - self._main_coroutine(constants, specification, output_directory) |
93 | | - ) |
94 | | - |
95 | | - except Exception as exception: |
96 | | - logger.exception( |
97 | | - "unexpected exception in the test method", exc_info=exception |
98 | | - ) |
99 | | - failure_message = str(exception) |
100 | | - |
101 | | - if failure_message is not None: |
102 | | - pytest.fail(failure_message) |
103 | | - |
104 | | - |
105 | | -# ---------------------------------------------------------------------------------------- |
106 | | -class DatabaseTesterHead(_BaseTester): |
| 54 | +class DatabaseTesterHead(BaseTester2): |
107 | 55 | """ |
108 | 56 | Test direct SQL access to the database. |
109 | 57 | """ |
@@ -138,7 +86,7 @@ async def _main_coroutine( |
138 | 86 |
|
139 | 87 |
|
140 | 88 | # ---------------------------------------------------------------------------------------- |
141 | | -class DatabaseTesterBody(_BaseTester): |
| 89 | +class DatabaseTesterBody(BaseTester2): |
142 | 90 | """ |
143 | 91 | Test direct SQL access to the database. |
144 | 92 | """ |
@@ -202,66 +150,3 @@ async def _main_coroutine( |
202 | 150 | finally: |
203 | 151 | # Connect from the database... necessary to allow asyncio loop to exit. |
204 | 152 | await database.disconnect() |
205 | | - |
206 | | - |
207 | | -# ---------------------------------------------------------------------------------------- |
208 | | -class DatabaseTesterBackupRestore(_BaseTester): |
209 | | - """ |
210 | | - Test direct SQL backup and restore. |
211 | | - """ |
212 | | - |
213 | | - async def _main_coroutine( |
214 | | - self, constants, database_specification, output_directory |
215 | | - ): |
216 | | - """ """ |
217 | | - |
218 | | - databases = Databases() |
219 | | - database = databases.build_object( |
220 | | - database_specification, |
221 | | - DatabaseDefinition(), |
222 | | - ) |
223 | | - |
224 | | - # Connect to database. |
225 | | - await database.connect(should_drop_database=True) |
226 | | - |
227 | | - try: |
228 | | - uuid1 = 1000 |
229 | | - uuid2 = 2000 |
230 | | - |
231 | | - # Write one record. |
232 | | - await database.insert( |
233 | | - Tablenames.BODY, |
234 | | - [{BodyFieldnames.LabVisit: "x", BodyFieldnames.ID: uuid1}], |
235 | | - ) |
236 | | - |
237 | | - # Backup. |
238 | | - await database.backup() |
239 | | - |
240 | | - # Write another record. |
241 | | - await database.insert( |
242 | | - Tablenames.BODY, |
243 | | - [{BodyFieldnames.LabVisit: "y", BodyFieldnames.ID: uuid2}], |
244 | | - ) |
245 | | - |
246 | | - # Backup again (with two records) |
247 | | - await database.backup() |
248 | | - |
249 | | - # Restore one in the past (when it had a single record). |
250 | | - await database.restore(1) |
251 | | - |
252 | | - all_sql = ( |
253 | | - f"SELECT * FROM {Tablenames.BODY} ORDER BY ID ASC /* first query */" |
254 | | - ) |
255 | | - records = await database.query(all_sql) |
256 | | - assert len(records) == 1, "first %s count expected 1" % (all_sql) |
257 | | - |
258 | | - # Restore most recent (two records). |
259 | | - await database.restore(0) |
260 | | - |
261 | | - all_sql = f"SELECT * FROM {Tablenames.BODY} ORDER BY ID ASC" |
262 | | - records = await database.query(all_sql) |
263 | | - assert len(records) == 2, "second %s count expected 2" % (all_sql) |
264 | | - |
265 | | - finally: |
266 | | - # Connect from the database... necessary to allow asyncio loop to exit. |
267 | | - await database.disconnect() |
0 commit comments