From: Christian Heller Date: Wed, 1 May 2024 22:31:26 +0000 (+0200) Subject: Minor code-stylistic improvements. X-Git-Url: https://plomlompom.com/repos/%7B%7Bdb.prefix%7D%7D/%7B%7B%20web_path%20%7D%7D/decks/pick_tasks?a=commitdiff_plain;h=bdc1f2e1962b745d72c6a94f5071c24455c54b18;p=plomtask Minor code-stylistic improvements. --- diff --git a/plomtask/misc.py b/plomtask/misc.py index 2c46c1c..ab39df0 100644 --- a/plomtask/misc.py +++ b/plomtask/misc.py @@ -2,8 +2,8 @@ from datetime import datetime from typing import Any from sqlite3 import Row -from plomtask.db import DatabaseConnection from time import sleep +from plomtask.db import DatabaseConnection TIMESTAMP_FMT = '%Y-%m-%d %H:%M:%S.%f' diff --git a/tests/versioned_attributes.py b/tests/versioned_attributes.py index df6fc46..fdb1939 100644 --- a/tests/versioned_attributes.py +++ b/tests/versioned_attributes.py @@ -1,10 +1,10 @@ """"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 ( @@ -14,13 +14,15 @@ 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 @@ -36,7 +38,7 @@ class TestsSansDB(TestCase): # 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 @@ -45,7 +47,7 @@ class TestsSansDB(TestCase): 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') @@ -53,30 +55,30 @@ class TestsSansDB(TestCase): 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): @@ -93,8 +95,9 @@ 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): @@ -122,4 +125,3 @@ class TestsWithDB(TestCaseWithDB): self.assertEqual(value, loaded_attr.history[timestamp]) self.assertEqual(len(attr.history.keys()), len(loaded_attr.history.keys())) -