def test_can_get_stats(self): user1 = User(0, "LOGIN_USER1", "FN_USER1", "SN_USER1", "MN_USER1", []) user2 = User(0, "LOGIN_USER2", "FN_USER2", "SN_USER2", "MN_USER2", []) _, user1 = (self.core.db.addUser(user1)) _, user2 = (self.core.db.addUser(user2)) alg1 = Algorithm(0, "TITLE_ALG1", "DESCR_ALG1", user1.user_id, "SOURCE_ALG1", [], 0, 0) alg2 = Algorithm(0, "TITLE_ALG2", "DESCR_ALG2", user1.user_id, "SOURCE_ALG2", [], 0, 0) alg3 = Algorithm(0, "TITLE_ALG3", "DESCR_ALG3", user2.user_id, "SOURCE_ALG3", [], 0, 0) _, alg1 = (self.core.db.addAlg(alg1)) _, alg2 = (self.core.db.addAlg(alg2)) _, alg3 = (self.core.db.addAlg(alg3)) test1 = Test(0, alg1.alg_id, "TITLE_TEST1", "SOURCE_TEST1") test2 = Test(0, alg1.alg_id, "TITLE_TEST2", "SOURCE_TEST2") test3 = Test(0, alg2.alg_id, "TITLE_TEST3", "SOURCE_TEST3") test4 = Test(0, alg3.alg_id, "TITLE_TEST4", "SOURCE_TEST4") _, test1 = (self.core.db.addTest(test1)) _, test2 = (self.core.db.addTest(test2)) _, test3 = (self.core.db.addTest(test3)) _, test4 = (self.core.db.addTest(test4)) task1 = Task(0, alg1.alg_id, 0, "MESSAGE_TASK1") task2 = Task(0, alg2.alg_id, 0, "MESSAGE_TASK2") task3 = Task(0, alg3.alg_id, 0, "MESSAGE_TASK3") _, task1 = (self.core.db.addTask(task1)) _, task2 = (self.core.db.addTask(task2)) _, task3 = (self.core.db.addTask(task3)) status, stats = self.core.db.getStats() self.assertEqual(status, DBStatus.s_ok) self.assertEqual(stats.num_of_users, 2) self.assertEqual(stats.num_of_algs, 3) self.assertEqual(stats.num_of_tests, 4) self.assertEqual(stats.num_of_tasks, 3)
def getAllTasks(self) -> Tuple[DBStatus, List[Task]]: try: tasks_collection = self.db["tasks"] tasks_tmp = list(tasks_collection.find({})) tasks = [] for task_d in tasks_tmp: tasks.append(Task.fromDict(task_d)) except Exception as e: logger.error(f"MongoDBWorker connection failed: {e}") return (DBStatus.s_connection_error, [Task(ERROR_ID, ERROR_ID)]) logger.debug("Tasks were found") return (DBStatus.s_ok, tasks)
def getTask(self, task_id) -> Tuple[DBStatus, Task]: try: tasks_collection = self.db["tasks"] task_d = tasks_collection.find_one({"task_id": task_id}) if task_d is None: logger.error("This task does not exist") return (DBStatus.s_data_issue, Task(ERROR_ID, ERROR_ID)) except Exception as e: logger.error(f"MongoDBWorker connection failed: {e}") return (DBStatus.s_connection_error, Task(ERROR_ID, ERROR_ID)) logger.debug("Task was found") return (DBStatus.s_ok, Task.fromDict(task_d))
def test_can_get_all_task(self): user = User(0, "LOGIN_USER", "FN_USER", "SN_USER", "MN_USER", []) _, user = (self.core.db.addUser(user)) alg = Algorithm(0, "TITLE_ALG", "DESCR_ALG", user.user_id, "SOURCE_ALG", [], 0, 0) _, alg = (self.core.db.addAlg(alg)) task1 = Task(0, alg.alg_id, 0, "MESSAGE_TASK1") task2 = Task(0, alg.alg_id, 0, "MESSAGE_TASK2") _, task1 = (self.core.db.addTask(task1)) _, task2 = (self.core.db.addTask(task2)) status, tasks = self.core.db.getAllTasks() self.assertEqual(status, DBStatus.s_ok) self.assertEqual(tasks[0], task1) self.assertEqual(tasks[1], task2)
def test_cant_add_task_with_incorrect_alg_id(self): user = User(0, "LOGIN_USER", "FN_USER", "SN_USER", "MN_USER", []) _, user = (self.core.db.addUser(user)) alg = Algorithm(0, "TITLE_ALG", "DESCR_ALG", user.user_id, "SOURCE_ALG", [], 0, 0) _, alg = (self.core.db.addAlg(alg)) task1 = Task(0, alg.alg_id + len("typo"), 0, "MESSAGE_TASK1") task2 = Task(0, alg.alg_id + len("typo"), 0, "MESSAGE_TASK2") status1, task1 = self.core.db.addTask(task1) status2, task2 = self.core.db.addTask(task2) self.assertEqual(status1, DBStatus.s_data_issue) self.assertEqual(status2, DBStatus.s_data_issue) self.assertEqual(task1.task_id, ERROR_ID) self.assertEqual(task2.task_id, ERROR_ID) self.assertEqual(self.core.db._getNextID("tasks"), 1)
def test_can_get_task_with_correct_id(self): user = User(0, "LOGIN_USER", "FN_USER", "SN_USER", "MN_USER", []) _, user = (self.core.db.addUser(user)) alg = Algorithm(0, "TITLE_ALG", "DESCR_ALG", user.user_id, "SOURCE_ALG", [], 0, 0) _, alg = (self.core.db.addAlg(alg)) task1 = Task(0, alg.alg_id, 0, "MESSAGE_TASK1") task2 = Task(0, alg.alg_id, 0, "MESSAGE_TASK2") _, task1 = (self.core.db.addTask(task1)) _, task2 = (self.core.db.addTask(task2)) status1, task3 = self.core.db.getTask(task1.task_id) status2, task4 = self.core.db.getTask(task2.task_id) self.assertEqual(status1, DBStatus.s_ok) self.assertEqual(status2, DBStatus.s_ok) self.assertEqual(task1, task3) self.assertEqual(task2, task4)
def addTask(self, task) -> Tuple[DBStatus, Task]: try: # check alg st = (self.getAlgByID(task.alg_id))[0] if st == DBStatus.s_data_issue: logger.error("This algorithm does not exist") return (DBStatus.s_data_issue, Task(ERROR_ID, ERROR_ID)) # add task tasks_collection = self.db["tasks"] task.task_id = self._getNextID("tasks") if task.task_id == DBStatus.s_data_issue: return (DBStatus.s_connection_error, Task(ERROR_ID, ERROR_ID)) tasks_collection.insert_one(task.__dict__) except Exception as e: logger.error(f"MongoDBWorker connection failed: {e}") return (DBStatus.s_connection_error, Task(ERROR_ID, ERROR_ID)) logger.debug("New task was successfully added") return (DBStatus.s_ok, task)