-
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.
- Loading branch information
1 parent
d102e24
commit 400dc54
Showing
3 changed files
with
123 additions
and
1 deletion.
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,29 @@ | ||
from __future__ import annotations | ||
|
||
from darwin.future.core.client import ClientCore | ||
from darwin.future.core.types.common import JSONType | ||
from darwin.future.data_objects.item import ItemLayout | ||
|
||
|
||
def set_item_layout( | ||
client: ClientCore, | ||
team_slug: str, | ||
dataset_ids: int | list[int], | ||
layout: ItemLayout, | ||
params: JSONType, | ||
) -> JSONType: | ||
assert ( | ||
params | ||
), "No parameters provided, please provide at least one non-dataset id filter" | ||
assert isinstance(params, dict), "Parameters must be a dictionary of filters" | ||
payload = { | ||
"filters": { | ||
"dataset_ids": dataset_ids | ||
if isinstance(dataset_ids, list) | ||
else [dataset_ids], | ||
**params, | ||
}, | ||
"layout": dict(layout), | ||
} | ||
|
||
return client.post(f"/v2/teams/{team_slug}/items/layout", 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import pytest | ||
import responses | ||
from requests import HTTPError | ||
|
||
from darwin.future.core.client import ClientCore | ||
from darwin.future.core.items.set_item_layout import set_item_layout | ||
from darwin.future.core.types.common import JSONType | ||
from darwin.future.data_objects.item import ItemLayout | ||
from darwin.future.tests.core.fixtures import * | ||
from darwin.future.tests.core.items.fixtures import * | ||
|
||
|
||
@responses.activate | ||
def test_set_item_layout_returns_blank_object( | ||
base_client: ClientCore, base_layout: ItemLayout | ||
) -> None: | ||
dataset_ids = [1, 2, 3] | ||
params = {"param1": "value1", "param2": "value2"} | ||
team_slug = "my_team" | ||
responses.add( | ||
responses.POST, | ||
f"{base_client.config.api_endpoint}v2/teams/{team_slug}/items/layout", | ||
json={}, | ||
status=200, | ||
) | ||
|
||
response = set_item_layout(base_client, team_slug, dataset_ids, base_layout, params) | ||
|
||
assert response == {} | ||
|
||
|
||
@responses.activate | ||
def test_set_item_layout_raises_on_incorrect_parameters( | ||
base_client: ClientCore, base_layout: ItemLayout | ||
) -> None: | ||
team_slug = "my_team" | ||
dataset_ids = [1, 2, 3] | ||
params: JSONType = {} | ||
|
||
with pytest.raises(AssertionError): | ||
set_item_layout(base_client, team_slug, dataset_ids, base_layout, params) | ||
|
||
|
||
@responses.activate | ||
def test_set_item_layout_raises_on_4xx_status_code( | ||
base_client: ClientCore, base_layout: ItemLayout | ||
) -> None: | ||
team_slug = "my_team" | ||
dataset_ids = [1, 2, 3] | ||
params = {"param1": "value1", "param2": "value2"} | ||
|
||
responses.add( | ||
responses.POST, | ||
f"{base_client.config.api_endpoint}v2/teams/{team_slug}/items/layout", | ||
json={"error": "Bad Request"}, | ||
status=400, | ||
) | ||
|
||
with pytest.raises(HTTPError): | ||
set_item_layout(base_client, team_slug, dataset_ids, base_layout, params) | ||
|
||
|
||
@responses.activate | ||
def test_set_item_layout_sends_correct_payload( | ||
base_client: ClientCore, base_layout: ItemLayout | ||
) -> None: | ||
team_slug = "my_team" | ||
dataset_ids = [1, 2, 3] | ||
params = {"param1": "value1", "param2": "value2"} | ||
|
||
responses.add( | ||
responses.POST, | ||
f"{base_client.config.api_endpoint}v2/teams/{team_slug}/items/layout", | ||
json={}, | ||
status=200, | ||
match=[ | ||
responses.json_params_matcher( | ||
{ | ||
"filters": {"dataset_ids": dataset_ids, **params}, | ||
"layout": dict(base_layout), | ||
} | ||
) | ||
], | ||
) | ||
|
||
set_item_layout(base_client, team_slug, dataset_ids, base_layout, params) |