return obj
return None
- def _cache(self) -> None:
+ def cache(self) -> None:
"""Update object in class's cache.
Also calls ._disappear if cache holds older reference to object of same
table_name = attr.table_name
for row_ in db_conn.row_where(table_name, 'parent', obj.id_):
attr.history_from_row(row_)
- obj._cache()
+ obj.cache()
return obj
@classmethod
values)
if not isinstance(self.id_, str):
self.id_ = cursor.lastrowid # type: ignore[assignment]
- self._cache()
+ self.cache()
for attr_name in self.to_save_versioned:
getattr(self, attr_name).save(db_conn)
for table, column, attr_name, key_index in self.to_save_relations:
default_init_kwargs = {'is_active': False}
test_versioneds = {'title': str, 'description': str}
- def test_Condition_from_table_row(self) -> None:
+ def test_from_table_row(self) -> None:
"""Test .from_table_row() properly reads in class from DB"""
- self.check_from_table_row()
+ super().test_from_table_row()
self.check_versioned_from_table_row('title', str)
self.check_versioned_from_table_row('description', str)
kwargs = {'date': self.default_ids[0], 'comment': 'foo'}
self.check_saving_and_caching(**kwargs)
- def test_Day_from_table_row(self) -> None:
- """Test .from_table_row() properly reads in class from DB"""
- self.check_from_table_row()
-
def test_Day_by_id(self) -> None:
"""Test .by_id()."""
self.check_by_id()
self.assertEqual(sorted(r.enables), sorted(set2))
self.assertEqual(sorted(r.disables), sorted(set3))
- def test_Process_from_table_row(self) -> None:
+ def test_from_table_row(self) -> None:
"""Test .from_table_row() properly reads in class from DB"""
- self.check_from_table_row()
+ super().test_from_table_row()
self.check_versioned_from_table_row('title', str)
self.check_versioned_from_table_row('description', str)
self.check_versioned_from_table_row('effort', float)
self.assertEqual(self.checked_class(id2), by_id_created)
self.check_storage([obj])
- def check_from_table_row(self, *args: Any) -> None:
- """Test .from_table_row() properly reads in class from DB"""
+ def test_from_table_row(self) -> None:
+ """Test .from_table_row() properly reads in class from DB."""
+ if not hasattr(self, 'checked_class'):
+ return
id_ = self.default_ids[0]
- obj = self.checked_class(id_, *args) # pylint: disable=not-callable
+ obj = self.checked_class(id_, **self.default_init_kwargs)
obj.save(self.db_conn)
assert isinstance(obj.id_, type(self.default_ids[0]))
for row in self.db_conn.row_where(self.checked_class.table_name,
'id', obj.id_):
+ # check .from_table_row reproduces state saved, no matter if obj
+ # later changed (with caching even)
hash_original = hash(obj)
+ attr_name = self.checked_class.to_save[-1]
+ attr = getattr(obj, attr_name)
+ if isinstance(attr, (int, float)):
+ setattr(obj, attr_name, attr + 1)
+ elif isinstance(attr, str):
+ setattr(obj, attr_name, attr + "_")
+ elif isinstance(attr, bool):
+ setattr(obj, attr_name, not attr)
+ obj.cache()
+ to_cmp = getattr(obj, attr_name)
retrieved = self.checked_class.from_table_row(self.db_conn, row)
+ self.assertNotEqual(to_cmp, getattr(retrieved, attr_name))
self.assertEqual(hash_original, hash(retrieved))
+ # check cache contains what .from_table_row just produced
self.assertEqual({retrieved.id_: retrieved},
self.checked_class.get_cache())