def deserialize_card(serialized_card: dict, manager: Manager) -> Card or None: """ Deserializes a card from the given dict using the registered routines in the given manager. If a referenced routine is missing, the returned card is unlinked. This function doesn't add the deserialized card to the given manager :param serialized_card: the serialized card (serialized using core.serial.disk.serializers.serialize_card) :param manager: the manager where we should lookup for referenced routines :return: the deserialized card or None if the format does not match the expected one """ if type(serialized_card) is not dict: return None if "name" not in serialized_card or "id" not in serialized_card: return None name = serialized_card["name"] card_id = serialized_card["id"] if type(card_id) is not str or type(name) is not str: return None card = Card(card_id, name) if "target" in serialized_card and type(serialized_card["target"]) is str: routine = manager.find_routine(serialized_card["target"]) if routine is not None: card.link_to(routine) return card
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 setUp(self): self.manager = Manager() self.task = Task("the task") self.routine = Routine("the routine") self.card = Card("the card id", "the card name")
class TestManager(unittest.TestCase): 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_manager_default_state(self): self.assertEqual(len(self.manager.tasks), 0) self.assertEqual(len(self.manager.routines), 0) self.assertEqual(len(self.manager.cards), 0) """ Tasks tests """ def test_register_task(self): self.manager.register_task(self.task) self.assertEqual(len(self.manager.tasks), 1) self.assertIs(self.task, self.manager.tasks["the task"]) def test_override_task(self): other_task = Task("the task") self.manager.register_task(self.task) self.manager.register_task(other_task) self.assertEqual(len(self.manager.tasks), 1) self.assertIs(other_task, self.manager.tasks["the task"]) def test_find_task(self): self.manager.register_task(self.task) task = self.manager.find_task("the task") self.assertIs(self.task, task) self.assertIsNone(self.manager.find_task("unknown task")) """ Routine tests """ def test_add_routine(self): self.manager.add_routine(self.routine) self.assertEqual(len(self.manager.routines), 1) 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 test_find_routine(self): self.manager.add_routine(self.routine) routine = self.manager.find_routine("the routine") self.assertIs(self.routine, routine) self.assertIsNone(self.manager.find_routine("unknown routine")) def test_remove_routine(self): self.manager.add_routine(self.routine) self.manager.remove_routine("the routine") self.assertEqual(len(self.manager.routines), 0) """ Card tests """ def test_add_card(self): self.manager.add_card(self.card) self.assertEqual(len(self.manager.cards), 1) def test_override_card(self): other_card = Card("the card id", "other name") self.manager.add_card(self.card) self.manager.add_card(other_card) self.assertEqual(len(self.manager.cards), 1) self.assertIs(other_card, self.manager.cards["the card id"]) def test_find_card_by_id(self): self.manager.add_card(self.card) card = self.manager.find_card_by_id("the card id") self.assertIs(self.card, card) self.assertIsNone(self.manager.find_card_by_id("unknown id")) def test_find_card_by_name(self): self.manager.add_card(self.card) card = self.manager.find_card_by_name("the card name") self.assertIs(self.card, card) self.assertIsNone(self.manager.find_card_by_name("unknown name")) def test_remove_card(self): self.manager.add_card(self.card) return_value = self.manager.remove_card("any card") self.assertEqual(len(self.manager.cards), 1) self.assertIsNone(return_value) return_value = self.manager.remove_card("the card id") self.assertEqual(len(self.manager.cards), 0) self.assertIs(self.card, return_value)
def setUp(self) -> None: self.manager = Manager() self.t = Task("a task") self.t.register_argument("first arg", string(min_length=4)) self.manager.register_task(self.t)
class TestDiskDeserializers(unittest.TestCase): def setUp(self) -> None: self.manager = Manager() self.t = Task("a task") self.t.register_argument("first arg", string(min_length=4)) self.manager.register_task(self.t) def test_routine_deserialize(self): serialized_routine = { "name": "A simple routine", "tasks": [{ "name": "a task", "values": { "first arg": "my string" } }] } r = des.deserialize_routine(serialized_routine, self.manager) self.assertIsNotNone(r) self.assertEqual(r.name, "A simple routine") self.assertEqual(len(r.tasks), 1) t, values = r.tasks[0] self.assertEqual(self.t, t) self.assertDictEqual(values, {"first arg": "my string"}) def test_routine_deserialize_invalid_value(self): serialized_routine = { "name": "A simple routine", "tasks": [{ "name": "a task", "values": { "first arg": "bad" } }] } r = des.deserialize_routine(serialized_routine, self.manager) self.assertIsNotNone(r) self.assertEqual(len(r.tasks), 0) def test_routine_deserialize_missing_task(self): serialized_routine = { "name": "A simple routine", "tasks": [{ "name": "a non existing task", "values": { "first arg": "my string" } }] } r = des.deserialize_routine(serialized_routine, self.manager) self.assertIsNotNone(r) self.assertEqual(len(r.tasks), 0) def test_routine_deserialize_missing_arg_value(self): serialized_routine = { "name": "A simple routine", "tasks": [{ "name": "a task", "values": {} }] } r = des.deserialize_routine(serialized_routine, self.manager) self.assertIsNotNone(r) self.assertEqual(len(r.tasks), 0) def test_routine_deserialize_wrong_format_task(self): serialized_routine = { "name": "A simple routine", "tasks": [{ "missing name": "a non existing task", "values": { "first arg": "my string" } }] } r = des.deserialize_routine(serialized_routine, self.manager) self.assertIsNotNone(r) self.assertEqual(len(r.tasks), 0) serialized_routine = { "name": "A simple routine", "tasks": [{ "name": ["Not", "a", "string"], "values": { "first arg": "my string" } }] } r = des.deserialize_routine(serialized_routine, self.manager) self.assertIsNotNone(r) self.assertEqual(len(r.tasks), 0) serialized_routine = { "name": "A simple routine", "tasks": [{ "name": "a task", "values": "not a dict" }] } r = des.deserialize_routine(serialized_routine, self.manager) self.assertIsNotNone(r) self.assertEqual(len(r.tasks), 0) def test_routine_deserialize_wrong_format(self): r = des.deserialize_routine({}, self.manager) self.assertIsNone(r) r = des.deserialize_routine({"name": 5, "tasks": []}, self.manager) self.assertIsNone(r) r = des.deserialize_routine({ "name": "ok", "tasks": None }, self.manager) self.assertIsNone(r) # noinspection PyTypeChecker r = des.deserialize_routine("An other type", self.manager) self.assertIsNone(r) def test_card_deserialize_unlinked(self): card = des.deserialize_card({ "name": "my card", "id": "the id" }, self.manager) self.assertEqual(card.name, "my card") self.assertEqual(card.id, "the id") self.assertFalse(card.is_linked()) def test_card_deserialize_linked_not_found(self): card = des.deserialize_card({ "name": "a card", "id": "an id" }, self.manager) self.assertEqual(card.name, "a card") self.assertEqual(card.id, "an id") 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_card_deserialize_wrong_format(self): # noinspection PyTypeChecker self.assertIsNone(des.deserialize_card([], self.manager)) self.assertIsNone(des.deserialize_card({}, self.manager)) self.assertIsNone( des.deserialize_card({ "name": 5, "id": "6546123" }, self.manager)) self.assertIsNone( des.deserialize_card({ "name": "a string", "id": { "other": "thing" } }, self.manager))
error = None if request.method == "POST": values = {} for arg_name in t.arguments: try: value = request.form["%sField" % arg_name] if validation.validate(t.arguments[arg_name], value): values[arg_name] = value else: err = "La valeur {} n'est pas valide pour l'argument {}.".format( value, arg_name) error = err if error is None else "{} {}".format( error, err) except KeyError: error = "Valeur(s) manquante(s) !" if error is None: r.add_task(t, values) save_manager() return redirect("/routines") return render_template("choose-args.html", routine=r, task=t, error=error) if __name__ == '__main__': web = Web(Manager(), lambda: 12) web.app.run(host="0.0.0.0", port=80)