From: Christian Heller Date: Thu, 20 Jun 2024 11:50:50 +0000 (+0200) Subject: Refactor tests, expand Days testing. X-Git-Url: https://plomlompom.com/repos/day?a=commitdiff_plain;h=b56761522f6ec874fde1eb9d4d69d8093fe70227;p=plomtask Refactor tests, expand Days testing. --- diff --git a/tests/conditions.py b/tests/conditions.py index 468b1e8..af5f661 100644 --- a/tests/conditions.py +++ b/tests/conditions.py @@ -65,31 +65,6 @@ class TestsWithServer(TestCaseWithServer): d['_versioned']['description'][i] = description return d - @staticmethod - def proc_as_dict(id_: int = 1, - title: str = 'A', - enables: None | list[dict[str, object]] = None, - disables: None | list[dict[str, object]] = None, - conditions: None | list[dict[str, object]] = None, - blockers: None | list[dict[str, object]] = None - ) -> dict[str, object]: - """Return JSON of Process to expect.""" - # pylint: disable=too-many-arguments - d = {'id': id_, - 'calendarize': False, - 'suppressed_steps': [], - 'explicit_steps': [], - '_versioned': { - 'title': {0: title}, - 'description': {0: ''}, - 'effort': {0: 1.0} - }, - 'conditions': conditions if conditions else [], - 'disables': disables if disables else [], - 'enables': enables if enables else [], - 'blockers': blockers if blockers else []} - return d - def test_do_POST_condition(self) -> None: """Test POST /condition and its effect on GET /condition[s].""" # check empty POST fails diff --git a/tests/days.py b/tests/days.py index f26885e..d14e0af 100644 --- a/tests/days.py +++ b/tests/days.py @@ -1,7 +1,6 @@ """Test Days module.""" from unittest import TestCase from datetime import datetime -from json import loads as json_loads from tests.utils import TestCaseWithDB, TestCaseWithServer from plomtask.dating import date_in_n_days from plomtask.days import Day @@ -16,6 +15,7 @@ class TestsSansDB(TestCase): """Test Day's date parsing.""" self.assertEqual(datetime(2024, 5, 1), Day('2024-05-01').datetime) self.assertEqual('Sunday', Day('2024-03-17').weekday) + self.assertEqual('March', Day('2024-03-17').month_name) self.assertEqual('2023-12-31', Day('2024-01-01').prev_date) self.assertEqual('2023-03-01', Day('2023-02-28').next_date) @@ -39,10 +39,9 @@ class TestsWithDB(TestCaseWithDB): day1 = Day(date1) day2 = Day(date2) day3 = Day(date3) - day1.save(self.db_conn) - day2.save(self.db_conn) - day3.save(self.db_conn) - # check date range is a closed interval + for day in [day1, day2, day3]: + day.save(self.db_conn) + # check date range includes limiter days self.assertEqual(Day.by_date_range_filled(self.db_conn, date1, date3), [day1, day2, day3]) # check first date range value excludes what's earlier @@ -66,37 +65,69 @@ class TestsWithDB(TestCaseWithDB): self.check_identity_with_cache_and_db([day1, day2, day3, day6]) # check 'today' is interpreted as today's date today = Day(date_in_n_days(0)) - today.save(self.db_conn) self.assertEqual(Day.by_date_range_filled(self.db_conn, 'today', 'today'), [today]) + prev_day = Day(date_in_n_days(-1)) + next_day = Day(date_in_n_days(1)) + self.assertEqual(Day.by_date_range_filled(self.db_conn, + 'yesterday', 'tomorrow'), + [prev_day, today, next_day]) class TestsWithServer(TestCaseWithServer): """Tests against our HTTP server/handler (and database).""" - def test_get_json(self) -> None: - """Test /day for JSON response.""" - self.conn.request('GET', '/day?date=2024-01-01') - response = self.conn.getresponse() - self.assertEqual(response.status, 200) - expected = {'day': {'id': '2024-01-01', - 'comment': '', - 'todos': []}, - 'top_nodes': [], - 'make_type': '', - 'enablers_for': {}, - 'disablers_for': {}, - 'conditions_present': [], - 'processes': []} - retrieved = json_loads(response.read().decode()) - self.assertEqual(expected, retrieved) + @staticmethod + def day_dict(date: str) -> dict[str, object]: + """Return JSON of Process to expect.""" + d: dict[str, object] = {'day': {'id': date, + 'comment': '', + 'todos': []}, + 'top_nodes': [], + 'make_type': '', + 'enablers_for': {}, + 'disablers_for': {}, + 'conditions_present': [], + 'processes': []} + return d + + def test_do_GET_day(self) -> None: + """Test GET /day basics.""" + # check undefined day + date = date_in_n_days(0) + expected = self.day_dict(date) + self.check_json_get('/day', expected) + # check "today", "yesterday", "tomorrow" days + self.check_json_get('/day?date=today', expected) + expected = self.day_dict(date_in_n_days(1)) + self.check_json_get('/day?date=tomorrow', expected) + expected = self.day_dict(date_in_n_days(-1)) + self.check_json_get('/day?date=yesterday', expected) + # check wrong day strings + self.check_get('/day?date=foo', 400) + self.check_get('/day?date=2024-02-30', 400) + # check defined day + date = '2024-01-01' + expected = self.day_dict(date) + self.check_json_get(f'/day?date={date}', expected) + # check saved day + post_day = {'day_comment': 'foo', 'make_type': ''} + self.check_post(post_day, f'/day?date={date}', 302, + f'/day?date={date}&make_type=') + assert isinstance(expected['day'], dict) + expected['day']['comment'] = 'foo' + self.check_json_get(f'/day?date={date}', expected) + # check GET parameter POST not affecting reply to non-parameter GET + post_day['make_type'] = 'foo' + self.check_post(post_day, f'/day?date={date}', 302, + f'/day?date={date}&make_type=foo') + self.check_json_get(f'/day?date={date}', expected) + expected['make_type'] = 'bar' + self.check_json_get(f'/day?date={date}&make_type=bar', expected) def test_do_GET(self) -> None: """Test /day and /calendar response codes, and / redirect.""" - self.check_get('/day', 200) - self.check_get('/day?date=3000-01-01', 200) - self.check_get('/day?date=FOO', 400) self.check_get('/calendar', 200) self.check_get('/calendar?start=&end=', 200) self.check_get('/calendar?start=today&end=today', 200) diff --git a/tests/utils.py b/tests/utils.py index f473c18..ed4101a 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -327,6 +327,33 @@ class TestCaseWithServer(TestCaseWithDB): self.server_thread.join() super().tearDown() + @staticmethod + def proc_as_dict(id_: int = 1, + title: str = 'A', + description: str = '', + effort: float = 1.0, + enables: None | list[dict[str, object]] = None, + disables: None | list[dict[str, object]] = None, + conditions: None | list[dict[str, object]] = None, + blockers: None | list[dict[str, object]] = None + ) -> dict[str, object]: + """Return JSON of Process to expect.""" + # pylint: disable=too-many-arguments + d = {'id': id_, + 'calendarize': False, + 'suppressed_steps': [], + 'explicit_steps': [], + '_versioned': { + 'title': {0: title}, + 'description': {0: description}, + 'effort': {0: effort} + }, + 'conditions': conditions if conditions else [], + 'disables': disables if disables else [], + 'enables': enables if enables else [], + 'blockers': blockers if blockers else []} + return d + def check_redirect(self, target: str) -> None: """Check that self.conn answers with a 302 redirect to target.""" response = self.conn.getresponse()