From 13bce001bd86de2c20dff2f93d9f08cfae95a16b Mon Sep 17 00:00:00 2001
From: Christian Heller <c.heller@plomlompom.de>
Date: Wed, 27 Mar 2024 01:45:10 +0100
Subject: [PATCH] Explicitly disallow Process(id_=0).

---
 plomtask/processes.py |  6 ++++--
 tests/processes.py    | 12 ++++--------
 2 files changed, 8 insertions(+), 10 deletions(-)

diff --git a/plomtask/processes.py b/plomtask/processes.py
index 8b5ff42..0300e73 100644
--- a/plomtask/processes.py
+++ b/plomtask/processes.py
@@ -3,14 +3,16 @@ from __future__ import annotations
 from sqlite3 import Row
 from datetime import datetime
 from plomtask.db import DatabaseConnection
-from plomtask.exceptions import NotFoundException
+from plomtask.exceptions import NotFoundException, BadFormatException
 
 
 class Process:
     """Template for, and metadata for, Todos, and their arrangements."""
 
     def __init__(self, id_: int | None) -> None:
-        self.id_ = id_ if id_ != 0 else None  # to avoid DB-confusing rowid=0
+        if (id_ is not None) and id_ < 1:
+            raise BadFormatException(f'illegal Process ID, must be >=1: {id_}')
+        self.id_ = id_
         self.title = VersionedAttribute(self, 'title', 'UNNAMED')
         self.description = VersionedAttribute(self, 'description', '')
         self.effort = VersionedAttribute(self, 'effort', 1.0)
diff --git a/tests/processes.py b/tests/processes.py
index 0488efc..e8967f7 100644
--- a/tests/processes.py
+++ b/tests/processes.py
@@ -3,7 +3,7 @@ from unittest import TestCase
 from urllib.parse import urlencode
 from tests.utils import TestCaseWithDB, TestCaseWithServer
 from plomtask.processes import Process
-from plomtask.exceptions import NotFoundException
+from plomtask.exceptions import NotFoundException, BadFormatException
 
 
 class TestsSansDB(TestCase):
@@ -25,10 +25,8 @@ class TestsWithDB(TestCaseWithDB):
         p_saved.save(self.db_conn)
         self.assertEqual(p_saved.id_,
                          Process.by_id(self.db_conn, 1, create=False).id_)
-        p_saved = Process(0)
-        p_saved.save(self.db_conn)
-        self.assertEqual(p_saved.id_,
-                         Process.by_id(self.db_conn, 2, create=False).id_)
+        with self.assertRaises(BadFormatException):
+            p_saved = Process(0)
         p_saved = Process(5)
         p_saved.save(self.db_conn)
         self.assertEqual(p_saved.id_,
@@ -50,8 +48,6 @@ class TestsWithDB(TestCaseWithDB):
             Process.by_id(self.db_conn, 1, create=False)
         self.assertNotEqual(Process(1).id_,
                             Process.by_id(self.db_conn, None, create=True).id_)
-        self.assertNotEqual(Process(1).id_,
-                            Process.by_id(self.db_conn, 0, create=True).id_)
         self.assertEqual(Process(1).id_,
                          Process.by_id(self.db_conn, 1, create=True).id_)
         self.assertEqual(Process(2).id_,
@@ -100,7 +96,7 @@ class TestsWithServer(TestCaseWithServer):
         self.conn.request('GET', '/process?id=')
         self.assertEqual(self.conn.getresponse().status, 200)
         self.conn.request('GET', '/process?id=0')
-        self.assertEqual(self.conn.getresponse().status, 200)
+        self.assertEqual(self.conn.getresponse().status, 400)
         self.conn.request('GET', '/process?id=FOO')
         self.assertEqual(self.conn.getresponse().status, 400)
         self.conn.request('GET', '/processes')
-- 
2.30.2