From 659216201f7468cb384cb5bbbc7c93727baa322e Mon Sep 17 00:00:00 2001 From: John Wilkie <124276291+JBWilkie@users.noreply.github.com> Date: Thu, 16 Nov 2023 14:02:07 +0000 Subject: [PATCH] [PY-403][external] Set Priority Meta Item & ItemQuery functions (#720) * Fixed missing path parameter in core move items to folder method * Fixed missing priority parameter in core set item priority method * Linting * Meta Item & ItemQuery functions to move between folders * Update darwin/future/tests/meta/queries/test_item.py Co-authored-by: Owen Jones * Added sad path tests * Meta functions & tests to set item priority * Linting --------- Co-authored-by: Owen Jones --- darwin/future/meta/objects/item.py | 13 ++++ darwin/future/meta/queries/item.py | 22 ++++++ .../tests/meta/objects/test_itemmeta.py | 58 ++++++++++++++ darwin/future/tests/meta/queries/test_item.py | 77 +++++++++++++++++++ 4 files changed, 170 insertions(+) diff --git a/darwin/future/meta/objects/item.py b/darwin/future/meta/objects/item.py index d531ee8a2..f2b40e5f4 100644 --- a/darwin/future/meta/objects/item.py +++ b/darwin/future/meta/objects/item.py @@ -6,6 +6,7 @@ from darwin.future.core.items.archive_items import archive_list_of_items from darwin.future.core.items.delete_items import delete_list_of_items from darwin.future.core.items.move_items_to_folder import move_list_of_items_to_folder +from darwin.future.core.items.set_item_priority import set_item_priority from darwin.future.core.items.restore_items import restore_list_of_items from darwin.future.data_objects.item import ItemCore, ItemLayout, ItemSlot from darwin.future.meta.objects.base import MetaBase @@ -68,6 +69,18 @@ def move_to_folder(self, path: str) -> None: filters = {"item_ids": [str(self.id)]} move_list_of_items_to_folder(self.client, team_slug, dataset_id, path, filters) + def set_priority(self, priority: int) -> None: + team_slug, dataset_id = ( + self.meta_params["team_slug"], + self.meta_params["dataset_id"] + if "dataset_id" in self.meta_params + else self.meta_params["dataset_ids"], + ) + assert isinstance(team_slug, str) + dataset_id = cast(Union[int, List[int]], dataset_id) + filters = {"item_ids": [str(self.id)]} + set_item_priority(self.client, team_slug, dataset_id, priority, filters) + def restore(self) -> None: team_slug, dataset_id = ( self.meta_params["team_slug"], diff --git a/darwin/future/meta/queries/item.py b/darwin/future/meta/queries/item.py index e868e8228..0874c1163 100644 --- a/darwin/future/meta/queries/item.py +++ b/darwin/future/meta/queries/item.py @@ -5,6 +5,7 @@ from darwin.future.core.items.delete_items import delete_list_of_items from darwin.future.core.items.get import list_items from darwin.future.core.items.move_items_to_folder import move_list_of_items_to_folder +from darwin.future.core.items.set_item_priority import set_item_priority from darwin.future.core.items.restore_items import restore_list_of_items from darwin.future.core.types.common import QueryString from darwin.future.core.types.query import PaginatedQuery @@ -84,6 +85,27 @@ def move_to_folder(self, path) -> None: filters = {"item_ids": [str(item) for item in ids]} move_list_of_items_to_folder(self.client, team_slug, dataset_ids, path, filters) + def set_priority(self, priority: int) -> None: + if "team_slug" not in self.meta_params: + raise ValueError("Must specify team_slug to query items") + if ( + "dataset_ids" not in self.meta_params + and "dataset_id" not in self.meta_params + ): + raise ValueError("Must specify dataset_ids to query items") + if not priority: + raise ValueError("Must specify priority to set items to") + dataset_ids = ( + self.meta_params["dataset_ids"] + if "dataset_ids" in self.meta_params + else self.meta_params["dataset_id"] + ) + team_slug = self.meta_params["team_slug"] + self.collect_all() + ids = [item.id for item in self] + filters = {"item_ids": [str(item) for item in ids]} + set_item_priority(self.client, team_slug, dataset_ids, priority, filters) + def restore(self) -> None: if "team_slug" not in self.meta_params: raise ValueError("Must specify team_slug to query items") diff --git a/darwin/future/tests/meta/objects/test_itemmeta.py b/darwin/future/tests/meta/objects/test_itemmeta.py index d9134bd5c..4d92a7326 100644 --- a/darwin/future/tests/meta/objects/test_itemmeta.py +++ b/darwin/future/tests/meta/objects/test_itemmeta.py @@ -112,6 +112,64 @@ def test_move_to_folder_with_bad_team_slug(item: Item) -> None: item.move_to_folder(path) +def test_set_priority(item: Item) -> None: + with responses.RequestsMock() as rsps: + team_slug = item.meta_params["team_slug"] + dataset_id = item.meta_params["dataset_id"] + priority = 10 + rsps.add( + rsps.POST, + item.client.config.api_endpoint + f"v2/teams/{team_slug}/items/priority", + status=200, + match=[ + json_params_matcher( + { + "filters": { + "item_ids": [str(item.id)], + "dataset_ids": [dataset_id], + }, + "priority": priority, + } + ) + ], + json={}, + ) + item.set_priority(priority) + + +def test_set_priority_raises_on_incorrect_parameters(item: Item) -> None: + with responses.RequestsMock() as rsps: + team_slug = item.meta_params["team_slug"] + dataset_id = item.meta_params["dataset_id"] + priority = "10" + rsps.add( + rsps.POST, + item.client.config.api_endpoint + f"v2/teams/{team_slug}/items/priority", + status=400, + match=[ + json_params_matcher( + { + "filters": { + "item_ids": [str(item.id)], + "dataset_ids": [dataset_id], + }, + "priority": priority, + } + ) + ], + json={}, + ) + with pytest.raises(BadRequest): + item.set_priority(priority) + + +def test_set_priority_with_bad_team_slug(item: Item) -> None: + with pytest.raises(AssertionError): + priority = 10 + item.meta_params["team_slug"] = 123 + item.set_priority(priority) + + def test_restore(item: Item) -> None: with responses.RequestsMock() as rsps: team_slug = item.meta_params["team_slug"] diff --git a/darwin/future/tests/meta/queries/test_item.py b/darwin/future/tests/meta/queries/test_item.py index abae86b5c..c1d2acb6d 100644 --- a/darwin/future/tests/meta/queries/test_item.py +++ b/darwin/future/tests/meta/queries/test_item.py @@ -148,6 +148,83 @@ def test_move_to_folder_raises_on_incorrect_parameters( item_query.move_to_folder(path) +def test_set_priority( + item_query: ItemQuery, items_json: List[dict], items: List[Item] +) -> None: + with responses.RequestsMock() as rsps: + rsps.add( + rsps.GET, + item_query.client.config.api_endpoint + "v2/teams/test/items", + match=[ + query_param_matcher( + {"page[offset]": "0", "page[size]": "500", "dataset_ids": "1"} + ) + ], + json={"items": items_json, "errors": []}, + ) + team_slug = items[0].meta_params["team_slug"] + dataset_id = items[0].meta_params["dataset_id"] + priority = 10 + rsps.add( + rsps.POST, + items[0].client.config.api_endpoint + + f"v2/teams/{team_slug}/items/priority", + status=200, + match=[ + json_params_matcher( + { + "filters": { + "item_ids": [str(item.id) for item in items], + "dataset_ids": [dataset_id], + }, + "priority": priority, + } + ) + ], + json={}, + ) + item_query.set_priority(priority) + + +def test_set_priority_raises_on_incorrect_parameters( + item_query: ItemQuery, items_json: List[dict], items: List[Item] +) -> None: + with responses.RequestsMock() as rsps: + rsps.add( + rsps.GET, + item_query.client.config.api_endpoint + "v2/teams/test/items", + match=[ + query_param_matcher( + {"page[offset]": "0", "page[size]": "500", "dataset_ids": "1"} + ) + ], + json={"items": items_json, "errors": []}, + ) + team_slug = items[0].meta_params["team_slug"] + dataset_id = items[0].meta_params["dataset_id"] + priority = "10" + rsps.add( + rsps.POST, + items[0].client.config.api_endpoint + + f"v2/teams/{team_slug}/items/priority", + status=400, + match=[ + json_params_matcher( + { + "filters": { + "item_ids": [str(item.id) for item in items], + "dataset_ids": [dataset_id], + }, + "priority": priority, + } + ) + ], + json={}, + ) + with pytest.raises(BadRequest): + item_query.set_priority(priority) + + def test_restore( item_query: ItemQuery, items_json: List[dict], items: List[Item] ) -> None: