class TestsWithServer(TestCaseWithServer):
"""Tests against our HTTP server/handler (and database)."""
+ checked_class = Todo
def _post_exp_todo(
self, id_: int, payload: dict[str, Any], exp: Expected) -> None:
# test we cannot just POST into non-existing Todo
self.check_post({}, '/todo', 404)
self.check_post({}, '/todo?id=FOO', 400)
- self.check_post({}, '/todo?id=0', 404)
+ self.check_post({}, '/todo?id=0', 400)
self.check_post({}, '/todo?id=1', 404)
# test malformed values on existing Todo
self.post_exp_day([], {'new_todo': [1]})
def test_GET_todo(self) -> None:
"""Test GET /todo response codes."""
# test malformed or illegal parameter values
- self.check_get('/todo', 404)
- self.check_get('/todo?id=', 404)
- self.check_get('/todo?id=foo', 400)
- self.check_get('/todo?id=0', 404)
- self.check_get('/todo?id=2', 404)
+ self.check_get_defaults('/todo')
# test all existing Processes are shown as available
exp = ExpectedGetTodo(1)
self.post_exp_process([exp], {}, 1)
default_init_kwargs: dict[str, Any] = {}
@staticmethod
- def _run_if_checked_class(f: Callable[..., None]) -> Callable[..., None]:
- def wrapper(self: TestCase) -> None:
- if hasattr(self, 'checked_class'):
- f(self)
- return wrapper
-
- @classmethod
- def _run_on_versioned_attributes(cls,
- f: Callable[..., None]
+ def _run_on_versioned_attributes(f: Callable[..., None]
) -> Callable[..., None]:
- @cls._run_if_checked_class
def wrapper(self: TestCase) -> None:
assert isinstance(self, TestCaseAugmented)
for attr_name in self.checked_class.to_save_versioned():
f(self, owner, attr_name, attr, default, to_set)
return wrapper
+ @classmethod
+ def _run_if_sans_db(cls, f: Callable[..., None]) -> Callable[..., None]:
+ def wrapper(self: TestCaseSansDB) -> None:
+ if issubclass(cls, TestCaseSansDB):
+ f(self)
+ return wrapper
+
+ @classmethod
+ def _run_if_with_db_but_not_server(cls,
+ f: Callable[..., None]
+ ) -> Callable[..., None]:
+ def wrapper(self: TestCaseWithDB) -> None:
+ if issubclass(cls, TestCaseWithDB) and\
+ not issubclass(cls, TestCaseWithServer):
+ f(self)
+ return wrapper
+
@classmethod
def _make_from_defaults(cls, id_: float | str | None) -> Any:
return cls.checked_class(id_, **cls.default_init_kwargs)
legal_ids: list[str] | list[int] = [1, 5]
illegal_ids: list[str] | list[int] = [0]
- @TestCaseAugmented._run_if_checked_class
+ @TestCaseAugmented._run_if_sans_db
def test_id_validation(self) -> None:
"""Test .id_ validation/setting."""
for id_ in self.illegal_ids:
obj = self._make_from_defaults(id_)
self.assertEqual(obj.id_, id_)
+ @TestCaseAugmented._run_if_sans_db
@TestCaseAugmented._run_on_versioned_attributes
def test_versioned_set(self,
_: Any,
attr.set(to_set[1])
self.assertEqual(timesorted_vals, expected)
+ @TestCaseAugmented._run_if_sans_db
@TestCaseAugmented._run_on_versioned_attributes
def test_versioned_newest(self,
_: Any,
attr.set(default)
self.assertEqual(attr.newest, default)
+ @TestCaseAugmented._run_if_sans_db
@TestCaseAugmented._run_on_versioned_attributes
def test_versioned_at(self,
_: Any,
self.assertEqual(start, end)
self.assertEqual(items, [obj_today])
+ @TestCaseAugmented._run_if_with_db_but_not_server
@TestCaseAugmented._run_on_versioned_attributes
def test_saving_versioned_attributes(self,
owner: Any,
attr_vals_saved = retrieve_attr_vals(attr)
self.assertEqual(to_set, attr_vals_saved)
- @TestCaseAugmented._run_if_checked_class
+ @TestCaseAugmented._run_if_with_db_but_not_server
def test_saving_and_caching(self) -> None:
"""Test effects of .cache() and .save()."""
id1 = self.default_ids[0]
with self.assertRaises(HandledException):
obj1.save(self.db_conn)
- @TestCaseAugmented._run_if_checked_class
+ @TestCaseAugmented._run_if_with_db_but_not_server
def test_by_id(self) -> None:
"""Test .by_id()."""
id1, id2, _ = self.default_ids
obj2.save(self.db_conn)
self.assertEqual(obj2, self.checked_class.by_id(self.db_conn, id2))
- @TestCaseAugmented._run_if_checked_class
+ @TestCaseAugmented._run_if_with_db_but_not_server
def test_by_id_or_create(self) -> None:
"""Test .by_id_or_create."""
# check .by_id_or_create fails if wrong class
self.checked_class.by_id(self.db_conn, item.id_)
self.assertEqual(self.checked_class(item.id_), item)
- @TestCaseAugmented._run_if_checked_class
+ @TestCaseAugmented._run_if_with_db_but_not_server
def test_from_table_row(self) -> None:
"""Test .from_table_row() properly reads in class directly from DB."""
id_ = self.default_ids[0]
self.assertEqual({retrieved.id_: retrieved},
self.checked_class.get_cache())
+ @TestCaseAugmented._run_if_with_db_but_not_server
@TestCaseAugmented._run_on_versioned_attributes
def test_versioned_history_from_row(self,
owner: Any,
for timestamp, value in attr.history.items():
self.assertEqual(value, loaded_attr.history[timestamp])
- @TestCaseAugmented._run_if_checked_class
+ @TestCaseAugmented._run_if_with_db_but_not_server
def test_all(self) -> None:
"""Test .all() and its relation to cache and savings."""
id1, id2, id3 = self.default_ids
self.assertEqual(sorted(self.checked_class.all(self.db_conn)),
sorted([item1, item2, item3]))
- @TestCaseAugmented._run_if_checked_class
+ @TestCaseAugmented._run_if_with_db_but_not_server
def test_singularity(self) -> None:
"""Test pointers made for single object keep pointing to it."""
id1 = self.default_ids[0]
retrieved = self.checked_class.by_id(self.db_conn, id1)
self.assertEqual(new_attr, getattr(retrieved, attr_name))
+ @TestCaseAugmented._run_if_with_db_but_not_server
@TestCaseAugmented._run_on_versioned_attributes
def test_versioned_singularity(self,
owner: Any,
attr_retrieved = getattr(retrieved, attr_name)
self.assertEqual(attr.history, attr_retrieved.history)
- @TestCaseAugmented._run_if_checked_class
+ @TestCaseAugmented._run_if_with_db_but_not_server
def test_remove(self) -> None:
"""Test .remove() effects on DB and cache."""
id_ = self.default_ids[0]
else:
self.assertEqual(self.conn.getresponse().status, expected_code)
- def check_get_defaults(self, path: str) -> None:
+ def check_get_defaults(self,
+ path: str,
+ default_id: str = '1',
+ id_name: str = 'id'
+ ) -> None:
"""Some standard model paths to test."""
- self.check_get(path, 200)
- self.check_get(f'{path}?id=', 200)
- self.check_get(f'{path}?id=foo', 400)
- self.check_get(f'/{path}?id=0', 500)
- self.check_get(f'{path}?id=1', 200)
+ nonexist_status = 200 if self.checked_class.can_create_by_id else 404
+ self.check_get(path, nonexist_status)
+ self.check_get(f'{path}?{id_name}=', 400)
+ self.check_get(f'{path}?{id_name}=foo', 400)
+ self.check_get(f'/{path}?{id_name}=0', 400)
+ self.check_get(f'{path}?{id_name}={default_id}', nonexist_status)
def check_json_get(self, path: str, expected: Expected) -> None:
"""Compare JSON on GET path with expected.