Exemplo n.º 1
0
    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([])
Exemplo n.º 2
0
def load_file_to_raw_list(p_filename):
    """
    Loads a todo file from the given filename and returns a list of todo
    strings (unparsed).
    """
    todofile = TodoFile(p_filename)
    return todofile.read()
Exemplo n.º 3
0
    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)
Exemplo n.º 4
0
    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))
Exemplo n.º 5
0
    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))
Exemplo n.º 6
0
    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 = TodoListBase(lines)
Exemplo n.º 7
0
    def setUp(self):
        super().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 = TodoListBase(lines)
Exemplo n.º 8
0
class TodoLoadTester(TopydoTest):
    """Test the auto_delete_whitespace configuration parameter"""
    def setUp(self):
        super().setUp()
        self.todoPath = 'test/data/TodoListTest.txt'
        self.todofile = TodoFile(self.todoPath)

    def test_load_default(self):
        todolist = TodoListBase(self.todofile.read())

        self.assertTrue(all([len(todo.source()) != 0 for todo in todolist]))

    def test_load_preserve_ws(self):
        config("test/data/listload.conf")
        todolist = TodoListBase(self.todofile.read())

        self.assertTrue(any([len(todo.source()) == 0 for todo in todolist]))

    def test_load_use_default(self):
        config("test/data/listload2.conf")
        todolist = TodoListBase(self.todofile.read())

        self.assertTrue(all([len(todo.source()) != 0 for todo in todolist]))
Exemplo n.º 9
0
    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([])
Exemplo n.º 10
0
class TodoListTester(TopydoTest):
    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 = TodoListBase(lines)

    def test_contexts(self):
        self.assertEqual(set(["Context1", "Context2"]), self.todolist.contexts())
        self.assertFalse(self.todolist.is_dirty())

    def test_projects(self):
        self.assertEqual(set(["Project1", "Project2"]), self.todolist.projects())
        self.assertFalse(self.todolist.is_dirty())

    def test_add1(self):
        text = "(C) Adding a new task @Context3 +Project3"
        count = self.todolist.count()
        todo = self.todolist.add(text)

        self.assertEqual(self.todolist.todo(count + 1).source(), text)
        self.assertEqual(set(["Project1", "Project2", "Project3"]), self.todolist.projects())
        self.assertEqual(set(["Context1", "Context2", "Context3"]), self.todolist.contexts())
        self.assertEqual(self.todolist.number(todo), 6)
        self.assertTrue(self.todolist.is_dirty())

    def test_add2(self):
        text = str(self.todolist)
        self.todolist.add("")
        self.assertEqual(str(self.todolist), text)

    def test_add3a(self):
        count = self.todolist.count()
        self.todolist.add("\n(C) New task")

        self.assertEqual(self.todolist.count(), count + 1)
        self.assertEqual(self.todolist.todo(count + 1).source(), "(C) New task")
        self.assertEqual(self.todolist.todo(count + 1).priority(), "C")

    def test_add3b(self):
        count = self.todolist.count()
        self.todolist.add("(C) New task\n")

        self.assertEqual(self.todolist.count(), count + 1)
        self.assertEqual(self.todolist.todo(count + 1).source(), "(C) New task")
        self.assertEqual(self.todolist.todo(count + 1).priority(), "C")

    def test_add4(self):
        text = str(self.todolist)
        self.todolist.add(" ")
        self.assertEqual(str(self.todolist), text)

    def test_add5(self):
        text = str(self.todolist)
        self.todolist.add("\n")
        self.assertEqual(str(self.todolist), text)

    def test_delete1(self):
        count = self.todolist.count()
        todo = self.todolist.todo(2)
        self.todolist.delete(todo)

        self.assertEqual(self.todolist.todo(2).source(), "(C) Baz @Context1 +Project1 key:value")
        self.assertEqual(self.todolist.count(), count - 1)
        self.assertTrue(self.todolist.is_dirty())
        self.assertRaises(InvalidTodoException, self.todolist.number, todo)

    def test_delete2(self):
        """ Try to remove a todo item that does not exist. """
        count = self.todolist.count()

        todo = Todo("Not in the list")
        self.todolist.delete(todo)

        self.assertEqual(self.todolist.count(), count)
        self.assertFalse(self.todolist.is_dirty())

    def test_append1(self):
        todo = self.todolist.todo(3)
        self.todolist.append(todo, "@Context3")

        self.assertEqual(todo.source(), "(C) Baz @Context1 +Project1 key:value @Context3")
        self.assertEqual(set(["Context1", "Context2", "Context3"]), self.todolist.contexts())
        self.assertTrue(self.todolist.is_dirty())

    def test_append2(self):
        todo = self.todolist.todo(3)
        text = todo.text()
        self.todolist.append(todo, "foo:bar")

        self.assertEqual(todo.text(), text)
        self.assertEqual(todo.source(), "(C) Baz @Context1 +Project1 key:value foo:bar")

    def test_append3(self):
        todo = self.todolist.todo(3)
        text = todo.text()
        self.todolist.append(todo, "")

        self.assertEqual(todo.text(), text)

    def test_todo(self):
        count = self.todolist.count()

        self.assertRaises(InvalidTodoException, self.todolist.todo, count + 100)
        self.assertFalse(self.todolist.is_dirty())

    def test_count(self):
        """ Test that empty lines are not counted. """
        self.assertEqual(self.todolist.count(), 5)

    def test_todo_number1(self):
        todo = Todo("No number")
        self.todolist.add_todo(todo)

        todo = self.todolist.todo(6)
        self.assertIsInstance(todo, Todo)
        self.assertEqual(todo.text(), "No number")

    def test_todo_number2(self):
        todo = Todo("Non-existent")
        self.assertRaises(InvalidTodoException, self.todolist.number, todo)

    def test_todo_complete(self):
        todo = self.todolist.todo(1)
        self.todolist.set_todo_completed(todo)
        self.assertTrue(self.todolist.todo(1).is_completed())
        self.assertTrue(self.todolist.is_dirty())

    def test_todo_priority1(self):
        todo = self.todolist.todo(1)
        self.todolist.set_priority(todo, "F")

        self.assertEqual(self.todolist.todo(1).priority(), "F")
        self.assertTrue(self.todolist.is_dirty())

    def test_todo_priority2(self):
        todo = self.todolist.todo(1)
        self.todolist.set_priority(todo, "C")

        self.assertFalse(self.todolist.is_dirty())

    def test_erase(self):
        self.todolist.erase()

        self.assertEqual(self.todolist.count(), 0)
        self.assertTrue(self.todolist.is_dirty())

    def test_regex1(self):
        """ Multiple hits should result in None. """
        self.assertRaises(InvalidTodoException, self.todolist.todo, "Project1")

    def test_regex3(self):
        todo = self.todolist.todo("project2")
        self.assertTrue(todo)
        self.assertEqual(todo.source(), "(D) Bar @Context1 +Project2")

    def test_uid1(self):
        config("test/data/todolist-uid.conf")

        self.assertEqual(self.todolist.todo("t5c").source(), "(C) Foo @Context2 Not@Context +Project1 Not+Project")

    def test_uid2(self):
        """ Changing the priority should not change the identifier. """
        config("test/data/todolist-uid.conf")

        todo = self.todolist.todo("t5c")
        self.todolist.set_priority(todo, "B")
        self.assertEqual(self.todolist.todo("t5c").source(), "(B) Foo @Context2 Not@Context +Project1 Not+Project")

    def test_uid3(self):
        """
        Must be able to handle integers when text identifiers are enabled.
        """

        config("test/data/todolist-uid.conf")
        self.assertRaises(InvalidTodoException, self.todolist.todo, 1)

    def test_new_uid(self):
        """ Make sure that item has new text ID after append. """

        config("test/data/todolist-uid.conf")
        todo = self.todolist.todo("t5c")
        self.todolist.append(todo, "A")

        self.assertNotEqual(self.todolist.number(todo), "t5c")
Exemplo n.º 11
0
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)
Exemplo n.º 12
0
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 = 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 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 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()))

    @mock.patch('topydo.lib.Config._Config.archive')
    def test_revert04(self, mock_archive):
        """ Test trimming of the backup_file """
        mock_archive.return_value = ''  # test for empty archive setting
        backup = BackupSimulator(self.todolist, self.archive, '1', ['add One'])
        command_executer(AddCommand, ["One"], self.todolist, None, self.out,
                         self.error, None)
        backup.save(self.todolist)

        backup = BackupSimulator(self.todolist, self.archive, '2', ['add Two'])
        command_executer(AddCommand, ["Two"], self.todolist, None, self.out,
                         self.error, None)
        backup.save(self.todolist)

        backup = BackupSimulator(self.todolist, self.archive, '3',
                                 ['add Three'])
        command_executer(AddCommand, ["Three"], self.todolist, None, self.out,
                         self.error, None)
        backup.save(self.todolist)

        backup = BackupSimulator(self.todolist, self.archive, '4',
                                 ['add Four'])
        command_executer(AddCommand, ["Four"], self.todolist, None, self.out,
                         self.error, None)
        backup.save(self.todolist)

        backup = BackupSimulator(self.todolist, self.archive, '5',
                                 ['add Five'])
        command_executer(AddCommand, ["Five"], self.todolist, None, self.out,
                         self.error, None)
        backup.save(self.todolist)

        result = len(ChangeSet().backup_dict.keys())
        self.assertEqual(result, 6)

        backup = BackupSimulator(self.todolist, self.archive, '6', ['add Six'])
        command_executer(AddCommand, ["Six"], self.todolist, None, self.out,
                         self.error, None)
        backup.save(self.todolist)

        backup = BackupSimulator(self.todolist, self.archive, '7',
                                 ['add Seven'])
        command_executer(AddCommand, ["Seven"], self.todolist, None, self.out,
                         self.error, None)
        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("Reverted to state before: add Seven\n"))

    def test_revert05(self):
        """ Test for possible backup collisions """
        backup = BackupSimulator(self.todolist, self.archive, '1', ['add One'])
        command_executer(AddCommand, ["One"], self.todolist, None, self.out,
                         self.error, None)
        backup.save(self.todolist)

        backup = BackupSimulator(self.todolist, self.archive, '2', ['add Two'])
        command_executer(AddCommand, ["Two"], self.todolist, None, self.out,
                         self.error, None)
        backup.save(self.todolist)

        backup = BackupSimulator(self.todolist, self.archive, '3',
                                 ['add Three'])
        command_executer(AddCommand, ["Three"], self.todolist, None, self.out,
                         self.error, None)
        backup.save(self.todolist)

        backup = BackupSimulator(self.todolist, self.archive, '4',
                                 ['delete Three'])
        command_executer(DeleteCommand, ["Three"], self.todolist, None,
                         self.out, self.error, None)
        backup.save(self.todolist)

        backup = BackupSimulator(self.todolist, self.archive, '5',
                                 ['add Four'])
        command_executer(AddCommand, ["Four"], self.todolist, None, self.out,
                         self.error, None)
        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("Reverted to state before: 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("Reverted to state before: 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("Reverted to state before: 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("Reverted to state before: 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("Reverted to state before: 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 = BackupSimulator(self.todolist, self.archive, '1', ['add One'])
        command_executer(AddCommand, ["One"], self.todolist, None, self.out,
                         self.error, None)
        backup.save(self.todolist)

        backup = BackupSimulator(self.todolist, self.archive, '2', ['add Two'])
        command_executer(AddCommand, ["Two"], self.todolist, None, self.out,
                         self.error, None)
        backup.save(self.todolist)

        backup = BackupSimulator(self.todolist, self.archive, '3',
                                 ['add Three'])
        command_executer(AddCommand, ["Three"], self.todolist, None, self.out,
                         self.error, None)
        backup.save(self.todolist)

        backup = BackupSimulator(self.todolist, self.archive, '4',
                                 ['delete Three'])
        command_executer(DeleteCommand, ["Three"], self.todolist, None,
                         self.out, self.error, None)
        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'
        command_executer(AddCommand, ["One"], self.todolist, None, self.out,
                         self.error, None)
        backup.save(self.todolist)

        changesets = list(backup.backup_dict.keys())
        changesets.remove('index')

        self.assertEqual(len(changesets), 1)
        self.assertEqual(self.errors, "")

    def test_revert_ls(self):
        backup = BackupSimulator(self.todolist, self.archive, '1', ['add One'])
        command_executer(AddCommand, ["One"], self.todolist, None, self.out,
                         self.error, None)
        backup.save(self.todolist)

        backup = BackupSimulator(self.todolist, self.archive, '2', ['add Two'])
        command_executer(AddCommand, ["Two"], self.todolist, None, self.out,
                         self.error, None)
        backup.save(self.todolist)

        backup = BackupSimulator(self.todolist, self.archive, '3',
                                 ['add Three'])
        command_executer(AddCommand, ["Three"], self.todolist, None, self.out,
                         self.error, None)
        backup.save(self.todolist)

        backup = BackupSimulator(self.todolist, self.archive, '4',
                                 ['delete Three'])
        command_executer(DeleteCommand, ["Three"], self.todolist, None,
                         self.out, self.error, None)
        backup.save(self.todolist)

        backup = BackupSimulator(self.todolist, self.archive, '5',
                                 ['add Four'])
        command_executer(AddCommand, ["Four"], self.todolist, None, self.out,
                         self.error, None)
        backup.save(self.todolist)

        command_executer(RevertCommand, ['ls'], self.todolist, None, self.out,
                         self.error, None)

        self.assertEqual(self.errors, "")
        self.assertTrue(
            self.output.endswith("""  1| 1970-01-01 00:00:05 | add Four
  2| 1970-01-01 00:00:04 | delete Three
  3| 1970-01-01 00:00:03 | add Three
  4| 1970-01-01 00:00:02 | add Two
  5| 1970-01-01 00:00:01 | add One\n"""))

    def test_revert_08(self):
        backup = BackupSimulator(self.todolist, self.archive, '1', ['add One'])
        command_executer(AddCommand, ["One"], self.todolist, None, self.out,
                         self.error, None)
        backup.save(self.todolist)

        backup = BackupSimulator(self.todolist, self.archive, '2', ['add Two'])
        command_executer(AddCommand, ["Two"], self.todolist, None, self.out,
                         self.error, None)
        backup.save(self.todolist)

        backup = BackupSimulator(self.todolist, self.archive, '3',
                                 ['add Three'])
        command_executer(AddCommand, ["Three"], self.todolist, None, self.out,
                         self.error, None)
        backup.save(self.todolist)

        backup = BackupSimulator(self.todolist, self.archive, '4',
                                 ['delete Three'])
        command_executer(DeleteCommand, ["Three"], self.todolist, None,
                         self.out, self.error, None)
        backup.save(self.todolist)

        backup = BackupSimulator(self.todolist, self.archive, '5',
                                 ['add Four'])
        command_executer(AddCommand, ["Four"], self.todolist, None, self.out,
                         self.error, None)
        backup.save(self.todolist)

        command_executer(RevertCommand, ['3'], self.todolist, None, self.out,
                         self.error, None)

        self.assertEqual(self.errors, "")
        self.assertTrue(
            self.output.endswith("Reverted to state before: add Three\n"))
        self.assertEqual(self.todolist.print_todos(),
                         "Foo\nBar\nBaz\n2015-11-06 One\n2015-11-06 Two")

    def test_revert_invalid(self):
        """ Test invalid input for revert. """
        command_executer(RevertCommand, ["foo"], self.todolist, None, self.out,
                         self.error, None)
        command_executer(RevertCommand, ["ls", "foo"], self.todolist, None,
                         self.out, self.error, None)
        command_executer(RevertCommand, ["1", "foo"], self.todolist, None,
                         self.out, self.error, None)
        usage_text = RevertCommand([], self.todolist).usage() + '\n'
        self.assertEqual(self.errors, usage_text * 3)

    def test_revert_out_of_range(self):
        command_executer(RevertCommand, ["158"], self.todolist, None, self.out,
                         self.error, None)
        self.assertEqual(self.errors, "Specified index is out range\n")

    def test_revert_no_todolist(self):
        """ Test attempt of revert with todolist missing """
        backup = BackupSimulator(self.todolist, self.archive, '1', ['add One'])
        command_executer(AddCommand, ["One"], self.todolist, None, self.out,
                         self.error, None)
        backup.save(self.todolist)

        backup = BackupSimulator(self.todolist, self.archive, '2', ['add Two'])
        command_executer(AddCommand, ["Two"], self.todolist, None, self.out,
                         self.error, None)
        backup.save(self.todolist)

        backup = BackupSimulator(self.todolist, self.archive, '3',
                                 ['add Three'])
        command_executer(AddCommand, ["Three"], self.todolist, None, self.out,
                         self.error, None)
        backup.save(self.todolist)

        backup = BackupSimulator(self.todolist, self.archive, '4',
                                 ['delete Three'])
        command_executer(DeleteCommand, ["Three"], self.todolist, None,
                         self.out, self.error, None)
        backup.save(self.todolist)

        command_executer(RevertCommand, ['1'], None, None, self.out,
                         self.error, None)

        result = len(ChangeSet().backup_dict.keys())
        self.assertEqual(result, 4)

    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)
Exemplo n.º 13
0
class TodoListTester(TopydoTest):
    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 test_contexts(self):
        self.assertEquals(set(['Context1', 'Context2']), \
            self.todolist.contexts())
        self.assertFalse(self.todolist.is_dirty())

    def test_projects(self):
        self.assertEquals(set(['Project1', 'Project2']), \
            self.todolist.projects())
        self.assertFalse(self.todolist.is_dirty())

    def test_add1(self):
        text = "(C) Adding a new task @Context3 +Project3"
        count = self.todolist.count()
        todo = self.todolist.add(text)

        self.assertEquals(self.todolist.todo(count + 1).source(), text)
        self.assertEquals(set(['Project1', 'Project2', 'Project3']), \
            self.todolist.projects())
        self.assertEquals(set(['Context1', 'Context2', 'Context3']), \
            self.todolist.contexts())
        self.assertEquals(self.todolist.number(todo), 6)
        self.assertTrue(self.todolist.is_dirty())

    def test_add2(self):
        text = str(self.todolist)
        self.todolist.add('')
        self.assertEquals(str(self.todolist), text)

    def test_add3a(self):
        count = self.todolist.count()
        self.todolist.add('\n(C) New task')

        self.assertEqual(self.todolist.count(), count + 1)
        self.assertEqual(
            self.todolist.todo(count + 1).source(), '(C) New task')
        self.assertEqual(self.todolist.todo(count + 1).priority(), 'C')

    def test_add3b(self):
        count = self.todolist.count()
        self.todolist.add('(C) New task\n')

        self.assertEqual(self.todolist.count(), count + 1)
        self.assertEqual(
            self.todolist.todo(count + 1).source(), '(C) New task')
        self.assertEqual(self.todolist.todo(count + 1).priority(), 'C')

    def test_add4(self):
        text = str(self.todolist)
        self.todolist.add(' ')
        self.assertEquals(str(self.todolist), text)

    def test_add5(self):
        text = str(self.todolist)
        self.todolist.add("\n")
        self.assertEquals(str(self.todolist), text)

    def test_delete1(self):
        count = self.todolist.count()
        todo = self.todolist.todo(2)
        self.todolist.delete(todo)

        self.assertEquals(self.todolist.todo(2).source(), \
            "(C) Baz @Context1 +Project1 key:value")
        self.assertEquals(self.todolist.count(), count - 1)
        self.assertTrue(self.todolist.is_dirty())
        self.assertRaises(InvalidTodoException, self.todolist.number, todo)

    def test_append1(self):
        todo = self.todolist.todo(3)
        self.todolist.append(todo, "@Context3")

        self.assertEquals(todo.source(), \
            "(C) Baz @Context1 +Project1 key:value @Context3")
        self.assertEquals(set(['Context1', 'Context2', 'Context3']), \
            self.todolist.contexts())
        self.assertTrue(self.todolist.is_dirty())

    def test_append2(self):
        todo = self.todolist.todo(3)
        text = todo.text()
        self.todolist.append(todo, "foo:bar")

        self.assertEquals(todo.text(), text)
        self.assertEquals(todo.source(), \
            "(C) Baz @Context1 +Project1 key:value foo:bar")

    def test_append3(self):
        todo = self.todolist.todo(3)
        text = todo.text()
        self.todolist.append(todo, '')

        self.assertEquals(todo.text(), text)

    def test_todo(self):
        count = self.todolist.count()

        self.assertRaises(InvalidTodoException, self.todolist.todo,
                          count + 100)
        self.assertFalse(self.todolist.is_dirty())

    def test_string(self):
        # readlines() always ends a string with \n, but join() in str(todolist)
        # doesn't necessarily.
        self.assertEquals(str(self.todolist) + '\n', self.text)

    def test_count(self):
        """ Test that empty lines are not counted. """
        self.assertEquals(self.todolist.count(), 5)

    def test_todo_by_dep_id(self):
        """ Tests that todos can be retrieved by their id tag. """
        self.todolist.add("(C) Foo id:1")

        self.assertTrue(self.todolist.todo_by_dep_id('1'))
        self.assertFalse(self.todolist.todo_by_dep_id('2'))

    def test_todo_number1(self):
        todo = Todo("No number")
        self.todolist.add_todo(todo)

        todo = self.todolist.todo(6)
        self.assertIsInstance(todo, Todo)
        self.assertEquals(todo.text(), "No number")

    def test_todo_number2(self):
        todo = Todo("Non-existent")
        self.assertRaises(InvalidTodoException, self.todolist.number, todo)

    def test_todo_complete(self):
        todo = self.todolist.todo(1)
        self.todolist.set_todo_completed(todo)
        self.assertTrue(self.todolist.todo(1).is_completed())
        self.assertTrue(self.todolist.is_dirty())

    def test_todo_priority1(self):
        todo = self.todolist.todo(1)
        self.todolist.set_priority(todo, 'F')

        self.assertEquals(self.todolist.todo(1).priority(), 'F')
        self.assertTrue(self.todolist.is_dirty())

    def test_todo_priority2(self):
        todo = self.todolist.todo(1)
        self.todolist.set_priority(todo, 'C')

        self.assertFalse(self.todolist.is_dirty())

    def test_erase(self):
        self.todolist.erase()

        self.assertEquals(self.todolist.count(), 0)
        self.assertTrue(self.todolist.is_dirty())

    def test_regex1(self):
        """ Multiple hits should result in None. """
        self.assertRaises(InvalidTodoException, self.todolist.todo, "Project1")

    def test_regex3(self):
        todo = self.todolist.todo("project2")
        self.assertTrue(todo)
        self.assertEquals(todo.source(), "(D) Bar @Context1 +Project2")

    def test_uid1(self):
        config("test/data/todolist-uid.conf")

        self.assertEquals(
            self.todolist.todo('6iu').source(),
            "(C) Foo @Context2 Not@Context +Project1 Not+Project")

    def test_uid2(self):
        """ Changing the priority should not change the identifier. """
        config("test/data/todolist-uid.conf")

        todo = self.todolist.todo('6iu')
        self.todolist.set_priority(todo, 'B')
        self.assertEquals(
            self.todolist.todo('6iu').source(),
            "(B) Foo @Context2 Not@Context +Project1 Not+Project")

    def test_uid3(self):
        """
        Must be able to handle integers when text identifiers are enabled.
        """

        config("test/data/todolist-uid.conf")
        self.assertRaises(InvalidTodoException, self.todolist.todo, 1)

    def test_new_uid(self):
        """ Make sure that item has new text ID after append. """

        config("test/data/todolist-uid.conf")
        todo = self.todolist.todo('6iu')
        self.todolist.append(todo, "A")

        self.assertNotEquals(self.todolist.number(todo), '6iu')
Exemplo n.º 14
0
class TodoListTester(TopydoTest):
    def setUp(self):
        super().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 = TodoListBase(lines)

    def test_contexts(self):
        self.assertEqual(set(['Context1', 'Context2']),
                         self.todolist.contexts())
        self.assertFalse(self.todolist.dirty)

    def test_projects(self):
        self.assertEqual(set(['Project1', 'Project2']),
                         self.todolist.projects())
        self.assertFalse(self.todolist.dirty)

    def test_add1(self):
        text = "(C) Adding a new task @Context3 +Project3"
        count = self.todolist.count()
        todo = self.todolist.add(text)

        self.assertEqual(self.todolist.todo(count+1).source(), text)
        self.assertEqual(set(['Project1', 'Project2', 'Project3']),
                         self.todolist.projects())
        self.assertEqual(set(['Context1', 'Context2', 'Context3']),
                         self.todolist.contexts())
        self.assertEqual(self.todolist.number(todo), 6)
        self.assertTrue(self.todolist.dirty)

    def test_add2(self):
        text = str(self.todolist)
        self.todolist.add('')
        self.assertEqual(str(self.todolist), text)

    def test_add3a(self):
        count = self.todolist.count()
        self.todolist.add('\n(C) New task')

        self.assertEqual(self.todolist.count(), count + 1)
        self.assertEqual(self.todolist.todo(count + 1).source(),
                         '(C) New task')
        self.assertEqual(self.todolist.todo(count + 1).priority(), 'C')

    def test_add3b(self):
        count = self.todolist.count()
        self.todolist.add('(C) New task\n')

        self.assertEqual(self.todolist.count(), count + 1)
        self.assertEqual(self.todolist.todo(count + 1).source(),
                         '(C) New task')
        self.assertEqual(self.todolist.todo(count + 1).priority(), 'C')

    def test_add4(self):
        text = str(self.todolist)
        self.todolist.add(' ')
        self.assertEqual(str(self.todolist), text)

    def test_add5(self):
        text = str(self.todolist)
        self.todolist.add("\n")
        self.assertEqual(str(self.todolist), text)

    def test_delete1(self):
        count = self.todolist.count()
        todo = self.todolist.todo(2)
        self.todolist.delete(todo)

        self.assertEqual(self.todolist.todo(2).source(),
                         "(C) Baz @Context1 +Project1 key:value")
        self.assertEqual(self.todolist.count(), count - 1)
        self.assertTrue(self.todolist.dirty)
        self.assertRaises(InvalidTodoException, self.todolist.number, todo)

    def test_delete2(self):
        """ Try to remove a todo item that does not exist. """
        count = self.todolist.count()

        todo = Todo('Not in the list')
        self.todolist.delete(todo)

        self.assertEqual(self.todolist.count(), count)
        self.assertFalse(self.todolist.dirty)

    def test_append1(self):
        todo = self.todolist.todo(3)
        self.todolist.append(todo, "@Context3")

        self.assertEqual(todo.source(),
                         "(C) Baz @Context1 +Project1 key:value @Context3")
        self.assertEqual(set(['Context1', 'Context2', 'Context3']),
                         self.todolist.contexts())
        self.assertTrue(self.todolist.dirty)

    def test_append2(self):
        todo = self.todolist.todo(3)
        text = todo.text()
        self.todolist.append(todo, "foo:bar")

        self.assertEqual(todo.text(), text)
        self.assertEqual(todo.source(),
                         "(C) Baz @Context1 +Project1 key:value foo:bar")

    def test_append3(self):
        todo = self.todolist.todo(3)
        text = todo.text()
        self.todolist.append(todo, '')

        self.assertEqual(todo.text(), text)

    def test_todo(self):
        count = self.todolist.count()

        self.assertRaises(InvalidTodoException, self.todolist.todo,
                          count + 100)
        self.assertFalse(self.todolist.dirty)

    def test_count(self):
        """ Test that empty lines are not counted. """
        self.assertEqual(self.todolist.count(), 5)

    def test_todo_number1(self):
        todo = Todo("No number")
        self.todolist.add_todo(todo)

        todo = self.todolist.todo(6)
        self.assertIsInstance(todo, Todo)
        self.assertEqual(todo.text(), "No number")

    def test_todo_number2(self):
        todo = Todo("Non-existent")
        self.assertRaises(InvalidTodoException, self.todolist.number, todo)

    def test_todo_complete(self):
        todo = self.todolist.todo(1)
        self.todolist.set_todo_completed(todo)
        self.assertTrue(self.todolist.todo(1).is_completed())
        self.assertTrue(self.todolist.dirty)

    def test_todo_priority1(self):
        todo = self.todolist.todo(1)
        self.todolist.set_priority(todo, 'F')

        self.assertEqual(self.todolist.todo(1).priority(), 'F')
        self.assertTrue(self.todolist.dirty)

    def test_todo_priority2(self):
        todo = self.todolist.todo(1)
        self.todolist.set_priority(todo, 'C')

        self.assertFalse(self.todolist.dirty)

    def test_erase(self):
        self.todolist.erase()

        self.assertEqual(self.todolist.count(), 0)
        self.assertTrue(self.todolist.dirty)

    def test_regex1(self):
        """ Multiple hits should result in None. """
        self.assertRaises(InvalidTodoException, self.todolist.todo, "Project1")

    def test_regex3(self):
        todo = self.todolist.todo("project2")
        self.assertTrue(todo)
        self.assertEqual(todo.source(), "(D) Bar @Context1 +Project2")

    def test_uid1(self):
        config("test/data/todolist-uid.conf")

        self.assertEqual(self.todolist.todo('t5c').source(),
                         "(C) Foo @Context2 Not@Context +Project1 Not+Project")

    def test_uid2(self):
        """ Changing the priority should not change the identifier. """
        config("test/data/todolist-uid.conf")

        todo = self.todolist.todo('t5c')
        self.todolist.set_priority(todo, 'B')
        self.assertEqual(self.todolist.todo('t5c').source(),
                         "(B) Foo @Context2 Not@Context +Project1 Not+Project")

    def test_uid3(self):
        """
        Must be able to handle integers when text identifiers are enabled.
        """
        config("test/data/todolist-uid.conf")
        self.assertRaises(InvalidTodoException, self.todolist.todo, 1)

    def test_uid4(self):
        """
        Handle UIDs properly when line numbers are configured.
        """
        config(p_overrides={('topydo', 'identifiers'): 'linenumber'})
        self.assertRaises(InvalidTodoException, self.todolist.todo, '11a')

    def test_uid5(self):
        """ Handle bogus UIDs """
        todo = Todo('invalid')
        self.assertRaises(InvalidTodoException, self.todolist.uid, todo)

    def test_new_uid(self):
        """ Make sure that item has new text ID after append. """
        config("test/data/todolist-uid.conf")
        todo = self.todolist.todo('t5c')
        self.todolist.append(todo, "A")

        self.assertNotEqual(self.todolist.number(todo), 't5c')

    def test_iteration(self):
        """ Confirms that the iternation method is working. """
        results = ["(C) Foo @Context2 Not@Context +Project1 Not+Project",
                   "(D) Bar @Context1 +Project2",
                   "(C) Baz @Context1 +Project1 key:value",
                   "(C) Drink beer @ home",
                   "(C) 13 + 29 = 42"]

        i = 0
        for todo in self.todolist:
            self.assertEqual(todo.src, results[i])
            i += 1

    def test_ids_linenumber(self):
        """ Confirms the ids method lists all todo IDs as line-numbers. """
        config(p_overrides={('topydo', 'identifiers'): 'linenumber'})
        results = {'1', '2', '3', '4', '5'}

        self.assertEqual(results, self.todolist.ids())

    def test_ids_uids(self):
        """ Confirms the ids method lists all todo IDs as text uids. """
        config("test/data/todolist-uid.conf")
        results = {'n8m', 'mfg', 'z63', 't5c', 'wa5'}

        self.assertEqual(results, self.todolist.ids())
Exemplo n.º 15
0
 def setUp(self):
     super().setUp()
     self.todoPath = 'test/data/TodoListTest.txt'
     self.todofile = TodoFile(self.todoPath)
Exemplo n.º 16
0
class TodoListTester(TopydoTest):
    def setUp(self):
        super().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 = TodoListBase(lines)

    def test_contexts(self):
        self.assertEqual(set(['Context1', 'Context2']),
                         self.todolist.contexts())
        self.assertFalse(self.todolist.is_dirty())

    def test_projects(self):
        self.assertEqual(set(['Project1', 'Project2']),
                         self.todolist.projects())
        self.assertFalse(self.todolist.is_dirty())

    def test_add1(self):
        text = "(C) Adding a new task @Context3 +Project3"
        count = self.todolist.count()
        todo = self.todolist.add(text)

        self.assertEqual(self.todolist.todo(count+1).source(), text)
        self.assertEqual(set(['Project1', 'Project2', 'Project3']),
                         self.todolist.projects())
        self.assertEqual(set(['Context1', 'Context2', 'Context3']),
                         self.todolist.contexts())
        self.assertEqual(self.todolist.number(todo), 6)
        self.assertTrue(self.todolist.is_dirty())

    def test_add2(self):
        text = str(self.todolist)
        self.todolist.add('')
        self.assertEqual(str(self.todolist), text)

    def test_add3a(self):
        count = self.todolist.count()
        self.todolist.add('\n(C) New task')

        self.assertEqual(self.todolist.count(), count + 1)
        self.assertEqual(self.todolist.todo(count + 1).source(),
                         '(C) New task')
        self.assertEqual(self.todolist.todo(count + 1).priority(), 'C')

    def test_add3b(self):
        count = self.todolist.count()
        self.todolist.add('(C) New task\n')

        self.assertEqual(self.todolist.count(), count + 1)
        self.assertEqual(self.todolist.todo(count + 1).source(),
                         '(C) New task')
        self.assertEqual(self.todolist.todo(count + 1).priority(), 'C')

    def test_add4(self):
        text = str(self.todolist)
        self.todolist.add(' ')
        self.assertEqual(str(self.todolist), text)

    def test_add5(self):
        text = str(self.todolist)
        self.todolist.add("\n")
        self.assertEqual(str(self.todolist), text)

    def test_delete1(self):
        count = self.todolist.count()
        todo = self.todolist.todo(2)
        self.todolist.delete(todo)

        self.assertEqual(self.todolist.todo(2).source(),
                         "(C) Baz @Context1 +Project1 key:value")
        self.assertEqual(self.todolist.count(), count - 1)
        self.assertTrue(self.todolist.is_dirty())
        self.assertRaises(InvalidTodoException, self.todolist.number, todo)

    def test_delete2(self):
        """ Try to remove a todo item that does not exist. """
        count = self.todolist.count()

        todo = Todo('Not in the list')
        self.todolist.delete(todo)

        self.assertEqual(self.todolist.count(), count)
        self.assertFalse(self.todolist.is_dirty())

    def test_append1(self):
        todo = self.todolist.todo(3)
        self.todolist.append(todo, "@Context3")

        self.assertEqual(todo.source(),
                         "(C) Baz @Context1 +Project1 key:value @Context3")
        self.assertEqual(set(['Context1', 'Context2', 'Context3']),
                         self.todolist.contexts())
        self.assertTrue(self.todolist.is_dirty())

    def test_append2(self):
        todo = self.todolist.todo(3)
        text = todo.text()
        self.todolist.append(todo, "foo:bar")

        self.assertEqual(todo.text(), text)
        self.assertEqual(todo.source(),
                         "(C) Baz @Context1 +Project1 key:value foo:bar")

    def test_append3(self):
        todo = self.todolist.todo(3)
        text = todo.text()
        self.todolist.append(todo, '')

        self.assertEqual(todo.text(), text)

    def test_todo(self):
        count = self.todolist.count()

        self.assertRaises(InvalidTodoException, self.todolist.todo,
                          count + 100)
        self.assertFalse(self.todolist.is_dirty())

    def test_count(self):
        """ Test that empty lines are not counted. """
        self.assertEqual(self.todolist.count(), 5)

    def test_todo_number1(self):
        todo = Todo("No number")
        self.todolist.add_todo(todo)

        todo = self.todolist.todo(6)
        self.assertIsInstance(todo, Todo)
        self.assertEqual(todo.text(), "No number")

    def test_todo_number2(self):
        todo = Todo("Non-existent")
        self.assertRaises(InvalidTodoException, self.todolist.number, todo)

    def test_todo_complete(self):
        todo = self.todolist.todo(1)
        self.todolist.set_todo_completed(todo)
        self.assertTrue(self.todolist.todo(1).is_completed())
        self.assertTrue(self.todolist.is_dirty())

    def test_todo_priority1(self):
        todo = self.todolist.todo(1)
        self.todolist.set_priority(todo, 'F')

        self.assertEqual(self.todolist.todo(1).priority(), 'F')
        self.assertTrue(self.todolist.is_dirty())

    def test_todo_priority2(self):
        todo = self.todolist.todo(1)
        self.todolist.set_priority(todo, 'C')

        self.assertFalse(self.todolist.is_dirty())

    def test_erase(self):
        self.todolist.erase()

        self.assertEqual(self.todolist.count(), 0)
        self.assertTrue(self.todolist.is_dirty())

    def test_regex1(self):
        """ Multiple hits should result in None. """
        self.assertRaises(InvalidTodoException, self.todolist.todo, "Project1")

    def test_regex3(self):
        todo = self.todolist.todo("project2")
        self.assertTrue(todo)
        self.assertEqual(todo.source(), "(D) Bar @Context1 +Project2")

    def test_uid1(self):
        config("test/data/todolist-uid.conf")

        self.assertEqual(self.todolist.todo('t5c').source(),
                         "(C) Foo @Context2 Not@Context +Project1 Not+Project")

    def test_uid2(self):
        """ Changing the priority should not change the identifier. """
        config("test/data/todolist-uid.conf")

        todo = self.todolist.todo('t5c')
        self.todolist.set_priority(todo, 'B')
        self.assertEqual(self.todolist.todo('t5c').source(),
                         "(B) Foo @Context2 Not@Context +Project1 Not+Project")

    def test_uid3(self):
        """
        Must be able to handle integers when text identifiers are enabled.
        """
        config("test/data/todolist-uid.conf")
        self.assertRaises(InvalidTodoException, self.todolist.todo, 1)

    def test_uid4(self):
        """
        Handle UIDs properly when line numbers are configured.
        """
        config(p_overrides={('topydo', 'identifiers'): 'linenumber'})
        self.assertRaises(InvalidTodoException, self.todolist.todo, '11a')

    def test_new_uid(self):
        """ Make sure that item has new text ID after append. """
        config("test/data/todolist-uid.conf")
        todo = self.todolist.todo('t5c')
        self.todolist.append(todo, "A")

        self.assertNotEqual(self.todolist.number(todo), 't5c')

    def test_iteration(self):
        """ Confirms that the iternation method is working. """
        results = ["(C) Foo @Context2 Not@Context +Project1 Not+Project",
                   "(D) Bar @Context1 +Project2",
                   "(C) Baz @Context1 +Project1 key:value",
                   "(C) Drink beer @ home",
                   "(C) 13 + 29 = 42"]

        i = 0
        for todo in self.todolist:
            self.assertEqual(todo.src, results[i])
            i += 1
Exemplo n.º 17
0
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)