home · contact · privacy
Enable server to alternatively output response ctx as JSON, for debugging and testing...
[plomtask] / tests / days.py
index 4727fac743dad82bc5aee56635d253ac9b63c192..901667f4c6e0276a2800bf4b21b15b03a17be2fb 100644 (file)
@@ -1,8 +1,9 @@
 """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 todays_date
+from plomtask.dating import date_in_n_days
 from plomtask.days import Day
 from plomtask.exceptions import BadFormatException
 
@@ -87,7 +88,7 @@ class TestsWithDB(TestCaseWithDB):
                          [day5, day6, day7])
         self.check_storage([day1, day2, day3, day6])
         # check 'today' is interpreted as today's date
-        today = Day(todays_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'),
@@ -105,6 +106,23 @@ class TestsWithDB(TestCaseWithDB):
 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)
+
     def test_do_GET(self) -> None:
         """Test /day and /calendar response codes, and / redirect."""
         self.check_get('/day', 200)
@@ -118,8 +136,8 @@ class TestsWithServer(TestCaseWithServer):
 
     def test_do_POST_day(self) -> None:
         """Test POST /day."""
-        form_data = {'day_comment': ''}
+        form_data = {'day_comment': '', 'make_type': 'full'}
         self.check_post(form_data, '/day', 400)
         self.check_post(form_data, '/day?date=foo', 400)
-        self.check_post(form_data, '/day?date=2024-01-01', 302)
+        self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302)
         self.check_post({'foo': ''}, '/day?date=2024-01-01', 400)