def test_created(self): """creation date works""" task_str = "2010-10-01 foo bar baz +proj1 +proj2 @context foo:bar" task = Task("foo bar baz", projects=['+proj1', '+proj2'], contexts=['@context'], tags={'foo':'bar'}) task.create = date(2010,10,1) self.assertEqual(str(task), task_str)
def test_simple_done(self): """Simple done - no date""" task_str = "x foo bar baz +proj1 +proj2 @context foo:bar" task = Task("foo bar baz", projects=['+proj1', '+proj2'], contexts=['@context'], tags={'foo':'bar'}) task.done = True self.assertEqual(str(task), task_str)
def test_priority(self): """priority is OK""" task_str = "(B) foo bar baz +proj1 +proj2 @context foo:bar" task = Task("foo bar baz", projects=['+proj1', '+proj2'], contexts=['@context'], tags={'foo':'bar'}) task.priority = "(B)" self.assertEqual(str(task), task_str)
def test_dated_done(self): """Dated done""" task_str = "x 2010-10-01 foo bar baz +proj1 +proj2 @context foo:bar" task = Task("foo bar baz", projects=['+proj1', '+proj2'], contexts=['@context'], tags={'foo':'bar'}) task.done = True task.finish = date(2010,10,1) self.assertEqual(str(task), task_str)
def test_relevancy_test(self): task = Task(1, '') for i_vis, vis in enumerate(['hidden', 'discreet', 'wide']): task.visibility = vis for (t_ctx, q_ctx), expected in TestContextAnalysis.relevancy_cases.items(): task.context = t_ctx result = task.is_relevant_to_context(q_ctx) self.assertEqual(result, expected[i_vis])
def test_create_finish(self): """create and finish dates come out right""" task_str = "x 2010-10-01 2010-09-05 foo bar baz +proj1 +proj2 @context foo:bar" task = Task("foo bar baz", projects=['+proj1', '+proj2'], contexts=['@context'], tags={'foo':'bar'}) task.done = True task.finish = date(2010,10,1) task.create = date(2010,9,5) self.assertEqual(str(task), task_str)
def test_relevancy_test(self): task = Task(1, '') for i_vis, vis in enumerate(['hidden', 'discreet', 'wide']): task.visibility = vis for (t_ctx, q_ctx ), expected in TestContextAnalysis.relevancy_cases.items(): task.context = t_ctx result = task.is_relevant_to_context(q_ctx) self.assertEqual(result, expected[i_vis])
def test_tasks_index_with_tasks(self): task_1 = Task('some task', False) task_2 = Task('some other task', True) db.session.add(task_1) db.session.add(task_2) db.session.commit() rv = self.app.get('/tasks') assert [task_1.to_json, task_2.to_json] == json.loads(rv.data)
def test_complete(self): """completion""" todo_line = "x foo bar +proj1 +proj2 @context tag:value" task = Task.parse(todo_line) self.assertEqual(task.done, True) self.assertEqual(task.finish, None) todo_line = "x 2013-12-15 foo bar +proj1 +proj2 @context tag:value" task = Task.parse(todo_line) self.assertEqual(task.done, True) self.assertEqual(task.finish, date(2013,12,15))
class TestObject: TEST_TRACKER = ToDoTracker() TEST_TASK_1 = Task("Task 1") TEST_TASK_2 = Task("Task 2") TEST_TASK_3 = Task("Task 3") TEST_TODO_1 = ToDo("ToDo 1") TEST_TODO_2 = ToDo("ToDo 2") TEST_TODO_3 = ToDo("ToDo 3") TEST_TODO_4 = ToDo("ToDo 4") TEST_TODO_5 = ToDo("ToDo 5")
def setUp(self): self.test_id = "testitem" self.test_text = "a test checklist item" self.ended = Task(task=self.test_text) self.ended.tags={"checklist":"%s_complete" % (self.test_id,)} self.ended.do() self.half_ended_insane = Task(task=self.test_text) self.half_ended_insane.tags={"checklist":"%s_complete" % (self.test_id,)} self.half_ended = Task(task=self.test_text) self.half_ended.tags={"checklist":"%s" % (self.test_id,)} self.half_ended.do() self.active = Task(task=self.test_text) self.active.tags={"checklist":self.test_id}
def test_dates(self): """date handling""" todo_line = "2010-2-23 foo bar +proj1 +proj2 @context tag:value" task = Task.parse(todo_line) self.assertEqual(task.create, date(2010,2,23)) todo_line = "x 2010-2-23 foo bar +proj1 +proj2 @context tag:value" task = Task.parse(todo_line) self.assertEqual(task.finish, date(2010,2,23)) self.assertEqual(task.create, None) todo_line = "x 2010-2-23 2010-2-22 foo bar +proj1 +proj2 @context tag:value" task = Task.parse(todo_line) self.assertEqual(task.finish, date(2010,2,23)) self.assertEqual(task.create, date(2010,2,22))
def test_tasks_reordering_existing_task(self): task_1 = Task('task 1', False, 0) task_2 = Task('task 2', False, 1) task_3 = Task('task 3', False, 2) task_4 = Task('task 4', False, 3) db.session.add(task_1) db.session.add(task_2) db.session.add(task_3) db.session.add(task_4) db.session.commit() rv = self.app.put('/tasks/' + str(task_3.id), content_type='application/json', content_length=11, data=json.dumps({'position': 1})) assert [0, 2, 1, 3] == map(lambda task: task.position, Task.query.order_by(Task.id.asc()).all())
def test_tasks_reordering_existing_task_response(self): task_1 = Task('task 1', False, 0) task_2 = Task('task 2', False, 1) db.session.add(task_1) db.session.add(task_2) db.session.commit() rv = self.app.put('/tasks/' + str(task_1.id), content_type='application/json', content_length=11, data=json.dumps({'position': 1})) assert { "completed": False, "id": 1, "text": "task 1", "position": 1 } == json.loads(rv.data)
class TestTasksSort(unittest.TestCase): # A list of correctly sorted tasks. The test consists in sorting them # and checking that the sorted list is the same as the original tasks = [ Task(0, '', created=NOW, priority=3), Task(1, '', created=NOW - timedelta(minutes=1), priority=2), Task(2, '', created=NOW - timedelta(minutes=2), deadline=NOW + timedelta(days=3)), Task(3, '', created=NOW - timedelta(minutes=3), deadline=NOW + timedelta(days=4)), Task(4, '', created=NOW - timedelta(minutes=6), context='world'), Task(5, '', created=NOW - timedelta(minutes=7)), Task(6, '', created=NOW - timedelta(minutes=5), context='hello') ] contexts = {'world': {'p': 2}} def test_task_sort(self): tasks_cpy = sorted( TestTasksSort.tasks, key=lambda t: t.order_infos(TestTasksSort.contexts), ) # tasks_cpy.sort(key=lambda t: t.order_infos(TestTasksSort.contexts)) self.assertEqual(tasks_cpy, TestTasksSort.tasks)
def test_leading_space(self): """leading spaces""" todo_line = " foo bar +proj1 +proj2 @context tag:value" task = Task.parse(todo_line) self.assertEqual(task.priority, "") self.assertEqual(task.create, None) self.assertEqual(task.finish, None) self.assertEqual(task.done, False) self.assertEqual(task.task, " foo bar") self.assertEqual(set(task.projects), set(["+proj1", "+proj2"])) self.assertEqual(set(task.contexts), set(["@context"])) self.assertEqual(task.tags, {"tag":"value"}) todo_line = " (A) foo bar +proj1 +proj2 @context tag:value" task = Task.parse(todo_line) self.assertEqual(task.priority, "") self.assertEqual(task.create, None) self.assertEqual(task.finish, None) self.assertEqual(task.done, False) self.assertEqual(task.task, " (A) foo bar") self.assertEqual(set(task.projects), set(["+proj1", "+proj2"])) self.assertEqual(set(task.contexts), set(["@context"])) self.assertEqual(task.tags, {"tag":"value"}) todo_line = " x foo bar +proj1 +proj2 @context tag:value" task = Task.parse(todo_line) self.assertEqual(task.priority, "") self.assertEqual(task.create, None) self.assertEqual(task.finish, None) self.assertEqual(task.done, False) self.assertEqual(task.task, " x foo bar") self.assertEqual(set(task.projects), set(["+proj1", "+proj2"])) self.assertEqual(set(task.contexts), set(["@context"])) self.assertEqual(task.tags, {"tag":"value"}) todo_line = " 2010-10-2 foo bar +proj1 +proj2 @context tag:value" task = Task.parse(todo_line) self.assertEqual(task.priority, "") self.assertEqual(task.create, date(2010,10,2)) self.assertEqual(task.finish, None) self.assertEqual(task.done, False) self.assertEqual(task.task, " foo bar") self.assertEqual(set(task.projects), set(["+proj1", "+proj2"])) self.assertEqual(set(task.contexts), set(["@context"])) self.assertEqual(task.tags, {"tag":"value"})
def post(self, opravilo_id): task = Task.get_by_id(int(opravilo_id)) text = self.request.get("new_text") task.task_text = text task.put() params = {"new_text": task} return self.render_template("opravila.html", params=params)
def post(self): task_name = self.request.get("task_name") task_text = self.request.get("task_text") done_check = self.request.get("done") if done_check: todo = Task(task_name=task_name, task_text=task_text, status=True) todo.put() else: todo = Task(task_name=task_name, task_text=task_text, status=False) todo.put() return self.render_template("index.html")
def test_task(self): """basic task parsing""" todo_line = "foo bar +proj1 +proj2 @context tag:value" task = Task.parse(todo_line) self.assertEqual(task.priority, "") self.assertEqual(task.create, None) self.assertEqual(task.finish, None) self.assertEqual(task.done, False) self.assertEqual(task.task, "foo bar") self.assertEqual(set(task.projects), set(["+proj1", "+proj2"])) self.assertEqual(set(task.contexts), set(["@context"])) self.assertEqual(task.tags, {"tag":"value"})
def test_prioritystuff(self): """Priority parsing helpers are good...""" t = Task() t.priority = "(A)" self.assertEqual(t.priority, "(A)") t.priority = "A" self.assertEqual(t.priority, "(A)") t.priority = "a" self.assertEqual(t.priority, "(A)") t.priority = "(a)" self.assertEqual(t.priority, "(A)") with self.assertRaises(Exception): t.priority = '1' with self.assertRaises(Exception): t.priority = '(a' with self.assertRaises(Exception): t.priority = 'foo' with self.assertRaises(Exception): t.priority = 'qq' with self.assertRaises(Exception): t.priority = 'ABC'
def test_set_is_done_2(): task = Task(title='Ugotować obiad', details='pokroic warzywa') task.set_is_done(True) assert task.is_done is True task.set_is_done(False) assert task.is_done is False
class TestDispatch(unittest.TestCase): cases = { '': [('show', { 'context': '' })], '-c hello': [('show', { 'context': 'hello' })], '-a hello': [('add_task', { 'content': 'hello' })], '-t 1 -p 3': [ ('get_task_by_id', { 'id_': '1' }), ('apply_mutator', { 'mutator': 'priority', 'value': 3 }), ], '-d 1': [ ('get_task_by_id', { 'id_': '1' }), ('set_done', {}), ], '-c hello -p 2': [('apply_context_mutator', { 'context': 'hello', 'mutator': 'priority', 'value': 2 })] } todolist = todo.TodoList([Task(id_=1, content='')], {}) def test_dispatch(self): for line, sequence in TestDispatch.cases.items(): argv = line.split() args = todo.parse_args(argv) calls = [] handler = get_trace_handler(calls) sys.settrace(handler) todo.dispatch(args, TestDispatch.todolist) sys.settrace(None) calls = calls[1:] for (name, args), (e_name, e_args) in zip(sequence, calls): self.assertEqual(name, e_name) for arg_name, arg_value in args.items(): self.assertIn(arg_name, e_args) self.assertEqual(arg_value, e_args[arg_name])
def test_tasks_uncompleting_existing_task(self): task = Task('some task', True) db.session.add(task) db.session.commit() rv = self.app.put('/tasks/' + str(task.id), content_type='application/json', content_length=16, data=json.dumps({'completed': False})) assert { "completed": False, "id": 1, "text": "some task", "position": 0 } == json.loads(rv.data)
def test_undo(self): """make sure undo works""" task = Task("foo bar", autodate=True) task.priority = 'a' task.do() task.do(False) self.assertEqual(task.finish, None) self.assertEqual(task.done, False) self.assertEqual(task.create, date.today()) self.assertEqual(task.priority, "(A)")
def test_new_task_must_have_positive_ID(self): """A new task must have a positive ID.""" self.assertRaises(ValueError, lambda: Task("New message.", -1))
def test_set_is_done(): task = Task(title='Sprzatanie', details='poodkurzac') task.set_is_done(True) assert task.is_done is True
def test_str_task_is_done(): task = Task(title='Sprzatanie', details='poodkurzac') task.set_is_done(True) result = task.__str__() expected = 'Sprzatanie\npoodkurzac\nis done: True' assert result == expected
def test_str(): task = Task(title='Sprzatanie', details='poodkurzac') result = task.__str__() expected = 'Sprzatanie\npoodkurzac\nis done: False' assert result == expected
def test_new_task_is_uncompleted(self): """A new task is uncompleted.""" new = Task("New message", 1) self.assertFalse(new.completed)
def test_task_can_convert_self_to_dictionary(self): """A task can return itself as a dictionary.""" new = Task("New message", 1) dict_str = new.to_dict() compare = {"text": "New message", "id": 1, "completed": False} self.assertEqual(dict_str, compare)
def test_do(self): """make sure do function works""" task = Task("foo bar") task.do() self.assertEqual(task.finish, date.today()) self.assertEqual(task.done, True)
def get(self): deleted_tasks = Task.query(Task.deleted == True).fetch() params = {"deleted_tasks": deleted_tasks} return self.render_template("izbrisana_opravila.html", params=params)
def post(self, opravilo_id): message_model = Task.get_by_id(int(opravilo_id)) message_model.key.delete() message_model.deleted = True message_model.put() return self.render_template("opravila.html")
def get(self): all_tasks = Task.query(Task.deleted == False).fetch() params = {"all_tasks": all_tasks} return self.render_template("opravila.html", params=params)
def test_empty_line(self): x = Task.parse("") self.assertEqual(x, None)
class TestChecklistItemCommon(TestCase): """use mocks to handle a simple recurring item, rather than conflating actual ones""" def setUp(self): self.test_id = "testitem" self.test_text = "a test checklist item" self.ended = Task(task=self.test_text) self.ended.tags={"checklist":"%s_complete" % (self.test_id,)} self.ended.do() self.half_ended_insane = Task(task=self.test_text) self.half_ended_insane.tags={"checklist":"%s_complete" % (self.test_id,)} self.half_ended = Task(task=self.test_text) self.half_ended.tags={"checklist":"%s" % (self.test_id,)} self.half_ended.do() self.active = Task(task=self.test_text) self.active.tags={"checklist":self.test_id} def test_task_ended(self): """Base: make sure the task_ended method works""" self.assertTrue(ChecklistItem.task_ended(self.ended)) self.assertFalse(ChecklistItem.task_ended(self.half_ended_insane)) self.assertFalse(ChecklistItem.task_ended(self.half_ended)) self.assertFalse(ChecklistItem.task_ended(self.active)) def test_process_no_history(self): """Base: ensure no history is OK""" # must have simple schedule next because not implemented... class TI(SchedulePasses, ChecklistItem): pass i = TI(id=self.test_id, text=self.test_text) t = i.process(None) self.assertEqual(t.tags['checklist'], self.test_id) self.assertEqual(t.task, self.test_text) self.assertEqual(t.create, date.today()) def test_latest_task_active(self): """Base: don't do anything if item isn't past-due""" class TestItem(PastDueFails, SchedulePasses, ChecklistItem): pass i = TestItem(id=self.test_id, text=self.test_text) self.assertEqual(i.process(self.active), None) def test_ended_scheduling(self): """Base: test scheduling secanrios with properly ended task""" class TestItem(PastDuePasses, SchedulePasses, ChecklistItem): pass i = TestItem(id=self.test_id, text=self.test_text) t = i.process(self.ended) self.assertEqual(t.tags['checklist'], self.test_id) self.assertEqual(t.task, self.test_text) self.assertEqual(t.create, date.today()) class TestItem(PastDuePasses, ScheduleFails, ChecklistItem): pass i = TestItem(id=self.test_id, text=self.test_text) t = i.process(self.ended) self.assertEqual(t,None) def test_finish_in_process(self): """Base: test that process ends things appropriately""" class TestItem(PastDuePasses, SchedulePasses, ChecklistItem): pass i = TestItem(id=self.test_id, text=self.test_text) t = i.process(self.half_ended) # assume task_ended works for now... it is tested other places... self.assertTrue(i.task_ended(self.half_ended)) id, status = self.half_ended.tags["checklist"].partition("_")[::2] self.assertEquals(status, "complete") t = i.process(self.active) self.assertTrue(i.task_ended(self.active)) id, status = self.active.tags["checklist"].partition("_")[::2] self.assertEquals(status, "incomplete") t = i.process(self.half_ended_insane) self.assertTrue(i.task_ended(self.half_ended_insane)) id, status = self.half_ended_insane.tags["checklist"].partition("_")[::2] self.assertEquals(status, "incomplete")
def test_task_must_have_content(self): """A new task must have content.""" self.assertRaises(ValueError, lambda: Task(None, 1))