From 80cd423427e523bde8268428a5a8c4573f54adaa Mon Sep 17 00:00:00 2001
From: Christian Heller <c.heller@plomlompom.de>
Date: Mon, 29 Apr 2024 05:57:44 +0200
Subject: [PATCH] Minor tests refactoring.

---
 tests/processes.py | 14 ++++++--------
 tests/todos.py     | 16 ++++++----------
 tests/utils.py     | 11 ++++++++++-
 3 files changed, 22 insertions(+), 19 deletions(-)

diff --git a/tests/processes.py b/tests/processes.py
index 06ed257..165f566 100644
--- a/tests/processes.py
+++ b/tests/processes.py
@@ -189,8 +189,7 @@ class TestsWithServer(TestCaseWithServer):
     def test_do_POST_process(self) -> None:
         """Test POST /process and its effect on the database."""
         self.assertEqual(0, len(Process.all(self.db_conn)))
-        form_data = {'title': 'foo', 'description': 'foo', 'effort': 1.1}
-        self.check_post(form_data, '/process?id=', 302, '/process?id=1')
+        form_data = self.post_process()
         self.assertEqual(1, len(Process.all(self.db_conn)))
         self.check_post(form_data, '/process?id=FOO', 400)
         form_data['effort'] = 'foo'
@@ -205,16 +204,16 @@ class TestsWithServer(TestCaseWithServer):
         self.assertEqual(1, len(Process.all(self.db_conn)))
         form_data = {'title': 'foo', 'description': 'foo', 'effort': 1.0,
                      'condition': []}
-        self.check_post(form_data, '/process?id=', 302, '/process?id=2')
+        self.post_process(2, form_data)
         form_data['condition'] = [1]
         self.check_post(form_data, '/process?id=', 404)
         form_data_cond = {'title': 'foo', 'description': 'foo'}
         self.check_post(form_data_cond, '/condition', 302, '/condition?id=1')
-        self.check_post(form_data, '/process?id=', 302, '/process?id=3')
+        self.post_process(3, form_data)
         form_data['disables'] = [1]
-        self.check_post(form_data, '/process?id=', 302, '/process?id=4')
+        self.post_process(4, form_data)
         form_data['enables'] = [1]
-        self.check_post(form_data, '/process?id=', 302, '/process?id=5')
+        self.post_process(5, form_data)
         form_data['delete'] = ''
         self.check_post(form_data, '/process?id=', 404)
         self.check_post(form_data, '/process?id=6', 404)
@@ -222,7 +221,6 @@ class TestsWithServer(TestCaseWithServer):
 
     def test_do_GET(self) -> None:
         """Test /process and /processes response codes."""
-        form_data = {'title': 'foo', 'description': 'foo', 'effort': 1.1}
-        self.check_post(form_data, '/process?id=', 302, '/process?id=1')
+        self.post_process()
         self.check_get_defaults('/process')
         self.check_get('/processes', 200)
diff --git a/tests/todos.py b/tests/todos.py
index 5736e5d..c6b2878 100644
--- a/tests/todos.py
+++ b/tests/todos.py
@@ -288,9 +288,8 @@ class TestsWithServer(TestCaseWithServer):
 
     def test_do_POST_day(self) -> None:
         """Test Todo posting of POST /day."""
-        form_data = {'title': '', 'description': '', 'effort': 1}
-        self.check_post(form_data, '/process?id=', 302, '/process?id=1')
-        self.check_post(form_data, '/process?id=', 302, '/process?id=2')
+        self.post_process()
+        self.post_process(2)
         proc = Process.by_id(self.db_conn, 1)
         proc2 = Process.by_id(self.db_conn, 2)
         form_data = {'comment': ''}
@@ -319,8 +318,7 @@ class TestsWithServer(TestCaseWithServer):
             self.check_post(form_data, '/todo?id=1', status, redir_url)
             return Todo.by_date(self.db_conn, '2024-01-01')[0]
         # test minimum
-        form_data = {'title': '', 'description': '', 'effort': 1}
-        self.check_post(form_data, '/process', 302, '/process?id=1')
+        self.post_process()
         form_data = {'comment': '', 'new_todo': 1}
         self.check_post(form_data, '/day?date=2024-01-01', 302)
         # test posting to bad URLs
@@ -378,10 +376,9 @@ class TestsWithServer(TestCaseWithServer):
 
     def test_do_POST_day_todo_adoption(self) -> None:
         """Test Todos posted to Day view may adopt existing Todos."""
-        form_data = {'title': '', 'description': '', 'effort': 1}
-        self.check_post(form_data, '/process', 302, '/process?id=1')
+        form_data = self.post_process()
         form_data['new_top_step'] = 1
-        self.check_post(form_data, '/process', 302, '/process?id=2')
+        form_data = self.post_process(2, form_data)
         form_data = {'comment': '', 'new_todo': 1}
         self.check_post(form_data, '/day?date=2024-01-01', 302)
         form_data = {'comment': '', 'new_todo': 2}
@@ -395,8 +392,7 @@ class TestsWithServer(TestCaseWithServer):
 
     def test_do_GET_todo(self) -> None:
         """Test GET /todo response codes."""
-        form_data = {'title': '', 'description': '', 'effort': 1}
-        self.check_post(form_data, '/process?id=', 302, '/process?id=1')
+        self.post_process()
         form_data = {'comment': '', 'new_todo': 1}
         self.check_post(form_data, '/day?date=2024-01-01', 302)
         self.check_get('/todo', 400)
diff --git a/tests/utils.py b/tests/utils.py
index 545a2ba..b7d1269 100644
--- a/tests/utils.py
+++ b/tests/utils.py
@@ -5,7 +5,7 @@ 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
@@ -84,3 +84,12 @@ class TestCaseWithServer(TestCaseWithDB):
         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
-- 
2.30.2