def test_start_sets_started_field(self): test_start_time = datetime.datetime.now() time.sleep(0.1) task = entities.Task() task.start() self.assertIsNotNone(task.started_time) self.assertGreater(task.started_time, test_start_time)
def test_execute_prints_task_id(self): existing_task_count = 3 existing_tasks = [] for _ in range(0, existing_task_count): existing_tasks.append(mock.Mock()) mock_console = mock.Mock() mock_console.print = mock.MagicMock() mock_settings = mock.Mock() mock_settings.command_add_next_key_id = 2 mock_settings.command_add_format = '{name}' mock_storage = mock.MagicMock() mock_storage.read_all = MagicMock(return_value=existing_tasks) mock_context = mock.Mock() mock_context.console = mock_console mock_context.settings = mock_settings mock_context.storage = mock_storage task = entities.Task() task.name = 'test name' command = addcommand.AddTaskCommand(mock_context) command.task = task command.execute() output = 'Task created: {}'.format(existing_task_count + 1) mock_console.print.assert_called_once_with(output)
def parse(self, line_number, line): ''' Parses a task. ''' task = entities.Task() task.index = line_number pattern = '(?P<name>\\w[^:]+):"(?P<value>[^"]+)"' for match in re.finditer(pattern, line): key = match.group('name') value = match.group('value') if key == 'description': task.name = value elif key == 'end': task.end_time = self._parse_datetime(value) elif key == 'entry': task.created_time = self._parse_datetime(value) elif key == 'start': task.started_time = self._parse_datetime(value) elif key == 'status': task.status = value elif key == 'uuid': task.id_number = value elif key.startswith('annotation_'): created_timestamp = key[11:] annotation_created = self._parse_datetime(created_timestamp) annotation = entities.TaskAnnotation(value, annotation_created) task.annotations.append(annotation) else: task.attributes[key] = value return task
def parse(self, context, args): if not args: raise Exception( 'Adding new task requires at least one word in name') task = entities.Task() task.created = datetime.datetime.now() task.id_number = uuid.uuid4() task.name = ' '.join(args) task.status = 'pending' command = AddTaskCommand(context) command.task = task return command
def test_execute_calls_write_on_storage(self): mock_settings = mock.Mock() mock_settings.command_add_next_key_id = 2 mock_settings.command_add_format = 'FEAT-{key_id:04}: {name}' mock_storage = mock.MagicMock() mock_storage.write = MagicMock() mock_context = mock.Mock() mock_context.settings = mock_settings mock_context.storage = mock_storage task = entities.Task() task.name = 'test name' command = addcommand.AddTaskCommand(mock_context) command.task = task command.execute() self.assertEqual('FEAT-0002: test name', task.name) mock_storage.write.assert_called_once_with(task)
def output_test(self, include_started, include_ended): task = entities.Task() annotation1_date = datetime.datetime(2019, 12, 1, 20, 59, 18) annotation2_date = datetime.datetime(2019, 12, 2, 20, 59, 18) annotation1 = entities.TaskAnnotation('annotation 1', annotation1_date) annotation2 = entities.TaskAnnotation('annotation 2', annotation2_date) task.annotations.append(annotation1) task.annotations.append(annotation2) task.attributes['project'] = 'project 1' task.attributes['priority'] = 'high' task.created_time = datetime.datetime(2019, 11, 29, 20, 59, 18) task.id_number = uuid.UUID('a0bde7e1-21b5-4378-9e06-1f72e0336c28') task.name = 'task name' task.status = 'pending' if include_started: task.started_time = datetime.datetime(2019, 11, 29, 20, 59, 18) if include_ended: task.end_time = datetime.datetime(2020, 11, 29, 20, 59, 18) expected = '[' expected += 'annotation_1575233958:"annotation 1"' expected += ' annotation_1575320358:"annotation 2"' expected += ' description:"task name"' if include_ended: expected += ' end:"1606683558"' expected += ' entry:"1575061158"' expected += ' priority:"high"' expected += ' project:"project 1"' if include_started: expected += ' start:"1575061158"' expected += ' status:"pending"' expected += ' uuid:"' + str(task.id_number) + '"' expected += ']' formatter = storage.TaskWarriorFormatter() actual = formatter.format(task) self.assertEqual(expected, actual)
def test_write_writes_to_file(self, mock_os, mock_path): expected_output = 'testing 1 2 3' self._formatter.format = mock.MagicMock(return_value=expected_output) task = entities.Task() task.created = datetime.datetime.now() test_data_location = '/tmp' test_pending_filename = 'test.dat' test_path = '/tmp/test.dat' temp_path = '/tmp/test.dat.tmp' mock_path.isfile.return_value = True mock_path.join.return_value = test_path mock_settings = Empty() mock_settings.data_location = test_data_location mock_settings.data_pending_filename = test_pending_filename mock_open = mock.mock_open() with mock.patch('storage.open', mock_open): target = storage.TextFileStorage(test_path, self._formatter) target.write([task]) calls = [ mock.call(test_path), mock.call(temp_path), mock.call(test_path) ] self.assertEqual(calls, mock_path.isfile.mock_calls) calls = [mock.call(temp_path), mock.call(test_path)] self.assertEqual(calls, mock_os.remove.mock_calls) mock_open.assert_called_with(temp_path, 'w+', newline='\n') handle = mock_open() handle.write.assert_called_once_with(expected_output + '\n') handle.__exit__.assert_called() mock_os.rename.assert_called_with(temp_path, test_path)
def test_constructor_sets_properties(self): test_start_time = datetime.datetime.now() time.sleep(0.1) task = entities.Task() self.assertIsNotNone(task.created_time) self.assertGreater(task.created_time, test_start_time)
def test_stop_clears_started_field(self): task = entities.Task() task.stop() self.assertIsNone(task.started_time)
def test_is_started_correct(self): task = entities.Task() task.start() self.assertTrue(task.is_started) task.stop() self.assertFalse(task.is_started)
def test_is_ended_correct(self): task = entities.Task() self.assertFalse(task.is_ended) task.end() self.assertTrue(task.is_ended)
def new_task_to_requirement(r, identifier, estimation): t = entities.Task(identifier, estimation) r.tasks[task] = t return t