def setUp(self): super().setUp() todos = [ "Foo", "Bar", "Baz", ] self.todolist = TodoList(todos) self.today = date.today() self.tmp_name = str(uuid4().hex.upper()[0:6]) archive_filename = tempfile.gettempdir( ) + os.sep + self.tmp_name + '_archive' todo_filename = tempfile.gettempdir( ) + os.sep + self.tmp_name + '_todo' config( p_overrides={ ('topydo', 'archive_filename'): archive_filename, ('topydo', 'filename'): todo_filename, ('topydo', 'backup_count'): '5' }) self.archive_file = TodoFile(archive_filename) self.archive = TodoList([])
def setUp(self): super(TodoListCleanDependencyTester, self).setUp() self.todolist = TodoList([]) self.todolist.add("Bar p:1") self.todolist.add("Baz p:1 id:2") self.todolist.add("Buzz p:2")
def test_todo_by_dep_id(self): """ Tests that todos can be retrieved by their id tag. """ todolist = TodoList([]) todolist.add("(C) Foo id:1") self.assertTrue(todolist.todo_by_dep_id("1")) self.assertFalse(todolist.todo_by_dep_id("2"))
def setUp(self): super(TodoListTester, self).setUp() self.todofile = TodoFile('test/data/TodoListTest.txt') lines = [line for line in self.todofile.read() \ if re.search(r'\S', line)] self.text = ''.join(lines) self.todolist = TodoList(lines)
def setUp(self): super(DeleteCommandTest, self).setUp() todos = [ "Foo id:1", "Bar p:1", ] self.todolist = TodoList(todos)
def setUp(self): super(DepriCommandTest, self).setUp() todos = [ "(A) Foo", "Bar", ] self.todolist = TodoList(todos)
def setUp(self): super().setUp() todos = [ "Foo id:1", "Bar p:1", "a @test with due:2015-06-03", "a @test with +project", ] self.todolist = TodoList(todos)
def setUp(self): super(EditCommandTest, self).setUp() todos = [ "Foo id:1", "Bar p:1 @test", "Baz @test", u("Fo\u00f3B\u0105\u017a"), ] self.todolist = TodoList(todos)
def setUp(self): super(TagCommandTest, self).setUp() todos = [ "Foo", "Bar due:2014-10-22", "Baz due:2014-10-20", "Fnord due:2014-10-20 due:2014-10-22", ] self.todolist = TodoList(todos)
def setUp(self): super().setUp() todos = [ "Foo id:1", "Bar p:1 @test", "Baz @test", u"Fo\u00f3B\u0105\u017a", ] self.todolist = TodoList(todos)
def test_progress29(self): """ Progress color determined by parent """ todolist = TodoList([ "Overdue id:1 due:2015-12-31", "Bar p:1 t:2016-01-01 due:2016-01-01", ]) color = progress_color(todolist.todo(2)) # the parent has no influence here self.assertEqual(color.color, 3)
def test_archive(self): todolist = load_file_to_todolist("test/data/ArchiveCommandTest.txt") archive = TodoList([]) command = ArchiveCommand(todolist, archive) command.execute() self.assertTrue(todolist.is_dirty()) self.assertTrue(archive.is_dirty()) self.assertEqual(todolist.print_todos(), "x Not complete\n(C) Active") self.assertEqual(archive.print_todos(), "x 2014-10-19 Complete\nx 2014-10-20 Another one complete")
def test_progress30(self): """ Progress color determined by parent """ todolist = TodoList([ "Foo id:1", "Bar p:1", ]) color = progress_color(todolist.todo(2)) # the parent has no influence here self.assertEqual(color.color, 2)
def test_view(self): """ Check filters and printer for views. """ todofile = TodoFile('test/data/FilterTest1.txt') ref = load_file('test/data/ViewTest1-result.txt') todolist = TodoList(todofile.read()) sorter = Sorter('text') todofilter = Filter.GrepFilter('+Project') view = todolist.view(sorter, [todofilter]) self.assertEqual(print_view(view), todolist_to_string(ref))
def test_archive(self): todolist = load_file_to_todolist("test/data/ArchiveCommandTest.txt") archive = TodoList([]) command = ArchiveCommand(todolist, archive) command.execute() self.assertTrue(todolist.dirty) self.assertTrue(archive.dirty) self.assertEqual(todolist.print_todos(), "x Not complete\n(C) Active") self.assertEqual(archive.print_todos(), "x 2014-10-19 Complete\nx 2014-10-20 Another one complete")
def setUp(self): super(PriorityCommandTest, self).setUp() todos = [ "(A) Foo", "Bar", "(B) a @test with due:2015-06-03", "a @test with +project p:1", "Baz id:1", ] self.todolist = TodoList(todos)
def setUp(self): super().setUp() todos = [ "Foo id:1", "Bar p:1", "Baz p:1", "Fnord id:2", "Garbage dependency p:99", "Fart p:2", ] self.todolist = TodoList(todos)
def test_progress28(self): """ Progress color determined by parent """ todolist = TodoList([ "Overdue id:1 due:2015-12-31", "Bar p:1", ]) color = progress_color(todolist.todo(2)) # color the subitem red because it has no color of its own and its # parent is overdue self.assertEqual(color.color, 1)
def setUp(self): super(DepriCommandTest, self).setUp() todos = [ "(A) Foo", "Bar", "(B) Baz", "(E) a @test with due:2015-06-03", "(Z) a @test with +project p:1", "(D) Bax id:1", ] self.todolist = TodoList(todos)
def setUp(self): super(TodoListDependencyTester, self).setUp() self.todolist = TodoList([]) self.todolist.add("Foo id:1") self.todolist.add("Bar p:1") self.todolist.add("Baz p:1 id:2") self.todolist.add("Buzz p:2") self.todolist.add("Fnord") self.todolist.add("Something with +Project") self.todolist.add("Another one with +Project") self.todolist.add("Todo with +AnotherProject") self.todolist.add("Todo without children id:3")
class DepriCommandTest(CommandTest.CommandTest): def setUp(self): super(DepriCommandTest, self).setUp() todos = [ "(A) Foo", "Bar", ] self.todolist = TodoList(todos) def test_set_prio1(self): command = DepriCommand(["1"], self.todolist, self.out, self.error) command.execute() self.assertTrue(self.todolist.is_dirty()) self.assertEquals(self.todolist.todo(1).priority(), None) self.assertEquals(self.output, "Priority removed.\nFoo\n") self.assertEquals(self.errors, "") def test_set_prio2(self): command = DepriCommand(["2"], self.todolist, self.out, self.error) command.execute() self.assertFalse(self.todolist.is_dirty()) self.assertEquals(self.todolist.todo(2).priority(), None) self.assertEquals(self.output, "") self.assertEquals(self.errors, "") def test_set_prio3(self): command = DepriCommand(["Foo"], self.todolist, self.out, self.error) command.execute() self.assertTrue(self.todolist.is_dirty()) self.assertEquals(self.todolist.todo(1).priority(), None) self.assertEquals(self.output, "Priority removed.\nFoo\n") self.assertEquals(self.errors, "") def test_invalid1(self): command = DepriCommand(["99"], self.todolist, self.out, self.error) command.execute() self.assertFalse(self.todolist.is_dirty()) self.assertFalse(self.output) self.assertEquals(self.errors, "Invalid todo number given.\n") def test_empty(self): command = DepriCommand([], self.todolist, self.out, self.error) command.execute() self.assertFalse(self.todolist.is_dirty()) self.assertFalse(self.output) self.assertEquals(self.errors, command.usage() + "\n") def test_help(self): command = DepriCommand(["help"], self.todolist, self.out, self.error) command.execute() self.assertEquals(self.output, "") self.assertEquals(self.errors, command.usage() + "\n\n" + command.help() + "\n")
def test_archive(self): todolist = TestFacilities.load_file_to_todolist( "test/data/ArchiveCommandTest.txt") archive = TodoList([]) command = ArchiveCommand(todolist, archive) command.execute() self.assertTrue(todolist.is_dirty()) self.assertTrue(archive.is_dirty()) self.assertEquals(str(todolist), "x Not complete\n(C) Active") self.assertEquals( str(archive), "x 2014-10-19 Complete\nx 2014-10-20 Another one complete")
def get_backup(self, p_todolist): """ Retrieves a backup for p_todolist from backup file and sets todolist, archive and call attributes to appropriate data from it. """ change_hash = hash_todolist(p_todolist) index = self._get_index() self.timestamp = index[[change[1] for change in index].index(change_hash)][0] d = self.backup_dict[self.timestamp] self.todolist = TodoList(d[0]) self.archive = TodoList(d[1]) self.call = d[2]
def test_revert02(self): backup = ChangeSet(self.todolist, self.archive, ['do 1']) backup.timestamp = '1' command1 = DoCommand(["1"], self.todolist, self.out, self.error, None) command1.execute() archive_command1 = ArchiveCommand(self.todolist, self.archive) archive_command1.execute() self.archive_file.write(self.archive.print_todos()) backup.save(self.todolist) backup = ChangeSet(self.todolist, self.archive, ['do Bar']) backup.timestamp = '2' command2 = DoCommand(["Bar"], self.todolist, self.out, self.error, None) command2.execute() archive_command2 = ArchiveCommand(self.todolist, self.archive) archive_command2.execute() self.archive_file.write(self.archive.print_todos()) backup.save(self.todolist) self.assertEqual(self.archive.print_todos(), "x {t} Foo\nx {t} Bar".format(t=self.today)) self.assertEqual(self.todolist.print_todos(), "Baz") revert_command = RevertCommand([], self.todolist, self.out, self.error, None) revert_command.execute() result = TodoList(self.archive_file.read()).print_todos() self.assertEqual(self.errors, "") self.assertTrue( self.output.endswith("Successfully reverted: do Bar\n")) self.assertEqual(result, "x {} Foo".format(self.today)) self.assertEqual(self.todolist.print_todos(), "Bar\nBaz")
def get_backup(self, p_todolist): """ Retrieves a backup for p_todolist from backup file and sets todolist, archive and label attributes to appropriate data from it. """ change_hash = hash_todolist(p_todolist) index = self._get_index() self.timestamp = index[[change[1] for change in index].index(change_hash)][0] d = self.backup_dict[self.timestamp] self.todolist = TodoList(d[0]) self.archive = TodoList(d[1]) self.label = d[2]
def test_revert02(self): backup = BackupSimulator(self.todolist, self.archive, '1', ['do 1']) command_executer(DoCommand, ["1"], self.todolist, self.archive, self.out, self.error, None) self.archive_file.write(self.archive.print_todos()) backup.save(self.todolist) # Use add_todolist and add_archive to also cover them backup = ChangeSet(p_label=['do Bar']) backup.add_todolist(self.todolist) backup.add_archive(self.archive) backup.timestamp = '2' command_executer(DoCommand, ["Bar"], self.todolist, self.archive, self.out, self.error, None) self.archive_file.write(self.archive.print_todos()) backup.save(self.todolist) self.assertEqual(self.archive.print_todos(), "x {t} Foo\nx {t} Bar".format(t=self.today)) self.assertEqual(self.todolist.print_todos(), "Baz") revert_command = RevertCommand([], self.todolist, self.out, self.error, None) revert_command.execute() result = TodoList(self.archive_file.read()).print_todos() self.assertEqual(self.errors, "") self.assertTrue( self.output.endswith("Reverted to state before: do Bar\n")) self.assertEqual(result, "x {} Foo".format(self.today)) self.assertEqual(self.todolist.print_todos(), "Bar\nBaz")
def read_backup(self, p_todolist=None, p_timestamp=None): """ Retrieves a backup for p_timestamp or p_todolist (if p_timestamp is not specified) from backup file and sets timestamp, todolist, archive and label attributes to appropriate data from it. """ if not p_timestamp: change_hash = hash_todolist(p_todolist) index = self._get_index() self.timestamp = index[[change[1] for change in index].index(change_hash)][0] else: self.timestamp = p_timestamp d = self.backup_dict[self.timestamp] self.todolist = TodoList(d[0]) self.archive = TodoList(d[1]) self.label = d[2]
def setUp(self): super().setUp() todos = [ "Foo", "Bar due:2014-10-22", "Baz due:2014-10-20", "Fnord due:2014-10-20 due:2014-10-22", ] self.todolist = TodoList(todos)
def setUp(self): super(PostponeCommandTest, self).setUp() self.today = date.today() self.past = date.today() - timedelta(1) self.future = date.today() + timedelta(1) self.start = date.today() - timedelta(2) self.future_start = self.future - timedelta(2) todos = [ "Foo", "Bar due:{}".format(self.today.isoformat()), "Baz due:{} t:{}".format(self.today.isoformat(), self.start.isoformat()), "Past due:{}".format(self.past.isoformat()), "Future due:{} t:{}".format(self.future.isoformat(), self.future_start.isoformat()), "FutureStart t:{}".format(self.future.isoformat()) ] self.todolist = TodoList(todos)
def setUp(self): super().setUp() todos = [ "(A) Foo", "Bar", "(B) Baz", "(E) a @test with due:2015-06-03", "(Z) a @test with +project p:1", "(D) Bax id:1", ] self.todolist = TodoList(todos)
def setUp(self): super().setUp() todos = [ "Foo", "Bar", "Baz", ] self.todolist = TodoList(todos) self.today = date.today() self.tmp_name = str(uuid4().hex.upper()[0:6]) archive_filename = tempfile.gettempdir() + os.sep + self.tmp_name + '_archive' todo_filename = tempfile.gettempdir() + os.sep + self.tmp_name + '_todo' config(p_overrides={('topydo', 'archive_filename'): archive_filename, ('topydo', 'filename'): todo_filename, ('topydo', 'backup_count'): '5'}) self.archive_file = TodoFile(archive_filename) self.archive = TodoList([])
def test_list49(self): """ Only show the top todo. """ todolist = TodoList([ "This item is hidden h:1", "This item is visible", ]) command = ListCommand(["-n", "1"], todolist, self.out, self.error) command.execute() self.assertEqual(self.output, '|2| This item is visible\n') self.assertEqual(self.errors, "")
def test_todo_by_dep_id(self): """ Tests that todos can be retrieved by their id tag. """ todolist = TodoList([]) todolist.add("(C) Foo id:1") self.assertTrue(todolist.todo_by_dep_id('1')) self.assertFalse(todolist.todo_by_dep_id('2'))
def setUp(self): super(DoCommandTest, self).setUp() todos = [ "Foo id:1", "Bar p:1", "Baz p:1", "Recurring! rec:1d", "x 2014-10-18 Already complete", "Inactive t:2030-12-31 id:2", "Subtodo of inactive p:2", "Strict due:2014-01-01 rec:1d", "Invalid rec:1", ] self.todolist = TodoList(todos) self.today = date.today() self.tomorrow = self.today + timedelta(1) self.yesterday = self.today - timedelta(1) self.yesterday = self.yesterday.isoformat() self.today = self.today.isoformat() self.tomorrow = self.tomorrow.isoformat()
def setUp(self): super().setUp() self.today = date.today() self.past = date.today() - timedelta(1) self.future = date.today() + timedelta(1) self.start = date.today() - timedelta(2) self.future_start = self.future - timedelta(2) todos = [ "Foo", "Bar due:{}".format(self.today.isoformat()), "Baz due:{} t:{}".format(self.today.isoformat(), self.start.isoformat()), "Past due:{}".format(self.past.isoformat()), "Future due:{} t:{}".format(self.future.isoformat(), self.future_start.isoformat()), "FutureStart t:{}".format(self.future.isoformat()), "InvalidDueDate due:2017-06-31", "InvalidStartDate t:2017-06-31", ] self.todolist = TodoList(todos)
def test_revert01(self): backup = BackupSimulator(self.todolist, self.archive, '1', ['do 1']) command_executer(DoCommand, ["1"], self.todolist, self.archive, self.out, self.error, None) self.archive_file.write(self.archive.print_todos()) backup.save(self.todolist) self.assertEqual(self.archive.print_todos(), "x {} Foo".format(self.today)) self.assertEqual(self.todolist.print_todos(), "Bar\nBaz") revert_command = RevertCommand([], self.todolist, self.out, self.error, None) revert_command.execute() result = TodoList(self.archive_file.read()).print_todos() self.assertEqual(self.errors, "") self.assertTrue( self.output.endswith("Reverted to state before: do 1\n")) self.assertEqual(result, "") self.assertEqual(self.todolist.print_todos(), "Foo\nBar\nBaz")
def setUp(self): super().setUp() todos = [ "Foo id:1", "Bar p:1", "Baz p:1", "Recurring! rec:1d", "x 2014-10-18 Already complete", "Inactive t:2030-12-31 id:2", "Subtodo of inactive p:2", "Strict due:2014-01-01 rec:1d", "Invalid rec:1", "a @test with due:2015-06-03", "a @test with +project", ] self.todolist = TodoList(todos) self.today = date.today() self.tomorrow = self.today + timedelta(1) self.yesterday = self.today - timedelta(1) self.yesterday = self.yesterday.isoformat() self.today = self.today.isoformat() self.tomorrow = self.tomorrow.isoformat()
class PriorityCommandTest(CommandTest): def setUp(self): super(PriorityCommandTest, self).setUp() todos = [ "(A) Foo", "Bar", "(B) a @test with due:2015-06-03", "a @test with +project p:1", "Baz id:1", ] self.todolist = TodoList(todos) def test_set_prio1(self): command = PriorityCommand(["1", "B"], self.todolist, self.out, self.error) command.execute() self.assertTrue(self.todolist.is_dirty()) self.assertEqual(self.output, "Priority changed from A to B\n| 1| (B) Foo\n") self.assertEqual(self.errors, "") def test_set_prio2(self): command = PriorityCommand(["2", "Z"], self.todolist, self.out, self.error) command.execute() self.assertTrue(self.todolist.is_dirty()) self.assertEqual(self.output, "Priority set to Z.\n| 2| (Z) Bar\n") self.assertEqual(self.errors, "") def test_set_prio3(self): command = PriorityCommand(["Foo", "B"], self.todolist, self.out, self.error) command.execute() self.assertTrue(self.todolist.is_dirty()) self.assertEqual(self.output, "Priority changed from A to B\n| 1| (B) Foo\n") self.assertEqual(self.errors, "") def test_set_prio4(self): command = PriorityCommand(["1", "A"], self.todolist, self.out, self.error) command.execute() self.assertFalse(self.todolist.is_dirty()) self.assertEqual(self.output, "| 1| (A) Foo\n") self.assertEqual(self.errors, "") def test_set_prio5(self): command = PriorityCommand(["Foo", "2", "C"], self.todolist, self.out, self.error) command.execute() self.assertTrue(self.todolist.is_dirty()) self.assertEqual(self.output, "Priority changed from A to C\n| 1| (C) Foo\nPriority set to C.\n| 2| (C) Bar\n") self.assertEqual(self.errors, "") def test_set_prio6(self): """ Allow priority to be set including parentheses. """ command = PriorityCommand(["Foo", "2", "(C)"], self.todolist, self.out, self.error) command.execute() self.assertTrue(self.todolist.is_dirty()) self.assertEqual(self.output, "Priority changed from A to C\n| 1| (C) Foo\nPriority set to C.\n| 2| (C) Bar\n") self.assertEqual(self.errors, "") def test_expr_prio1(self): command = PriorityCommand(["-e", "@test", "C"], self.todolist, self.out, self.error, None) command.execute() result = "Priority changed from B to C\n| 3| (C) a @test with due:2015-06-03\nPriority set to C.\n| 4| (C) a @test with +project p:1\n" self.assertTrue(self.todolist.is_dirty()) self.assertEqual(self.output, result) self.assertEqual(self.errors, "") def test_expr_prio2(self): command = PriorityCommand(["-e", "@test", "due:2015-06-03", "C"], self.todolist, self.out, self.error, None) command.execute() result = "Priority changed from B to C\n| 3| (C) a @test with due:2015-06-03\n" self.assertTrue(self.todolist.is_dirty()) self.assertEqual(self.output, result) self.assertEqual(self.errors, "") def test_expr_prio3(self): command = PriorityCommand(["-e", "@test", "due:2015-06-03", "+project", "C"], self.todolist, self.out, self.error, None) command.execute() self.assertFalse(self.todolist.is_dirty()) def test_expr_prio4(self): """ Don't prioritize unrelevant todo items. """ command = PriorityCommand(["-e", "Baz", "C"], self.todolist, self.out, self.error, None) command.execute() self.assertFalse(self.todolist.is_dirty()) def test_expr_prio5(self): """ Force prioritizing unrelevant items with additional -x flag. """ command = PriorityCommand(["-xe", "Baz", "D"], self.todolist, self.out, self.error, None) command.execute() self.assertTrue(self.todolist.is_dirty()) self.assertEqual(self.output, "Priority set to D.\n| 5| (D) Baz id:1\n") self.assertEqual(self.errors, "") def test_invalid1(self): command = PriorityCommand(["99", "A"], self.todolist, self.out, self.error) command.execute() self.assertFalse(self.todolist.is_dirty()) self.assertFalse(self.output) self.assertEqual(self.errors, "Invalid todo number given.\n") def test_invalid2(self): command = PriorityCommand(["1", "99", "A"], self.todolist, self.out, self.error) command.execute() self.assertFalse(self.todolist.is_dirty()) self.assertFalse(self.output) self.assertEqual(self.errors, "Invalid todo number given: 99.\n") def test_invalid3(self): command = PriorityCommand(["98", "99", "A"], self.todolist, self.out, self.error) command.execute() self.assertFalse(self.todolist.is_dirty()) self.assertFalse(self.output) self.assertEqual(self.errors, "Invalid todo number given: 98.\nInvalid todo number given: 99.\n") def test_invalid4(self): command = PriorityCommand(["1", "ZZ"], self.todolist, self.out, self.error) command.execute() self.assertFalse(self.todolist.is_dirty()) self.assertFalse(self.output) self.assertEqual(self.errors, "Invalid priority given.\n") def test_invalid5(self): command = PriorityCommand(["A"], self.todolist, self.out, self.error) command.execute() self.assertFalse(self.todolist.is_dirty()) self.assertFalse(self.output) self.assertEqual(self.errors, command.usage() + "\n") def test_invalid6(self): command = PriorityCommand(["1"], self.todolist, self.out, self.error) command.execute() self.assertFalse(self.todolist.is_dirty()) self.assertFalse(self.output) self.assertEqual(self.errors, command.usage() + "\n") def test_invalid7(self): """ Throw an error with invalid argument containing special characters. """ command = PriorityCommand([u("Fo\u00d3B\u0105r"), "Bar", "C"], self.todolist, self.out, self.error, None) command.execute() self.assertFalse(self.todolist.is_dirty()) self.assertEqual(self.output, "") self.assertEqual(self.errors, u("Invalid todo number given: Fo\u00d3B\u0105r.\n")) def test_invalid8(self): """ Test that there's only one capital surrounded by non-word characters that makes up a priority. """ command = PriorityCommand(["2", "(Aa)"], self.todolist, self.out, self.error) command.execute() self.assertFalse(self.todolist.is_dirty()) self.assertEqual(self.output, "") self.assertEqual(self.errors, "Invalid priority given.\n") def test_invalid9(self): """ Test that there's only one capital surrounded by non-word characters that makes up a priority. """ command = PriorityCommand(["2", "Aa"], self.todolist, self.out, self.error) command.execute() self.assertFalse(self.todolist.is_dirty()) self.assertEqual(self.output, "") self.assertEqual(self.errors, "Invalid priority given.\n") def test_empty(self): command = PriorityCommand([], self.todolist, self.out, self.error) command.execute() self.assertFalse(self.todolist.is_dirty()) self.assertFalse(self.output) self.assertEqual(self.errors, command.usage() + "\n") def test_help(self): command = PriorityCommand(["help"], self.todolist, self.out, self.error) command.execute() self.assertEqual(self.output, "") self.assertEqual(self.errors, command.usage() + "\n\n" + command.help() + "\n")
class TodoListCleanDependencyTester(TopydoTest): def setUp(self): super(TodoListCleanDependencyTester, self).setUp() self.todolist = TodoList([]) self.todolist.add("Bar p:1") self.todolist.add("Baz p:1 id:2") self.todolist.add("Buzz p:2") def test_clean_dependencies(self): self.todolist.clean_dependencies() self.assertFalse(self.todolist.todo(1).has_tag("p")) self.assertFalse(self.todolist.todo(2).has_tag("p")) self.assertTrue(self.todolist.todo(2).has_tag("id", "2")) self.assertTrue(self.todolist.todo(3).has_tag("p", "2"))
class TodoListDependencyTester(TopydoTest): def setUp(self): super().setUp() self.todolist = TodoList([]) self.todolist.add("Foo id:1") self.todolist.add("Bar p:1") self.todolist.add("Baz p:1 id:2") self.todolist.add("Buzz p:2") self.todolist.add("Fnord") self.todolist.add("Something with +Project") self.todolist.add("Another one with +Project") self.todolist.add("Todo with +AnotherProject") self.todolist.add("Todo without children id:3") self.todolist.add("Orphan p:4") def test_check_dep(self): children = self.todolist.children(self.todolist.todo(1)) self.assertEqual(sorted([todo.source() for todo in children]), sorted(['Bar p:1', 'Baz p:1 id:2', 'Buzz p:2'])) children = self.todolist.children(self.todolist.todo(1), True) self.assertEqual(sorted([todo.source() for todo in children]), sorted(['Bar p:1', 'Baz p:1 id:2'])) children = self.todolist.children(self.todolist.todo(3)) self.assertEqual(sorted([todo.source() for todo in children]), ['Buzz p:2']) parents = self.todolist.parents(self.todolist.todo(4)) self.assertEqual(sorted([todo.source() for todo in parents]), sorted(['Foo id:1', 'Baz p:1 id:2'])) parents = self.todolist.parents(self.todolist.todo(4), True) self.assertEqual(sorted([todo.source() for todo in parents]), ['Baz p:1 id:2']) self.assertEqual(self.todolist.children(self.todolist.todo(2)), []) self.assertEqual(self.todolist.parents(self.todolist.todo(1)), []) def test_add_dep1(self): todo4 = self.todolist.todo(4) todo5 = self.todolist.todo(5) self.todolist.add_dependency(todo5, todo4) self.assertTrue(todo5.has_tag('id', '5')) self.assertTrue(todo4.has_tag('p', '5')) def test_add_dep2(self): """ Make sure that previous add_dependency invocation stored the edge_id properly. """ todo1 = self.todolist.todo(1) todo4 = self.todolist.todo(4) todo5 = self.todolist.todo(5) self.todolist.add_dependency(todo5, todo4) self.todolist.add_dependency(todo4, todo1) self.assertTrue(todo4.has_tag('id', '6')) self.assertTrue(todo1.has_tag('p', '6')) def test_add_dep3(self): """ Test that projects are not added double. """ todo6 = self.todolist.todo(6) todo7 = self.todolist.todo(7) projects = todo7.projects().copy() self.todolist.add_dependency(todo6, todo7) self.assertEqual(projects, todo7.projects()) def test_add_dep4(self): """ Test that a new project is added to the sub todo. """ config("test/data/config3") todo6 = self.todolist.todo(6) todo8 = self.todolist.todo(8) self.todolist.add_dependency(todo6, todo8) self.assertEqual(set(["Project", "AnotherProject"]), todo8.projects()) def test_remove_dep1(self): from_todo = self.todolist.todo(3) to_todo = self.todolist.todo(4) self.todolist.remove_dependency(from_todo, to_todo) self.assertFalse(from_todo.has_tag('id')) self.assertFalse(to_todo.has_tag('p')) self.assertFalse(self.todolist.todo_by_dep_id('2')) def test_remove_dep2(self): old = str(self.todolist) from_todo = self.todolist.todo(1) to_todo = self.todolist.todo(4) self.todolist.remove_dependency(from_todo, to_todo) self.assertEqual(str(self.todolist), old) self.assertTrue(self.todolist.todo_by_dep_id('1')) self.assertTrue(self.todolist.todo_by_dep_id('2')) self.assertTrue(self.todolist.todo_by_dep_id('3')) def test_remove_dep3(self): """ Try to remove non-existing dependency. """ old = str(self.todolist) from_todo = self.todolist.todo(4) to_todo = self.todolist.todo(1) self.todolist.remove_dependency(from_todo, to_todo) self.assertEqual(str(self.todolist), old) self.assertTrue(self.todolist.todo_by_dep_id('1')) self.assertTrue(self.todolist.todo_by_dep_id('2')) self.assertTrue(self.todolist.todo_by_dep_id('3')) def test_remove_todo_check_children(self): todo = self.todolist.todo(2) self.todolist.delete(todo) todo = self.todolist.todo(2) self.assertTrue(self.todolist.children(todo)) def test_remove_task(self): todo = self.todolist.todo(3) self.todolist.delete(todo) self.assertFalse(todo.has_tag('p', '2')) self.assertFalse(self.todolist.todo_by_dep_id('2')) todo = self.todolist.todo(1) children = self.todolist.children(todo) self.assertEqual([t.source() for t in children], ['Bar p:1']) def test_add_double_dep(self): todo1 = self.todolist.todo(1) todo2 = self.todolist.todo(2) self.todolist.add_dependency(todo1, todo2) self.assertEqual(todo1.source(), 'Foo id:1') self.assertEqual(todo2.source(), 'Bar p:1') def test_todo_by_dep_id(self): """ Tests that todos can be retrieved by their id tag. """ todolist = TodoList([]) todolist.add("(C) Foo id:1") self.assertTrue(todolist.todo_by_dep_id('1')) self.assertFalse(todolist.todo_by_dep_id('2')) def test_add_after_dependencies(self): """ Test that information is properly stored after dependency related information was retrieved from the todo list. """ todo = self.todolist.todo(1) self.todolist.parents(todo) self.todolist.add('New dependency id:99') self.todolist.add('Child p:99') self.assertTrue(self.todolist.dirty) self.assertTrue(self.todolist.todo_by_dep_id('99')) def test_delete01(self): """ Check that dependency tags are cleaned up. """ todo = self.todolist.todo(4) self.todolist.delete(todo, p_leave_tags=False) self.assertTrue(self.todolist.is_dirty()) self.assertEqual(self.todolist.todo(3).source(), "Baz p:1") def test_delete02(self): """ Check that dependency tags are left when requested. """ todo = self.todolist.todo(4) self.todolist.delete(todo, p_leave_tags=True) self.assertTrue(self.todolist.is_dirty()) self.assertEqual(self.todolist.todo(3).source(), "Baz p:1 id:2") def test_delete03(self): """ Check that dependency tags are left when requested. """ todo = self.todolist.todo(3) self.todolist.delete(todo, p_leave_tags=True) self.assertTrue(self.todolist.is_dirty()) self.assertEqual(self.todolist.todo(3).source(), "Buzz p:2")
class TodoListCleanDependencyTester(TopydoTest): """ Tests for cleaning up the graph: * Transitive reduction * Remove obsolete id: tags * Remove obsolete p: tags """ def setUp(self): super().setUp() self.todolist = TodoList([]) def test_clean_dependencies1(self): """ Clean p: tags from non-existing parent items. """ self.todolist.add("Bar p:1") self.todolist.add("Baz p:1 id:2") self.todolist.add("Buzz p:2") self.todolist.clean_dependencies() self.assertFalse(self.todolist.todo(1).has_tag('p')) self.assertFalse(self.todolist.todo(2).has_tag('p')) self.assertTrue(self.todolist.todo(2).has_tag('id', '2')) self.assertTrue(self.todolist.todo(3).has_tag('p', '2')) def test_clean_dependencies2(self): """ Clean p: items when siblings are still connected to parent. """ self.todolist.add("Foo id:1") self.todolist.add("Bar p:1") self.todolist.add("Baz p:1 id:2") self.todolist.add("Buzz p:1 p:2") self.todolist.clean_dependencies() self.assertFalse(self.todolist.todo(4).has_tag('p', '1')) self.assertTrue(self.todolist.todo(1).has_tag('id', '1')) self.assertTrue(self.todolist.todo(2).has_tag('p', '1')) def test_clean_dependencies3(self): """ Clean id: tags from todo items without child todos. """ self.todolist.add("Foo id:1") self.todolist.clean_dependencies() self.assertFalse(self.todolist.todo(1).has_tag('id')) self.assertFalse(self.todolist.todo_by_dep_id('1'))
class TodoListDependencyTester(TopydoTest): def setUp(self): super(TodoListDependencyTester, self).setUp() self.todolist = TodoList([]) self.todolist.add("Foo id:1") self.todolist.add("Bar p:1") self.todolist.add("Baz p:1 id:2") self.todolist.add("Buzz p:2") self.todolist.add("Fnord") self.todolist.add("Something with +Project") self.todolist.add("Another one with +Project") self.todolist.add("Todo with +AnotherProject") self.todolist.add("Todo without children id:3") def test_check_dep(self): children = self.todolist.children(self.todolist.todo(1)) self.assertEqual(sorted([todo.source() for todo in children]), sorted(["Bar p:1", "Baz p:1 id:2", "Buzz p:2"])) children = self.todolist.children(self.todolist.todo(1), True) self.assertEqual(sorted([todo.source() for todo in children]), sorted(["Bar p:1", "Baz p:1 id:2"])) children = self.todolist.children(self.todolist.todo(3)) self.assertEqual(sorted([todo.source() for todo in children]), ["Buzz p:2"]) parents = self.todolist.parents(self.todolist.todo(4)) self.assertEqual(sorted([todo.source() for todo in parents]), sorted(["Foo id:1", "Baz p:1 id:2"])) parents = self.todolist.parents(self.todolist.todo(4), True) self.assertEqual(sorted([todo.source() for todo in parents]), ["Baz p:1 id:2"]) self.assertEqual(self.todolist.children(self.todolist.todo(2)), []) self.assertEqual(self.todolist.parents(self.todolist.todo(1)), []) def test_add_dep1(self): todo4 = self.todolist.todo(4) todo5 = self.todolist.todo(5) self.todolist.add_dependency(todo5, todo4) self.assertTrue(todo5.has_tag("id", "4")) self.assertTrue(todo4.has_tag("p", "4")) def test_add_dep2(self): """ Make sure that previous add_dependency invocation stored the edge_id properly. """ todo1 = self.todolist.todo(1) todo4 = self.todolist.todo(4) todo5 = self.todolist.todo(5) self.todolist.add_dependency(todo5, todo4) self.todolist.add_dependency(todo4, todo1) self.assertTrue(todo4.has_tag("id", "5")) self.assertTrue(todo1.has_tag("p", "5")) def test_add_dep3(self): """ Test that projects are not added double. """ todo6 = self.todolist.todo(6) todo7 = self.todolist.todo(7) projects = todo7.projects().copy() self.todolist.add_dependency(todo6, todo7) self.assertEqual(projects, todo7.projects()) def test_add_dep4(self): """ Test that a new project is added to the sub todo. """ config("test/data/config3") todo6 = self.todolist.todo(6) todo8 = self.todolist.todo(8) self.todolist.add_dependency(todo6, todo8) self.assertEqual(set(["Project", "AnotherProject"]), todo8.projects()) def test_remove_dep1(self): from_todo = self.todolist.todo(3) to_todo = self.todolist.todo(4) self.todolist.remove_dependency(from_todo, to_todo) self.assertFalse(from_todo.has_tag("id")) self.assertFalse(to_todo.has_tag("p")) def test_remove_dep2(self): old = str(self.todolist) from_todo = self.todolist.todo(1) to_todo = self.todolist.todo(4) self.todolist.remove_dependency(from_todo, to_todo) self.assertEqual(str(self.todolist), old) def test_remove_todo_check_children(self): todo = self.todolist.todo(2) self.todolist.delete(todo) todo = self.todolist.todo(2) self.assertTrue(self.todolist.children(todo)) def test_remove_task(self): todo = self.todolist.todo(3) self.todolist.delete(todo) self.assertFalse(todo.has_tag("p", "2")) todo = self.todolist.todo(1) children = self.todolist.children(todo) self.assertEqual([t.source() for t in children], ["Bar p:1"]) def test_add_double_dep(self): todo1 = self.todolist.todo(1) todo2 = self.todolist.todo(2) self.todolist.add_dependency(todo1, todo2) self.assertEqual(todo1.source(), "Foo id:1") self.assertEqual(todo2.source(), "Bar p:1") def test_todo_by_dep_id(self): """ Tests that todos can be retrieved by their id tag. """ todolist = TodoList([]) todolist.add("(C) Foo id:1") self.assertTrue(todolist.todo_by_dep_id("1")) self.assertFalse(todolist.todo_by_dep_id("2"))
class DepCommandTest(CommandTest): def setUp(self): super().setUp() todos = [ "Foo id:1", "Bar p:1", "Baz p:1", "Fnord id:2", "Garbage dependency p:99", "Fart p:2", ] self.todolist = TodoList(todos) def test_add1(self): command = DepCommand(["add", "1", "to", "4"], self.todolist, self.out, self.error) command.execute() self.assertTrue(self.todolist.is_dirty()) self.assertTrue(self.todolist.todo(4).has_tag('p', '1')) self.assertEqual(self.output, "") self.assertEqual(self.errors, "") def test_add2(self): command = DepCommand(["add", "1", "4"], self.todolist, self.out, self.error) command.execute() self.assertTrue(self.todolist.is_dirty()) self.assertTrue(self.todolist.todo(4).has_tag('p', '1')) self.assertEqual(self.output, "") self.assertEqual(self.errors, "") def test_add3(self): command = DepCommand(["add", "99", "3"], self.todolist, self.out, self.error) command.execute() self.assertFalse(self.todolist.is_dirty()) self.assertEqual(self.output, "") self.assertEqual(self.errors, "Invalid todo number given.\n") def test_add4(self): command = DepCommand(["add", "A", "3"], self.todolist, self.out, self.error) command.execute() self.assertFalse(self.todolist.is_dirty()) self.assertEqual(self.output, "") self.assertEqual(self.errors, "Invalid todo number given.\n") def test_add5(self): command = DepCommand(["add", "1"], self.todolist, self.out, self.error) command.execute() self.assertFalse(self.todolist.is_dirty()) self.assertEqual(self.output, "") self.assertEqual(self.errors, command.usage() + "\n") def test_add6(self): command = DepCommand(["add", "1", "after", "4"], self.todolist, self.out, self.error) command.execute() self.assertTrue(self.todolist.is_dirty()) self.assertTrue(self.todolist.todo(4).has_tag('p', '1')) self.assertEqual(self.output, "") self.assertEqual(self.errors, "") def test_add7(self): command = DepCommand(["add", "1", "before", "4"], self.todolist, self.out, self.error) command.execute() self.assertTrue(self.todolist.is_dirty()) self.assertTrue(self.todolist.todo(1).has_tag('p', '2')) self.assertEqual(self.output, "") self.assertEqual(self.errors, "") def test_add8(self): command = DepCommand(["add", "1", "partof", "4"], self.todolist, self.out, self.error) command.execute() self.assertTrue(self.todolist.is_dirty()) self.assertTrue(self.todolist.todo(1).has_tag('p', '2')) self.assertEqual(self.output, "") self.assertEqual(self.errors, "") def test_add9(self): command = DepCommand(["add", "Foo", "to", "4"], self.todolist, self.out, self.error) command.execute() self.assertTrue(self.todolist.is_dirty()) self.assertTrue(self.todolist.todo(4).has_tag('p', '1')) self.assertEqual(self.output, "") self.assertEqual(self.errors, "") def add_parentsof_helper(self, p_args): command = DepCommand(p_args, self.todolist, self.out, self.error) command.execute() self.assertTrue(self.todolist.is_dirty()) self.assertTrue(self.todolist.todo(4).has_tag('p', '1')) self.assertEqual(self.output, "") self.assertEqual(self.errors, "") def test_add10(self): self.add_parentsof_helper(["add", "4", "parents-of", "2"]) def test_add11(self): self.add_parentsof_helper(["add", "4", "parent-of", "2"]) def test_add12(self): self.add_parentsof_helper(["add", "4", "parentsof", "2"]) def test_add13(self): self.add_parentsof_helper(["add", "4", "parentof", "2"]) def test_add14(self): command = DepCommand(["add", "4", "parents-of", "5"], self.todolist, self.out, self.error) command.execute() self.assertFalse(self.todolist.is_dirty()) self.assertEqual(self.output, "") self.assertEqual(self.errors, "") def add_childrenof_helper(self, p_args): command = DepCommand(p_args, self.todolist, self.out, self.error) command.execute() self.assertTrue(self.todolist.is_dirty()) self.assertTrue(self.todolist.todo(2).has_tag('p', '2')) self.assertTrue(self.todolist.todo(3).has_tag('p', '2')) self.assertEqual(self.output, "") self.assertEqual(self.errors, "") def test_add15(self): self.add_childrenof_helper(["add", "4", "children-of", "1"]) def test_add16(self): self.add_childrenof_helper(["add", "4", "child-of", "1"]) def test_add17(self): self.add_childrenof_helper(["add", "4", "childrenof", "1"]) def test_add18(self): self.add_childrenof_helper(["add", "4", "childof", "1"]) def test_add19(self): command = DepCommand(["add", "4", "children-of", "5"], self.todolist, self.out, self.error) command.execute() self.assertFalse(self.todolist.is_dirty()) self.assertEqual(self.output, "") self.assertEqual(self.errors, "") def rm_helper(self, p_args): """ Helper function that checks the removal of the dependency from todo 1 to todo 3. """ command = DepCommand(p_args, self.todolist, self.out, self.error) command.execute() self.assertTrue(self.todolist.is_dirty()) self.assertTrue(self.todolist.todo(1).has_tag('id', '1')) self.assertFalse(self.todolist.todo(3).has_tag('p', '1')) self.assertEqual(self.output, "") self.assertEqual(self.errors, "") def test_rm1(self): self.rm_helper(["rm", "1", "to", "3"]) def test_rm2(self): self.rm_helper(["rm", "1", "3"]) def test_del1(self): self.rm_helper(["del", "1", "to", "3"]) def test_del2(self): self.rm_helper(["del", "1", "3"]) def test_rm3(self): command = DepCommand(["rm", "99", "3"], self.todolist, self.out, self.error) command.execute() self.assertFalse(self.todolist.is_dirty()) self.assertEqual(self.output, "") self.assertEqual(self.errors, "Invalid todo number given.\n") def test_rm4(self): command = DepCommand(["rm", "A", "3"], self.todolist, self.out, self.error) command.execute() self.assertFalse(self.todolist.is_dirty()) self.assertEqual(self.output, "") self.assertEqual(self.errors, "Invalid todo number given.\n") def test_rm5(self): command = DepCommand(["rm", "1"], self.todolist, self.out, self.error) command.execute() self.assertFalse(self.todolist.is_dirty()) self.assertEqual(self.output, "") self.assertEqual(self.errors, command.usage() + "\n") def test_ls1(self): command = DepCommand(["ls", "1", "to"], self.todolist, self.out, self.error) command.execute() self.assertFalse(self.todolist.is_dirty()) self.assertEqual(self.output, "| 2| Bar p:1\n| 3| Baz p:1\n") self.assertEqual(self.errors, "") def test_ls2(self): command = DepCommand(["ls", "99", "to"], self.todolist, self.out, self.error) command.execute() self.assertFalse(self.todolist.is_dirty()) self.assertEqual(self.output, "") self.assertEqual(self.errors, "Invalid todo number given.\n") def test_ls3(self): command = DepCommand(["ls", "to", "3"], self.todolist, self.out, self.error) command.execute() self.assertFalse(self.todolist.is_dirty()) self.assertEqual(self.output, "| 1| Foo id:1\n") self.assertEqual(self.errors, "") def test_ls4(self): command = DepCommand(["ls", "to", "99"], self.todolist, self.out, self.error) command.execute() self.assertFalse(self.todolist.is_dirty()) self.assertEqual(self.output, "") self.assertEqual(self.errors, "Invalid todo number given.\n") def test_ls5(self): command = DepCommand(["ls", "1"], self.todolist, self.out, self.error) command.execute() self.assertFalse(self.todolist.is_dirty()) self.assertEqual(self.output, "") self.assertEqual(self.errors, command.usage() + "\n") def test_ls6(self): command = DepCommand(["ls"], self.todolist, self.out, self.error) command.execute() self.assertFalse(self.todolist.is_dirty()) self.assertFalse(self.output) self.assertEqual(self.errors, command.usage() + "\n") def test_ls7(self): command = DepCommand(["ls", "top", "99"], self.todolist, self.out, self.error) command.execute() self.assertFalse(self.todolist.is_dirty()) self.assertEqual(self.output, "") self.assertEqual(self.errors, command.usage() + "\n") def gc_helper(self, p_subcommand): command = DepCommand([p_subcommand], self.todolist, self.out, self.error) command.execute() self.assertTrue(self.todolist.is_dirty()) self.assertFalse(self.output) self.assertFalse(self.errors) self.assertFalse(self.todolist.todo(5).has_tag('p', '99')) def test_clean(self): self.gc_helper("clean") def test_gc(self): self.gc_helper("gc") def test_invalid_subsubcommand(self): command = DepCommand(["foo"], self.todolist, self.out, self.error) command.execute() self.assertFalse(self.output) self.assertEqual(self.errors, command.usage() + "\n") self.assertFalse(self.todolist.is_dirty()) def test_no_subsubcommand(self): command = DepCommand([], self.todolist, self.out, self.error) command.execute() self.assertFalse(self.output) self.assertEqual(self.errors, command.usage() + "\n") self.assertFalse(self.todolist.is_dirty()) def test_help(self): command = DepCommand(["help"], self.todolist, self.out, self.error) command.execute() self.assertEqual(self.output, "") self.assertEqual(self.errors, command.usage() + "\n\n" + command.help() + "\n")
def setUp(self): super().setUp() self.todolist = TodoList([])
class EditCommandTest(CommandTest): def setUp(self): super().setUp() todos = [ "Foo id:1", "Bar p:1 @test", "Baz @test", u"Fo\u00f3B\u0105\u017a", ] self.todolist = TodoList(todos) @mock.patch('topydo.commands.EditCommand._is_edited') @mock.patch('topydo.commands.EditCommand.EditCommand._todos_from_temp') @mock.patch('topydo.commands.EditCommand.EditCommand._open_in_editor') def test_edit01(self, mock_open_in_editor, mock_todos_from_temp, mock_is_edited): """ Preserve dependencies after editing. """ mock_open_in_editor.return_value = 0 mock_todos_from_temp.return_value = [Todo('Foo id:1')] mock_is_edited.return_value = True command = EditCommand(["1"], self.todolist, self.out, self.error, None) command.execute() self.assertEqual(self.errors, "") self.assertTrue(self.todolist.dirty) self.assertEqual(self.todolist.print_todos(), u"Bar p:1 @test\nBaz @test\nFo\u00f3B\u0105\u017a\nFoo id:1") @mock.patch('topydo.commands.EditCommand._is_edited') @mock.patch('topydo.commands.EditCommand.EditCommand._todos_from_temp') @mock.patch('topydo.commands.EditCommand.EditCommand._open_in_editor') def test_edit02(self, mock_open_in_editor, mock_todos_from_temp, mock_is_edited): """ Edit some todo. """ mock_open_in_editor.return_value = 0 mock_todos_from_temp.return_value = [Todo('Lazy Cat')] mock_is_edited.return_value = True command = EditCommand(["Bar"], self.todolist, self.out, self.error, None) command.execute() self.assertTrue(self.todolist.dirty) self.assertEqual(self.errors, "") self.assertEqual(self.todolist.print_todos(), u"Foo id:1\nBaz @test\nFo\u00f3B\u0105\u017a\nLazy Cat") def test_edit03(self): """ Throw an error after invalid todo number given as argument. """ command = EditCommand(["FooBar"], self.todolist, self.out, self.error, None) command.execute() self.assertFalse(self.todolist.dirty) self.assertEqual(self.errors, "Invalid todo number given.\n") def test_edit04(self): """ Throw an error with pointing invalid argument. """ command = EditCommand(["Bar", "5"], self.todolist, self.out, self.error, None) command.execute() self.assertFalse(self.todolist.dirty) self.assertEqual(self.errors, "Invalid todo number given: 5.\n") def test_edit05(self): """ Throw an error with invalid argument containing special characters. """ command = EditCommand([u"Fo\u00d3B\u0105r", "Bar"], self.todolist, self.out, self.error, None) command.execute() self.assertFalse(self.todolist.dirty) self.assertEqual(self.errors, u"Invalid todo number given: Fo\u00d3B\u0105r.\n") @mock.patch('topydo.commands.EditCommand._is_edited') @mock.patch('topydo.commands.EditCommand.EditCommand._todos_from_temp') @mock.patch('topydo.commands.EditCommand.EditCommand._open_in_editor') def test_edit06(self, mock_open_in_editor, mock_todos_from_temp, mock_is_edited): """ Edit todo with special characters. """ mock_open_in_editor.return_value = 0 mock_todos_from_temp.return_value = [Todo('Lazy Cat')] mock_is_edited.return_value = True command = EditCommand([u"Fo\u00f3B\u0105\u017a"], self.todolist, self.out, self.error, None) command.execute() self.assertTrue(self.todolist.dirty) self.assertEqual(self.errors, "") self.assertEqual(self.todolist.print_todos(), u"Foo id:1\nBar p:1 @test\nBaz @test\nLazy Cat") @mock.patch('topydo.commands.EditCommand._is_edited') @mock.patch('topydo.commands.EditCommand.EditCommand._todos_from_temp') @mock.patch('topydo.commands.EditCommand.EditCommand._open_in_editor') def test_edit07(self, mock_open_in_editor, mock_todos_from_temp, mock_is_edited): """ Don't perform write if tempfile is unchanged """ mock_open_in_editor.return_value = 0 mock_todos_from_temp.return_value = [Todo('Only one line')] mock_is_edited.return_value = False command = EditCommand(["1", "Bar"], self.todolist, self.out, self.error, None) command.execute() self.assertFalse(self.todolist.dirty) self.assertEqual(self.errors, "Editing aborted. Nothing to do.\n") self.assertEqual(self.todolist.print_todos(), u"Foo id:1\nBar p:1 @test\nBaz @test\nFo\u00f3B\u0105\u017a") @mock.patch('topydo.commands.EditCommand._is_edited') @mock.patch('topydo.commands.EditCommand.EditCommand._todos_from_temp') @mock.patch('topydo.commands.EditCommand.EditCommand._open_in_editor') def test_edit_expr(self, mock_open_in_editor, mock_todos_from_temp, mock_is_edited): """ Edit todos matching expression. """ mock_open_in_editor.return_value = 0 mock_todos_from_temp.return_value = [Todo('Lazy Cat'), Todo('Lazy Dog')] mock_is_edited.return_value = True command = EditCommand(["-e", "@test"], self.todolist, self.out, self.error, None) command.execute() expected = u"| 3| Lazy Cat\n| 4| Lazy Dog\n" self.assertEqual(self.errors, "") self.assertTrue(self.todolist.dirty) self.assertEqual(self.output, expected) self.assertEqual(self.todolist.print_todos(), u"Foo id:1\nFo\u00f3B\u0105\u017a\nLazy Cat\nLazy Dog") @mock.patch.dict(os.environ, {'EDITOR': 'vi'}) @mock.patch('topydo.commands.EditCommand.check_call') def test_edit_archive(self, mock_call): """ Edit archive file. """ mock_call.return_value = 0 archive = config().archive() command = EditCommand(["-d"], self.todolist, self.out, self.error, None) command.execute() self.assertEqual(self.errors, "") mock_call.assert_called_once_with(['vi', archive]) @mock.patch.dict(os.environ, {'EDITOR': 'vi'}) @mock.patch('topydo.commands.EditCommand.check_call') def test_edit_todotxt(self, mock_call): """ Edit todo file. """ mock_call.return_value = 0 todotxt = config().todotxt() result = self.todolist.print_todos() # copy TodoList content *before* executing command command = EditCommand([], self.todolist, self.out, self.error, None) command.execute() self.assertEqual(self.errors, "") self.assertEqual(self.todolist.print_todos(), result) mock_call.assert_called_once_with(['vi', todotxt]) @mock.patch.dict(os.environ, {'EDITOR': 'vi'}) @mock.patch.dict(os.environ, {'TOPYDO_EDITOR': 'nano'}) @mock.patch('topydo.commands.EditCommand.check_call') def test_edit_editor1(self, mock_call): """ $TOPYDO_EDITOR overrides $EDITOR """ mock_call.return_value = 0 todotxt = config().todotxt() command = EditCommand([], self.todolist, self.out, self.error, None) command.execute() self.assertEqual(self.errors, "") mock_call.assert_called_once_with(['nano', todotxt]) @mock.patch.dict(os.environ, {'EDITOR': 'vi'}) @mock.patch.dict(os.environ, {'TOPYDO_EDITOR': 'nano'}) @mock.patch('topydo.commands.EditCommand.check_call') def test_edit_editor2(self, mock_call): """ $TOPYDO_EDITOR overrides $EDITOR """ mock_call.return_value = 0 todotxt = config().todotxt() command = EditCommand([], self.todolist, self.out, self.error, None) command.execute() self.assertEqual(self.errors, "") mock_call.assert_called_once_with(['nano', todotxt]) @mock.patch.dict(os.environ, {'EDITOR': 'vi'}) @mock.patch.dict(os.environ, {'TOPYDO_EDITOR': 'nano'}) @mock.patch('topydo.commands.EditCommand.check_call') def test_edit_editor3(self, mock_call): """ Editor on commandline overrides $TOPYDO_EDITOR """ mock_call.return_value = 0 command = EditCommand(["-E", "foo"], self.todolist, self.out, self.error, None) command.execute() self.assertEqual(self.errors, "") mock_call.assert_called_once_with(['foo', config().todotxt()]) @mock.patch.dict(os.environ, {'EDITOR': 'vi'}) @mock.patch.dict(os.environ, {'TOPYDO_EDITOR': 'nano'}) @mock.patch('topydo.commands.EditCommand.check_call') def test_edit_editor4(self, mock_call): """ Editor in configuration file is overridden by $TOPYDO_EDITOR """ mock_call.return_value = 0 config(p_overrides={('edit', 'editor'): 'foo'}) command = EditCommand([], self.todolist, self.out, self.error, None) command.execute() self.assertEqual(self.errors, "") mock_call.assert_called_once_with(['nano', config().todotxt()]) @mock.patch.dict(os.environ, {'EDITOR': 'vi'}) @mock.patch('topydo.commands.EditCommand.check_call') def test_edit_editor5(self, mock_call): """ Editor in configuration file overrides $EDITOR """ mock_call.return_value = 0 config(p_overrides={('edit', 'editor'): 'foo'}) command = EditCommand([], self.todolist, self.out, self.error, None) command.execute() self.assertEqual(self.errors, "") mock_call.assert_called_once_with(['foo', config().todotxt()]) @mock.patch.dict(os.environ, {'EDITOR': ''}) @mock.patch('topydo.commands.EditCommand.check_call') def test_edit_editor6(self, mock_call): """ Ultimate fallback is vi """ mock_call.return_value = 0 command = EditCommand([], self.todolist, self.out, self.error, None) command.execute() self.assertEqual(self.errors, "") mock_call.assert_called_once_with(['vi', config().todotxt()]) def test_edit_name(self): name = EditCommand.name() self.assertEqual(name, 'edit') def test_help(self): command = EditCommand(["help"], self.todolist, self.out, self.error, None) command.execute() self.assertEqual(self.output, "") self.assertEqual(self.errors, command.usage() + "\n\n" + command.help() + "\n")
class RevertCommandTest(CommandTest): def setUp(self): super().setUp() todos = [ "Foo", "Bar", "Baz", ] self.todolist = TodoList(todos) self.today = date.today() self.tmp_name = str(uuid4().hex.upper()[0:6]) archive_filename = tempfile.gettempdir() + os.sep + self.tmp_name + '_archive' todo_filename = tempfile.gettempdir() + os.sep + self.tmp_name + '_todo' config(p_overrides={('topydo', 'archive_filename'): archive_filename, ('topydo', 'filename'): todo_filename, ('topydo', 'backup_count'): '5'}) self.archive_file = TodoFile(archive_filename) self.archive = TodoList([]) def test_revert01(self): backup = ChangeSet(p_label=['do 1']) backup.add_todolist(self.todolist) backup.add_archive(self.archive) backup.timestamp = '1' command = DoCommand(["1"], self.todolist, self.out, self.error, None) command.execute() archive_command = ArchiveCommand(self.todolist, self.archive) archive_command.execute() self.archive_file.write(self.archive.print_todos()) backup.save(self.todolist) self.assertEqual(self.archive.print_todos(), "x {} Foo".format(self.today)) self.assertEqual(self.todolist.print_todos(), "Bar\nBaz") revert_command = RevertCommand([], self.todolist, self.out, self.error, None) revert_command.execute() result = TodoList(self.archive_file.read()).print_todos() self.assertEqual(self.errors, "") self.assertTrue(self.output.endswith("Successfully reverted: do 1\n")) self.assertEqual(result, "") self.assertEqual(self.todolist.print_todos(), "Foo\nBar\nBaz") def test_revert02(self): backup = ChangeSet(self.todolist, self.archive, ['do 1']) backup.timestamp = '1' command1 = DoCommand(["1"], self.todolist, self.out, self.error, None) command1.execute() archive_command1 = ArchiveCommand(self.todolist, self.archive) archive_command1.execute() self.archive_file.write(self.archive.print_todos()) backup.save(self.todolist) backup = ChangeSet(self.todolist, self.archive, ['do Bar']) backup.timestamp = '2' command2 = DoCommand(["Bar"], self.todolist, self.out, self.error, None) command2.execute() archive_command2 = ArchiveCommand(self.todolist, self.archive) archive_command2.execute() self.archive_file.write(self.archive.print_todos()) backup.save(self.todolist) self.assertEqual(self.archive.print_todos(), "x {t} Foo\nx {t} Bar".format(t=self.today)) self.assertEqual(self.todolist.print_todos(), "Baz") revert_command = RevertCommand([], self.todolist, self.out, self.error, None) revert_command.execute() result = TodoList(self.archive_file.read()).print_todos() self.assertEqual(self.errors, "") self.assertTrue(self.output.endswith("Successfully reverted: do Bar\n")) self.assertEqual(result, "x {} Foo".format(self.today)) self.assertEqual(self.todolist.print_todos(), "Bar\nBaz") def test_revert03(self): """ Test behavior when no backup is found """ command = RevertCommand([], self.todolist, self.out, self.error) command.execute() self.assertEqual(self.errors, "No backup was found for the current state of {}\n".format(config().todotxt())) def test_revert04(self): """ Test trimming of the backup_file """ backup = ChangeSet(self.todolist, self.archive, ['add One']) backup.timestamp = '1' command1 = AddCommand(["One"], self.todolist, self.out, self.error, None) command1.execute() backup.save(self.todolist) backup = ChangeSet(self.todolist, self.archive, ['add Two']) backup.timestamp = '2' command2 = AddCommand(["Two"], self.todolist, self.out, self.error, None) command2.execute() backup.save(self.todolist) backup = ChangeSet(self.todolist, self.archive, ['add Three']) backup.timestamp = '3' command3 = AddCommand(["Three"], self.todolist, self.out, self.error, None) command3.execute() backup.save(self.todolist) backup = ChangeSet(self.todolist, self.archive, ['add Four']) backup.timestamp = '4' command4 = AddCommand(["Four"], self.todolist, self.out, self.error, None) command4.execute() backup.save(self.todolist) backup = ChangeSet(self.todolist, self.archive, ['add Five']) backup.timestamp = '5' command5 = AddCommand(["Five"], self.todolist, self.out, self.error, None) command5.execute() backup.save(self.todolist) result = len(ChangeSet().backup_dict.keys()) self.assertEqual(result, 6) backup = ChangeSet(self.todolist, self.archive, ['add Six']) backup.timestamp = '6' command6 = AddCommand(["Six"], self.todolist, self.out, self.error, None) command6.execute() backup.save(self.todolist) backup = ChangeSet(self.todolist, self.archive, ['add Seven']) backup.timestamp = '7' command7 = AddCommand(["Seven"], self.todolist, self.out, self.error, None) command7.execute() backup.save(self.todolist) result = len(ChangeSet().backup_dict.keys()) self.assertEqual(result, 6) revert_command = RevertCommand([], self.todolist, self.out, self.error, None) revert_command.execute() backup = ChangeSet() changesets = list(backup.backup_dict.keys()) changesets.remove('index') index_timestamps = [change[0] for change in backup._get_index()] result = list(set(index_timestamps) - set(changesets)) self.assertEqual(len(changesets), 4) self.assertEqual(result, []) self.assertEqual(self.errors, "") self.assertTrue(self.output.endswith("Successfully reverted: add Seven\n")) def test_revert05(self): """ Test for possible backup collisions """ backup = ChangeSet(self.todolist, self.archive, ['add One']) backup.timestamp = '1' command1 = AddCommand(["One"], self.todolist, self.out, self.error, None) command1.execute() backup.save(self.todolist) backup = ChangeSet(self.todolist, self.archive, ['add Two']) backup.timestamp = '2' command2 = AddCommand(["Two"], self.todolist, self.out, self.error, None) command2.execute() backup.save(self.todolist) backup = ChangeSet(self.todolist, self.archive, ['add Three']) backup.timestamp = '3' command3 = AddCommand(["Three"], self.todolist, self.out, self.error, None) command3.execute() backup.save(self.todolist) backup = ChangeSet(self.todolist, self.archive, ['delete Three']) backup.timestamp = '4' command4 = DeleteCommand(["Three"], self.todolist, self.out, self.error, None) command4.execute() backup.save(self.todolist) backup = ChangeSet(self.todolist, self.archive, ['add Four']) backup.timestamp = '5' command4 = AddCommand(["Four"], self.todolist, self.out, self.error, None) command4.execute() backup.save(self.todolist) revert_command = RevertCommand([], self.todolist, self.out, self.error, None) revert_command.execute() self.assertEqual(self.errors, "") self.assertTrue(self.output.endswith("Successfully reverted: add Four\n")) self.assertEqual(self.todolist.print_todos(), "Foo\nBar\nBaz\n{t} One\n{t} Two".format(t=self.today)) revert_command = RevertCommand([], self.todolist, self.out, self.error, None) revert_command.execute() self.assertEqual(self.errors, "") self.assertTrue(self.output.endswith("Successfully reverted: delete Three\n")) self.assertEqual(self.todolist.print_todos(), "Foo\nBar\nBaz\n{t} One\n{t} Two\n{t} Three".format(t=self.today)) revert_command = RevertCommand([], self.todolist, self.out, self.error, None) revert_command.execute() self.assertEqual(self.errors, "") self.assertTrue(self.output.endswith("Successfully reverted: add Three\n")) self.assertEqual(self.todolist.print_todos(), "Foo\nBar\nBaz\n{t} One\n{t} Two".format(t=self.today)) revert_command = RevertCommand([], self.todolist, self.out, self.error, None) revert_command.execute() self.assertEqual(self.errors, "") self.assertTrue(self.output.endswith("Successfully reverted: add Two\n")) self.assertEqual(self.todolist.print_todos(), "Foo\nBar\nBaz\n{t} One".format(t=self.today)) revert_command = RevertCommand([], self.todolist, self.out, self.error, None) revert_command.execute() self.assertEqual(self.errors, "") self.assertTrue(self.output.endswith("Successfully reverted: add One\n")) self.assertEqual(self.todolist.print_todos(), "Foo\nBar\nBaz") def test_revert06(self): """ Test attempt of deletion with non-existing backup key""" backup = ChangeSet(self.todolist, self.archive, ['add One']) backup.timestamp = '1' command1 = AddCommand(["One"], self.todolist, self.out, self.error, None) command1.execute() backup.save(self.todolist) backup = ChangeSet(self.todolist, self.archive, ['add Two']) backup.timestamp = '2' command2 = AddCommand(["Two"], self.todolist, self.out, self.error, None) command2.execute() backup.save(self.todolist) backup = ChangeSet(self.todolist, self.archive, ['add Three']) backup.timestamp = '3' command3 = AddCommand(["Three"], self.todolist, self.out, self.error, None) command3.execute() backup.save(self.todolist) backup = ChangeSet(self.todolist, self.archive, ['delete Three']) backup.timestamp = '4' command4 = DeleteCommand(["Three"], self.todolist, self.out, self.error, None) command4.execute() backup.save(self.todolist) backup = ChangeSet() backup.delete('Foo') changesets = list(backup.backup_dict.keys()) changesets.remove('index') index_timestamps = [change[0] for change in backup._get_index()] result = list(set(index_timestamps) - set(changesets)) self.assertEqual(len(changesets), 4) self.assertEqual(result, []) self.assertEqual(self.errors, "") def test_revert07(self): """ Test backup when no archive file is set """ backup = ChangeSet(self.todolist, None, ['add One']) backup.timestamp = '1' command1 = AddCommand(["One"], self.todolist, self.out, self.error, None) command1.execute() backup.save(self.todolist) changesets = list(backup.backup_dict.keys()) changesets.remove('index') self.assertEqual(len(changesets), 1) self.assertEqual(self.errors, "") def test_backup_config01(self): config(p_overrides={('topydo', 'backup_count'): '1'}) self.assertEqual(config().backup_count(), 1) def test_backup_config02(self): config(p_overrides={('topydo', 'backup_count'): '0'}) self.assertEqual(config().backup_count(), 0) def test_backup_config03(self): config(p_overrides={('topydo', 'backup_count'): '-88'}) self.assertEqual(config().backup_count(), 0) def test_backup_config04(self): config(p_overrides={('topydo', 'backup_count'): 'foo'}) self.assertEqual(config().backup_count(), 5) def test_revert_name(self): name = RevertCommand.name() self.assertEqual(name, 'revert') def test_help(self): command = RevertCommand(["help"], self.todolist, self.out, self.error) command.execute() self.assertEqual(self.output, "") self.assertEqual(self.errors, command.usage() + "\n\n" + command.help() + "\n") def tearDown(self): for filename in glob('/tmp/' + self.tmp_name + '*'): os.remove(filename)
class TagCommandTest(CommandTest): def setUp(self): super().setUp() todos = [ "Foo", "Bar due:2014-10-22", "Baz due:2014-10-20", "Fnord due:2014-10-20 due:2014-10-22", ] self.todolist = TodoList(todos) def test_add_tag1(self): command = TagCommand(["1", "due", "2014-10-22"], self.todolist, self.out, self.error) command.execute() self.assertEqual(self.todolist.todo(1).source(), "Foo due:2014-10-22") self.assertEqual(self.output, "| 1| Foo due:2014-10-22\n") self.assertEqual(self.errors, "") self.assertTrue(self.todolist.dirty) def test_add_tag2(self): command = TagCommand(["Foo", "due", "2014-10-22"], self.todolist, self.out, self.error) command.execute() self.assertEqual(self.todolist.todo(1).source(), "Foo due:2014-10-22") self.assertEqual(self.output, "| 1| Foo due:2014-10-22\n") self.assertEqual(self.errors, "") self.assertTrue(self.todolist.dirty) def test_add_tag3(self): command = TagCommand(["-a", "2", "due", "2014-10-19"], self.todolist, self.out, self.error) command.execute() self.assertEqual(self.todolist.todo(2).source(), "Bar due:2014-10-22 due:2014-10-19") self.assertEqual(self.output, "| 2| Bar due:2014-10-22 due:2014-10-19\n") self.assertEqual(self.errors, "") self.assertTrue(self.todolist.dirty) def test_add_tag4(self): command = TagCommand(["Foox", "due", "2014-10-22"], self.todolist, self.out, self.error) command.execute() self.assertFalse(self.todolist.dirty) self.assertFalse(self.output) self.assertEqual(self.errors, "Invalid todo number.\n") def test_force_add_tag01(self): '''Tries to different values to a tag for the same name 3 times.''' for letter in ['a', 'b', 'c']: command = TagCommand(['-a', '1', 'k', letter], self.todolist, self.out, self.error) command.execute() self.assertEqual(self.errors, "") self.assertTrue(self.todolist.dirty) self.assertEqual(self.todolist.todo(1).source(), "Foo k:a k:b k:c") def test_set_tag04(self): command = TagCommand(["3", "due", "2014-10-20"], self.todolist, self.out, self.error) command.execute() self.assertFalse(self.todolist.dirty) self.assertEqual(self.output, "| 3| Baz due:2014-10-20\n") self.assertEqual(self.errors, "") def test_set_tag05(self): command = TagCommand(["4", "due", "2014-10-20"], self.todolist, self.out, self.error, lambda t: "all") command.execute() self.assertTrue(self.todolist.dirty) self.assertEqual(self.output, " 1. 2014-10-20\n 2. 2014-10-22\n| 4| Fnord due:2014-10-20 due:2014-10-20\n") self.assertEqual(self.errors, "") def test_set_tag06(self): command = TagCommand(["4", "due", "2014-10-20"], self.todolist, self.out, self.error, lambda t: "1") command.execute() self.assertFalse(self.todolist.dirty) self.assertEqual(self.output, " 1. 2014-10-20\n 2. 2014-10-22\n| 4| Fnord due:2014-10-20 due:2014-10-22\n") self.assertEqual(self.errors, "") def test_set_tag07(self): command = TagCommand(["4", "due", "2014-10-20"], self.todolist, self.out, self.error, lambda t: "2") command.execute() self.assertTrue(self.todolist.dirty) self.assertEqual(self.output, " 1. 2014-10-20\n 2. 2014-10-22\n| 4| Fnord due:2014-10-20 due:2014-10-20\n") self.assertEqual(self.errors, "") def test_set_tag08(self): command = TagCommand(["4", "due", "2014-10-20"], self.todolist, self.out, self.error, lambda t: "") command.execute() self.assertFalse(self.todolist.dirty) self.assertEqual(self.output, " 1. 2014-10-20\n 2. 2014-10-22\n| 4| Fnord due:2014-10-20 due:2014-10-22\n") self.assertEqual(self.errors, "") def test_set_tag09(self): command = TagCommand(["4", "due", "2014-10-20"], self.todolist, self.out, self.error, lambda t: "99") command.execute() self.assertFalse(self.todolist.dirty) self.assertEqual(self.output, " 1. 2014-10-20\n 2. 2014-10-22\n| 4| Fnord due:2014-10-20 due:2014-10-22\n") self.assertEqual(self.errors, "") def test_set_tag10(self): command = TagCommand(["-f", "4", "due", "2014-10-20"], self.todolist, self.out, self.error, lambda t: "99") command.execute() self.assertTrue(self.todolist.dirty) self.assertEqual(self.output, "| 4| Fnord due:2014-10-20 due:2014-10-20\n") self.assertEqual(self.errors, "") @freeze_time('2015, 11, 19') def test_set_tag11(self): command = TagCommand(["3", "due", "today"], self.todolist, self.out, self.error) command.execute() self.assertTrue(self.todolist.dirty) self.assertEqual(self.output, "| 3| Baz due:2015-11-19\n") self.assertEqual(self.errors, "") def test_set_tag12(self): """ Do not convert relative dates for tags that were not configured as start/due date. """ command = TagCommand(["3", "foo", "today"], self.todolist, self.out, self.error) command.execute() self.assertTrue(self.todolist.dirty) self.assertEqual(self.output, "| 3| Baz due:2014-10-20 foo:today\n") self.assertEqual(self.errors, "") def test_rm_tag01(self): command = TagCommand(["1", "due"], self.todolist, self.out, self.error) command.execute() self.assertFalse(self.todolist.dirty) self.assertEqual(self.output, "| 1| Foo\n") self.assertEqual(self.errors, "") def test_rm_tag02(self): command = TagCommand(["2", "due"], self.todolist, self.out, self.error) command.execute() self.assertTrue(self.todolist.dirty) self.assertEqual(self.output, "| 2| Bar\n") self.assertEqual(self.errors, "") def test_rm_tag03(self): command = TagCommand(["4", "due"], self.todolist, self.out, self.error, lambda t: "all") command.execute() self.assertTrue(self.todolist.dirty) self.assertEqual(self.output, " 1. 2014-10-20\n 2. 2014-10-22\n| 4| Fnord\n") self.assertEqual(self.errors, "") def test_rm_tag04(self): command = TagCommand(["4", "due"], self.todolist, self.out, self.error, lambda t: "1") command.execute() self.assertTrue(self.todolist.dirty) self.assertEqual(self.output, " 1. 2014-10-20\n 2. 2014-10-22\n| 4| Fnord due:2014-10-22\n") self.assertEqual(self.errors, "") def test_rm_tag06(self): command = TagCommand(["4", "due"], self.todolist, self.out, self.error, lambda t: "99") command.execute() self.assertFalse(self.todolist.dirty) self.assertEqual(self.output, " 1. 2014-10-20\n 2. 2014-10-22\n| 4| Fnord due:2014-10-20 due:2014-10-22\n") self.assertEqual(self.errors, "") def test_rm_tag07(self): command = TagCommand(["4", "due"], self.todolist, self.out, self.error, lambda t: "A") command.execute() self.assertFalse(self.todolist.dirty) self.assertEqual(self.output, " 1. 2014-10-20\n 2. 2014-10-22\n| 4| Fnord due:2014-10-20 due:2014-10-22\n") self.assertEqual(self.errors, "") def test_rm_tag08(self): command = TagCommand(["5", "due"], self.todolist, self.out, self.error) command.execute() self.assertFalse(self.todolist.dirty) self.assertEqual(self.output, "") self.assertEqual(self.errors, "Invalid todo number.\n") def test_rm_tag09(self): command = TagCommand(["A", "due"], self.todolist, self.out, self.error) command.execute() self.assertFalse(self.todolist.dirty) self.assertEqual(self.output, "") self.assertEqual(self.errors, "Invalid todo number.\n") def test_rm_tag10(self): command = TagCommand(["-f", "4", "due"], self.todolist, self.out, self.error, lambda t: "A") command.execute() self.assertTrue(self.todolist.dirty) self.assertEqual(self.output, "| 4| Fnord\n") self.assertEqual(self.errors, "") def test_no_tag(self): command = TagCommand(["4"], self.todolist, self.out, self.error) command.execute() self.assertFalse(self.todolist.dirty) self.assertEqual(self.output, "") self.assertEqual(self.errors, command.usage() + "\n") def test_help(self): command = TagCommand(["help"], self.todolist, self.out, self.error) command.execute() self.assertEqual(self.output, "") self.assertEqual(self.errors, command.usage() + "\n\n" + command.help() + "\n")
class DepriCommandTest(CommandTest): def setUp(self): super().setUp() todos = [ "(A) Foo", "Bar", "(B) Baz", "(E) a @test with due:2015-06-03", "(Z) a @test with +project p:1", "(D) Bax id:1", ] self.todolist = TodoList(todos) def test_depri1(self): command = DepriCommand(["1"], self.todolist, self.out, self.error) command.execute() self.assertTrue(self.todolist.dirty) self.assertEqual(self.todolist.todo(1).priority(), None) self.assertEqual(self.output, "Priority removed.\n| 1| Foo\n") self.assertEqual(self.errors, "") def test_depri2(self): command = DepriCommand(["2"], self.todolist, self.out, self.error) command.execute() self.assertFalse(self.todolist.dirty) self.assertEqual(self.todolist.todo(2).priority(), None) self.assertEqual(self.output, "") self.assertEqual(self.errors, "") def test_depri3(self): command = DepriCommand(["Foo"], self.todolist, self.out, self.error) command.execute() self.assertTrue(self.todolist.dirty) self.assertEqual(self.todolist.todo(1).priority(), None) self.assertEqual(self.output, "Priority removed.\n| 1| Foo\n") self.assertEqual(self.errors, "") def test_depri4(self): command = DepriCommand(["1", "Baz"], self.todolist, self.out, self.error) command.execute() self.assertTrue(self.todolist.dirty) self.assertEqual(self.todolist.todo(1).priority(), None) self.assertEqual(self.todolist.todo(3).priority(), None) self.assertEqual(self.output, "Priority removed.\n| 1| Foo\nPriority removed.\n| 3| Baz\n") self.assertEqual(self.errors, "") def test_expr_depri1(self): command = DepriCommand(["-e", "@test"], self.todolist, self.out, self.error, None) command.execute() result = "Priority removed.\n| 4| a @test with due:2015-06-03\nPriority removed.\n| 5| a @test with +project p:1\n" self.assertTrue(self.todolist.dirty) self.assertEqual(self.output, result) self.assertEqual(self.errors, "") def test_expr_depri2(self): command = DepriCommand(["-e", "@test", "due:2015-06-03"], self.todolist, self.out, self.error, None) command.execute() result = "Priority removed.\n| 4| a @test with due:2015-06-03\n" self.assertTrue(self.todolist.dirty) self.assertEqual(self.output, result) self.assertEqual(self.errors, "") def test_expr_depri3(self): command = DepriCommand(["-e", "@test", "due:2015-06-03", "+project"], self.todolist, self.out, self.error, None) command.execute() self.assertFalse(self.todolist.dirty) def test_expr_depri4(self): """ Don't remove priority from unrelevant todo items. """ command = DepriCommand(["-e", "Bax"], self.todolist, self.out, self.error, None) command.execute() self.assertFalse(self.todolist.dirty) def test_expr_depri5(self): """ Force unprioritizing unrelevant items with additional -x flag. """ command = DepriCommand(["-xe", "Bax"], self.todolist, self.out, self.error, None) command.execute() self.assertTrue(self.todolist.dirty) self.assertEqual(self.output, "Priority removed.\n| 6| Bax id:1\n") self.assertEqual(self.errors, "") def test_invalid1(self): command = DepriCommand(["99"], self.todolist, self.out, self.error) command.execute() self.assertFalse(self.todolist.dirty) self.assertFalse(self.output) self.assertEqual(self.errors, "Invalid todo number given.\n") def test_invalid2(self): command = DepriCommand(["99", "1"], self.todolist, self.out, self.error) command.execute() self.assertFalse(self.todolist.dirty) self.assertFalse(self.output) self.assertEqual(self.errors, "Invalid todo number given: 99.\n") def test_invalid3(self): command = DepriCommand(["99", "FooBar"], self.todolist, self.out, self.error) command.execute() self.assertFalse(self.todolist.dirty) self.assertFalse(self.output) self.assertEqual(self.errors, "Invalid todo number given: 99.\nInvalid todo number given: FooBar.\n") def test_invalid4(self): """ Throw an error with invalid argument containing special characters. """ command = DepriCommand([u"Fo\u00d3B\u0105r", "Bar"], self.todolist, self.out, self.error, None) command.execute() self.assertFalse(self.todolist.dirty) self.assertFalse(self.output) self.assertEqual(self.errors, u"Invalid todo number given: Fo\u00d3B\u0105r.\n") def test_empty(self): command = DepriCommand([], self.todolist, self.out, self.error) command.execute() self.assertFalse(self.todolist.dirty) self.assertFalse(self.output) self.assertEqual(self.errors, command.usage() + "\n") def test_depri_name(self): name = DepriCommand.name() self.assertEqual(name, 'depri') def test_help(self): command = DepriCommand(["help"], self.todolist, self.out, self.error) command.execute() self.assertEqual(self.output, "") self.assertEqual(self.errors, command.usage() + "\n\n" + command.help() + "\n")
class DeleteCommandTest(CommandTest): def setUp(self): super().setUp() todos = [ "Foo id:1", "Bar p:1", "a @test with due:2015-06-03", "a @test with +project", ] self.todolist = TodoList(todos) def test_del1(self): command = DeleteCommand(["1"], self.todolist, self.out, self.error, _no_prompt) command.execute() self.assertTrue(self.todolist.is_dirty()) self.assertEqual(self.todolist.todo(1).source(), "Bar") self.assertEqual(self.output, "| 2| Bar p:1\nRemoved: Foo id:1\n") self.assertEqual(self.errors, "") def test_del1_regex(self): command = DeleteCommand(["Foo"], self.todolist, self.out, self.error, _no_prompt) command.execute() self.assertTrue(self.todolist.is_dirty()) self.assertEqual(self.todolist.todo(1).source(), "Bar") self.assertEqual(self.output, "| 2| Bar p:1\nRemoved: Foo id:1\n") self.assertEqual(self.errors, "") def test_del2(self): command = DeleteCommand(["1"], self.todolist, self.out, self.error, _yes_prompt) command.execute() self.assertTrue(self.todolist.is_dirty()) self.assertEqual(self.todolist.count(), 2) self.assertEqual(self.output, "| 2| Bar p:1\nRemoved: Bar\nRemoved: Foo\n") self.assertEqual(self.errors, "") def test_del3(self): command = DeleteCommand(["-f", "1"], self.todolist, self.out, self.error, _yes_prompt) command.execute() self.assertTrue(self.todolist.is_dirty()) self.assertEqual(self.todolist.count(), 3) # force won't delete subtasks self.assertEqual(self.output, "| 2| Bar p:1\nRemoved: Foo id:1\n") self.assertEqual(self.errors, "") def test_del4(self): command = DeleteCommand(["--force", "1"], self.todolist, self.out, self.error, _yes_prompt) command.execute() self.assertTrue(self.todolist.is_dirty()) self.assertEqual(self.todolist.count(), 3) # force won't delete subtasks self.assertEqual(self.output, "| 2| Bar p:1\nRemoved: Foo id:1\n") self.assertEqual(self.errors, "") def test_del5(self): command = DeleteCommand(["2"], self.todolist, self.out, self.error) command.execute() self.assertTrue(self.todolist.is_dirty()) self.assertEqual(self.todolist.todo(1).source(), "Foo") self.assertEqual(self.output, "Removed: Bar p:1\nThe following todo item(s) became active:\n| 1| Foo\n") self.assertEqual(self.errors, "") def test_del7(self): command = DeleteCommand(["99"], self.todolist, self.out, self.error) command.execute() self.assertFalse(self.todolist.is_dirty()) self.assertEqual(self.output, "") self.assertEqual(self.errors, "Invalid todo number given.\n") def test_del8(self): command = DeleteCommand(["A"], self.todolist, self.out, self.error) command.execute() self.assertFalse(self.todolist.is_dirty()) self.assertEqual(self.output, "") self.assertEqual(self.errors, "Invalid todo number given.\n") def test_del9(self): """ Test deletion with textual IDs. """ config("test/data/todolist-uid.conf") command = DeleteCommand(["8to"], self.todolist, self.out, self.error) command.execute() result = "Foo\na @test with due:2015-06-03\na @test with +project" self.assertEqual(self.todolist.print_todos(), result) self.assertRaises(InvalidTodoException, self.todolist.todo, 'b0n') def test_multi_del1(self): """ Test deletion of multiple items. """ command = DeleteCommand(["1", "2"], self.todolist, self.out, self.error, _no_prompt) command.execute() result = "a @test with due:2015-06-03\na @test with +project" self.assertEqual(self.todolist.count(), 2) self.assertEqual(self.todolist.print_todos(), result) def test_multi_del2(self): """ Test deletion of multiple items. """ command = DeleteCommand(["1", "2"], self.todolist, self.out, self.error, _yes_prompt) command.execute() result = "a @test with due:2015-06-03\na @test with +project" self.assertEqual(self.todolist.count(), 2) self.assertEqual(self.todolist.print_todos(), result) def test_multi_del3(self): """ Fail if any of supplied todo numbers is invalid. """ command = DeleteCommand(["99", "2"], self.todolist, self.out, self.error, _yes_prompt) command.execute() self.assertFalse(self.todolist.is_dirty()) self.assertEqual(self.output, "") self.assertEqual(self.errors, "Invalid todo number given: 99.\n") def test_multi_del4(self): """ Check output when all supplied todo numbers are invalid. """ command = DeleteCommand(["99", "A"], self.todolist, self.out, self.error, _yes_prompt) command.execute() self.assertFalse(self.todolist.is_dirty()) self.assertEqual(self.output, "") self.assertEqual(self.errors, "Invalid todo number given: 99.\nInvalid todo number given: A.\n") def test_multi_del5(self): """ Throw an error with invalid argument containing special characters. """ command = DeleteCommand([u"Fo\u00d3B\u0105r", "Bar"], self.todolist, self.out, self.error, None) command.execute() self.assertFalse(self.todolist.is_dirty()) self.assertEqual(self.output, "") self.assertEqual(self.errors, u"Invalid todo number given: Fo\u00d3B\u0105r.\n") def test_expr_del1(self): command = DeleteCommand(["-e", "@test"], self.todolist, self.out, self.error, None) command.execute() result = "Removed: a @test with due:2015-06-03\nRemoved: a @test with +project\n" self.assertTrue(self.todolist.is_dirty()) self.assertEqual(self.todolist.count(), 2) self.assertEqual(self.output, result) self.assertEqual(self.errors, "") def test_expr_del2(self): command = DeleteCommand(["-e", "@test", "due:2015-06-03"], self.todolist, self.out, self.error, None) command.execute() self.assertTrue(self.todolist.is_dirty()) self.assertEqual(self.output, "Removed: a @test with due:2015-06-03\n") self.assertEqual(self.errors, "") def test_expr_del3(self): command = DeleteCommand(["-e", "@test", "due:2015-06-03", "+project"], self.todolist, self.out, self.error, None) command.execute() self.assertFalse(self.todolist.is_dirty()) def test_expr_del4(self): """ Remove only relevant todo items. """ command = DeleteCommand(["-e", ""], self.todolist, self.out, self.error, None) command.execute() result = "Foo" self.assertTrue(self.todolist.is_dirty()) self.assertEqual(self.todolist.count(), 1) self.assertEqual(self.todolist.print_todos(), result) def test_expr_del5(self): """ Force deleting unrelevant items with additional -x flag. """ command = DeleteCommand(["-xe", ""], self.todolist, self.out, self.error, _yes_prompt) command.execute() self.assertTrue(self.todolist.is_dirty()) self.assertEqual(self.todolist.count(), 0) def test_empty(self): command = DeleteCommand([], self.todolist, self.out, self.error) command.execute() self.assertFalse(self.todolist.is_dirty()) self.assertFalse(self.output) self.assertEqual(self.errors, command.usage() + "\n") def test_help(self): command = DeleteCommand(["help"], self.todolist, self.out, self.error) command.execute() self.assertEqual(self.output, "") self.assertEqual(self.errors, command.usage() + "\n\n" + command.help() + "\n")