home · contact · privacy
Explicitly disallow Process(id_=0).
authorChristian Heller <c.heller@plomlompom.de>
Wed, 27 Mar 2024 00:45:10 +0000 (01:45 +0100)
committerChristian Heller <c.heller@plomlompom.de>
Wed, 27 Mar 2024 00:45:10 +0000 (01:45 +0100)
plomtask/processes.py
tests/processes.py

index 8b5ff4221ea292b1c8bcffb787671b44129f0a66..0300e73ccbaec2f15a5478a892b9fcc9d231eaa3 100644 (file)
@@ -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)
index 0488efc0f2a6b661feeae79d454843dd97c809ab..e8967f70273e2345df966d599d50fdbeb4069e39 100644 (file)
@@ -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')