def test_read_task(): def basic_string(n): return "Hello world" task_id = "00000" usecase = TimeTracking(Mock(), Mock()) usecase.task_tracker.get_task.side_effect = basic_string assert usecase.read_task(task_id) == "Hello world"
def test_complete_runs(): task_tracker = Mock() local_storage = Mock() usecase = TimeTracking(task_tracker, local_storage) task_id = "000000" usecase.complete_task(task_id) # Fix me! This isn't passing assert usecase.task_tracker.complete_task.called_with(task_id)
def test_stop_should_throw_error_for_task_not_being_tracked(): task_id = "1000000" # create mocks task_tracker = Mock() local_storage = get_local_storage()() usecase = TimeTracking(task_tracker, local_storage) with pytest.raises(TaskDoesNotExist): usecase.stop_tracking(task_id)
def test_complete_for_invalid_task(): def raise_exc(n): raise RemoteTaskNotFound() task_id = "000000" usecase = TimeTracking(Mock(), Mock()) # We need to check that it's still valid first usecase.task_tracker.get_task.side_effect = raise_exc with pytest.raises(RemoteTaskNotFound): usecase.complete_task(task_id)
def test_read_task_with_invalid_task_id(): def raise_exc(n): raise RemoteTaskNotFound bad_task_id = "000000" task_tracker = Mock() local_storage = Mock() usecase = TimeTracking(task_tracker, local_storage) usecase.task_tracker.get_task.side_effect = raise_exc with pytest.raises(RemoteTaskNotFound): usecase.read_task(bad_task_id)
def test_start_should_throw_error_for_task_we_are_already_tracking(): task_id = "1000000" # create mocks task_tracker = Mock() local_storage = get_local_storage()() usecase = TimeTracking(task_tracker, local_storage) task = Task(task_id, datetime.now()) # This is a testing only method. usecase.local_storage.DB.append(task_serializer.task_to_dict(task)) # check that local storage is checked and causes an error with pytest.raises(TaskAlreadyExists): usecase.start_tracking(task_id)
def test_start_should_throw_error_for_invalid_remote_task_id(): def raise_exc(n): raise RemoteTaskNotFound task_id = "1000000" # create mocks task_tracker = Mock() local_storage = get_local_storage()() usecase = TimeTracking(task_tracker, local_storage) # set task tracker to respond badly to one of the task IDs usecase.task_tracker.get_task.side_effect = raise_exc # check that the exception is thrown with pytest.raises(RemoteTaskNotFound): usecase.start_tracking(task_id)
def test_start_should_start_tracking(): task_id = "1000000" # create mocks task_tracker = Mock() local_storage = get_local_storage()() usecase = TimeTracking(task_tracker, local_storage) usecase.start_tracking(task_id) # Is Asana checked for validity? assert usecase.task_tracker.get_task.called_once() # Is the data stored? I.e. was it actually written somewhere task = usecase.local_storage.get_task(task_id) assert task.id == task_id
def test_list_tasks(): OUTPUT = [ TaskTrackerTask(id="0", name="Test 0", assigned_to="", due=datetime.now(), complete=False, description="", time_spent=0.0), TaskTrackerTask(id="1", name="Test 1", assigned_to="", due=datetime.now(), complete=False, description="", time_spent=0.0), TaskTrackerTask(id="2", name="Test 2", assigned_to="", due=datetime.now(), complete=False, description="", time_spent=0.0) ] def list_tasks(): return OUTPUT task_tracker = Mock() local_storage = get_local_storage()() usecase = TimeTracking(task_tracker, local_storage) usecase.task_tracker.list_tasks.side_effect = list_tasks output = usecase.list_tasks() assert usecase.task_tracker.list_tasks.called_once() assert output == OUTPUT
def find_task_from_slug(work_time: TimeTracking, slug: str) -> str: assigned_tasks = work_time.list_tasks() matches = [] for task in assigned_tasks: if task.name.lower().startswith(slug.lower()): matches.append(task) if len(matches) == 0: raise RemoteTaskNotFound elif len(matches) == 1: return matches[0].id else: print(f"* Too many matches found. Which would you like to use? ") return pick_task_from_list(matches)
def test_complete_closes_locally_running_task_if_exists(): task_id = "000000" task_tracker = Mock() local_storage = get_local_storage()() usecase = TimeTracking(task_tracker, local_storage) usecase.start_tracking(task_id) usecase.complete_task(task_id) # check that the task is now actually stopped with pytest.raises(TaskDoesNotExist): usecase.stop_tracking(task_id) assert usecase.task_tracker.sync_time.called_once_with(task_id, 0) # Check complete task is called after assert usecase.task_tracker.complete_task.called_once_with(task_id)
def test_stop_should_stop_tracking(): task_id = "1000000" # create mocks task_tracker = Mock() local_storage = get_local_storage()() usecase = TimeTracking(task_tracker, local_storage) usecase.start_tracking(task_id) usecase.stop_tracking(task_id) with pytest.raises(TaskDoesNotExist): usecase.local_storage.get_task(task_id)
def test_stop_should_sync_tracking_with_remote(): task_id = "1000000" # create mocks task_tracker = Mock() local_storage = get_local_storage()() usecase = TimeTracking(task_tracker, local_storage) usecase.start_tracking(task_id) usecase.stop_tracking(task_id) # check that there's been no errors, and the time # sync was called to asana. assert usecase.task_tracker.sync_time.called_once()
def test_stop_should_throw_error_for_task_that_no_longer_exist(): def raise_exc(n, m): raise RemoteTaskNotFound task_id = "1000000" # create mocks task_tracker = Mock() local_storage = get_local_storage()() usecase = TimeTracking(task_tracker, local_storage) usecase.start_tracking(task_id) # simulate the task no longer existing. # this error would be thrown by get_task usecase.task_tracker.sync_time.side_effect = raise_exc with pytest.raises(RemoteTaskNotFound): usecase.stop_tracking(task_id)
for task in assigned_tasks: if task.name.lower().startswith(slug.lower()): matches.append(task) if len(matches) == 0: raise RemoteTaskNotFound elif len(matches) == 1: return matches[0].id else: print(f"* Too many matches found. Which would you like to use? ") return pick_task_from_list(matches) if __name__ == "__main__": work_time = TimeTracking(AsanaTaskTracker(), FileLocalStorage()) if args.magic: try: args.id = find_task_from_slug(work_time, args.magic) except RemoteTaskNotFound: print("-> Task not found. Please try another slug.") sys.exit(0) if args.action == "start": try: work_time.start_tracking(args.id) print(f"-> Started tracking {args.id}") except TaskAlreadyExists: print("-> Already tracking. Doing nothing.")