home · contact · privacy
Refactor tests, expand Days testing.
authorChristian Heller <c.heller@plomlompom.de>
Thu, 20 Jun 2024 11:50:50 +0000 (13:50 +0200)
committerChristian Heller <c.heller@plomlompom.de>
Thu, 20 Jun 2024 11:50:50 +0000 (13:50 +0200)
tests/conditions.py
tests/days.py
tests/utils.py

index 468b1e8b13499ea3c6775baf40f68cad1b646a87..af5f661d809995e26bdcc681c4dcff73f0fdb7a6 100644 (file)
@@ -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
index f26885e27468bdab9f9d9fc5ff383f3011cb8403..d14e0afc58bd358ff92c1a032ea6f0367081acd2 100644 (file)
@@ -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)
index f473c180ba565355a3b6ac0a03acb85d2fa307de..ed4101a6c32a52d1e26fadb99c35fd8c44d2178a 100644 (file)
@@ -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()