def test_routine_serialization(self): routine = Routine("My routine") task1 = Task("Task 1") task1.register_argument("arg1", arg_type.integer(minimum=0)) task2 = Task("Task 2") task2.register_argument("arg1", arg_type.choice("Yes", "No")) task2.register_argument("arg2", arg_type.integer(maximum=909)) routine.add_task(task1, {"arg1": 10}) routine.add_task(task2, {"arg1": "Yes", "arg2": 700}) serialized = serial.serialize_routine(routine) self.assertDictEqual( serialized, { "name": "My routine", "tasks": [{ "name": "Task 1", "values": { "arg1": 10 }, }, { "name": "Task 2", "values": { "arg1": "Yes", "arg2": 700 } }] })
def test_modify_task(self): routine = Routine("my routine") routine.add_task(self.task, {"old": "values"}) routine.modify_task(0, {"new": "super value"}) task, values = routine.tasks[0] self.assertIs(self.task, task) self.assertDictEqual(values, {"new": "super value"})
def test_remove_task(self): routine = Routine("my routine") routine.add_task(self.task, {"a": "b"}) task, values = routine.remove_task(0) self.assertIs(self.task, task) self.assertDictEqual(values, {"a": "b"}) self.assertEqual(len(routine.tasks), 0)
def test_add_task(self): routine = Routine("my routine") routine.add_task(self.task, {"arg1": 1}) self.assertEqual(len(routine.tasks), 1) task, values = routine.tasks[0] self.assertIs(self.task, task) self.assertDictEqual(values, {"arg1": 1})
def test_execute_routine_passes_args(self): routine = Routine("other routine") def check_args(arguments): self.assertDictEqual(arguments, {"one": 1, "two": 2}) self.task.execute_task = check_args routine.add_task(self.task, {"one": 1, "two": 2}) routine.execute_routine()
def test_execute_routine_calls_tasks(self): routine = Routine("the routine") def raise_exception(argument): raise Exception() self.task.execute_task = raise_exception routine.add_task(self.task, {}) with self.assertRaises(Exception): routine.execute_routine()
def deserialize_routine(obj: dict, manager: Manager) -> Routine or None: """ Deserialize a routine from the given dict using the tasks registered in the given manager. If some referenced tasks are missing, they're discarded from the routine. This function doesn't add deserialized routine to given manager. :param obj: the serialized routine (serialized using core.serial.disk.serializers.serialize_routine) :param manager: the manager where we should lookup to find tasks referenced by the routine :return: the deserialized routine or None if the given obj doesn't match expected format """ if type(obj) is not dict: return None if "name" not in obj or "tasks" not in obj: return None if type(obj["name"]) is not str or type(obj["tasks"]) is not list: return None name = obj["name"] tasks = obj["tasks"] routine = Routine(name) for task in tasks: if type(task) is not dict: continue if "name" not in task or "values" not in task: continue if type(task["name"]) is not str or type(task["values"]) is not dict: continue t = manager.find_task(task["name"]) if t is None: continue values = task["values"] valid = True for arg_name in t.arguments: if arg_name not in values: valid = False break if not validate(t.arguments[arg_name], values[arg_name]): valid = False break if not valid: continue if t.on_validation(values): routine.add_task(t, values) return routine