Skip to content

Commit 2c44931

Browse files
author
David Erb
committed
splits up testing
1 parent c585b37 commit 2c44931

File tree

5 files changed

+213
-138
lines changed

5 files changed

+213
-138
lines changed

src/soakdb3_api/databases/database_definition.py

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import logging
22

3+
from soakdb3_api.databases.constants import Tablenames
4+
35
# Base class for all aiosqlite database objects.
46
from soakdb3_api.databases.table_definitions import BodyTable, HeadTable, VisitTable
57

@@ -35,27 +37,8 @@ async def add_table_definitions(self, database):
3537
# ----------------------------------------------------------------------------------------
3638
async def apply_revision(self, database, revision):
3739

38-
# Let the base class add any common updates.
39-
# Usually only does anything if upgrading to revision 1
40-
# which means we are starting with a legacy database with no revision information.
41-
await NormsqlAiosqlite.apply_revision(self, database, revision)
42-
4340
# Updating to revision 1 presumably means
4441
# this is a legacy database with no revision table in it.
42+
# TODO: Test revision 1 adding Visit table.
4543
if revision == 1:
4644
await database.create_table(Tablenames.VISIT)
47-
48-
# if revision == 2:
49-
# await self.execute(
50-
# f"ALTER TABLE {Tablenames.ROCKMAKER_IMAGES} ADD COLUMN {ImageFieldnames.NEWFIELD} TEXT",
51-
# why=f"revision 2: add {Tablenames.ROCKMAKER_IMAGES} {ImageFieldnames.NEWFIELD} column",
52-
# )
53-
# await self.execute(
54-
# "CREATE INDEX %s_%s ON %s(%s)"
55-
# % (
56-
# Tablenames.ROCKMAKER_IMAGES,
57-
# ImageFieldnames.NEWFIELD,
58-
# Tablenames.ROCKMAKER_IMAGES,
59-
# ImageFieldnames.NEWFIELD,
60-
# )
61-
# )

tests/base_tester.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,33 @@ def main(self, constants, output_directory):
3333

3434
if failure_message is not None:
3535
pytest.fail(failure_message)
36+
37+
38+
# ----------------------------------------------------------------------------------------
39+
class BaseTester2:
40+
"""
41+
Provide asyncio loop and error checking over *Tester classes.
42+
"""
43+
44+
def main(self, constants, specification, output_directory):
45+
"""
46+
This is the main program which calls the test using asyncio.
47+
"""
48+
49+
multiprocessing.current_process().name = "main"
50+
51+
failure_message = None
52+
try:
53+
# Run main test in asyncio event loop.
54+
asyncio.run(
55+
self._main_coroutine(constants, specification, output_directory)
56+
)
57+
58+
except Exception as exception:
59+
logger.exception(
60+
"unexpected exception in the test method", exc_info=exception
61+
)
62+
failure_message = str(exception)
63+
64+
if failure_message is not None:
65+
pytest.fail(failure_message)

tests/test_database.py

Lines changed: 3 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
import asyncio
21
import logging
3-
import multiprocessing
42

5-
import pytest
63
from dls_normsql.constants import ClassTypes
74
from dls_normsql.databases import Databases
85

96
from soakdb3_api.databases.constants import BodyFieldnames, HeadFieldnames, Tablenames
107
from soakdb3_api.databases.database_definition import DatabaseDefinition
8+
from tests.base_tester import BaseTester2
119

1210
logger = logging.getLogger(__name__)
1311

@@ -53,57 +51,7 @@ def test(self, constants, logging_setup, output_directory):
5351

5452

5553
# ----------------------------------------------------------------------------------------
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):
10755
"""
10856
Test direct SQL access to the database.
10957
"""
@@ -138,7 +86,7 @@ async def _main_coroutine(
13886

13987

14088
# ----------------------------------------------------------------------------------------
141-
class DatabaseTesterBody(_BaseTester):
89+
class DatabaseTesterBody(BaseTester2):
14290
"""
14391
Test direct SQL access to the database.
14492
"""
@@ -202,66 +150,3 @@ async def _main_coroutine(
202150
finally:
203151
# Connect from the database... necessary to allow asyncio loop to exit.
204152
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()

tests/test_database_backup.py

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import logging
2+
3+
from dls_normsql.databases import Databases
4+
5+
from soakdb3_api.databases.constants import BodyFieldnames, Tablenames
6+
from soakdb3_api.databases.database_definition import DatabaseDefinition
7+
from tests.base_tester import BaseTester2
8+
9+
logger = logging.getLogger(__name__)
10+
11+
12+
# ----------------------------------------------------------------------------------------
13+
class TestDatabaseSqliteBackupRestore:
14+
def test(self, constants, logging_setup, output_directory):
15+
"""
16+
Tests the sqlite implementation of XchemBeDatabase.
17+
"""
18+
19+
database_specification = {
20+
"type": "dls_normsql.aiosqlite",
21+
"filename": "%s/soakdb3.sqlite" % (output_directory),
22+
}
23+
24+
# Test direct SQL access to the database.
25+
DatabaseTesterBackupRestore().main(
26+
constants,
27+
database_specification,
28+
output_directory,
29+
)
30+
31+
32+
# ----------------------------------------------------------------------------------------
33+
class DatabaseTesterBackupRestore(BaseTester2):
34+
"""
35+
Test direct SQL backup and restore.
36+
"""
37+
38+
async def _main_coroutine(
39+
self, constants, database_specification, output_directory
40+
):
41+
""" """
42+
43+
databases = Databases()
44+
database = databases.build_object(
45+
database_specification,
46+
DatabaseDefinition(),
47+
)
48+
49+
# Connect to database.
50+
await database.connect(should_drop_database=True)
51+
52+
try:
53+
uuid1 = 1000
54+
uuid2 = 2000
55+
56+
# Write one record.
57+
await database.insert(
58+
Tablenames.BODY,
59+
[{BodyFieldnames.LabVisit: "x", BodyFieldnames.ID: uuid1}],
60+
)
61+
62+
# Backup.
63+
await database.backup()
64+
65+
# Write another record.
66+
await database.insert(
67+
Tablenames.BODY,
68+
[{BodyFieldnames.LabVisit: "y", BodyFieldnames.ID: uuid2}],
69+
)
70+
71+
# Backup again (with two records)
72+
await database.backup()
73+
74+
# Restore one in the past (when it had a single record).
75+
await database.restore(1)
76+
77+
all_sql = (
78+
f"SELECT * FROM {Tablenames.BODY} ORDER BY ID ASC /* first query */"
79+
)
80+
records = await database.query(all_sql)
81+
assert len(records) == 1, "first %s count expected 1" % (all_sql)
82+
83+
# Restore most recent (two records).
84+
await database.restore(0)
85+
86+
all_sql = f"SELECT * FROM {Tablenames.BODY} ORDER BY ID ASC"
87+
records = await database.query(all_sql)
88+
assert len(records) == 2, "second %s count expected 2" % (all_sql)
89+
90+
finally:
91+
# Connect from the database... necessary to allow asyncio loop to exit.
92+
await database.disconnect()

tests/test_database_revision.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import logging
2+
3+
from dls_normsql.constants import Tablenames as DlsNormsqlTablenames
4+
from dls_normsql.databases import Databases
5+
6+
from soakdb3_api.databases.constants import Tablenames
7+
from soakdb3_api.databases.database_definition import DatabaseDefinition
8+
from tests.base_tester import BaseTester2
9+
10+
logger = logging.getLogger(__name__)
11+
12+
13+
# ----------------------------------------------------------------------------------------
14+
class TestDatabaseRevision:
15+
def test(self, constants, logging_setup, output_directory):
16+
"""
17+
Tests the sqlite implementation of XchemBeDatabase.
18+
"""
19+
20+
database_specification = {
21+
"type": "dls_normsql.aiosqlite",
22+
"filename": "%s/soakdb3.sqlite" % (output_directory),
23+
}
24+
25+
# Test direct SQL access to the database.
26+
DatabaseTesterRevision().main(
27+
constants,
28+
database_specification,
29+
output_directory,
30+
)
31+
32+
33+
# ----------------------------------------------------------------------------------------
34+
class DatabaseTesterRevision(BaseTester2):
35+
"""
36+
Test direct SQL access to the database.
37+
"""
38+
39+
async def _main_coroutine(
40+
self, constants, database_specification, output_directory
41+
):
42+
""" """
43+
databases = Databases()
44+
database = databases.build_object(
45+
database_specification,
46+
DatabaseDefinition(),
47+
)
48+
49+
try:
50+
# Connect to database.
51+
await database.connect(should_drop_database=True)
52+
53+
sql = f"SELECT * FROM {DlsNormsqlTablenames.REVISION}"
54+
records = await database.query(sql)
55+
assert len(records) == 1
56+
57+
sql = f"SELECT * FROM {Tablenames.VISIT}"
58+
records = await database.query(sql)
59+
assert len(records) == 0
60+
61+
await database.execute(f"DROP TABLE {DlsNormsqlTablenames.REVISION}")
62+
await database.execute(f"DROP TABLE {Tablenames.VISIT}")
63+
64+
finally:
65+
# Connect from the database... necessary to allow asyncio loop to exit.
66+
await database.disconnect()
67+
68+
try:
69+
# Connect to database, revision should be applied to add back the Visit and Revision tables.
70+
await database.connect()
71+
72+
# Make sure we are up to date with the latest database schema revision.
73+
await database.apply_revisions()
74+
75+
sql = f"SELECT * FROM {DlsNormsqlTablenames.REVISION}"
76+
records = await database.query(sql)
77+
assert len(records) == 1
78+
79+
sql = f"SELECT * FROM {Tablenames.VISIT}"
80+
records = await database.query(sql)
81+
assert len(records) == 0
82+
83+
finally:
84+
# Connect from the database... necessary to allow asyncio loop to exit.
85+
await database.disconnect()

0 commit comments

Comments
 (0)