-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add assign batch action SDK
- Loading branch information
Showing
5 changed files
with
230 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from __future__ import annotations | ||
|
||
from darwin.future.core.client import ClientCore | ||
from darwin.future.core.types.common import JSONDict, JSONType | ||
|
||
|
||
def assign_items( | ||
client: ClientCore, | ||
team_slug: str, | ||
dataset_ids: int | list[int], | ||
assignee_id: int, | ||
workflow_id: str, | ||
filters: JSONDict, | ||
) -> JSONType: | ||
""" | ||
Assign a user to all items matched by filters. | ||
Args: | ||
client (ClientCore): The Darwin Core client. | ||
team_slug (str): The team slug. | ||
dataset_ids (int | list[int]): The dataset ids. | ||
assignee_id (int): The user id to assign. | ||
workflow_id (str): The workflow id that selected items have to belong to. | ||
filters Dict[str, UnknownType]: The parameters of the filter. | ||
Returns: | ||
JSONType: The response data. | ||
""" | ||
assert ( | ||
filters | ||
), "No parameters provided, please provide at least one non-dataset id filter" | ||
payload = { | ||
"filters": { | ||
"dataset_ids": dataset_ids | ||
if isinstance(dataset_ids, list) | ||
else [dataset_ids], | ||
**filters, | ||
}, | ||
"assignee_id": assignee_id, | ||
"workflow_id": workflow_id, | ||
} | ||
|
||
return client.post(f"/v2/teams/{team_slug}/items/assign", data=payload) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import pytest | ||
import responses | ||
|
||
from darwin.future.core.client import ClientCore | ||
from darwin.future.core.items.assign_items import assign_items | ||
from darwin.future.exceptions import BadRequest | ||
from darwin.future.tests.core.fixtures import * | ||
|
||
|
||
@responses.activate | ||
def test_assign_items(base_client: ClientCore) -> None: | ||
team_slug = "test-team" | ||
dataset_ids = [1, 2, 3] | ||
assignee_id = 123456 | ||
workflow_id = "123456" | ||
item_ids = [ | ||
"00000000-0000-0000-0000-000000000001", | ||
"00000000-0000-0000-0000-000000000002", | ||
] | ||
filters = {"item_ids": item_ids} | ||
|
||
responses.add( | ||
responses.POST, | ||
base_client.config.api_endpoint + "v2/teams/test-team/items/assign", | ||
json={"created_commands": 1}, | ||
status=200, | ||
) | ||
|
||
response = assign_items( | ||
client=base_client, | ||
team_slug=team_slug, | ||
dataset_ids=dataset_ids, | ||
assignee_id=assignee_id, | ||
workflow_id=workflow_id, | ||
filters=filters, | ||
) | ||
|
||
assert response == {"created_commands": 1} | ||
|
||
|
||
@responses.activate | ||
def test_assign_items_filters_error(base_client: ClientCore) -> None: | ||
team_slug = "test-team" | ||
dataset_ids = [1, 2, 3] | ||
assignee_id = 123456 | ||
workflow_id = "123456" | ||
filters = {} | ||
|
||
responses.add( | ||
responses.POST, | ||
base_client.config.api_endpoint + "v2/teams/test-team/items/assign", | ||
json={"created_commands": 1}, | ||
status=200, | ||
) | ||
|
||
with pytest.raises(AssertionError) as excinfo: | ||
assign_items( | ||
client=base_client, | ||
team_slug=team_slug, | ||
dataset_ids=dataset_ids, | ||
assignee_id=assignee_id, | ||
workflow_id=workflow_id, | ||
filters=filters, | ||
) | ||
(msg,) = excinfo.value.args | ||
assert ( | ||
msg | ||
== "No parameters provided, please provide at least one non-dataset id filter" | ||
) | ||
|
||
|
||
@responses.activate | ||
def test_assign_items_bad_request_error(base_client: ClientCore) -> None: | ||
team_slug = "test-team" | ||
dataset_ids = [1, 2, 3] | ||
assignee_id = 123456 | ||
workflow_id = "123456" | ||
item_ids = [ | ||
"00000000-0000-0000-0000-000000000001", | ||
"00000000-0000-0000-0000-000000000002", | ||
] | ||
filters = {"item_ids": item_ids} | ||
|
||
responses.add( | ||
responses.POST, | ||
base_client.config.api_endpoint + "v2/teams/test-team/items/assign", | ||
json={"error": "Bad Request"}, | ||
status=400, | ||
) | ||
|
||
with pytest.raises(BadRequest): | ||
assign_items( | ||
client=base_client, | ||
team_slug=team_slug, | ||
dataset_ids=dataset_ids, | ||
assignee_id=assignee_id, | ||
workflow_id=workflow_id, | ||
filters=filters, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters