Commit 159ee189 authored by Bram Schoenmakers's avatar Bram Schoenmakers

Fix deletion of todo items by textual ID.

* When using textual IDs, TodoListBase.number() wouldn't return an
  integer, making deletion fail (which expected an int).
* When updating the todo - UID map, erase it first. Otherwise deleted
* items will be still in there.
parent c7b66a4c
...@@ -15,8 +15,10 @@ ...@@ -15,8 +15,10 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
import CommandTest import CommandTest
from topydo.lib.Config import config
from topydo.lib.DeleteCommand import DeleteCommand from topydo.lib.DeleteCommand import DeleteCommand
from topydo.lib.TodoList import TodoList from topydo.lib.TodoList import TodoList
from topydo.lib.TodoListBase import InvalidTodoException
class DeleteCommandTest(CommandTest.CommandTest): class DeleteCommandTest(CommandTest.CommandTest):
def setUp(self): def setUp(self):
...@@ -98,6 +100,16 @@ class DeleteCommandTest(CommandTest.CommandTest): ...@@ -98,6 +100,16 @@ class DeleteCommandTest(CommandTest.CommandTest):
self.assertEquals(self.output, "") self.assertEquals(self.output, "")
self.assertEquals(self.errors, "Invalid todo number given.\n") self.assertEquals(self.errors, "Invalid todo number given.\n")
def test_del9(self):
""" Test deletion with textual IDs. """
config("test/data/todolist-uid.conf")
command = DeleteCommand(["b0n"], self.todolist, self.out, self.error)
command.execute()
self.assertEquals(str(self.todolist), "Foo")
self.assertRaises(InvalidTodoException, self.todolist.todo, 'b0n')
def test_empty(self): def test_empty(self):
command = DeleteCommand([], self.todolist, self.out, self.error) command = DeleteCommand([], self.todolist, self.out, self.error)
command.execute() command.execute()
......
...@@ -90,7 +90,7 @@ class TodoList(TodoListBase): ...@@ -90,7 +90,7 @@ class TodoList(TodoListBase):
def delete(self, p_todo): def delete(self, p_todo):
""" Deletes a todo item from the list. """ """ Deletes a todo item from the list. """
number = self.number(p_todo) number = self._todos.index(p_todo)
for child in self.children(p_todo): for child in self.children(p_todo):
self.remove_dependency(p_todo, child) self.remove_dependency(p_todo, child)
...@@ -98,7 +98,8 @@ class TodoList(TodoListBase): ...@@ -98,7 +98,8 @@ class TodoList(TodoListBase):
for parent in self.parents(p_todo): for parent in self.parents(p_todo):
self.remove_dependency(parent, p_todo) self.remove_dependency(parent, p_todo)
del self._todos[number - 1] del self._todos[number]
self._update_todo_ids()
self.dirty = True self.dirty = True
......
...@@ -153,8 +153,9 @@ class TodoListBase(object): ...@@ -153,8 +153,9 @@ class TodoListBase(object):
def delete(self, p_todo): def delete(self, p_todo):
""" Deletes a todo item from the list. """ """ Deletes a todo item from the list. """
number = self.number(p_todo) number = self._todos.index(p_todo)
del self._todos[number - 1] del self._todos[number]
self._update_todo_ids()
self.dirty = True self.dirty = True
def erase(self): def erase(self):
...@@ -249,6 +250,9 @@ class TodoListBase(object): ...@@ -249,6 +250,9 @@ class TodoListBase(object):
# the idea is to have a hash that is independent of the position of the # the idea is to have a hash that is independent of the position of the
# todo. Use the text (without tags) of the todo to keep the id as stable # todo. Use the text (without tags) of the todo to keep the id as stable
# as possible (not influenced by priorities or due dates, etc.) # as possible (not influenced by priorities or due dates, etc.)
self._todo_id_map = {}
self._id_todo_map = {}
uids = hash_list_values(self._todos, lambda t: hash(t.text())) uids = hash_list_values(self._todos, lambda t: hash(t.text()))
for (todo, uid) in uids: for (todo, uid) in uids:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment