Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PY-384] Set item layout #710

Merged
merged 7 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions darwin/future/core/items/set_item_layout.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from __future__ import annotations

from typing import Dict

from darwin.future.core.client import ClientCore
from darwin.future.core.types.common import JSONType
from darwin.future.data_objects.item import ItemLayout
from darwin.future.data_objects.typing import UnknownType


def set_item_layout(
client: ClientCore,
team_slug: str,
dataset_ids: int | list[int],
layout: ItemLayout,
filters: Dict[str, UnknownType],
) -> JSONType:
"""
Set the layout of a dataset and filtered items via filters.

Args:
client (ClientCore): The Darwin Core client.
team_slug (str): The team slug.
dataset_ids (int | list[int]): The dataset ids.
layout (ItemLayout): The layout.
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,
},
"layout": dict(layout),
}

return client.post(f"/v2/teams/{team_slug}/items/layout", data=payload)
2 changes: 1 addition & 1 deletion darwin/future/tests/core/datasets/test_list_datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ def test_it_returns_an_error_if_the_client_returns_an_http_error(
with pytest.raises(BadRequest) as execinfo:
list_datasets(base_client)

assert execinfo.value.args[0].status_code == 400 # type: ignore
assert execinfo.value.args[0].status_code == 400
9 changes: 8 additions & 1 deletion darwin/future/tests/core/items/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@

import pytest

from darwin.future.data_objects.item import Folder, Item
from darwin.future.data_objects.item import Folder, Item, ItemLayout


@pytest.fixture
def base_layout() -> ItemLayout:
return ItemLayout(
slots=["slot1", "slot2"], type="grid", layout_shape=[2, 1], version=2
)


@pytest.fixture
Expand Down
88 changes: 88 additions & 0 deletions darwin/future/tests/core/items/test_set_item_layout.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
from typing import Dict

import pytest
import responses

from darwin.future.core.client import ClientCore
from darwin.future.core.items.set_item_layout import set_item_layout
from darwin.future.data_objects.item import ItemLayout
from darwin.future.data_objects.typing import UnknownType
from darwin.future.exceptions import BadRequest
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={"affected_item_count": 2},
status=200,
)

response = set_item_layout(base_client, team_slug, dataset_ids, base_layout, params)

assert response == {"affected_item_count": 2}


@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: Dict[str, UnknownType] = {}

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(BadRequest):
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)
Loading