diff --git a/libs/labelbox/src/labelbox/schema/project.py b/libs/labelbox/src/labelbox/schema/project.py index 6a6a9dfed..5bfa8538b 100644 --- a/libs/labelbox/src/labelbox/schema/project.py +++ b/libs/labelbox/src/labelbox/schema/project.py @@ -1723,6 +1723,11 @@ def move_data_rows_to_task_queue( Returns: None if successful, or a raised error on failure + Raises: + ResourceNotFoundError: If the task queue or the project does + not exist. + LabelboxError: If the data rows could not be moved. + """ if not isinstance(data_row_ids, get_args(DataRowIdentifiers)): @@ -1747,19 +1752,32 @@ def move_data_rows_to_task_queue( % method ) - task_id = self.client.execute( - query_str, - { - "projectId": self.uid, - "queueId": task_queue_id, - "dataRowIdentifiers": { - "ids": [id for id in data_row_ids], - "idType": data_row_ids.id_type, + try: + result = self.client.execute( + query_str, + { + "projectId": self.uid, + "queueId": task_queue_id, + "dataRowIdentifiers": { + "ids": [id for id in data_row_ids], + "idType": data_row_ids.id_type, + }, }, - }, - timeout=180.0, - experimental=True, - )["project"][method]["taskId"] + timeout=180.0, + experimental=True, + raise_return_resource_not_found=True, + ) + except ResourceNotFoundError as e: + missing = ( + f"project {self.uid!r}" + if task_queue_id is None + else f"task queue {task_queue_id!r} or project {self.uid!r}" + ) + raise ResourceNotFoundError( + message=f"Data rows were not moved: {missing} was not found " + f"({e.message})" + ) from e + task_id = result["project"][method]["taskId"] task = self._wait_for_task(task_id) if task.status != "COMPLETE": diff --git a/libs/labelbox/tests/integration/test_task_queue.py b/libs/labelbox/tests/integration/test_task_queue.py index 0cd66cb62..626f98d06 100644 --- a/libs/labelbox/tests/integration/test_task_queue.py +++ b/libs/labelbox/tests/integration/test_task_queue.py @@ -1,4 +1,8 @@ import time + +import pytest +from lbox.exceptions import ResourceNotFoundError + from labelbox import Project from labelbox.schema.identifiables import GlobalKeys, UniqueIds @@ -88,3 +92,8 @@ def test_move_to_task(configured_batch_project_with_label): UniqueIds([data_row.uid]), review_queue.uid ) _validate_moved(project, "MANUAL_REVIEW_QUEUE", 1) + + +def test_move_to_task_invalid_queue_id(project: Project): + with pytest.raises(ResourceNotFoundError, match="invalid-id"): + project.move_data_rows_to_task_queue(UniqueIds(["", ""]), "invalid-id") diff --git a/libs/labelbox/tests/unit/test_unit_project_move_data_rows_to_task_queue.py b/libs/labelbox/tests/unit/test_unit_project_move_data_rows_to_task_queue.py new file mode 100644 index 000000000..fcce33818 --- /dev/null +++ b/libs/labelbox/tests/unit/test_unit_project_move_data_rows_to_task_queue.py @@ -0,0 +1,168 @@ +import json +from unittest.mock import MagicMock, patch + +import pytest +import requests +from lbox.exceptions import LabelboxError, ResourceNotFoundError +from lbox.request_client import RequestClient + +from labelbox import GlobalKeys, UniqueIds +from labelbox.schema.project import Project + +PROJECT_FIELDS = { + "id": "test_project_id", + "name": "test", + "createdAt": "2021-06-01T00:00:00.000Z", + "updatedAt": "2021-06-01T00:00:00.000Z", + "autoAuditNumberOfLabels": 1, + "autoAuditPercentage": 100, + "dataRowCount": 1, + "description": "test", + "editorTaskType": "MODEL_CHAT_EVALUATION", + "lastActivityTime": "2021-06-01T00:00:00.000Z", + "allowedMediaType": "IMAGE", + "setupComplete": "2021-06-01T00:00:00.000Z", + "modelSetupComplete": None, + "uploadType": "Auto", + "isBenchmarkEnabled": False, + "isConsensusEnabled": False, +} + +TASK_RESPONSE = { + "project": {"createBulkAddRowsToQueueTask": {"taskId": "task_1"}} +} + + +@pytest.fixture +def mock_client(): + return MagicMock() + + +@pytest.fixture +def project(mock_client): + return Project(mock_client, PROJECT_FIELDS) + + +def _task(status, errors=None): + task = MagicMock() + task.status = status + task.errors = errors + return task + + +def _not_found_response(message): + response = requests.Response() + response.status_code = 200 + response._content = json.dumps( + { + "data": None, + "errors": [ + { + "message": message, + "extensions": {"code": "RESOURCE_NOT_FOUND"}, + } + ], + } + ).encode() + return response + + +def test_move_data_rows_to_task_queue_sends_mutation_and_waits_for_task( + project, mock_client +): + mock_client.execute.return_value = TASK_RESPONSE + + with patch.object( + Project, "_wait_for_task", return_value=_task("COMPLETE") + ) as wait_for_task: + result = project.move_data_rows_to_task_queue( + UniqueIds(["dr_1", "dr_2"]), "queue_1" + ) + + assert result is None + wait_for_task.assert_called_once_with("task_1") + mock_client.execute.assert_called_once() + query_str, params = mock_client.execute.call_args[0] + assert "AddDataRowsToTaskQueueAsyncPyApi" in query_str + assert params == { + "projectId": "test_project_id", + "queueId": "queue_1", + "dataRowIdentifiers": {"ids": ["dr_1", "dr_2"], "idType": "ID"}, + } + assert ( + mock_client.execute.call_args.kwargs["raise_return_resource_not_found"] + is True + ) + + +def test_move_data_rows_to_task_queue_raises_when_task_fails( + project, mock_client +): + mock_client.execute.return_value = TASK_RESPONSE + + with patch.object( + Project, + "_wait_for_task", + return_value=_task("FAILED", errors={"message": "boom"}), + ): + with pytest.raises(LabelboxError, match="boom"): + project.move_data_rows_to_task_queue(GlobalKeys(["gk_1"]), None) + + +def test_move_data_rows_to_task_queue_raises_when_queue_not_found( + project, mock_client +): + mock_client.execute.side_effect = ResourceNotFoundError( + message="Resource not found" + ) + + with pytest.raises(ResourceNotFoundError) as exc_info: + project.move_data_rows_to_task_queue( + UniqueIds(["dr_1"]), "missing_queue" + ) + + message = str(exc_info.value) + assert "missing_queue" in message + assert "test_project_id" in message + assert "Resource not found" in message + + +def test_move_data_rows_to_task_queue_names_only_the_project_for_done_queue( + project, mock_client +): + mock_client.execute.side_effect = ResourceNotFoundError( + message="Resource not found" + ) + + with pytest.raises(ResourceNotFoundError) as exc_info: + project.move_data_rows_to_task_queue(UniqueIds(["dr_1"])) + + message = str(exc_info.value) + assert "test_project_id" in message + assert "task queue" not in message + + +def test_move_data_rows_to_task_queue_not_found_through_request_client(): + """A RESOURCE_NOT_FOUND answer from the server must reach the caller as + a ResourceNotFoundError, not as a TypeError from reading a None result. + """ + request_client = RequestClient( + sdk_version="test", api_key="test_api_key", endpoint="http://test" + ) + client = MagicMock() + client.execute.side_effect = request_client.execute + project = Project(client, PROJECT_FIELDS) + + with patch.object( + requests.Session, + "send", + return_value=_not_found_response("Task queue not found"), + ): + with pytest.raises(LabelboxError) as exc_info: + project.move_data_rows_to_task_queue( + UniqueIds(["", ""]), "invalid-id" + ) + + assert isinstance(exc_info.value, ResourceNotFoundError) + assert "invalid-id" in str(exc_info.value) + assert "Task queue not found" in str(exc_info.value)