Skip to content

Commit

Permalink
fix: fix label query with asset externalid filters (#1494)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonas1312 authored Oct 2, 2023
1 parent e66584c commit d307310
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/kili/core/graphql/operations/label/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def __init__(
asset_id: Optional[str] = None,
asset_status_in: Optional[List[str]] = None,
asset_external_id_in: Optional[List[str]] = None,
asset_external_id_strictly_in: Optional[List[str]] = None,
author_in: Optional[List[str]] = None,
created_at: Optional[str] = None,
created_at_gte: Optional[str] = None,
Expand All @@ -35,6 +36,7 @@ def __init__(
self.asset_id = asset_id
self.asset_status_in = asset_status_in
self.asset_external_id_in = asset_external_id_in
self.asset_external_id_strictly_in = asset_external_id_strictly_in
self.author_in = author_in
self.created_at = created_at
self.created_at_gte = created_at_gte
Expand All @@ -55,7 +57,8 @@ def graphql_where_builder(self):
"id": self.label_id,
"asset": {
"id": self.asset_id,
"externalIdStrictlyIn": self.asset_external_id_in,
"externalIdIn": self.asset_external_id_in,
"externalIdStrictlyIn": self.asset_external_id_strictly_in,
"statusIn": self.asset_status_in,
},
"project": {
Expand Down Expand Up @@ -83,7 +86,7 @@ class LabelQuery(GraphQLQuery):
FORMAT_TYPE = LabelFormatType

@staticmethod
def query(fragment):
def query(fragment: str) -> str:
"""Return the GraphQL labels query."""
return f"""
query labels($where: LabelWhere!, $first: PageSize!, $skip: Int!) {{
Expand Down
14 changes: 14 additions & 0 deletions src/kili/entrypoints/queries/label/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def labels(
asset_id: Optional[str] = None,
asset_status_in: Optional[List[str]] = None,
asset_external_id_in: Optional[List[str]] = None,
asset_external_id_strictly_in: Optional[List[str]] = None,
author_in: Optional[List[str]] = None,
created_at: Optional[str] = None,
created_at_gte: Optional[str] = None,
Expand All @@ -63,6 +64,7 @@ def labels(
"jsonResponse",
"labelType",
"secondsToLabel",
"isLatestLabelForUser",
"assetId",
),
first: Optional[int] = None,
Expand All @@ -87,6 +89,7 @@ def labels(
asset_id: Optional[str] = None,
asset_status_in: Optional[List[str]] = None,
asset_external_id_in: Optional[List[str]] = None,
asset_external_id_strictly_in: Optional[List[str]] = None,
author_in: Optional[List[str]] = None,
created_at: Optional[str] = None,
created_at_gte: Optional[str] = None,
Expand All @@ -98,6 +101,7 @@ def labels(
"jsonResponse",
"labelType",
"secondsToLabel",
"isLatestLabelForUser",
"assetId",
),
first: Optional[int] = None,
Expand All @@ -122,6 +126,7 @@ def labels(
asset_id: Optional[str] = None,
asset_status_in: Optional[List[str]] = None,
asset_external_id_in: Optional[List[str]] = None,
asset_external_id_strictly_in: Optional[List[str]] = None,
author_in: Optional[List[str]] = None,
created_at: Optional[str] = None,
created_at_gte: Optional[str] = None,
Expand All @@ -133,6 +138,7 @@ def labels(
"jsonResponse",
"labelType",
"secondsToLabel",
"isLatestLabelForUser",
"assetId",
),
first: Optional[int] = None,
Expand All @@ -157,6 +163,7 @@ def labels(
asset_id: Optional[str] = None,
asset_status_in: Optional[List[str]] = None,
asset_external_id_in: Optional[List[str]] = None,
asset_external_id_strictly_in: Optional[List[str]] = None,
author_in: Optional[List[str]] = None,
created_at: Optional[str] = None,
created_at_gte: Optional[str] = None,
Expand All @@ -168,6 +175,7 @@ def labels(
"jsonResponse",
"labelType",
"secondsToLabel",
"isLatestLabelForUser",
"assetId",
),
first: Optional[int] = None,
Expand All @@ -192,6 +200,7 @@ def labels(
asset_id: Optional[str] = None,
asset_status_in: Optional[List[str]] = None,
asset_external_id_in: Optional[List[str]] = None,
asset_external_id_strictly_in: Optional[List[str]] = None,
author_in: Optional[List[str]] = None,
created_at: Optional[str] = None,
created_at_gte: Optional[str] = None,
Expand Down Expand Up @@ -229,6 +238,7 @@ def labels(
asset_status_in: Returned labels should have a status that belongs to that list, if given.
Possible choices : `TODO`, `ONGOING`, `LABELED`, `TO REVIEW` or `REVIEWED`.
asset_external_id_in: Returned labels should have an external id that belongs to that list, if given.
asset_external_id_strictly_in: Returned labels should have an external id that exactly matches one of the ids in that list, if given.
author_in: Returned labels should have been made by authors in that list, if given.
An author can be designated by the first name, the last name, or the first name + last name.
created_at: Returned labels should have their creation date equal to this date.
Expand Down Expand Up @@ -286,6 +296,7 @@ def labels(
asset_id=asset_id,
asset_status_in=asset_status_in,
asset_external_id_in=asset_external_id_in,
asset_external_id_strictly_in=asset_external_id_strictly_in,
author_in=author_in,
created_at=created_at,
created_at_gte=created_at_gte,
Expand Down Expand Up @@ -676,6 +687,7 @@ def count_labels(
asset_id: Optional[str] = None,
asset_status_in: Optional[List[str]] = None,
asset_external_id_in: Optional[List[str]] = None,
asset_external_id_strictly_in: Optional[List[str]] = None,
author_in: Optional[List[str]] = None,
created_at: Optional[str] = None,
created_at_gte: Optional[str] = None,
Expand All @@ -697,6 +709,7 @@ def count_labels(
asset_status_in: Returned labels should have a status that belongs to that list, if given.
Possible choices : `TODO`, `ONGOING`, `LABELED` or `REVIEWED`
asset_external_id_in: Returned labels should have an external id that belongs to that list, if given.
asset_external_id_strictly_in: Returned labels should have an external id that exactly matches one of the ids in that list, if given.
author_in: Returned labels should have been made by authors in that list, if given.
An author can be designated by the first name, the last name, or the first name + last name.
created_at: Returned labels should have a label whose creation date is equal to this date.
Expand Down Expand Up @@ -724,6 +737,7 @@ def count_labels(
asset_id=asset_id,
asset_status_in=asset_status_in,
asset_external_id_in=asset_external_id_in,
asset_external_id_strictly_in=asset_external_id_strictly_in,
author_in=author_in,
created_at=created_at,
created_at_gte=created_at_gte,
Expand Down
70 changes: 70 additions & 0 deletions tests/e2e/test_query_labels.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
from typing import Dict

import pytest

from kili.client import Kili


@pytest.fixture(scope="module")
def project_id(kili: Kili):
project = kili.create_project(
"TEXT",
json_interface={
"jobs": {
"CLASSIFICATION_JOB": {
"content": {
"categories": {"A": {"children": [], "name": "A"}},
"input": "radio",
},
"instruction": "Classif",
"mlTask": "CLASSIFICATION",
"required": 1,
"isChild": False,
}
}
},
title="test e2e query labels sdk",
)

kili.append_many_to_dataset(
project_id=project["id"],
content_array=["text asset 1", "text asset 2"],
external_id_array=["asset_1", "asset_2"],
)

kili.append_labels(
project_id=project["id"],
asset_external_id_array=["asset_1", "asset_2"],
json_response_array=[{"CLASSIFICATION_JOB": {"categories": [{"name": "A"}]}}] * 2,
)

yield project["id"]

kili.delete_project(project["id"])


@pytest.mark.parametrize(
("labels_query_params", "nb_expected_labels"),
[
({"asset_external_id_in": ["asset_"]}, 2),
({"asset_external_id_in": ["asset_1"]}, 1),
({"asset_external_id_strictly_in": ["asset_1"]}, 1),
({"asset_external_id_strictly_in": ["asset_2"]}, 1),
({"asset_external_id_strictly_in": ["asset_"]}, 0),
],
)
def test_given_project_with_labels_when_i_query_with_filters_then_it_works(
project_id: str, kili: Kili, labels_query_params: Dict, nb_expected_labels: int
):
# Given
_ = project_id

# When
labels = kili.labels(
project_id=project_id,
fields=("labelOf.externalId",),
**labels_query_params,
)

# Then
assert len(labels) == nb_expected_labels

0 comments on commit d307310

Please sign in to comment.