home · contact · privacy
Move VersionedAttributes code into appropriotely named module.
[plomtask] / tests / utils.py
index d41e7b39ad5fe07abdc701be674d9f81678cd30f..b7d126931f2195402db85b0ca61c87b791f3be77 100644 (file)
@@ -5,15 +5,24 @@ from http.client import HTTPConnection
 from urllib.parse import urlencode
 from datetime import datetime
 from os import remove as remove_file
-from typing import Mapping
+from typing import Mapping, Any
 from plomtask.db import DatabaseFile, DatabaseConnection
 from plomtask.http import TaskHandler, TaskServer
+from plomtask.processes import Process, ProcessStep
+from plomtask.conditions import Condition
+from plomtask.days import Day
+from plomtask.todos import Todo
 
 
 class TestCaseWithDB(TestCase):
     """Module tests not requiring DB setup."""
 
     def setUp(self) -> None:
+        Condition.empty_cache()
+        Day.empty_cache()
+        Process.empty_cache()
+        ProcessStep.empty_cache()
+        Todo.empty_cache()
         timestamp = datetime.now().timestamp()
         self.db_file = DatabaseFile(f'test_db:{timestamp}')
         self.db_file.remake()
@@ -56,12 +65,31 @@ class TestCaseWithServer(TestCaseWithDB):
     def check_post(self, data: Mapping[str, object], target: str,
                    expected_code: int, redirect_location: str = '') -> None:
         """Check that POST of data to target yields expected_code."""
-        encoded_form_data = urlencode(data).encode('utf-8')
+        encoded_form_data = urlencode(data, doseq=True).encode('utf-8')
         headers = {'Content-Type': 'application/x-www-form-urlencoded',
                    'Content-Length': str(len(encoded_form_data))}
         self.conn.request('POST', target,
                           body=encoded_form_data, headers=headers)
         if 302 == expected_code:
+            if redirect_location == '':
+                redirect_location = target
             self.check_redirect(redirect_location)
         else:
             self.assertEqual(self.conn.getresponse().status, expected_code)
+
+    def check_get_defaults(self, path: str) -> 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)
+
+    def post_process(self, id_: int = 1,
+                     form_data: dict[str, Any] | None = None
+                     ) -> dict[str, Any]:
+        """POST basic Process."""
+        if not form_data:
+            form_data = {'title': 'foo', 'description': 'foo', 'effort': 1.1}
+        self.check_post(form_data, '/process?id=', 302, f'/process?id={id_}')
+        return form_data