X-Git-Url: https://plomlompom.com/repos/?a=blobdiff_plain;f=tests%2Fprocesses.py;h=2ac8cf4cf7787be287792b583a7402c6ed013855;hb=23012cd370777b60a25839788d131173d2abee91;hp=390b1302932dce72da1ebc375a5f1ec6f59d8eec;hpb=b8207d9912e195d4693457060244cde37c8c53cd;p=plomtask diff --git a/tests/processes.py b/tests/processes.py index 390b130..2ac8cf4 100644 --- a/tests/processes.py +++ b/tests/processes.py @@ -1,10 +1,9 @@ """Test Processes module.""" from unittest import TestCase -from typing import Any from tests.utils import TestCaseWithDB, TestCaseWithServer -from plomtask.processes import Process, ProcessStep +from plomtask.processes import Process, ProcessStep, ProcessStepsNode from plomtask.conditions import Condition -from plomtask.exceptions import NotFoundException, BadFormatException +from plomtask.exceptions import NotFoundException, HandledException class TestsSansDB(TestCase): @@ -18,7 +17,7 @@ class TestsSansDB(TestCase): def test_Process_legal_ID(self) -> None: """Test Process cannot be instantiated with id_=0.""" - with self.assertRaises(BadFormatException): + with self.assertRaises(HandledException): Process(0) @@ -28,65 +27,60 @@ class TestsWithDB(TestCaseWithDB): def setUp(self) -> None: super().setUp() self.proc1 = Process(None) - self.proc1.save_without_steps(self.db_conn) + self.proc1.save(self.db_conn) self.proc2 = Process(None) - self.proc2.save_without_steps(self.db_conn) + self.proc2.save(self.db_conn) self.proc3 = Process(None) - self.proc3.save_without_steps(self.db_conn) + self.proc3.save(self.db_conn) def test_Process_ids(self) -> None: - """Test Process.save_without_steps() re Process.id_.""" + """Test Process.save() re Process.id_.""" self.assertEqual(self.proc1.id_, Process.by_id(self.db_conn, 1, create=False).id_) self.assertEqual(self.proc2.id_, Process.by_id(self.db_conn, 2, create=False).id_) proc5 = Process(5) - proc5.save_without_steps(self.db_conn) + proc5.save(self.db_conn) self.assertEqual(proc5.id_, Process.by_id(self.db_conn, 5, create=False).id_) def test_Process_steps(self) -> None: """Test addition, nesting, and non-recursion of ProcessSteps""" - assert self.proc2.id_ is not None - assert self.proc3.id_ is not None - self.proc1.add_step(self.db_conn, None, self.proc2.id_, None) - p_1_dict: dict[int, dict[str, Any]] = {1: { - 'process': self.proc2, 'parent_id': None, - 'is_explicit': True, 'steps': {}, 'seen': False - }} + def add_step(proc: Process, + steps_proc: list[tuple[int | None, int, int | None]], + step_tuple: tuple[int | None, int, int | None], + expected_id: int) -> None: + steps_proc += [step_tuple] + proc.set_steps(self.db_conn, steps_proc) + steps_proc[-1] = (expected_id, step_tuple[1], step_tuple[2]) + assert isinstance(self.proc2.id_, int) + assert isinstance(self.proc3.id_, int) + steps_proc1: list[tuple[int | None, int, int | None]] = [] + add_step(self.proc1, steps_proc1, (None, self.proc2.id_, None), 1) + p_1_dict: dict[int, ProcessStepsNode] = {} + p_1_dict[1] = ProcessStepsNode(self.proc2, None, True, {}, False) self.assertEqual(self.proc1.get_steps(self.db_conn, None), p_1_dict) - s_b = self.proc1.add_step(self.db_conn, None, self.proc3.id_, None) - p_1_dict[2] = { - 'process': self.proc3, 'parent_id': None, - 'is_explicit': True, 'steps': {}, 'seen': False - } + add_step(self.proc1, steps_proc1, (None, self.proc3.id_, None), 2) + step_2 = self.proc1.explicit_steps[-1] + assert isinstance(step_2.id_, int) + p_1_dict[2] = ProcessStepsNode(self.proc3, None, True, {}, False) self.assertEqual(self.proc1.get_steps(self.db_conn, None), p_1_dict) - s_c = self.proc2.add_step(self.db_conn, None, self.proc3.id_, None) - assert s_c.id_ is not None - p_1_dict[1]['steps'] = {3: { - 'process': self.proc3, 'parent_id': None, - 'is_explicit': False, 'steps': {}, 'seen': False - }} + steps_proc2: list[tuple[int | None, int, int | None]] = [] + add_step(self.proc2, steps_proc2, (None, self.proc3.id_, None), 3) + p_1_dict[1].steps[3] = ProcessStepsNode(self.proc3, None, + False, {}, False) self.assertEqual(self.proc1.get_steps(self.db_conn, None), p_1_dict) - self.proc1.add_step(self.db_conn, None, self.proc2.id_, s_b.id_) - p_1_dict[2]['steps'][4] = { - 'process': self.proc2, 'parent_id': s_b.id_, 'seen': False, - 'is_explicit': True, 'steps': {3: { - 'process': self.proc3, 'parent_id': None, - 'is_explicit': False, 'steps': {}, 'seen': True - }}} + add_step(self.proc1, steps_proc1, (None, self.proc2.id_, step_2.id_), + 4) + step_3 = ProcessStepsNode(self.proc3, None, False, {}, True) + p_1_dict[2].steps[4] = ProcessStepsNode(self.proc2, step_2.id_, True, + {3: step_3}, False) self.assertEqual(self.proc1.get_steps(self.db_conn, None), p_1_dict) - self.proc1.add_step(self.db_conn, None, self.proc3.id_, 999) - p_1_dict[5] = { - 'process': self.proc3, 'parent_id': None, - 'is_explicit': True, 'steps': {}, 'seen': False - } + add_step(self.proc1, steps_proc1, (None, self.proc3.id_, 999), 5) + p_1_dict[5] = ProcessStepsNode(self.proc3, None, True, {}, False) self.assertEqual(self.proc1.get_steps(self.db_conn, None), p_1_dict) - self.proc1.add_step(self.db_conn, None, self.proc3.id_, 3) - p_1_dict[6] = { - 'process': self.proc3, 'parent_id': None, - 'is_explicit': True, 'steps': {}, 'seen': False - } + add_step(self.proc1, steps_proc1, (None, self.proc3.id_, 3), 6) + p_1_dict[6] = ProcessStepsNode(self.proc3, None, True, {}, False) self.assertEqual(self.proc1.get_steps(self.db_conn, None), p_1_dict) self.assertEqual(self.proc1.used_as_step_by(self.db_conn), @@ -97,14 +91,14 @@ class TestsWithDB(TestCaseWithDB): [self.proc1, self.proc2]) def test_Process_conditions(self) -> None: - """Test setting Process.conditions/fulfills/undoes.""" - for target in ('conditions', 'fulfills', 'undoes'): + """Test setting Process.conditions/enables/disables.""" + for target in ('conditions', 'enables', 'disables'): c1 = Condition(None, False) c1.save(self.db_conn) - assert c1.id_ is not None + assert isinstance(c1.id_, int) c2 = Condition(None, False) c2.save(self.db_conn) - assert c2.id_ is not None + assert isinstance(c2.id_, int) self.proc1.set_conditions(self.db_conn, [], target) self.assertEqual(getattr(self.proc1, target), []) self.proc1.set_conditions(self.db_conn, [c1.id_], target) @@ -132,26 +126,53 @@ class TestsWithDB(TestCaseWithDB): def test_ProcessStep_singularity(self) -> None: """Test pointers made for single object keep pointing to it.""" - assert self.proc2.id_ is not None - step = self.proc1.add_step(self.db_conn, None, self.proc2.id_, None) - assert step.id_ is not None + assert isinstance(self.proc2.id_, int) + self.proc1.set_steps(self.db_conn, [(None, self.proc2.id_, None)]) + step = self.proc1.explicit_steps[-1] + assert isinstance(step.id_, int) step_retrieved = ProcessStep.by_id(self.db_conn, step.id_) step.parent_step_id = 99 self.assertEqual(step.parent_step_id, step_retrieved.parent_step_id) def test_Process_singularity(self) -> None: - """Test pointers made for single object keep pointing to it.""" - assert self.proc2.id_ is not None - self.proc1.add_step(self.db_conn, None, self.proc2.id_, None) + """Test pointers made for single object keep pointing to it, and + subsequent retrievals don't overload relations.""" + assert isinstance(self.proc1.id_, int) + assert isinstance(self.proc2.id_, int) + c1 = Condition(None, False) + c1.save(self.db_conn) + assert isinstance(c1.id_, int) + self.proc1.set_conditions(self.db_conn, [c1.id_]) + self.proc1.set_steps(self.db_conn, [(None, self.proc2.id_, None)]) + self.proc1.save(self.db_conn) p_retrieved = Process.by_id(self.db_conn, self.proc1.id_) self.assertEqual(self.proc1.explicit_steps, p_retrieved.explicit_steps) + self.assertEqual(self.proc1.conditions, p_retrieved.conditions) + self.proc1.save(self.db_conn) def test_Process_versioned_attributes_singularity(self) -> None: """Test behavior of VersionedAttributes on saving (with .title).""" + assert isinstance(self.proc1.id_, int) self.proc1.title.set('named') p_loaded = Process.by_id(self.db_conn, self.proc1.id_) self.assertEqual(self.proc1.title.history, p_loaded.title.history) + def test_Process_removal(self) -> None: + """Test removal of Processes.""" + assert isinstance(self.proc3.id_, int) + self.proc1.remove(self.db_conn) + self.assertEqual({self.proc2.id_, self.proc3.id_}, + set(p.id_ for p in Process.all(self.db_conn))) + self.proc2.set_steps(self.db_conn, [(None, self.proc3.id_, None)]) + self.proc2.explicit_steps[0].remove(self.db_conn) + retrieved = Process.by_id(self.db_conn, self.proc2.id_) + self.assertEqual(retrieved.explicit_steps, []) + self.proc2.set_steps(self.db_conn, [(None, self.proc3.id_, None)]) + step = retrieved.explicit_steps[0] + self.proc2.remove(self.db_conn) + with self.assertRaises(NotFoundException): + ProcessStep.by_id(self.db_conn, step.id_) + class TestsWithServer(TestCaseWithServer): """Module tests against our HTTP server/handler (and database).""" @@ -160,7 +181,7 @@ class TestsWithServer(TestCaseWithServer): """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, '/') + self.check_post(form_data, '/process?id=', 302, '/process?id=1') self.assertEqual(1, len(Process.all(self.db_conn))) self.check_post(form_data, '/process?id=FOO', 400) form_data['effort'] = 'foo' @@ -175,21 +196,25 @@ 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, '/') + self.check_post(form_data, '/process?id=', 302, '/process?id=2') 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, '/') - self.check_post(form_data, '/process?id=', 302, '/') - form_data['undoes'] = [1] - self.check_post(form_data, '/process?id=', 302, '/') - form_data['fulfills'] = [1] - self.check_post(form_data, '/process?id=', 302, '/') + self.check_post(form_data_cond, '/condition', 302, '/condition?id=1') + self.check_post(form_data, '/process?id=', 302, '/process?id=3') + form_data['disables'] = [1] + self.check_post(form_data, '/process?id=', 302, '/process?id=4') + form_data['enables'] = [1] + self.check_post(form_data, '/process?id=', 302, '/process?id=5') + form_data['delete'] = '' + self.check_post(form_data, '/process?id=', 404) + self.check_post(form_data, '/process?id=6', 404) + self.check_post(form_data, '/process?id=5', 302, '/processes') def test_do_GET(self) -> None: """Test /process and /processes response codes.""" self.check_get('/process', 200) self.check_get('/process?id=', 200) - self.check_get('/process?id=0', 400) + self.check_get('/process?id=0', 500) self.check_get('/process?id=FOO', 400) self.check_get('/processes', 200)