def test_task(self) -> None: """Ensure tasks can be created and queried as expected""" assert self.db is not None, "No db initialized" db: MephistoDB = self.db project_name, project_id = get_test_project(db) # Check creation and retrieval of a task task_name_1 = "test_task" task_type = "mock" task_id_1 = db.new_task(task_name_1, task_type, project_id=project_id) self.assertIsNotNone(task_id_1) self.assertTrue(isinstance(task_id_1, str)) task_row = db.get_task(task_id_1) self.assertEqual(task_row["task_name"], task_name_1) self.assertEqual(task_row["task_type"], task_type) self.assertEqual(task_row["project_id"], project_id) self.assertIsNone(task_row["parent_task_id"]) task = Task(db, task_id_1) self.assertEqual(task.task_name, task_name_1) # Check creation of a task with a parent task, but no project task_name_2 = "test_task_2" task_id_2 = db.new_task(task_name_2, task_type, parent_task_id=task_id_1) self.assertIsNotNone(task_id_2) self.assertTrue(isinstance(task_id_2, str)) task_row = db.get_task(task_id_2) self.assertEqual(task_row["task_name"], task_name_2) self.assertEqual(task_row["task_type"], task_type) self.assertEqual(task_row["parent_task_id"], task_id_1) self.assertIsNone(task_row["project_id"]) task = Task(db, task_id_2) self.assertEqual(task.task_name, task_name_2) # Check finding for tasks tasks = db.find_tasks() self.assertEqual(len(tasks), 2) self.assertTrue(isinstance(tasks[0], Task)) # Check finding for specific tasks tasks = db.find_tasks(task_name=task_name_1) self.assertEqual(len(tasks), 1) self.assertTrue(isinstance(tasks[0], Task)) self.assertEqual(tasks[0].db_id, task_id_1) self.assertEqual(tasks[0].task_name, task_name_1) tasks = db.find_tasks(project_id=project_id) self.assertEqual(len(tasks), 1) self.assertTrue(isinstance(tasks[0], Task)) self.assertEqual(tasks[0].db_id, task_id_1) self.assertEqual(tasks[0].task_name, task_name_1) tasks = db.find_tasks(parent_task_id=task_id_1) self.assertEqual(len(tasks), 1) self.assertTrue(isinstance(tasks[0], Task)) self.assertEqual(tasks[0].db_id, task_id_2) self.assertEqual(tasks[0].task_name, task_name_2) tasks = db.find_tasks(task_name="fake_name") self.assertEqual(len(tasks), 0)
def get_task(self) -> "Task": """Return the task used to initialize this run""" if self.__task is None: from mephisto.data_model.task import Task self.__task = Task(self.db, self.task_id) return self.__task
def test_task_fails(self) -> None: """Ensure task creation fails under specific cases""" assert self.db is not None, "No db initialized" db: MephistoDB = self.db # Cant get non-existent entry with self.assertRaises(EntryDoesNotExistException): task = Task(db, self.get_fake_id("Task")) task_name = "test_task" task_name_2 = "test_task_2" task_type = "mock" task_id = db.new_task(task_name, task_type) # Can't create same task again with self.assertRaises(EntryAlreadyExistsException): task_id = db.new_task(task_name, task_type) # Can't create task with invalid project with self.assertRaises(EntryDoesNotExistException): fake_id = self.get_fake_id("Project") task_id = db.new_task(task_name_2, task_type, project_id=fake_id) # Can't create task with invalid parent task with self.assertRaises(EntryDoesNotExistException): fake_id = self.get_fake_id("Task") task_id = db.new_task(task_name_2, task_type, parent_task_id=fake_id) # Can't use no name with self.assertRaises(MephistoDBException): task_id = db.new_task("", task_type) # Ensure no tasks were created tasks = db.find_tasks() self.assertEqual(len(tasks), 1)
def get_task(self) -> "Task": """Return the Task this agent is working within""" if self._task is None: if self._task_run is not None: self._task = self._task_run.get_task() else: from mephisto.data_model.task import Task self._task = Task(self.db, self.task_id) return self._task
def get_task(self) -> Task: """ Return the task run that this assignment is part of """ if self.__task is None: if self.__task_run is not None: self.__task = self.__task_run.get_task() else: self.__task = Task(self.db, self.task_id) return self.__task
def find_tasks( self, task_name: Optional[str] = None, project_id: Optional[str] = None, parent_task_id: Optional[str] = None, ) -> List[Task]: """ Try to find any task that matches the above. When called with no arguments, return all tasks. """ with self.table_access_condition: conn = self._get_connection() c = conn.cursor() c.execute( """ SELECT * from tasks WHERE (?1 IS NULL OR task_name = ?1) AND (?2 IS NULL OR project_id = ?2) AND (?3 IS NULL OR parent_task_id = ?3) """, (task_name, nonesafe_int(project_id), nonesafe_int(parent_task_id)), ) rows = c.fetchall() return [Task(self, str(r["task_id"]), row=r) for r in rows]