From: Christian Heller Date: Sun, 14 Jul 2024 16:07:50 +0000 (+0200) Subject: Reduce depedency of Days testing on plomtask.dating code. X-Git-Url: https://plomlompom.com/repos/%7B%7Bprefix%7D%7D/%7B%7B%20web_path%20%7D%7D/test.html?a=commitdiff_plain;h=bb6e7bce95d43cd88ee57535a7b624101187bb6d;p=plomtask Reduce depedency of Days testing on plomtask.dating code. --- diff --git a/tests/days.py b/tests/days.py index 00ba5fd..f57c532 100644 --- a/tests/days.py +++ b/tests/days.py @@ -2,9 +2,13 @@ from datetime import datetime, timedelta from typing import Callable from tests.utils import TestCaseSansDB, TestCaseWithDB, TestCaseWithServer -from plomtask.dating import date_in_n_days, DATE_FORMAT +from plomtask.dating import date_in_n_days as tested_date_in_n_days from plomtask.days import Day +# so far the same as plomtask.dating.DATE_FORMAT, but for testing purposes we +# want to explicitly state our expectations here indepedently from that +TESTING_DATE_FORMAT = '%Y-%m-%d' + class TestsSansDB(TestCaseSansDB): """Days module tests not requiring DB setup.""" @@ -13,10 +17,11 @@ class TestsSansDB(TestCaseSansDB): illegal_ids = ['foo', '2023-02-29', '2024-02-30', '2024-02-01 23:00:00'] def test_date_in_n_days(self) -> None: - """Test dating.date_in_n_days, as we rely on it in later tests.""" + """Test dating.date_in_n_days""" for n in [-100, -2, -1, 0, 1, 2, 1000]: date = datetime.now() + timedelta(days=n) - self.assertEqual(date_in_n_days(n), date.strftime(DATE_FORMAT)) + self.assertEqual(tested_date_in_n_days(n), + date.strftime(TESTING_DATE_FORMAT)) def test_Day_datetime_weekday_neighbor_dates(self) -> None: """Test Day's date parsing and neighbourhood resolution.""" @@ -92,6 +97,17 @@ class TestsWithDB(TestCaseWithDB): class TestsWithServer(TestCaseWithServer): """Tests against our HTTP server/handler (and database).""" + @staticmethod + def _testing_date_in_n_days(n: int) -> str: + """Return in TEST_DATE_FORMAT date from today + n days. + + As with TESTING_DATE_FORMAT, we assume this equal the original's code + at plomtask.dating.date_in_n_days, but want to state our expectations + explicitly to rule out importing issues from the original. + """ + date = datetime.now() + timedelta(days=n) + return date.strftime(TESTING_DATE_FORMAT) + @classmethod def GET_day_dict(cls, date: str) -> dict[str, object]: """Return JSON of GET /day to expect.""" @@ -113,12 +129,12 @@ class TestsWithServer(TestCaseWithServer): NB: the date string list to key 'days' implies/expects a continuous (= gaps filled) alphabetical order of dates by virtue of range(start, - end+1) and date_in_n_days tested in TestsSansDB.test_date_in_n_days. + end+1) and date_in_n_days. """ - today_date = date_in_n_days(0) - start_date = date_in_n_days(start) - end_date = date_in_n_days(end) - dates = [date_in_n_days(i) for i in range(start, end+1)] + today_date = cls._testing_date_in_n_days(0) + start_date = cls._testing_date_in_n_days(start) + end_date = cls._testing_date_in_n_days(end) + dates = [cls._testing_date_in_n_days(i) for i in range(start, end+1)] days = [cls._day_as_dict(d) for d in dates] library = {'Day': cls.as_refs(days)} if len(days) > 0 else {} return {'today': today_date, 'start': start_date, 'end': end_date, @@ -199,7 +215,7 @@ class TestsWithServer(TestCaseWithServer): self.check_get('/day?date=foo', 400) self.check_get('/day?date=2024-02-30', 400) # check undefined day - date = date_in_n_days(0) + date = self._testing_date_in_n_days(0) expected = self.GET_day_dict(date) self.check_json_get('/day', expected) # NB: GET ?date="today"/"yesterday"/"tomorrow" in test_basic_POST_day @@ -283,7 +299,7 @@ class TestsWithServer(TestCaseWithServer): ('today', 0, 'b'), ('yesterday', -1, 'c'), ('tomorrow', +1, 'd')]: - date = name if dist is None else date_in_n_days(dist) + date = name if dist is None else self._testing_date_in_n_days(dist) post = {'day_comment': test_str, 'make_type': f'x:{test_str}'} post_url = f'/day?date={name}' redir_url = f'{post_url}&make_type={post["make_type"]}' @@ -382,10 +398,10 @@ class TestsWithServer(TestCaseWithServer): self.check_json_get('/calendar?start=tomorrow&end=today', expected) # check saved day shows up in results, proven by its comment post_day: dict[str, object] = {'day_comment': 'foo', 'make_type': ''} - date1 = date_in_n_days(-2) + date1 = self._testing_date_in_n_days(-2) self._post_day(f'date={date1}', post_day) - start_date = date_in_n_days(-5) - end_date = date_in_n_days(+5) + start_date = self._testing_date_in_n_days(-5) + end_date = self._testing_date_in_n_days(+5) url = f'/calendar?start={start_date}&end={end_date}' expected = self.GET_calendar_dict(-5, +5) assert isinstance(expected['_library'], dict)