From 8f24ac2d7f1d63f8ee9017a3a877f05c3b5de8ec Mon Sep 17 00:00:00 2001 From: John Wilkie Date: Mon, 13 Nov 2023 22:02:23 +0000 Subject: [PATCH 1/2] Meta functions & tests to restore items --- darwin/future/meta/objects/item.py | 13 +++++++ darwin/future/meta/queries/item.py | 20 +++++++++++ .../tests/meta/objects/test_itemmeta.py | 23 ++++++++++++ darwin/future/tests/meta/queries/test_item.py | 35 +++++++++++++++++++ 4 files changed, 91 insertions(+) diff --git a/darwin/future/meta/objects/item.py b/darwin/future/meta/objects/item.py index 7b438f721..22c14954c 100644 --- a/darwin/future/meta/objects/item.py +++ b/darwin/future/meta/objects/item.py @@ -4,6 +4,7 @@ from uuid import UUID from darwin.future.core.items.delete_items import delete_list_of_items +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 @@ -53,6 +54,18 @@ def delete(self) -> None: filters = {"item_ids": [str(self.id)]} delete_list_of_items(self.client, team_slug, dataset_id, filters) + def restore(self) -> 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)]} + restore_list_of_items(self.client, team_slug, dataset_id, filters) + @property def name(self) -> str: return self._element.name diff --git a/darwin/future/meta/queries/item.py b/darwin/future/meta/queries/item.py index 5fd13e0e1..bbc6e5769 100644 --- a/darwin/future/meta/queries/item.py +++ b/darwin/future/meta/queries/item.py @@ -3,6 +3,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.restore_items import restore_list_of_items from darwin.future.core.types.common import QueryString from darwin.future.core.types.query import PaginatedQuery from darwin.future.meta.objects.item import Item @@ -59,3 +60,22 @@ def delete(self) -> None: ids = [item.id for item in self] filters = {"item_ids": [str(item) for item in ids]} delete_list_of_items(self.client, team_slug, dataset_ids, filters) + + def restore(self) -> 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") + 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]} + restore_list_of_items(self.client, team_slug, dataset_ids, filters) diff --git a/darwin/future/tests/meta/objects/test_itemmeta.py b/darwin/future/tests/meta/objects/test_itemmeta.py index ada047eb2..70aaf753b 100644 --- a/darwin/future/tests/meta/objects/test_itemmeta.py +++ b/darwin/future/tests/meta/objects/test_itemmeta.py @@ -44,3 +44,26 @@ def test_delete(item: Item) -> None: json={}, ) item.delete() + + +def test_restore(item: Item) -> None: + with responses.RequestsMock() as rsps: + team_slug = item.meta_params["team_slug"] + dataset_id = item.meta_params["dataset_id"] + rsps.add( + rsps.POST, + item.client.config.api_endpoint + f"v2/teams/{team_slug}/items/restore", + status=200, + match=[ + json_params_matcher( + { + "filters": { + "item_ids": [str(item.id)], + "dataset_ids": [dataset_id], + } + } + ) + ], + json={}, + ) + item.restore() diff --git a/darwin/future/tests/meta/queries/test_item.py b/darwin/future/tests/meta/queries/test_item.py index 3824a8788..0a1a95ce2 100644 --- a/darwin/future/tests/meta/queries/test_item.py +++ b/darwin/future/tests/meta/queries/test_item.py @@ -70,3 +70,38 @@ def test_delete( json={}, ) item_query.delete() + + +def test_restore( + 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"] + rsps.add( + rsps.POST, + items[0].client.config.api_endpoint + f"v2/teams/{team_slug}/items/restore", + status=200, + match=[ + json_params_matcher( + { + "filters": { + "item_ids": [str(item.id) for item in items], + "dataset_ids": [dataset_id], + } + } + ) + ], + json={}, + ) + item_query.restore() From bf785ea7539a14f721128418e6c0b7b6e8c5f290 Mon Sep 17 00:00:00 2001 From: John Wilkie Date: Tue, 14 Nov 2023 12:24:07 +0000 Subject: [PATCH 2/2] Test fixes --- darwin/future/meta/objects/item.py | 13 +++++ darwin/future/meta/queries/item.py | 22 +++++++- .../tests/meta/objects/test_itemmeta.py | 51 +++++++++++++++++++ darwin/future/tests/meta/queries/test_item.py | 35 ++++++------- 4 files changed, 101 insertions(+), 20 deletions(-) diff --git a/darwin/future/meta/objects/item.py b/darwin/future/meta/objects/item.py index 22c14954c..20d954bbd 100644 --- a/darwin/future/meta/objects/item.py +++ b/darwin/future/meta/objects/item.py @@ -4,6 +4,7 @@ from uuid import UUID 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.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 @@ -54,6 +55,18 @@ def delete(self) -> None: filters = {"item_ids": [str(self.id)]} delete_list_of_items(self.client, team_slug, dataset_id, filters) + def move_to_folder(self, path: str) -> 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)]} + move_list_of_items_to_folder(self.client, team_slug, dataset_id, path, 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 862051830..356278cab 100644 --- a/darwin/future/meta/queries/item.py +++ b/darwin/future/meta/queries/item.py @@ -3,6 +3,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.restore_items import restore_list_of_items from darwin.future.core.types.common import QueryString from darwin.future.core.types.query import PaginatedQuery @@ -61,7 +62,7 @@ def delete(self) -> None: filters = {"item_ids": [str(item) for item in ids]} delete_list_of_items(self.client, team_slug, dataset_ids, filters) - def restore(self) -> None: + def move_to_folder(self, path) -> None: if "team_slug" not in self.meta_params: raise ValueError("Must specify team_slug to query items") if ( @@ -80,4 +81,23 @@ def restore(self) -> None: self.collect_all() ids = [item.id for item in self] filters = {"item_ids": [str(item) for item in ids]} + move_list_of_items_to_folder(self.client, team_slug, dataset_ids, path, filters) + + def restore(self) -> 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") + 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]} restore_list_of_items(self.client, team_slug, dataset_ids, filters) diff --git a/darwin/future/tests/meta/objects/test_itemmeta.py b/darwin/future/tests/meta/objects/test_itemmeta.py index a2abc8df4..6e9093381 100644 --- a/darwin/future/tests/meta/objects/test_itemmeta.py +++ b/darwin/future/tests/meta/objects/test_itemmeta.py @@ -48,6 +48,57 @@ def test_delete(item: Item) -> None: item.delete() +def test_move_to_folder(item: Item) -> None: + with responses.RequestsMock() as rsps: + team_slug = item.meta_params["team_slug"] + dataset_id = item.meta_params["dataset_id"] + path = "/new_folder" + rsps.add( + rsps.POST, + item.client.config.api_endpoint + f"v2/teams/{team_slug}/items/path", + status=200, + match=[ + json_params_matcher( + { + "filters": { + "item_ids": [str(item.id)], + "dataset_ids": [dataset_id], + }, + "path": path, + } + ) + ], + json={}, + ) + item.move_to_folder(path) + + +def test_move_to_folder_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"] + path = 1234 + rsps.add( + rsps.POST, + item.client.config.api_endpoint + f"v2/teams/{team_slug}/items/path", + status=400, + match=[ + json_params_matcher( + { + "filters": { + "item_ids": [str(item.id)], + "dataset_ids": [dataset_id], + }, + "path": path, + } + ) + ], + json={}, + ) + with pytest.raises(BadRequest): + item.move_to_folder(path) + + 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 0718fb387..35fb32092 100644 --- a/darwin/future/tests/meta/queries/test_item.py +++ b/darwin/future/tests/meta/queries/test_item.py @@ -73,7 +73,7 @@ def test_delete( item_query.delete() -def test_restore( +def test_move_to_folder( item_query: ItemQuery, items_json: List[dict], items: List[Item] ) -> None: with responses.RequestsMock() as rsps: @@ -89,9 +89,10 @@ def test_restore( ) team_slug = items[0].meta_params["team_slug"] dataset_id = items[0].meta_params["dataset_id"] + path = "/new_folder" rsps.add( rsps.POST, - items[0].client.config.api_endpoint + f"v2/teams/{team_slug}/items/restore", + items[0].client.config.api_endpoint + f"v2/teams/{team_slug}/items/path", status=200, match=[ json_params_matcher( @@ -99,17 +100,17 @@ def test_restore( "filters": { "item_ids": [str(item.id) for item in items], "dataset_ids": [dataset_id], - } + }, + "path": path, } ) ], json={}, ) - item_query.restore() -<<<<<<< HEAD + item_query.move_to_folder(path) -def test_move_to_folder( +def test_move_to_folder_raises_on_incorrect_parameters( item_query: ItemQuery, items_json: List[dict], items: List[Item] ) -> None: with responses.RequestsMock() as rsps: @@ -125,11 +126,11 @@ def test_move_to_folder( ) team_slug = items[0].meta_params["team_slug"] dataset_id = items[0].meta_params["dataset_id"] - path = "/new_folder" + path = 1234 rsps.add( rsps.POST, items[0].client.config.api_endpoint + f"v2/teams/{team_slug}/items/path", - status=200, + status=400, match=[ json_params_matcher( { @@ -143,10 +144,11 @@ def test_move_to_folder( ], json={}, ) - item_query.move_to_folder(path) + with pytest.raises(BadRequest): + item_query.move_to_folder(path) -def test_move_to_folder_raises_on_incorrect_parameters( +def test_restore( item_query: ItemQuery, items_json: List[dict], items: List[Item] ) -> None: with responses.RequestsMock() as rsps: @@ -162,25 +164,20 @@ def test_move_to_folder_raises_on_incorrect_parameters( ) team_slug = items[0].meta_params["team_slug"] dataset_id = items[0].meta_params["dataset_id"] - path = 1234 rsps.add( rsps.POST, - items[0].client.config.api_endpoint + f"v2/teams/{team_slug}/items/path", - status=400, + items[0].client.config.api_endpoint + f"v2/teams/{team_slug}/items/restore", + status=200, match=[ json_params_matcher( { "filters": { "item_ids": [str(item.id) for item in items], "dataset_ids": [dataset_id], - }, - "path": path, + } } ) ], json={}, ) - with pytest.raises(BadRequest): - item_query.move_to_folder(path) -======= ->>>>>>> ca73356d39135338ef89ca016c46a216cc4f1a8d + item_query.restore()