"""Actionables."""
from __future__ import annotations
-from sqlite3 import Row
from plomtask.db import DatabaseConnection, BaseModel
-from plomtask.days import Day
from plomtask.processes import Process
from plomtask.conditions import Condition
from plomtask.exceptions import (NotFoundException, BadFormatException,
# pylint: disable=too-many-instance-attributes
- name = 'Todo'
table_name = 'todos'
to_save = ['process_id', 'is_done', 'date']
def __init__(self, id_: int | None, process: Process,
- is_done: bool, day: Day) -> None:
+ is_done: bool, date: str) -> None:
self.set_int_id(id_)
self.process = process
self._is_done = is_done
- self.day = day
+ self.date = date
self.children: list[Todo] = []
self.parents: list[Todo] = []
self.conditions: list[Condition] = []
self.fulfills = process.fulfills[:]
self.undoes = process.undoes[:]
- @classmethod
- def from_table_row(cls, db_conn: DatabaseConnection, row: Row) -> Todo:
- """Make Todo from database row, write to DB cache."""
- todo = cls(id_=row[0],
- process=Process.by_id(db_conn, row[1]),
- is_done=bool(row[2]),
- day=Day.by_date(db_conn, row[3]))
- assert isinstance(todo.id_, int)
- db_conn.cached_todos[todo.id_] = todo
- return todo
-
@classmethod
def by_id(cls, db_conn: DatabaseConnection, id_: int | None) -> Todo:
"""Get Todo of .id_=id_ and children (from DB cache if possible)."""
todo = None
for row in db_conn.exec('SELECT * FROM todos WHERE id = ?',
(id_,)):
+ row = list(row)
+ if row[1] == 0:
+ raise NotFoundException('calling Todo of '
+ 'unsaved Process')
+ row[1] = Process.by_id(db_conn, row[1])
todo = cls.from_table_row(db_conn, row)
break
if todo is None:
for row in db_conn.exec('SELECT todo FROM todo_fulfills '
'WHERE condition = ?', (condition.id_,)):
todo = cls.by_id(db_conn, row[0])
- if todo.day.date == date:
+ if todo.date == date:
enablers += [todo]
return enablers
for row in db_conn.exec('SELECT todo FROM todo_undoes '
'WHERE condition = ?', (condition.id_,)):
todo = cls.by_id(db_conn, row[0])
- if todo.day.date == date:
+ if todo.date == date:
disablers += [todo]
return disablers
"""Return ID of tasked Process."""
return self.process.id_
- @property
- def date(self) -> str:
- """Return date of used Day."""
- return self.day.date
-
@property
def is_done(self) -> bool:
"""Wrapper around self._is_done so we can control its setter."""
"""Test Todos module."""
from tests.utils import TestCaseWithDB, TestCaseWithServer
from plomtask.todos import Todo
-from plomtask.days import Day
from plomtask.processes import Process
from plomtask.conditions import Condition
from plomtask.exceptions import (NotFoundException, BadFormatException,
def setUp(self) -> None:
super().setUp()
- self.day1 = Day('2024-01-01')
- self.day1.save(self.db_conn)
- self.day2 = Day('2024-01-02')
- self.day2.save(self.db_conn)
+ self.date1 = '2024-01-01'
+ self.date2 = '2024-01-02'
self.proc = Process(None)
self.proc.save(self.db_conn)
self.cond1 = Condition(None)
def test_Todo_by_id(self) -> None:
"""Test creation and findability of Todos."""
process_unsaved = Process(None)
- todo = Todo(None, process_unsaved, False, self.day1)
+ todo = Todo(None, process_unsaved, False, self.date1)
with self.assertRaises(NotFoundException):
todo.save(self.db_conn)
process_unsaved.save(self.db_conn)
def test_Todo_by_date(self) -> None:
"""Test findability of Todos by date."""
- t1 = Todo(None, self.proc, False, self.day1)
+ t1 = Todo(None, self.proc, False, self.date1)
t1.save(self.db_conn)
- t2 = Todo(None, self.proc, False, self.day1)
+ t2 = Todo(None, self.proc, False, self.date1)
t2.save(self.db_conn)
- self.assertEqual(Todo.by_date(self.db_conn, self.day1.date), [t1, t2])
- self.assertEqual(Todo.by_date(self.db_conn, self.day2.date), [])
+ self.assertEqual(Todo.by_date(self.db_conn, self.date1), [t1, t2])
+ self.assertEqual(Todo.by_date(self.db_conn, self.date2), [])
self.assertEqual(Todo.by_date(self.db_conn, 'foo'), [])
def test_Todo_from_process(self) -> None:
"""Test spawning of Todo attributes from Process."""
- assert self.cond1.id_ is not None
- assert self.cond2.id_ is not None
+ assert isinstance(self.cond1.id_, int)
+ assert isinstance(self.cond2.id_, int)
self.proc.set_conditions(self.db_conn, [self.cond1.id_])
- todo = Todo(None, self.proc, False, self.day1)
+ todo = Todo(None, self.proc, False, self.date1)
self.assertEqual(todo.conditions, [self.cond1])
todo.set_conditions(self.db_conn, [self.cond2.id_])
self.assertEqual(todo.conditions, [self.cond2])
self.assertEqual(self.proc.conditions, [self.cond1])
self.proc.set_fulfills(self.db_conn, [self.cond1.id_])
- todo = Todo(None, self.proc, False, self.day1)
+ todo = Todo(None, self.proc, False, self.date1)
self.assertEqual(todo.fulfills, [self.cond1])
todo.set_fulfills(self.db_conn, [self.cond2.id_])
self.assertEqual(todo.fulfills, [self.cond2])
self.assertEqual(self.proc.fulfills, [self.cond1])
self.proc.set_undoes(self.db_conn, [self.cond1.id_])
- todo = Todo(None, self.proc, False, self.day1)
+ todo = Todo(None, self.proc, False, self.date1)
self.assertEqual(todo.undoes, [self.cond1])
todo.set_undoes(self.db_conn, [self.cond2.id_])
self.assertEqual(todo.undoes, [self.cond2])
def test_Todo_on_conditions(self) -> None:
"""Test effect of Todos on Conditions."""
- assert self.cond1.id_ is not None
- assert self.cond2.id_ is not None
- todo = Todo(None, self.proc, False, self.day1)
+ assert isinstance(self.cond1.id_, int)
+ assert isinstance(self.cond2.id_, int)
+ todo = Todo(None, self.proc, False, self.date1)
todo.save(self.db_conn)
todo.set_fulfills(self.db_conn, [self.cond1.id_])
todo.set_undoes(self.db_conn, [self.cond2.id_])
def test_Todo_enablers_disablers(self) -> None:
"""Test Todo.enablers_for_at/disablers_for_at."""
- assert self.cond1.id_ is not None
- assert self.cond2.id_ is not None
- todo1 = Todo(None, self.proc, False, self.day1)
+ assert isinstance(self.cond1.id_, int)
+ assert isinstance(self.cond2.id_, int)
+ todo1 = Todo(None, self.proc, False, self.date1)
todo1.save(self.db_conn)
todo1.set_fulfills(self.db_conn, [self.cond1.id_])
todo1.set_undoes(self.db_conn, [self.cond2.id_])
todo1.save(self.db_conn)
- todo2 = Todo(None, self.proc, False, self.day1)
+ todo2 = Todo(None, self.proc, False, self.date1)
todo2.save(self.db_conn)
todo2.set_fulfills(self.db_conn, [self.cond2.id_])
todo2.save(self.db_conn)
- todo3 = Todo(None, self.proc, False, self.day2)
+ todo3 = Todo(None, self.proc, False, self.date2)
todo3.save(self.db_conn)
todo3.set_fulfills(self.db_conn, [self.cond2.id_])
todo3.save(self.db_conn)
- date1 = self.day1.date
- date2 = self.day2.date
- enablers = Todo.enablers_for_at(self.db_conn, self.cond1, date1)
+ enablers = Todo.enablers_for_at(self.db_conn, self.cond1, self.date1)
self.assertEqual(enablers, [todo1])
- enablers = Todo.enablers_for_at(self.db_conn, self.cond1, date2)
+ enablers = Todo.enablers_for_at(self.db_conn, self.cond1, self.date2)
self.assertEqual(enablers, [])
- disablers = Todo.disablers_for_at(self.db_conn, self.cond1, date1)
+ disablers = Todo.disablers_for_at(self.db_conn, self.cond1, self.date1)
self.assertEqual(disablers, [])
- disablers = Todo.disablers_for_at(self.db_conn, self.cond1, date2)
+ disablers = Todo.disablers_for_at(self.db_conn, self.cond1, self.date2)
self.assertEqual(disablers, [])
- enablers = Todo.enablers_for_at(self.db_conn, self.cond2, date1)
+ enablers = Todo.enablers_for_at(self.db_conn, self.cond2, self.date1)
self.assertEqual(enablers, [todo2])
- enablers = Todo.enablers_for_at(self.db_conn, self.cond2, date2)
+ enablers = Todo.enablers_for_at(self.db_conn, self.cond2, self.date2)
self.assertEqual(enablers, [todo3])
- disablers = Todo.disablers_for_at(self.db_conn, self.cond2, date1)
+ disablers = Todo.disablers_for_at(self.db_conn, self.cond2, self.date1)
self.assertEqual(disablers, [todo1])
- disablers = Todo.disablers_for_at(self.db_conn, self.cond2, date2)
+ disablers = Todo.disablers_for_at(self.db_conn, self.cond2, self.date2)
self.assertEqual(disablers, [])
def test_Todo_children(self) -> None:
"""Test Todo.children relations."""
- todo_1 = Todo(None, self.proc, False, self.day1)
- todo_2 = Todo(None, self.proc, False, self.day1)
+ todo_1 = Todo(None, self.proc, False, self.date1)
+ todo_2 = Todo(None, self.proc, False, self.date1)
with self.assertRaises(HandledException):
todo_1.add_child(todo_2)
todo_1.save(self.db_conn)
todo_2.save(self.db_conn)
todo_1.add_child(todo_2)
todo_1.save(self.db_conn)
+ assert isinstance(todo_1.id_, int)
todo_retrieved = Todo.by_id(self.db_conn, todo_1.id_)
self.assertEqual(todo_retrieved.children, [todo_2])
with self.assertRaises(BadFormatException):
def test_Todo_conditioning(self) -> None:
"""Test Todo.doability conditions."""
- assert self.cond1.id_ is not None
- todo_1 = Todo(None, self.proc, False, self.day1)
+ assert isinstance(self.cond1.id_, int)
+ todo_1 = Todo(None, self.proc, False, self.date1)
todo_1.save(self.db_conn)
- todo_2 = Todo(None, self.proc, False, self.day1)
+ todo_2 = Todo(None, self.proc, False, self.date1)
todo_2.save(self.db_conn)
todo_2.add_child(todo_1)
with self.assertRaises(BadFormatException):
def test_Todo_singularity(self) -> None:
"""Test pointers made for single object keep pointing to it."""
- todo = Todo(None, self.proc, False, self.day1)
+ todo = Todo(None, self.proc, False, self.date1)
todo.save(self.db_conn)
retrieved_todo = Todo.by_id(self.db_conn, 1)
todo.is_done = True
self.assertEqual(retrieved_todo.is_done, True)
- retrieved_todo = Todo.by_date(self.db_conn, self.day1.date)[0]
+ retrieved_todo = Todo.by_date(self.db_conn, self.date1)[0]
retrieved_todo.is_done = False
self.assertEqual(todo.is_done, False)