def test_asdict(): """_asdict() should return a dictionary.""" t_task = Task('do something', 'okken', True, 21) t_dict = t_task._asdict() expected = {'summary': 'do something', 'owner': 'okken', 'done': True, 'id': 21} assert t_dict == expected
def test_unique_id_2(): """unique_id() should return an unused id.""" ids = [] ids.append(tasks.add(Task('one'))) ids.append(tasks.add(Task('two'))) ids.append(tasks.add(Task('three'))) # grab a unique id uid = tasks.unique_id() # make sure it isn't in the list of existing ids assert uid not in ids
def tasks_mult_per_owner(): """Several owners with several tasks each.""" return (Task('Make a cookie', 'Raphael'), Task('Use an emoji', 'Raphael'), Task('Move to Berlin', 'Raphael'), Task('Create', 'Michelle'), Task('Inspire', 'Michelle'), Task('Encourage', 'Michelle'), Task('Do a handstand', 'Daniel'), Task('Write some books', 'Daniel'), Task('Eat ice cream', 'Daniel'))
def test_add_1(tasks_db): """tasks.get() using id returned from add() works.""" task = Task('breathe', 'BRIAN', True) task_id = tasks.add(task) t_from_db = tasks.get(task_id) # everything but the id should be the same assert equivalent(t_from_db, task)
def test_add_returns_valid_id(): """tasks.add(<valid task>) should return an integer.""" # GIVEN an initialized tasks db # WHEN a new task is added # THEN returned task_id is of type int new_task = Task('do something') task_id = tasks.add(new_task) assert isinstance(task_id, int)
def test_add_increases_count(db_with_3_tasks): """Test tasks.add() affect on tasks.count().""" # GIVEN a db with 3 tasks # WHEN another task is added tasks.add(Task('throw a party')) # THEN the count increases by 1 assert tasks.count() == 4
def test_added_task_has_id_set(): """Make sure the task_id field is set by tasks.add().""" # GIVEN an initialized tasks db # AND a new task is added new_task = Task('sit in chair', owner='me', done=True) task_id = tasks.add(new_task) # WHEN task is retrieved task_from_db = tasks.get(task_id) # THEN task_id matches id field assert task_from_db.id == task_id
def test_added_task_has_id_set(tasks_db): """Make sure the task_id field is set by tasks.add().""" # GIVEN an initialized tasks db # AND a new task is added new_task = Task('sit in chair', owner='me', done=True) task_id = tasks.add(new_task) # WHEN task is retrieved task_from_db = tasks.get(task_id) # THEN task_id matches id field assert task_from_db.id == task_id # AND contents are equivalent (except for id) # the [:-1] syntax returns a list with all but the last element assert task_from_db[:-1] == new_task[:-1]
def test_dict_equality(): """Different tasks compared as dicts should not be equal.""" t1_dict = Task('make sandwich', 'okken')._asdict() t2_dict = Task('make sandwich', 'okkem')._asdict() assert t1_dict == t2_dict
def test_add_3(tasks_db, summary, owner, done): """Demonstrate parametrize with multiple parameters.""" task = Task(summary, owner, done) task_id = tasks.add(task) t_from_db = tasks.get(task_id) assert equivalent(t_from_db, task)
task = Task('breathe', 'BRIAN', True) task_id = tasks.add(task) t_from_db = tasks.get(task_id) # everything but the id should be the same assert equivalent(t_from_db, task) def equivalent(t1, t2): """Check two tasks for equivalence.""" # Compare everything but the id field return ((t1.summary == t2.summary) and (t1.owner == t2.owner) and (t1.done == t2.done)) @pytest.mark.parametrize('task', [ Task('sleep', done=True), Task('wake', 'brian'), Task('breathe', 'BRIAN', True), Task('exercise', 'BrIaN', False) ]) def test_add_2(tasks_db, task): """Demonstrate parametrize with one parameter.""" task_id = tasks.add(task) t_from_db = tasks.get(task_id) assert equivalent(t_from_db, task) @pytest.mark.parametrize('summary, owner, done', [ ('sleep', None, False), ('wake', 'brian', False), ('breathe', 'BRIAN', True),
"""Test the tasks.add() API function.""" import pytest from tasks_proj.src import tasks from tasks_proj.src.tasks import Task tasks_to_try = (Task('sleep', done=True), Task('wake', 'brian'), Task('breathe', 'BRIAN', True), Task('exercise', 'BrIaN', False)) task_ids = [ 'Task({},{},{})'.format(t.summary, t.owner, t.done) for t in tasks_to_try ] def equivalent(t1, t2): """Check two tasks for equivalence.""" return ((t1.summary == t2.summary) and (t1.owner == t2.owner) and (t1.done == t2.done)) @pytest.fixture(params=tasks_to_try) def a_task(request): return request.param def test_add_a(tasks_db, a_task): task_id = tasks.add(a_task) t_from_db = tasks.get(task_id) assert equivalent(t_from_db, a_task)
def test_replace(): """replace() should change passed in fields.""" t_before = Task('finish book', 'brian', False) t_after = t_before._replace(id=10, done=True) t_expected = Task('finish book', 'brian', True, 10) assert t_after == t_expected
def tasks_just_a_few(): """All summaries and owners are unique.""" return (Task('Write some code', 'Brian', True), Task("Code review Brian's code", 'Katie', False), Task('Fix what Brian did', 'Michelle', False))
def test_task_equality(): """Different tasks should not be equal.""" t1 = Task('sit there', 'brian') t2 = Task('do something', 'okken') assert t1 != t2
def test_defaults(): """Using no parameters should invoke defaults.""" t1 = Task() t2 = Task(None, None, False, None) assert t1 == t2
def test_member_access(): """Check .field functionality of namedtuple.""" t = Task('buy milk', 'brian') assert t.summary == 'buy milk' assert t.owner == 'brian' assert (t.done, t.id) == (False, None)