""""Test Versioned Attributes in the abstract."""
from unittest import TestCase
-from tests.utils import TestCaseWithDB
from time import sleep
from datetime import datetime
+from tests.utils import TestCaseWithDB
from plomtask.misc import VersionedAttribute, TIMESTAMP_FMT
-from plomtask.db import BaseModel
+from plomtask.db import BaseModel
SQL_TEST_TABLE = '''
CREATE TABLE versioned_tests (
PRIMARY KEY (parent, timestamp)
);
'''
+
+
class TestParentType(BaseModel[int]):
- pass
+ """Dummy abstracting whatever may use VersionedAttributes."""
class TestsSansDB(TestCase):
"""Tests not requiring DB setup."""
-
+
def test_VersionedAttribute_set(self) -> None:
"""Test .set() behaves as expected."""
# check value gets set even if already is the default
# check that different value _will_ be set/added
attr.set('B')
self.assertEqual(sorted(attr.history.values()), ['A', 'B'])
- # check that a previously used value can be set if not most recent
+ # check that a previously used value can be set if not most recent
attr.set('A')
self.assertEqual(sorted(attr.history.values()), ['A', 'A', 'B'])
# again check for same value not being set twice in a row, even for
self.assertEqual(sorted(attr.history.values()), ['A', 'A', 'B', 'D'])
attr.set('D')
self.assertEqual(sorted(attr.history.values()), ['A', 'A', 'B', 'D'])
-
+
def test_VersionedAttribute_newest(self) -> None:
"""Test .newest returns newest element, or default on empty."""
attr = VersionedAttribute(None, '', 'A')
attr.set('B')
self.assertEqual(attr.newest, 'B')
attr.set('C')
-
+
def test_VersionedAttribute_at(self) -> None:
"""Test .at() returns values nearest to queried time, or default."""
# check .at() return default on empty history
attr = VersionedAttribute(None, '', 'A')
- timestamp_A = datetime.now().strftime(TIMESTAMP_FMT)
- self.assertEqual(attr.at(timestamp_A), 'A')
- # check value exactly at timestamp returned
+ timestamp_a = datetime.now().strftime(TIMESTAMP_FMT)
+ self.assertEqual(attr.at(timestamp_a), 'A')
+ # check value exactly at timestamp returned
attr.set('B')
- timestamp_B = list(attr.history.keys())[0]
- self.assertEqual(attr.at(timestamp_B), 'B')
- # check earliest value returned if exists, rather than default
- self.assertEqual(attr.at(timestamp_A), 'B')
- # check reverts to previous value for timestamps not indexed
+ timestamp_b = list(attr.history.keys())[0]
+ self.assertEqual(attr.at(timestamp_b), 'B')
+ # check earliest value returned if exists, rather than default
+ self.assertEqual(attr.at(timestamp_a), 'B')
+ # check reverts to previous value for timestamps not indexed
sleep(0.00001)
timestamp_between = datetime.now().strftime(TIMESTAMP_FMT)
sleep(0.00001)
attr.set('C')
- timestamp_C = sorted(attr.history.keys())[-1]
- self.assertEqual(attr.at(timestamp_C), 'C')
+ timestamp_c = sorted(attr.history.keys())[-1]
+ self.assertEqual(attr.at(timestamp_c), 'C')
self.assertEqual(attr.at(timestamp_between), 'B')
sleep(0.00001)
- timestamp_after_C = datetime.now().strftime(TIMESTAMP_FMT)
- self.assertEqual(attr.at(timestamp_after_C), 'C')
+ timestamp_after_c = datetime.now().strftime(TIMESTAMP_FMT)
+ self.assertEqual(attr.at(timestamp_after_c), 'C')
class TestsWithDB(TestCaseWithDB):
# check mere .set() calls do not by themselves reflect in the DB
attr.set('B')
self.assertEqual([],
- self.db_conn.row_where('versioned_tests', 'parent', 1))
- # check .save() makes history appear in DB
+ self.db_conn.row_where('versioned_tests',
+ 'parent', 1))
+ # check .save() makes history appear in DB
attr.save(self.db_conn)
vals_found = []
for row in self.db_conn.row_where('versioned_tests', 'parent', 1):
self.assertEqual(value, loaded_attr.history[timestamp])
self.assertEqual(len(attr.history.keys()),
len(loaded_attr.history.keys()))
-