Mutation and testing
We can modify our TODO list:
def remove_finished(todo: list): """remove completed items from the TODO list""" completed_items = list(filter(lambda item: item.done == True, todo)) for item in completed_items: todo_list.remove(item)
We can test this function:
def test_remove_finished(): lst = [TodoItem(date(2020, 11, 8), [], "a", False), TodoItem(date(2020, 11, 20), ["a"], "b", True)] remove_finished(lst) test("item removed", lst, [TodoItem(date(2020, 11, 8), [], "a", False)])
We’ve defined a todo_list
variable in todo.py
–why not use that variable in
our test?
Modifying TODO items
As we’ve seen, Python allows us to modify data. Dataclasses are no exception:
> avocado_item.done = True > avocado_item > todo_list
We can write a function to mark items with a particular description as completed:
def complete_by_description(todo: list, descr: str): for item in todo: if item.description == descr: item.done = True return
Here we have a return
statement, but we’re not returning anything. What would
happen if we removed the return
statement?
How would we test this function?
def test_complete_by_description(): item1 = TodoItem(date(2020, 11, 25), "desc1", ["tag"], False) item2 = TodoItem(date(2020, 11, 25), "desc2", ["tag"], False) todo = [item1, item2] complete_by_description(todo, "desc1") test("completed in list", todo, [TodoItem(date(2020, 11, 25), "desc1", ["tag"], True), TodoItem(date(2020, 11, 25), "desc2", ["tag"], False)]) test("correct item completed", item1.done, True)