home
·
contact
·
privacy
projects
/
plomtask
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Some test utils refactoring.
[plomtask]
/
tests
/
versioned_attributes.py
diff --git
a/tests/versioned_attributes.py
b/tests/versioned_attributes.py
index 5238d70bfdeb257b9d36e6fc58bfa8abb26255ad..a75fc3cec338d5bed5bf5739101287cd8efa1851 100644
(file)
--- a/
tests/versioned_attributes.py
+++ b/
tests/versioned_attributes.py
@@
-6,7
+6,7
@@
from tests.utils import TestCaseWithDB
from plomtask.versioned_attributes import VersionedAttribute, TIMESTAMP_FMT
from plomtask.db import BaseModel
from plomtask.versioned_attributes import VersionedAttribute, TIMESTAMP_FMT
from plomtask.db import BaseModel
-SQL_TEST_TABLE = '''
+SQL_TEST_TABLE
_STR
= '''
CREATE TABLE versioned_tests (
parent INTEGER NOT NULL,
timestamp TEXT NOT NULL,
CREATE TABLE versioned_tests (
parent INTEGER NOT NULL,
timestamp TEXT NOT NULL,
@@
-14,6
+14,14
@@
CREATE TABLE versioned_tests (
PRIMARY KEY (parent, timestamp)
);
'''
PRIMARY KEY (parent, timestamp)
);
'''
+SQL_TEST_TABLE_FLOAT = '''
+CREATE TABLE versioned_tests (
+ parent INTEGER NOT NULL,
+ timestamp TEXT NOT NULL,
+ value REAL NOT NULL,
+ PRIMARY KEY (parent, timestamp)
+);
+'''
class TestParentType(BaseModel[int]):
class TestParentType(BaseModel[int]):
@@
-81,47
+89,56
@@
class TestsSansDB(TestCase):
self.assertEqual(attr.at(timestamp_after_c), 'C')
self.assertEqual(attr.at(timestamp_after_c), 'C')
-class TestsWithDB(TestCaseWithDB):
+class TestsWithDB
Str
(TestCaseWithDB):
"""Module tests requiring DB setup."""
"""Module tests requiring DB setup."""
+ default_vals: list[str | float] = ['A', 'B', 'C']
+ init_sql = SQL_TEST_TABLE_STR
def setUp(self) -> None:
super().setUp()
def setUp(self) -> None:
super().setUp()
- self.db_conn.exec(SQL_TEST_TABLE)
+ self.db_conn.exec(self.init_sql)
+ self.test_parent = TestParentType(1)
+ self.attr = VersionedAttribute(self.test_parent,
+ 'versioned_tests', self.default_vals[0])
def test_VersionedAttribute_save(self) -> None:
"""Test .save() to write to DB."""
def test_VersionedAttribute_save(self) -> None:
"""Test .save() to write to DB."""
- test_parent = TestParentType(1)
- attr = VersionedAttribute(test_parent, 'versioned_tests', 'A')
# check mere .set() calls do not by themselves reflect in the DB
# check mere .set() calls do not by themselves reflect in the DB
-
attr.set('B'
)
+
self.attr.set(self.default_vals[1]
)
self.assertEqual([],
self.db_conn.row_where('versioned_tests',
'parent', 1))
# check .save() makes history appear in DB
self.assertEqual([],
self.db_conn.row_where('versioned_tests',
'parent', 1))
# check .save() makes history appear in DB
- attr.save(self.db_conn)
+
self.
attr.save(self.db_conn)
vals_found = []
for row in self.db_conn.row_where('versioned_tests', 'parent', 1):
vals_found += [row[2]]
vals_found = []
for row in self.db_conn.row_where('versioned_tests', 'parent', 1):
vals_found += [row[2]]
- self.assertEqual([
'B'
], vals_found)
+ self.assertEqual([
self.default_vals[1]
], vals_found)
# check .save() also updates history in DB
# check .save() also updates history in DB
-
attr.set('C'
)
- attr.save(self.db_conn)
+
self.attr.set(self.default_vals[2]
)
+
self.
attr.save(self.db_conn)
vals_found = []
for row in self.db_conn.row_where('versioned_tests', 'parent', 1):
vals_found += [row[2]]
vals_found = []
for row in self.db_conn.row_where('versioned_tests', 'parent', 1):
vals_found += [row[2]]
- self.assertEqual(['B', 'C'], sorted(vals_found))
+ self.assertEqual([self.default_vals[1], self.default_vals[2]],
+ sorted(vals_found))
def test_VersionedAttribute_history_from_row(self) -> None:
""""Test .history_from_row() properly interprets DB rows."""
def test_VersionedAttribute_history_from_row(self) -> None:
""""Test .history_from_row() properly interprets DB rows."""
- test_parent = TestParentType(1)
- attr = VersionedAttribute(test_parent, 'versioned_tests', 'A')
- attr.set('B')
- attr.set('C')
- attr.save(self.db_conn)
- loaded_attr = VersionedAttribute(test_parent, 'versioned_tests', 'A')
+ self.attr.set(self.default_vals[1])
+ self.attr.set(self.default_vals[2])
+ self.attr.save(self.db_conn)
+ loaded_attr = VersionedAttribute(self.test_parent, 'versioned_tests',
+ self.default_vals[0])
for row in self.db_conn.row_where('versioned_tests', 'parent', 1):
loaded_attr.history_from_row(row)
for row in self.db_conn.row_where('versioned_tests', 'parent', 1):
loaded_attr.history_from_row(row)
- for timestamp, value in attr.history.items():
+ for timestamp, value in
self.
attr.history.items():
self.assertEqual(value, loaded_attr.history[timestamp])
self.assertEqual(value, loaded_attr.history[timestamp])
- self.assertEqual(len(attr.history.keys()),
+ self.assertEqual(len(
self.
attr.history.keys()),
len(loaded_attr.history.keys()))
len(loaded_attr.history.keys()))
+
+
+class TestsWithDBFloat(TestsWithDBStr):
+ """Module tests requiring DB setup."""
+ default_vals: list[str | float] = [0.9, 1.1, 2]
+ init_sql = SQL_TEST_TABLE_FLOAT