From 400dc54a18cd70b30c5f705053a86d64a12d7571 Mon Sep 17 00:00:00 2001 From: Nathan Perkins Date: Tue, 7 Nov 2023 17:24:16 +0000 Subject: [PATCH 1/6] set_item_layout and testing --- darwin/future/core/items/set_item_layout.py | 29 +++++++ darwin/future/tests/core/items/fixtures.py | 9 +- .../tests/core/items/test_set_item_layout.py | 86 +++++++++++++++++++ 3 files changed, 123 insertions(+), 1 deletion(-) create mode 100644 darwin/future/core/items/set_item_layout.py create mode 100644 darwin/future/tests/core/items/test_set_item_layout.py diff --git a/darwin/future/core/items/set_item_layout.py b/darwin/future/core/items/set_item_layout.py new file mode 100644 index 000000000..7ca4fdc85 --- /dev/null +++ b/darwin/future/core/items/set_item_layout.py @@ -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) diff --git a/darwin/future/tests/core/items/fixtures.py b/darwin/future/tests/core/items/fixtures.py index b0fcb9402..92d57a026 100644 --- a/darwin/future/tests/core/items/fixtures.py +++ b/darwin/future/tests/core/items/fixtures.py @@ -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 diff --git a/darwin/future/tests/core/items/test_set_item_layout.py b/darwin/future/tests/core/items/test_set_item_layout.py new file mode 100644 index 000000000..98cb9ba1f --- /dev/null +++ b/darwin/future/tests/core/items/test_set_item_layout.py @@ -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) From 02b80e991d9a1c520c86730fe839db150799eb10 Mon Sep 17 00:00:00 2001 From: Nathan Perkins Date: Tue, 7 Nov 2023 17:29:23 +0000 Subject: [PATCH 2/6] comments on function --- darwin/future/core/items/set_item_layout.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/darwin/future/core/items/set_item_layout.py b/darwin/future/core/items/set_item_layout.py index 7ca4fdc85..bd1efdab2 100644 --- a/darwin/future/core/items/set_item_layout.py +++ b/darwin/future/core/items/set_item_layout.py @@ -12,6 +12,19 @@ def set_item_layout( layout: ItemLayout, params: JSONType, ) -> JSONType: + """ + Set the layout of a dataset and filtered items via params. + + 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. + params (JSONType): The parameters. + + Returns: + JSONType: The response data. + """ assert ( params ), "No parameters provided, please provide at least one non-dataset id filter" From de127ad24ad3b6c87d15684890d9a622fa915af3 Mon Sep 17 00:00:00 2001 From: Nathan Perkins Date: Tue, 7 Nov 2023 17:31:21 +0000 Subject: [PATCH 3/6] expected respone --- darwin/future/tests/core/items/test_set_item_layout.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/darwin/future/tests/core/items/test_set_item_layout.py b/darwin/future/tests/core/items/test_set_item_layout.py index 98cb9ba1f..84e145bf9 100644 --- a/darwin/future/tests/core/items/test_set_item_layout.py +++ b/darwin/future/tests/core/items/test_set_item_layout.py @@ -20,13 +20,13 @@ def test_set_item_layout_returns_blank_object( responses.add( responses.POST, f"{base_client.config.api_endpoint}v2/teams/{team_slug}/items/layout", - json={}, + json={"affected_item_count": 2}, status=200, ) response = set_item_layout(base_client, team_slug, dataset_ids, base_layout, params) - assert response == {} + assert response == {"affected_item_count": 2} @responses.activate From 3cfbdcec8088b84787d8bc4fa00de6ff82877a91 Mon Sep 17 00:00:00 2001 From: Nathan Perkins Date: Tue, 7 Nov 2023 17:48:56 +0000 Subject: [PATCH 4/6] code consistency --- darwin/future/core/items/set_item_layout.py | 14 ++++++++------ .../tests/core/items/test_set_item_layout.py | 6 ++++-- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/darwin/future/core/items/set_item_layout.py b/darwin/future/core/items/set_item_layout.py index bd1efdab2..2a2a3a837 100644 --- a/darwin/future/core/items/set_item_layout.py +++ b/darwin/future/core/items/set_item_layout.py @@ -1,8 +1,11 @@ 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( @@ -10,31 +13,30 @@ def set_item_layout( team_slug: str, dataset_ids: int | list[int], layout: ItemLayout, - params: JSONType, + filters: Dict[str, UnknownType], ) -> JSONType: """ - Set the layout of a dataset and filtered items via params. + 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. - params (JSONType): The parameters. + filters Dict[str, UnknownType]: The parameters of the filter. Returns: JSONType: The response data. """ assert ( - params + filters ), "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, + **filters, }, "layout": dict(layout), } diff --git a/darwin/future/tests/core/items/test_set_item_layout.py b/darwin/future/tests/core/items/test_set_item_layout.py index 84e145bf9..8cca506ed 100644 --- a/darwin/future/tests/core/items/test_set_item_layout.py +++ b/darwin/future/tests/core/items/test_set_item_layout.py @@ -1,11 +1,13 @@ +from typing import Dict + 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.data_objects.typing import UnknownType from darwin.future.tests.core.fixtures import * from darwin.future.tests.core.items.fixtures import * @@ -35,7 +37,7 @@ def test_set_item_layout_raises_on_incorrect_parameters( ) -> None: team_slug = "my_team" dataset_ids = [1, 2, 3] - params: JSONType = {} + params: Dict[str, UnknownType] = {} with pytest.raises(AssertionError): set_item_layout(base_client, team_slug, dataset_ids, base_layout, params) From 74acf9966b020d7e83a11c0645f773b610fa5ccf Mon Sep 17 00:00:00 2001 From: Nathan Perkins Date: Fri, 10 Nov 2023 11:08:53 +0000 Subject: [PATCH 5/6] BadRequest --- darwin/future/tests/core/items/test_set_item_layout.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/darwin/future/tests/core/items/test_set_item_layout.py b/darwin/future/tests/core/items/test_set_item_layout.py index 8cca506ed..d6ad725e0 100644 --- a/darwin/future/tests/core/items/test_set_item_layout.py +++ b/darwin/future/tests/core/items/test_set_item_layout.py @@ -8,6 +8,7 @@ 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 * @@ -58,7 +59,7 @@ def test_set_item_layout_raises_on_4xx_status_code( status=400, ) - with pytest.raises(HTTPError): + with pytest.raises(BadRequest): set_item_layout(base_client, team_slug, dataset_ids, base_layout, params) From 4be844687ad3d32734cfc9604c42630d744a279f Mon Sep 17 00:00:00 2001 From: Nathan Perkins Date: Fri, 10 Nov 2023 11:09:44 +0000 Subject: [PATCH 6/6] mypy check --- darwin/future/tests/core/datasets/test_list_datasets.py | 2 +- darwin/future/tests/core/items/test_set_item_layout.py | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/darwin/future/tests/core/datasets/test_list_datasets.py b/darwin/future/tests/core/datasets/test_list_datasets.py index 4de7fa065..58177b185 100644 --- a/darwin/future/tests/core/datasets/test_list_datasets.py +++ b/darwin/future/tests/core/datasets/test_list_datasets.py @@ -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 diff --git a/darwin/future/tests/core/items/test_set_item_layout.py b/darwin/future/tests/core/items/test_set_item_layout.py index d6ad725e0..3dddae791 100644 --- a/darwin/future/tests/core/items/test_set_item_layout.py +++ b/darwin/future/tests/core/items/test_set_item_layout.py @@ -2,7 +2,6 @@ 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