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 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
def test_card_link(self): card = Card("card id", "card name") routine = Routine("routine name") self.assertFalse(card.is_linked()) card.link_to(routine) self.assertTrue(card.is_linked()) self.assertEqual(card.routine_name, "routine name") card.unlink() self.assertFalse(card.is_linked())
def test_card_deserialize_linked_found(self): routine = Routine("routine name") self.manager.add_routine(routine) card = des.deserialize_card( { "name": "card name", "id": "card id", "target": "routine name" }, self.manager) self.assertTrue(card.is_linked()) self.assertEqual(card.routine_name, "routine name")
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_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_card_serialization(self): card = Card("card id", "card name") self.assertDictEqual(serial.serialize_card(card), { "name": "card name", "id": "card id", }) routine = Routine("routine name") card = Card("the card id", "the card name") card.link_to(routine) self.assertDictEqual(serial.serialize_card(card), { "name": "the card name", "id": "the card id", "target": "routine name" })
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 test_empty_name(self): with self.assertRaises(AssertionError): Routine("")
def setUp(self): self.manager = Manager() self.task = Task("the task") self.routine = Routine("the routine") self.card = Card("the card id", "the card name")
def test_override_routine(self): other_routine = Routine("the routine") self.manager.add_routine(self.routine) self.manager.add_routine(other_routine) self.assertEqual(len(self.manager.routines), 1) self.assertIs(other_routine, self.manager.routines["the routine"])
def routines(): error = None success = None if request.method == "POST": try: action = request.form["action"] except KeyError: action = "" try: value = request.form["value"] except KeyError: value = "" if action == "run": r = manager.find_routine(value) if r is not None: execution.ExecutionThread(r).start() success = "La routine s'est lancé sans erreur" else: error = "Impossible de trouver une routine avec ce nom." elif action == "delete": r = manager.find_routine(value) if r is not None: manager.remove_routine(r.name) save_manager() success = "La routine a bien été supprimée" else: error = "Impossible de trouver une routine avec ce nom." elif action == "create": if len(value) == 0: error = "La nom de la routine ne doit pas être vide." elif len(value) > 40: error = "Le nom de la routine ne peut dépasser 40 charctères." elif manager.find_routine(value) is None: r = Routine(value) manager.add_routine(r) save_manager() success = "La routine a bien été créée." else: error = "Une routine avec ce nom existe déjà." elif action == "update": r = manager.find_routine(value) if r is not None: try: task_index = request.form["task"] task_index = int(task_index) (t, _) = r.tasks[task_index] values = {} for arg_name in t.arguments: arg = t.arguments[arg_name] arg_value = request.form["{}Field".format( arg_name)] if not validation.validate(arg, arg_value): err = "La valeur {} n'est pas valide pour l'argument {}.".format( arg_value, arg_name) error = err if error is None else "{} {}".format( error, err) else: values[arg_name] = arg_value if error is None: ok, error = t.on_validation(values) if ok: r.modify_task(task_index, values) save_manager() success = "La routine a bien été mise à jour." except KeyError: error = "Une erreur est survenue" except ValueError: error = "Une erreur est survenue" else: error = "Impossible de trouver une routine avec ce nom" elif action == "delete-task": r = manager.find_routine(value) if r is not None: try: task_index = request.form["task"] task_index = int(task_index) r.remove_task(task_index) save_manager() success = "La tâche a bien été supprimée" except KeyError: error = "Une erreur est survenue côté serveur" except ValueError: error = "Une erreur est survenue côté serveur" except IndexError: error = "Cette tâche n'existe pas" else: error = "Il n'existe pas de routine avec ce nom" return render_template("routines.html", routines=manager.routines.values(), error=error, success=success)