class TestSnapshots(unittest.TestCase): def setUp(self) -> None: self.db = JsonFileDatabase() self.db.initialize(Task()) self.db.clear() self.tasks = Tasks(self.db) def tearDown(self) -> None: self.db.clear() def test_count_all(self): self.tasks.add("task1", "label1", "project1", "today") t1 = self.tasks.get_task_by_name("task1") self.tasks.complete(t1.unique_id) self.tasks.add("task2", "label1", "project1", "today") t2 = self.tasks.get_task_by_name("task2") self.tasks.delete(t2.unique_id) self.tasks.add("task3", "label1", "project1", "today") self.tasks.add("task4", "label1", "project1", "today") snapshots = Snapshots(self.tasks) snapshots.count_all_tasks() summary, snapshot_list = snapshots.get_snapshot() self.assertIsNotNone(summary) self.assertTrue(summary.count == 4) self.assertTrue(summary.deleted == 1) self.assertTrue(summary.incomplete == 2) self.assertTrue(summary.completed == 1) def test_count_by_due_date_range(self): self.tasks.add("task1", "label1", "project1", "2021-07-14") self.tasks.add("task2", "label1", "project1", "2021-09-01") self.tasks.add("task3", "label1", "project1", "2021-09-01") self.tasks.add("task4", "label1", "project1", "2021-11-01") snapshots = Snapshots(self.tasks) snapshots.count_tasks_by_due_date_range("2021-07-13", "2021-11-02") summary, snapshot_list = snapshots.get_snapshot() self.assertIsNotNone(summary) self.assertEqual(summary.count, 4) self.assertEqual(summary.completed, 0) self.assertEqual(summary.incomplete, 4) self.assertEqual(summary.deleted, 0) self.assertTrue(len(snapshot_list) == 3) def test_count_project(self): self.tasks.add("task1", "label1", "project1", "today") self.tasks.add("task2", "label1", "project2", "today") self.tasks.add("task3", "label1", "project2", "today") self.tasks.add("task4", "label1", "project2", "today") snapshots = Snapshots(self.tasks) snapshots.count_tasks_by_project("project2") summary, snapshot_list = snapshots.get_snapshot() self.assertIsNotNone(summary) self.assertEqual(summary.count, 3) self.assertEqual(summary.completed, 0) self.assertEqual(summary.incomplete, 3) self.assertEqual(summary.deleted, 0) self.assertTrue(len(snapshot_list) == 1) def test_invalid_operation(self): snapshots = Snapshots(self.tasks) summary, snapshot_list = snapshots.get_snapshot() self.assertIsNotNone(summary) self.assertEqual(summary.count, 0) self.assertEqual(summary.completed, 0) self.assertEqual(summary.incomplete, 0) self.assertEqual(summary.deleted, 0) self.assertTrue(len(snapshot_list) == 0)
class TestJsonDatabase(unittest.TestCase): def setUp(self) -> None: self.json_db = JsonFileDatabase() self.json_db.initialize(Task()) self.task1 = Task("task1") self.task1.index = 1 self.task2 = Task("task2") self.task2.index = 2 self.task3 = Task("task3") self.task3.index = 3 self.task4 = Task("task4") self.task4.index = 4 self.task5 = Task("task5") self.task5.index = 5 self.task6 = Task("task6") self.task6.index = 6 self.task7 = Task("task7") self.task7.index = 7 self.task8 = Task("task8") self.task8.index = 8 self.task9 = Task("task9") self.task9.index = 9 self.task10 = Task("task10") self.task10.index = 10 def tearDown(self) -> None: self.json_db.clear() def test_multiple_insert_using_should_not_create_duplicates(self): self.json_db.set([ self.task1, self.task2, self.task3, self.task4, self.task5, self.task6, self.task7, self.task8, self.task9, self.task10 ]) self.json_db.set([ self.task1, self.task2, self.task3, self.task4, self.task5, self.task6, self.task7, self.task8, self.task9, self.task10 ]) task_list = self.json_db.to_object_list(self.json_db.get(), Task()) self.assertTrue(len(task_list) == 10) def test_object_serialization(self): self.json_db.set([self.task1]) dict_list = self.json_db.get() task_list = self.json_db.to_object_list(dict_list, Task()) self.assertTrue(len(task_list) == 1) task = task_list[0] self.assertEqual(self.task1.text, task.text) self.assertEqual(self.task1.index, task.index) self.assertEqual(self.task1.unique_id, task.unique_id) self.assertEqual(self.task1.project, task.project) self.assertEqual(self.task1.date_expression, task.date_expression) self.assertEqual(self.task1.priority, task.priority) self.assertEqual(self.task1.label, task.label) def test_object_must_be_subclass_of_DatabaseObject(self): with self.assertRaises(ValueError): self.json_db.set([MissingSubclass]) def test_object_must_contain_iter_method(self): with self.assertRaises(TypeError): self.json_db.set([MissingIterMethod()]) def test_replace_object(self): self.json_db.set([self.task1]) task_list = self.json_db.to_object_list(self.json_db.get(), Task()) self.assertTrue(len(task_list) == 1) self.task1.text = "testtask1" self.json_db.replace(self.task1) task_list = self.json_db.to_object_list(self.json_db.get(), Task()) self.assertTrue(len(task_list) == 1) self.assertEqual(self.task1.index, 1) self.assertEqual(self.task1.text, "testtask1") def test_append_object(self): self.json_db.set([self.task1]) task_list = self.json_db.to_object_list(self.json_db.get(), Task()) self.assertTrue(len(task_list) == 1) self.json_db.append(self.task2) task_list = self.json_db.to_object_list(self.json_db.get(), Task()) self.assertTrue(len(task_list) == 2) self.assertEqual(task_list[0].index, 1) self.assertEqual(task_list[1].index, 2) def test_get_index(self): task_list = [dict(self.task1), dict(self.task2), dict(self.task3)] index = self.json_db.get_list_index(self.task2, task_list) self.assertEqual(1, index) index = self.json_db.get_list_index(self.task4, task_list) self.assertIsNone(index)
class TestCsvFileImporter(unittest.TestCase): def setUp(self): self.db = JsonFileDatabase() self.db.initialize(Task()) self.tasks = Tasks(self.db) self.importer = CsvFileImporter(self.tasks) def tearDown(self): self.db.clear() def test_convert_tasks(self): obj_list = list() obj_list.append({ 'index': '1', 'done': 'True', 'text': 'task1', 'project': 'tmp', 'label': 'current', 'due_date': '2020-09-10', 'last_updated': '2020-09-10 22:37:23', 'deleted': 'False', 'unique_id': 'bc2d81c94e3844228ccb9bfe2613c089' }) obj_list.append({ 'index': '2', 'done': 'False', 'text': 'task2', 'project': 'tmp', 'label': 'current', 'due_date': '2020-10-06', 'last_updated': '2020-10-06 22:00:46', 'deleted': 'False', 'unique_id': '3413035ab1f14cccb70a315e42c3242e' }) obj_list.append({ 'index': '3', 'done': 'False', 'text': 'task3', 'project': 'tmp', 'label': 'current', 'due_date': '2020-10-06', 'last_updated': '2020-10-06 22:01:05', 'deleted': 'False', 'unique_id': '1fd2878c4c64446fbbce21b79a7ce1ca' }) task_list = self.importer.convert(obj_list) self.assertTrue(len(task_list) == 3) def test_import_tasks(self): task100 = Task('Task100') task100.label = "added" task100.unique_id = "1" task101 = Task('Task100') task101.label = "updated" task101.deleted = False task101.unique_id = "1" task102 = Task('Task100') task102.deleted = True task102.label = "deleted" task102.unique_id = "1" tasks_list = [task100, task101, task102] sync_results = self.importer.import_tasks(tasks_list) sync_results_list = sync_results.get_list() self.assertTrue(len(sync_results_list) == 3) print(sync_results_list) self.assertTrue(sync_results_list[0] == SyncAction.ADDED) self.assertTrue(sync_results_list[1] == SyncAction.UPDATED) self.assertTrue(sync_results_list[2] == SyncAction.DELETED)
class TestTasks(unittest.TestCase): def setUp(self): self.vars = CommonVariables() self.db = JsonFileDatabase() self.db.initialize(Task()) self.tasks = Tasks(self.db) self.task1 = Task("Task1") self.task1.label = "waiting" self.task1.date_expression = "may 2" self.task1.external_id = 1 self.tasks.append(self.task1) self.task2 = Task("Task2") self.task2.label = "computer" self.task2.date_expression = "may 3" self.tasks.append(self.task2) self.task3 = Task("Task3") self.task3.label = "office" self.task3.date_expression = "may 4" self.tasks.append(self.task3) self.task4 = Task("Task4") self.task4.label = "waiting" self.task4.date_expression = "may 5" self.tasks.append(self.task4) self.task5 = Task("Task5") self.task5.label = "office" self.task5.date_expression = "may 6" self.tasks.append(self.task5) self.task6 = Task("Task6") self.task6.label = "call" self.task6.date_expression = "may 7" self.tasks.append(self.task6) self.task7 = Task("Task7") self.task7.label = "computer" self.task7.date_expression = "may 8" self.tasks.append(self.task7) self.task8 = Task("Task8") self.task8.label = "call" self.task8.date_expression = "may 9" self.tasks.append(self.task8) self.task9 = Task("Task9") self.task9.label = "office" self.task9.date_expression = "may 10" self.tasks.append(self.task9) self.task10 = Task("Task10") self.task10.label = "waiting" self.task10.date_expression = "may 11" self.tasks.append(self.task10) self.future_task = Task("Future Task") future_datetime = datetime.now() + timedelta(days=1) date_string = future_datetime.strftime(self.vars.date_format) self.future_task.due_date = DueDate(date_string) self.tasks.append(self.future_task) def tearDown(self): self.db.clear() def test_get_task(self): task = self.tasks.get_task_by_index(1) self.assertTrue(task.index == 1) def test_add_task(self): task = Task("Task11") self.tasks.append(task) task_list = self.tasks.get_object_list() print(f"test_add_task: len {len(task_list)}") self.assertTrue(len(task_list) == 12) def test_deleted_task(self): self.tasks.delete(self.task1.unique_id) task_list = self.tasks.get_object_list() self.assertTrue(len(task_list) == 11) task = task_list[0] self.assertEqual(task.unique_id, self.task1.unique_id) self.assertTrue(task.deleted) def test_complete_task(self): due_date_list = DateGenerator().get_due_dates("every sa") tasks = Tasks(self.db) tasks.clear_objects() tasks.add("Repetitive task", "current", "home", "every sa") task_list = tasks.get_object_list() self.assertTrue(len(due_date_list) == len(task_list)) for task in task_list: task.complete() for task in task_list: self.assertTrue(task.is_completed()) def test_task_is_complete(self): tasks = Tasks(self.db) tasks.clear_objects() tasks.add("Simple Task", "current", "home", "today") task_list = tasks.get_object_list() self.assertTrue(len(task_list) == 1) task1 = task_list[0] self.assertTrue(task1.due_date.date_string == Today().to_date_string()) task1.complete() self.assertTrue(task1.is_completed()) def test_sorting_by_label(self): task_list = self.tasks.get_tasks_by_label("call") self.assertTrue(type(task_list), list) self.assertTrue(len(task_list) == 2) first_task = task_list[0] self.assertTrue(first_task.label == "call") def test_get_unique_labels(self): unique_label_list = self.tasks.get_label_list() self.assertListEqual( unique_label_list, ['call', 'computer', 'current', 'office', 'waiting']) def test_get_list_by_type(self): returned_result = self.tasks.get_list_by_type( "label", "call", self.tasks.get_object_list()) self.assertTrue(len(returned_result) == 2) def test_get_list_by_date_expression(self): task_list = self.tasks.get_tasks_by_date("tomorrow") self.assertTrue(len(task_list) == 1) task = task_list[0] self.assertTrue(task.text == "Future Task") def test_replace(self): task = self.tasks.get_task_by_name("Task1") task.deleted = True existing_task = self.tasks.replace(self.task1, task) self.assertTrue(existing_task.deleted) self.assertEqual(task.text, "Task1") def test_reset(self): tasks = Tasks(self.db) tasks.clear_objects() tasks.add("InitialTask", "current", "home", "2020-05-11") initial_task = tasks.get_task_by_name("InitialTask") tasks.reset(initial_task.unique_id) self.assertEqual(initial_task.due_date.date_string, "2020-05-11") modified_task = tasks.get_task_by_name("InitialTask") current_date_string = Today().to_date_string() self.assertTrue( modified_task.due_date.date_string == current_date_string) def test_reschedule_tasks(self): tasks = Tasks(self.db) tasks.clear_objects() tasks.add("task1", "label1", "project1", "2021-10-22") task1 = tasks.get_task_by_name("task1") self.assertIsNotNone(task1) # Deleted tasks should not be rescheduled tasks.add("task2", "label2", "project2", "2021-10-22") task2 = tasks.get_task_by_name("task2") self.assertIsNotNone(task2) tasks.delete(task2.unique_id) # Completed tasks should not be rescheduled tasks.add("task3", "label3", "project3", "2021-10-22") task3 = tasks.get_task_by_name("task3") self.assertIsNotNone(task3) tasks.complete(task3.unique_id) today = Today() tasks.reschedule(today) task_list = tasks.get_tasks_by_date(today.to_date_string()) self.assertTrue(len(task_list) == 1) rescheduled_task = task_list[0] self.assertEqual(rescheduled_task.text, "task1") self.assertEqual(rescheduled_task.label, "label1") self.assertEqual(rescheduled_task.project, "project1")