home · contact · privacy
Refactor VersionedAttribute tests.
authorChristian Heller <c.heller@plomlompom.de>
Wed, 1 May 2024 23:17:42 +0000 (01:17 +0200)
committerChristian Heller <c.heller@plomlompom.de>
Wed, 1 May 2024 23:17:42 +0000 (01:17 +0200)
tests/versioned_attributes.py

index 5238d70bfdeb257b9d36e6fc58bfa8abb26255ad..69c31fef1ac83853092229192c765ed5a4122824 100644 (file)
@@ -87,25 +87,26 @@ class TestsWithDB(TestCaseWithDB):
     def setUp(self) -> None:
         super().setUp()
         self.db_conn.exec(SQL_TEST_TABLE)
+        self.test_parent = TestParentType(1)
+        self.attr = VersionedAttribute(self.test_parent,
+                                       'versioned_tests', 'A')
 
     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
-        attr.set('B')
+        self.attr.set('B')
         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]]
         self.assertEqual(['B'], vals_found)
         # check .save() also updates history in DB
-        attr.set('C')
-        attr.save(self.db_conn)
+        self.attr.set('C')
+        self.attr.save(self.db_conn)
         vals_found = []
         for row in self.db_conn.row_where('versioned_tests', 'parent', 1):
             vals_found += [row[2]]
@@ -113,15 +114,14 @@ class TestsWithDB(TestCaseWithDB):
 
     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('B')
+        self.attr.set('C')
+        self.attr.save(self.db_conn)
+        loaded_attr = VersionedAttribute(self.test_parent,
+                                         'versioned_tests', 'A')
         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(len(attr.history.keys()),
+        self.assertEqual(len(self.attr.history.keys()),
                          len(loaded_attr.history.keys()))