From b255f56691db0056f1ee164b43329a664e043064 Mon Sep 17 00:00:00 2001
From: Christian Heller <c.heller@plomlompom.de>
Date: Thu, 2 May 2024 01:17:42 +0200
Subject: [PATCH] Refactor VersionedAttribute tests.

---
 tests/versioned_attributes.py | 28 ++++++++++++++--------------
 1 file changed, 14 insertions(+), 14 deletions(-)

diff --git a/tests/versioned_attributes.py b/tests/versioned_attributes.py
index 5238d70..69c31fe 100644
--- a/tests/versioned_attributes.py
+++ b/tests/versioned_attributes.py
@@ -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()))
-- 
2.30.2