Skip to content

Commit

Permalink
feat: update tests to delete QueryAssets
Browse files Browse the repository at this point in the history
  • Loading branch information
theodu committed Aug 28, 2023
1 parent be721d0 commit bb39dd5
Show file tree
Hide file tree
Showing 20 changed files with 116 additions and 242 deletions.
4 changes: 2 additions & 2 deletions src/kili/entrypoints/mutations/asset/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,8 +516,8 @@ def verify_last_batch(last_batch: Dict, results: List) -> None:
# it happens when no assets have been sent to review
if isinstance(result, dict) and "id" in result:
assets_in_review = self.kili_api_gateway.list_assets(
["id"],
AssetWhere(project_id=result["id"], asset_id_in=asset_ids, status_in=["TO_REVIEW"]),
["id"],
QueryOptions(disable_tqdm=True),
None,
)
Expand Down Expand Up @@ -598,8 +598,8 @@ def verify_last_batch(last_batch: Dict, results: List) -> None:
result = self.format_result("data", results[0])
if isinstance(result, dict) and "id" in result:
assets_in_queue = self.kili_api_gateway.list_assets(
["id"],
AssetWhere(project_id=result["id"], asset_id_in=asset_ids, status_in=["ONGOING"]),
["id"],
QueryOptions(disable_tqdm=True),
None,
)
Expand Down
2 changes: 1 addition & 1 deletion src/kili/entrypoints/queries/label/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -640,8 +640,8 @@ def export_labels_as_df(
"""
services.get_project(self, project_id, ["id"])
assets_gen = self.kili_api_gateway.list_assets(
asset_fields + ["labels." + field for field in fields],
AssetWhere(project_id=project_id),
asset_fields + ["labels." + field for field in fields],
QueryOptions(disable_tqdm=False),
None,
)
Expand Down
2 changes: 1 addition & 1 deletion src/kili/gateways/kili_api_gateway/asset/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ class AssetOperationMixin:

def list_assets(
self,
fields: List[str],
where: AssetWhere,
fields: List[str],
options: QueryOptions,
post_call_function: Optional[Callable],
):
Expand Down
2 changes: 1 addition & 1 deletion src/kili/services/asset_import/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,8 +445,8 @@ def filter_duplicate_external_ids(self, assets):
if len(assets) == 0:
raise ImportValidationError("No assets to import")
assets_in_project = self.kili.kili_api_gateway.list_assets(
["externalId"],
AssetWhere(project_id=self.project_params.project_id),
["externalId"],
QueryOptions(disable_tqdm=True),
None,
)
Expand Down
4 changes: 2 additions & 2 deletions src/kili/services/copy_project/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ def download_and_upload_assets(assets):
return self._upload_assets(new_project_id, downloaded_assets)

asset_gen = self.kili.kili_api_gateway.list_assets(
fields, where, options, download_and_upload_assets
where, fields, options, download_and_upload_assets
)
# Generator needs to be iterated over to actually fetch assets
for _ in asset_gen:
Expand Down Expand Up @@ -237,8 +237,8 @@ def _upload_assets(self, new_project_id, assets):
# pylint: disable=too-many-locals
def _copy_labels(self, from_project_id: str, new_project_id: str) -> None:
assets_new_project = self.kili.kili_api_gateway.list_assets(
["id", "externalId"],
AssetWhere(project_id=new_project_id),
["id", "externalId"],
QueryOptions(disable_tqdm=True),
None,
)
Expand Down
10 changes: 3 additions & 7 deletions src/kili/services/export/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

import requests

from kili.core.graphql.operations.asset.queries import AssetQuery, AssetWhere
from kili.core.helpers import validate_category_search_query
from kili.gateways.kili_api_gateway.asset.types import AssetWhere
from kili.gateways.kili_api_gateway.queries import QueryOptions
from kili.services.export.types import ExportType
from kili.use_cases.asset.media_downloader import get_download_assets_function
Expand Down Expand Up @@ -139,7 +139,7 @@ def fetch_assets(
where = AssetWhere(**asset_where_params)

if download_media:
count = AssetQuery(kili.graphql_client, kili.http_client).count(where)
count = kili.kili_api_gateway.count_assets(where)
if count > THRESHOLD_WARN_MANY_ASSETS:
warnings.warn(
f"Downloading many assets ({count}). This might take a while. Consider"
Expand All @@ -151,11 +151,7 @@ def fetch_assets(
post_call_function, fields = get_download_assets_function(
kili.kili_api_gateway, download_media, fields, project_id, local_media_dir
)
assets = list(
AssetQuery(kili.graphql_client, kili.http_client)(
where, fields, options, post_call_function
)
)
assets = list(kili.kili_api_gateway.list_assets(where, fields, options, post_call_function))
attach_name_to_assets_labels_author(assets, export_type)
return assets

Expand Down
2 changes: 1 addition & 1 deletion src/kili/services/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ def _build_id_map(kili: Kili, asset_external_ids, project_id):
for external_ids_batch in pagination.BatchIteratorBuilder(asset_external_ids, 1000):
assets_generators.append(
kili.kili_api_gateway.list_assets(
["id", "externalId"],
AssetWhere(project_id, external_id_strictly_in=external_ids_batch),
["id", "externalId"],
QueryOptions(disable_tqdm=True),
None,
)
Expand Down
2 changes: 1 addition & 1 deletion src/kili/use_cases/asset/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def list_assets(
post_call_function, fields = get_download_assets_function(
self._kili_api_gateway, download_media, fields, where.project_id, local_media_dir
)
assets_gen = self._kili_api_gateway.list_assets(fields, where, options, post_call_function)
assets_gen = self._kili_api_gateway.list_assets(where, fields, options, post_call_function)

if label_output_format == "parsed_label":
project: LabelParsingProject = self._kili_api_gateway.get_project(
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/entrypoints/cli/project/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
import pytest_mock
from click.testing import CliRunner

from kili.core.graphql.operations.asset.queries import AssetQuery
from kili.core.graphql.operations.project.queries import ProjectQuery
from kili.entrypoints.cli.project.create import create_project
from kili.entrypoints.cli.project.describe import describe_project
from kili.entrypoints.cli.project.export import export_labels
from kili.entrypoints.cli.project.import_ import import_assets
from kili.entrypoints.cli.project.list_ import list_projects
from kili.gateways.kili_api_gateway.asset import AssetOperationMixin
from tests.integration.entrypoints.cli.helpers import debug_subprocess_pytest

from .mocks.assets import mocked__project_assets
Expand Down Expand Up @@ -454,7 +454,7 @@ def test_import(
def test_export(name: str, test_case: List[str], mocker: pytest_mock.MockerFixture):
mocker.patch.dict("os.environ", {"KILI_API_KEY": "toto", "KILI_SDK_SKIP_CHECKS": "True"})
mocker.patch.object(ProjectQuery, "__call__", side_effect=mocked__ProjectQuery)
mocker.patch.object(AssetQuery, "__call__", side_effect=mocked__project_assets)
mocker.patch.object(AssetOperationMixin, "list_assets", side_effect=mocked__project_assets)
mocker.patch(
"kili.services.export.format.base.AbstractExporter._has_data_connection", return_value=False
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@


from typing import Dict, Generator, List
from unittest.mock import patch
from unittest.mock import MagicMock, patch

import pytest
from typeguard import check_type

from kili.core.graphql.operations.asset.queries import AssetQuery
from kili.core.graphql.operations.user.queries import UserQuery
from kili.entrypoints.queries.asset import QueriesAsset
from kili.entrypoints.queries.user import QueriesUser
from kili.presentation.client.asset import AssetClientMethods


@pytest.mark.parametrize(
Expand Down Expand Up @@ -38,16 +37,19 @@ def test_users_query_return_type(mocker, args, kwargs, expected_return_type):
(("project-id",), {}, List[Dict]),
(("project-id",), {"as_generator": False}, List[Dict]),
(("project-id",), {"as_generator": True}, Generator[Dict, None, None]),
(("project-id",), {"label_output_format": "parsed_label"}, List[Dict]),
(
("project-id",),
{"label_output_format": "parsed_label", "as_generator": True},
Generator[Dict, None, None],
),
((), {"project_id": "project-id"}, List[Dict]),
((), {"project_id": "project-id", "as_generator": True}, Generator[Dict, None, None]),
((), {"project_id": "project-id", "as_generator": False}, List[Dict]),
],
)
@patch.object(AssetQuery, "__call__")
def test_assets_query_return_type(mocker, args, kwargs, expected_return_type):
kili = QueriesAsset()
kili.graphql_client = mocker.MagicMock()
kili.http_client = mocker.MagicMock()

result = kili.assets(*args, **kwargs)
def test_assets_query_return_type(kili_api_gateway, args, kwargs, expected_return_type):
asset_client_methods = AssetClientMethods()
asset_client_methods.kili_api_gateway = MagicMock()
result = asset_client_methods.assets(*args, **kwargs)
assert check_type("result", result, expected_return_type) is None
57 changes: 40 additions & 17 deletions tests/integration/use_cases/test_asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,29 @@ def test_given_query_parameters_I_can_query_assets_and_get_their_labels_parsed(
kili_api_gateway: KiliAPIGateway,
):
# mocking
json_response = {"JOB_0": {"categories": [{"name": "CATGORY_A"}]}}
label = {
"author": {
"id": "cldbnzmmq00go0jwc20fq1jkl",
"email": "[email protected]",
},
"createdAt": "2023-05-11T16:01:48.093Z",
"id": "clhjbhrul015m0k7hct21drz4",
"jsonResponse": {"JOB_0": {"text": "some text abc"}},
}
asset = {
"labels": [label],
"latestLabel": label,
"content": "https://storage.googleapis.com/label-backend-staging/",
"createdAt": "2023-05-11T15:55:01.134Z",
"externalId": "4bad2303e43bfefa0169d890c68f5c9d--cherry-blossom-tree-blossom-trees.jpg",
"id": "asset_id",
"labels": [{"jsonResponse": json_response}],
"latestLabel": {"jsonResponse": json_response},
"isHoneypot": False,
"jsonMetadata": {},
"skipped": False,
"status": "LABELED",
}
json_interface = {
"jobs": {
"JOB_0": {
"mlTask": "CLASSIFICATION",
"isChild": False,
"content": {
"categories": {
"CATGORY_A": {"children": [], "name": "category A", "id": "category30"},
},
"input": "checkbox",
},
}
}
"jobs": {"JOB_0": {"mlTask": "TRANSCRIPTION", "required": 1, "isChild": False}}
}
kili_api_gateway.list_assets.return_value = (asset for asset in [asset])
kili_api_gateway.get_project.return_value = {
Expand All @@ -67,7 +71,22 @@ def test_given_query_parameters_I_can_query_assets_and_get_their_labels_parsed(
# given parameters to query assets
asset_use_cases = AssetUseCases(kili_api_gateway)
where = AssetWhere(project_id="project_id")
fields = ["id", "label.jsonResponse", "latestLabel.jsonresponse"]
fields = [
"content",
"createdAt",
"externalId",
"id",
"isHoneypot",
"jsonMetadata",
"labels.author.id",
"labels.author.email",
"labels.createdAt",
"labels.id",
"labels.jsonResponse",
"skipped",
"status",
"latestLabel.jsonResponse",
]
options = QueryOptions(disable_tqdm=False)

# when creating query assets
Expand All @@ -79,12 +98,16 @@ def test_given_query_parameters_I_can_query_assets_and_get_their_labels_parsed(
local_media_dir=None,
label_output_format="parsed_label",
)
returned_asset = list(asset_gen)[0]

# then
returned_assets = list(asset_gen)
assert len(returned_assets) == 1
returned_asset = returned_assets[0]
assert returned_asset == asset
assert isinstance(returned_asset["latestLabel"], ParsedLabel)
assert returned_asset["latestLabel"].jobs["JOB_0"].text == "some text abc"
assert isinstance(returned_asset["labels"][0], ParsedLabel)
assert returned_asset["labels"][0].jobs["JOB_0"].text == "some text abc"


def test_given_query_parameters_I_can_query_assets_and_download_their_media(
Expand Down
21 changes: 8 additions & 13 deletions tests/unit/services/asset_import/test_import_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
from uuid import UUID

from kili.core.graphql.operations.asset.mutations import GQL_APPEND_MANY_ASSETS
from kili.core.graphql.operations.asset.queries import AssetQuery
from kili.core.graphql.operations.organization.queries import OrganizationQuery
from kili.core.graphql.operations.project.queries import ProjectQuery
from kili.orm import Asset
from kili.gateways.kili_api_gateway.asset import AssetOperationMixin
from kili.services.asset_import import import_assets
from kili.services.asset_import.exceptions import MimeTypeError
from tests.unit.services.asset_import.base import ImportTestCase
Expand All @@ -21,19 +20,15 @@
@patch("kili.utils.bucket.generate_unique_id", mocked_unique_id)
@patch("kili.utils.bucket.request_signed_urls", mocked_request_signed_urls)
@patch("kili.utils.bucket.upload_data_via_rest", mocked_upload_data_via_rest)
@patch.object(
AssetQuery,
"__call__",
return_value=[],
)
@patch.object(AssetOperationMixin, "list_assets", return_value=[])
@patch.object(
OrganizationQuery,
"__call__",
side_effect=mocked_organization_with_upload_from_local(upload_local_data=True),
)
class TestContentType(ImportTestCase):
@patch.object(ProjectQuery, "__call__", side_effect=mocked_project_input_type("VIDEO_LEGACY"))
@patch.object(AssetQuery, "count", return_value=1)
@patch.object(AssetOperationMixin, "count_assets", return_value=1)
def test_cannot_upload_an_image_to_video_project(self, *_):
url = "https://storage.googleapis.com/label-public-staging/car/car_1.jpg"
path_image = self.downloader(url)
Expand All @@ -42,23 +37,23 @@ def test_cannot_upload_an_image_to_video_project(self, *_):
import_assets(self.kili, self.project_id, assets, disable_tqdm=True)

@patch.object(ProjectQuery, "__call__", side_effect=mocked_project_input_type("IMAGE"))
@patch.object(AssetQuery, "count", return_value=1)
@patch.object(AssetOperationMixin, "count_assets", return_value=1)
def test_cannot_import_files_not_found_to_an_image_project(self, *_):
path = "./doesnotexist.png"
assets = [{"content": path, "external_id": "image"}]
with self.assertRaises(FileNotFoundError):
import_assets(self.kili, self.project_id, assets, disable_tqdm=True)

@patch.object(ProjectQuery, "__call__", side_effect=mocked_project_input_type("PDF"))
@patch.object(AssetQuery, "count", return_value=1)
@patch.object(AssetOperationMixin, "count_assets", return_value=1)
def test_cannot_upload_raw_text_to_pdf_project(self, *_):
path = "Hello world"
assets = [{"content": path, "external_id": "image"}]
with self.assertRaises(FileNotFoundError):
import_assets(self.kili, self.project_id, assets, disable_tqdm=True)

@patch.object(ProjectQuery, "__call__", side_effect=mocked_project_input_type("TEXT"))
@patch.object(AssetQuery, "count", return_value=1)
@patch.object(AssetOperationMixin, "count_assets", return_value=1)
def test_return_the_ids_of_created_assets(self, *_):
assets = [{"content": "One"}, {"content": "Two"}, {"content": "Three"}]

Expand All @@ -73,7 +68,7 @@ def graphql_execute_side_effect(*args, **kwargs):
assert set(created_assets) == {"id1", "id2", "id3"}

@patch.object(ProjectQuery, "__call__", side_effect=mocked_project_input_type("TEXT"))
@patch.object(AssetQuery, "count", return_value=1)
@patch.object(AssetOperationMixin, "count_assets", return_value=1)
def test_generate_different_uuid4_external_ids_if_not_given(self, *_):
assets = [{"content": "One"}, {"content": "Two"}, {"content": "Three"}]
self.kili.graphql_client.execute.reset_mock()
Expand All @@ -92,7 +87,7 @@ def test_generate_different_uuid4_external_ids_if_not_given(self, *_):
assert external_ids_are_uniques

@patch.object(ProjectQuery, "__call__", side_effect=mocked_project_input_type("IMAGE"))
@patch.object(AssetQuery, "count", return_value=1)
@patch.object(AssetOperationMixin, "count_assets", return_value=1)
@patch("kili.services.asset_import.base.BaseBatchImporter.verify_batch_imported")
def test_import_assets_verify(self, mocked_verify_batch_imported, *_):
assets = [{"content": "https://hosted-data", "external_id": "externalid"}]
Expand Down
Loading

0 comments on commit bb39dd5

Please sign in to comment.