def test_add_task(self): tasklist = TaskList() tasklist.entity_id = "tasklistid" expected_task = Task() expected_task.entity_id = "abcid" expected_task.tasklist_id = tasklist.entity_id expected_task.title = "Task title" now = datetime.now() expected_task_updated_date = datetime(now.year, now.month, now.day, now.hour, now.minute, now.second) expected_task_position = 10456 expected_task_status = TaskStatus.NEEDS_ACTION result_str_dict = { GoogleKeywords.ID: StrConverter().to_str(expected_task.entity_id), GoogleKeywords.TITLE: StrConverter().to_str(expected_task.title), GoogleKeywords.UPDATED: RFC3339Converter().to_str(expected_task_updated_date), GoogleKeywords.POSITION: IntConverter().to_str(expected_task_position), GoogleKeywords.STATUS: TaskStatusConverter().to_str(expected_task_status) } mock_service_proxy = mock() mock_insert_request = mock() when(mock_service_proxy).insert(tasklist=tasklist.entity_id, body=any(dict)).thenReturn(mock_insert_request) when(mock_insert_request).execute().thenReturn(result_str_dict) task_service = TaskService(mock_service_proxy) actual_task = task_service.add_task(expected_task) self.assertIsNotNone(actual_task) self.assertEqual(expected_task.entity_id, actual_task.entity_id) self.assertEqual(expected_task.tasklist_id, actual_task.tasklist_id) self.assertEqual(expected_task.title, actual_task.title) self.assertEqual(expected_task_updated_date, actual_task.updated_date) self.assertEqual(expected_task_position, actual_task.position) self.assertEqual(expected_task_status, actual_task.task_status)
def test_delete_task(self): # IDs used to specify which task to delete and get. tasklist = TaskList() tasklist.entity_id = "tasklistid" # Fake an updated update date by capturing a date, adding five minutes, # and using that new date as the date source for a str dict that is # returned by the fake service proxy? existing_updated_date = datetime(2012, 3, 21, 13, 52, 06) expected_updated_date = datetime(2012, 3, 21, 13, 57, 06) input_task = Task() input_task.entity_id = "abcid" input_task.tasklist_id = tasklist.entity_id input_task.title = "Task title" input_task.position = 10456 input_task.task_status = TaskStatus.COMPLETED input_task.updated_date = existing_updated_date expected_task = Task() expected_task.entity_id = input_task.entity_id expected_task.title = input_task.entity_id expected_task.position = input_task.position expected_task.task_status = input_task.task_status expected_task.updated_date = expected_updated_date # Mock service and request objects. mock_service_proxy = mock() mock_delete_request = mock() mock_get_request = mock() # Note that this str dict serves as the result of a subsequent get # called on the same task after that task has been deleted. It includes # the expected (mock updated) update date. get_result_str_dict = { GoogleKeywords.ID: StrConverter().to_str(expected_task.entity_id), GoogleKeywords.TITLE: StrConverter().to_str(expected_task.title), GoogleKeywords.UPDATED: RFC3339Converter().to_str(expected_updated_date), GoogleKeywords.POSITION: IntConverter().to_str(expected_task.position), GoogleKeywords.STATUS: TaskStatusConverter().to_str(expected_task.task_status), GoogleKeywords.DELETED: BooleanConverter().to_str(True) } # Set up service proxy mock behavior in order to provide the delete # method with the necessary backend. when(mock_service_proxy).delete(tasklist=tasklist.entity_id, task=input_task.entity_id).thenReturn(mock_delete_request) when(mock_delete_request).execute().thenReturn("") when(mock_service_proxy).get(tasklist=tasklist.entity_id, task=input_task.entity_id).thenReturn(mock_get_request) when(mock_get_request).execute().thenReturn(get_result_str_dict) # Create a new TaskService. task_service = TaskService(mock_service_proxy) # Delete the task, and store the updated results. actual_task = task_service.delete_task(input_task) # Task should be present and have these updated properties: # * update date # * deleted self.assertIsNotNone(actual_task) self.assertEqual(expected_updated_date, actual_task.updated_date) self.assertTrue(actual_task.is_deleted, True)