def test_translate(self): datastore = DataStore() root_task = datastore.task_factory('root-task', newtask=True) root_task.add_tag('@my-tag') root_task.add_tag('@my-other-tag') root_task.set_title('my task') datastore.push_task(root_task) child_task = datastore.task_factory('child-task', newtask=True) child_task.set_title('my first child') child_task.add_tag('@my-tag') child_task.add_tag('@my-other-tag') child_task.set_text("@my-tag, @my-other-tag, \n\ntask content") datastore.push_task(child_task) root_task.add_child(child_task.get_id()) child2_task = datastore.task_factory('done-child-task', newtask=True) child2_task.set_title('my done child') child2_task.add_tag('@my-tag') child2_task.add_tag('@my-other-tag') child2_task.set_text("@my-tag, @my-other-tag, \n\nother task txt") child2_task.set_status(Task.STA_DONE) datastore.push_task(child2_task) root_task.add_child(child2_task.get_id()) root_task.set_text(f"@my-tag, @my-other-tag\n\nline\n" f"{{!{child_task.get_id()}!}}\n" f"{{!{child2_task.get_id()}!}}\n") self.assertEqual( [child_task.get_id(), child2_task.get_id()], root_task.get_children()) self.assertEqual([root_task.get_id()], child_task.get_parents()) self.assertEqual([], PARENT_FIELD.get_gtg(root_task, NAMESPACE)) self.assertEqual(['child-task', 'done-child-task'], CHILDREN_FIELD.get_gtg(root_task, NAMESPACE)) self.assertEqual(['root-task'], PARENT_FIELD.get_gtg(child_task, NAMESPACE)) self.assertEqual([], CHILDREN_FIELD.get_gtg(child_task, NAMESPACE)) root_vtodo = Translator.fill_vtodo(root_task, 'calname', NAMESPACE) child_vtodo = Translator.fill_vtodo(child_task, 'calname', NAMESPACE) child2_vtodo = Translator.fill_vtodo(child2_task, 'calname', NAMESPACE) self.assertEqual([], PARENT_FIELD.get_dav(vtodo=root_vtodo.vtodo)) self.assertEqual(['child-task', 'done-child-task'], CHILDREN_FIELD.get_dav(vtodo=root_vtodo.vtodo)) self.assertEqual(['root-task'], PARENT_FIELD.get_dav(vtodo=child_vtodo.vtodo)) self.assertEqual([], CHILDREN_FIELD.get_dav(vtodo=child_vtodo.vtodo)) self.assertTrue('\r\nRELATED-TO;RELTYPE=CHILD:child-task\r\n' in root_vtodo.serialize()) self.assertTrue('\r\nRELATED-TO;RELTYPE=PARENT:root-task\r\n' in child_vtodo.serialize()) root_contents = root_vtodo.contents['vtodo'][0].contents child_cnt = child_vtodo.contents['vtodo'][0].contents child2_cnt = child2_vtodo.contents['vtodo'][0].contents for cnt in root_contents, child_cnt, child2_cnt: self.assertEqual(['my-tag', 'my-other-tag'], cnt['categories'][0].value) self.assertEqual('my first child', child_cnt['summary'][0].value) self.assertEqual('my done child', child2_cnt['summary'][0].value) self.assertEqual('task content', child_cnt['description'][0].value) self.assertEqual('other task txt', child2_cnt['description'][0].value) self.assertEqual('line\n[ ] my first child\n[x] my done child', root_contents['description'][0].value)
class TestDatastore(unittest.TestCase): """ Tests for the DataStore object. """ def setUp(self): """ Creates the environment for the tests @returns: None """ self.datastore = DataStore() self.requester = self.datastore.get_requester() def test_task_factory(self): """ Test for the task_factory function """ # generate a Task with a random id tid = str(uuid.uuid4()) task = self.datastore.task_factory(tid, newtask=True) self.assertTrue(isinstance(task, GTG.core.task.Task)) self.assertEqual(task.get_id(), tid) self.assertEqual(task.is_new(), True) tid = str(uuid.uuid4()) task = self.datastore.task_factory(tid, newtask=False) self.assertEqual(task.is_new(), False) def test_new_task_and_has_task(self): """ Tests the new_task function """ task = self.datastore.new_task() tid = task.get_id() self.assertTrue(isinstance(tid, str)) self.assertTrue(tid != '') self.assertTrue(task.is_new()) self.assertTrue(self.datastore.has_task(tid)) self.assertTrue(len(self.datastore.get_all_tasks()) == 1) def test_get_all_tasks(self): """ Tests the get_all_tasks function """ task_ids = [] for i in xrange(1, 10): task = self.datastore.new_task() task_ids.append(task.get_id()) return_list = self.datastore.get_all_tasks() self.assertEqual(len(return_list), i) task_ids.sort() return_list.sort() self.assertEqual(task_ids, return_list) def test_get_task(self): ''' Tests the get_task function ''' task = self.datastore.new_task() self.assertTrue(isinstance(self.datastore.get_task(task.get_id()), GTG.core.task.Task)) self.assertEqual(self.datastore.get_task(task.get_id()), task) def test_get_requester(self): ''' Tests the get_requester function ''' requester = self.datastore.get_requester() self.assertTrue(isinstance(requester, GTG.core.requester.Requester)) def test_get_tasks_tree(self): ''' Tests the get_tasks_tree function ''' tasks_tree = self.datastore.get_tasks_tree() self.assertTrue(isinstance(tasks_tree, Tree)) def test_push_task(self): ''' Tests the push_task function ''' task_ids = [] for i in xrange(1, 10): tid = str(uuid.uuid4()) if tid not in task_ids: task_ids.append(tid) task = self.datastore.task_factory(tid) return_value1 = self.datastore.push_task(task) self.assertTrue(return_value1) # we do it twice, but it should be pushed only once if it's # working correctly (the second should be discarded) return_value2 = self.datastore.push_task(task) self.assertFalse(return_value2) stored_tasks = self.datastore.get_all_tasks() task_ids.sort() stored_tasks.sort() self.assertEqual(task_ids, stored_tasks) def test_register_backend(self): ''' Tests the register_backend function. It also tests the get_all_backends and get_backend function as a side effect ''' # create a simple backend dictionary backend = FakeBackend(enabled=True) tasks_in_backend_count = randint(1, 20) for temp in xrange(0, tasks_in_backend_count): backend.fake_add_random_task() backend_dic = {'backend': backend, 'pid': 'a'} self.datastore.register_backend(backend_dic) all_backends = self.datastore.get_all_backends(disabled=True) self.assertEqual(len(all_backends), 1) registered_backend = self.datastore.get_backend(backend.get_id()) self.assertEqual(backend.get_id(), registered_backend.get_id()) self.assertTrue(isinstance(registered_backend, GTG.core.datastore.TaskSource)) self.assertTrue(registered_backend.is_enabled()) self.assertEqual(registered_backend.fake_get_initialized_count(), 1) # we give some time for the backend to push all its tasks sleep_within_loop(1) self.assertEqual(len(self.datastore.get_all_tasks()), tasks_in_backend_count) # same test, disabled backend backend = FakeBackend(enabled=False) for temp in xrange(1, randint(2, 20)): backend.fake_add_random_task() backend_dic = {'backend': backend, 'pid': 'b'} self.datastore.register_backend(backend_dic) all_backends = self.datastore.get_all_backends(disabled=True) self.assertEqual(len(all_backends), 2) all_backends = self.datastore.get_all_backends(disabled=False) self.assertEqual(len(all_backends), 1) registered_backend = self.datastore.get_backend(backend.get_id()) self.assertEqual(backend.get_id(), registered_backend.get_id()) self.assertTrue(isinstance(registered_backend, GTG.core.datastore.TaskSource)) self.assertFalse(registered_backend.is_enabled()) self.assertEqual(registered_backend.fake_get_initialized_count(), 0) # we give some time for the backend to push all its tasks (is # shouldn't, since it's disabled, but we give time anyway time.sleep(1) self.assertEqual(len(self.datastore.get_all_tasks()), tasks_in_backend_count) def test_set_backend_enabled(self): ''' Tests the set_backend_enabled function ''' enabled_backend = FakeBackend(enabled=True) disabled_backend = FakeBackend(enabled=False) self.datastore.register_backend({'backend': enabled_backend, 'pid': str(uuid.uuid4()), GenericBackend.KEY_DEFAULT_BACKEND: False}) self.datastore.register_backend({'backend': disabled_backend, 'pid': str(uuid.uuid4()), GenericBackend.KEY_DEFAULT_BACKEND: False}) # enabling an enabled backend self.datastore.set_backend_enabled(enabled_backend.get_id(), True) self.assertEqual(enabled_backend.fake_get_initialized_count(), 1) self.assertTrue(enabled_backend.is_enabled()) # disabling a disabled backend self.datastore.set_backend_enabled(disabled_backend.get_id(), False) self.assertEqual(disabled_backend.fake_get_initialized_count(), 0) self.assertFalse(disabled_backend.is_enabled()) # disabling an enabled backend self.datastore.set_backend_enabled(enabled_backend.get_id(), False) self.assertEqual(enabled_backend.fake_get_initialized_count(), 1) countdown = 10 while countdown >= 0 and enabled_backend.is_enabled(): time.sleep(0.1) self.assertFalse(enabled_backend.is_enabled()) # #enabling a disabled backend # self.datastore.set_backend_enabled(disabled_backend.get_id(), True) # self.assertEqual(disabled_backend.fake_get_initialized_count(), 1) # self.assertTrue(disabled_backend.is_enabled()) def test_remove_backend(self): """ Tests the remove_backend function """ enabled_backend = FakeBackend(enabled=True) disabled_backend = FakeBackend(enabled=False) self.datastore.register_backend({'backend': enabled_backend, 'pid': str(uuid.uuid4()), GenericBackend.KEY_DEFAULT_BACKEND: False}) self.datastore.register_backend({'backend': disabled_backend, 'pid': str(uuid.uuid4()), GenericBackend.KEY_DEFAULT_BACKEND: False}) # removing an enabled backend self.datastore.remove_backend(enabled_backend.get_id()) # waiting countdown = 10 while countdown >= 0 and enabled_backend.is_enabled(): time.sleep(0.1) self.assertFalse(enabled_backend.is_enabled()) self.assertEqual( len(self.datastore.get_all_backends(disabled=True)), 1) # removing a disabled backend self.datastore.remove_backend(disabled_backend.get_id()) self.assertFalse(disabled_backend.is_enabled()) self.assertEqual( len(self.datastore.get_all_backends(disabled=True)), 0) def test_flush_all_tasks(self): ''' Tests the flush_all_tasks function ''' # we add some tasks in the datastore tasks_in_datastore_count = 10 # randint(1, 20) for temp in xrange(0, tasks_in_datastore_count): self.datastore.new_task() datastore_stored_tids = self.datastore.get_all_tasks() self.assertEqual(tasks_in_datastore_count, len(datastore_stored_tids)) # we enable a backend backend = FakeBackend(enabled=True) self.datastore.register_backend({'backend': backend, 'pid': 'a'}) # we wait for the signal storm to wear off sleep_within_loop(2) # we sync self.datastore.get_backend(backend.get_id()).sync() # and we inject task in the backend tasks_in_backend_count = 5 # randint(1, 20) for temp in xrange(0, tasks_in_backend_count): backend.fake_add_random_task() backend_stored_tids = backend.fake_get_task_ids() self.assertEqual(tasks_in_backend_count, len(backend_stored_tids)) self.datastore.flush_all_tasks(backend.get_id()) # we wait for the signal storm to wear off sleep_within_loop(2) # we sync self.datastore.get_backend(backend.get_id()).sync() all_tasks_count = tasks_in_backend_count + tasks_in_datastore_count new_datastore_stored_tids = self.datastore.get_all_tasks() new_backend_stored_tids = backend.fake_get_task_ids() self.assertEqual(len(new_backend_stored_tids), all_tasks_count) self.assertEqual(len(new_datastore_stored_tids), all_tasks_count) new_datastore_stored_tids.sort() new_backend_stored_tids.sort() self.assertEqual(new_backend_stored_tids, new_datastore_stored_tids)
class TestDatastore(unittest.TestCase): """ Tests for the DataStore object. """ def setUp(self): """ Creates the environment for the tests @returns: None """ self.datastore = DataStore() self.requester = self.datastore.get_requester() def test_task_factory(self): """ Test for the task_factory function """ # generate a Task with a random id tid = str(uuid.uuid4()) task = self.datastore.task_factory(tid, newtask=True) self.assertTrue(isinstance(task, GTG.core.task.Task)) self.assertEqual(task.get_id(), tid) self.assertEqual(task.is_new(), True) tid = str(uuid.uuid4()) task = self.datastore.task_factory(tid, newtask=False) self.assertEqual(task.is_new(), False) def test_new_task_and_has_task(self): """ Tests the new_task function """ task = self.datastore.new_task() tid = task.get_id() self.assertTrue(isinstance(tid, str)) self.assertTrue(tid != '') self.assertTrue(task.is_new()) self.assertTrue(self.datastore.has_task(tid)) self.assertTrue(len(self.datastore.get_all_tasks()) == 1) def test_get_all_tasks(self): """ Tests the get_all_tasks function """ task_ids = [] for i in xrange(1, 10): task = self.datastore.new_task() task_ids.append(task.get_id()) return_list = self.datastore.get_all_tasks() self.assertEqual(len(return_list), i) task_ids.sort() return_list.sort() self.assertEqual(task_ids, return_list) def test_get_task(self): ''' Tests the get_task function ''' task = self.datastore.new_task() self.assertTrue(isinstance(self.datastore.get_task(task.get_id()), GTG.core.task.Task)) self.assertEqual(self.datastore.get_task(task.get_id()), task) def test_get_requester(self): ''' Tests the get_requester function ''' requester = self.datastore.get_requester() self.assertTrue(isinstance(requester, GTG.core.requester.Requester)) def test_get_tasks_tree(self): ''' Tests the get_tasks_tree function ''' tasks_tree = self.datastore.get_tasks_tree() self.assertTrue(isinstance(tasks_tree, Tree)) def test_push_task(self): ''' Tests the push_task function ''' task_ids = [] for i in xrange(1, 10): tid = str(uuid.uuid4()) if tid not in task_ids: task_ids.append(tid) task = self.datastore.task_factory(tid) return_value1 = self.datastore.push_task(task) self.assertTrue(return_value1) # we do it twice, but it should be pushed only once if it's # working correctly (the second should be discarded) return_value2 = self.datastore.push_task(task) self.assertFalse(return_value2) stored_tasks = self.datastore.get_all_tasks() task_ids.sort() stored_tasks.sort() self.assertEqual(task_ids, stored_tasks) def test_register_backend(self): ''' Tests the register_backend function. It also tests the get_all_backends and get_backend function as a side effect ''' # create a simple backend dictionary backend = FakeBackend(enabled=True) tasks_in_backend_count = randint(1, 20) for temp in xrange(0, tasks_in_backend_count): backend.fake_add_random_task() backend_dic = {'backend': backend, 'pid': 'a'} self.datastore.register_backend(backend_dic) all_backends = self.datastore.get_all_backends(disabled=True) self.assertEqual(len(all_backends), 1) registered_backend = self.datastore.get_backend(backend.get_id()) self.assertEqual(backend.get_id(), registered_backend.get_id()) self.assertTrue(isinstance(registered_backend, GTG.core.datastore.TaskSource)) self.assertTrue(registered_backend.is_enabled()) self.assertEqual(registered_backend.fake_get_initialized_count(), 1) # we give some time for the backend to push all its tasks sleep_within_loop(1) self.assertEqual(len(self.datastore.get_all_tasks()), tasks_in_backend_count) # same test, disabled backend backend = FakeBackend(enabled=False) for temp in xrange(1, randint(2, 20)): backend.fake_add_random_task() backend_dic = {'backend': backend, 'pid': 'b'} self.datastore.register_backend(backend_dic) all_backends = self.datastore.get_all_backends(disabled=True) self.assertEqual(len(all_backends), 2) all_backends = self.datastore.get_all_backends(disabled=False) self.assertEqual(len(all_backends), 1) registered_backend = self.datastore.get_backend(backend.get_id()) self.assertEqual(backend.get_id(), registered_backend.get_id()) self.assertTrue(isinstance(registered_backend, GTG.core.datastore.TaskSource)) self.assertFalse(registered_backend.is_enabled()) self.assertEqual(registered_backend.fake_get_initialized_count(), 0) # we give some time for the backend to push all its tasks (is # shouldn't, since it's disabled, but we give time anyway time.sleep(1) self.assertEqual(len(self.datastore.get_all_tasks()), tasks_in_backend_count) def test_set_backend_enabled(self): ''' Tests the set_backend_enabled function ''' enabled_backend = FakeBackend(enabled=True) disabled_backend = FakeBackend(enabled=False) self.datastore.register_backend({'backend': enabled_backend, 'pid': str(uuid.uuid4()), GenericBackend.KEY_DEFAULT_BACKEND: False}) self.datastore.register_backend({'backend': disabled_backend, 'pid': str(uuid.uuid4()), GenericBackend.KEY_DEFAULT_BACKEND: False}) # enabling an enabled backend self.datastore.set_backend_enabled(enabled_backend.get_id(), True) self.assertEqual(enabled_backend.fake_get_initialized_count(), 1) self.assertTrue(enabled_backend.is_enabled()) # disabling a disabled backend self.datastore.set_backend_enabled(disabled_backend.get_id(), False) self.assertEqual(disabled_backend.fake_get_initialized_count(), 0) self.assertFalse(disabled_backend.is_enabled()) # disabling an enabled backend self.datastore.set_backend_enabled(enabled_backend.get_id(), False) self.assertEqual(enabled_backend.fake_get_initialized_count(), 1) countdown = 10 while countdown >= 0 and enabled_backend.is_enabled(): time.sleep(0.1) self.assertFalse(enabled_backend.is_enabled()) # #enabling a disabled backend # self.datastore.set_backend_enabled(disabled_backend.get_id(), True) # self.assertEqual(disabled_backend.fake_get_initialized_count(), 1) # self.assertTrue(disabled_backend.is_enabled()) def test_remove_backend(self): """ Tests the remove_backend function """ enabled_backend = FakeBackend(enabled=True) disabled_backend = FakeBackend(enabled=False) self.datastore.register_backend({'backend': enabled_backend, 'pid': str(uuid.uuid4()), GenericBackend.KEY_DEFAULT_BACKEND: False}) self.datastore.register_backend({'backend': disabled_backend, 'pid': str(uuid.uuid4()), GenericBackend.KEY_DEFAULT_BACKEND: False}) # removing an enabled backend self.datastore.remove_backend(enabled_backend.get_id()) # waiting countdown = 10 while countdown >= 0 and enabled_backend.is_enabled(): time.sleep(0.1) self.assertFalse(enabled_backend.is_enabled()) self.assertEqual( len(self.datastore.get_all_backends(disabled=True)), 1) # removing a disabled backend self.datastore.remove_backend(disabled_backend.get_id()) self.assertFalse(disabled_backend.is_enabled()) self.assertEqual( len(self.datastore.get_all_backends(disabled=True)), 0) def test_flush_all_tasks(self): ''' Tests the flush_all_tasks function ''' # we add some tasks in the datastore tasks_in_datastore_count = 10 # randint(1, 20) for temp in xrange(0, tasks_in_datastore_count): self.datastore.new_task() datastore_stored_tids = self.datastore.get_all_tasks() self.assertEqual(tasks_in_datastore_count, len(datastore_stored_tids)) # we enable a backend backend = FakeBackend(enabled=True) self.datastore.register_backend({'backend': backend, 'pid': 'a'}) # we wait for the signal storm to wear off sleep_within_loop(2) # we sync self.datastore.get_backend(backend.get_id()).sync() # and we inject task in the backend tasks_in_backend_count = 5 # randint(1, 20) for temp in xrange(0, tasks_in_backend_count): backend.fake_add_random_task() backend_stored_tids = backend.fake_get_task_ids() self.assertEqual(tasks_in_backend_count, len(backend_stored_tids)) self.datastore.flush_all_tasks(backend.get_id()) # we wait for the signal storm to wear off sleep_within_loop(2) # we sync self.datastore.get_backend(backend.get_id()).sync() all_tasks_count = tasks_in_backend_count + tasks_in_datastore_count new_datastore_stored_tids = self.datastore.get_all_tasks() new_backend_stored_tids = backend.fake_get_task_ids() self.assertEqual(len(new_backend_stored_tids), all_tasks_count) self.assertEqual(len(new_datastore_stored_tids), all_tasks_count) new_datastore_stored_tids.sort() new_backend_stored_tids.sort() self.assertEqual(new_backend_stored_tids, new_datastore_stored_tids)