Skip to content

Commit

Permalink
[PY-402][external] Archive Meta Item & ItemQuery functions (#719)
Browse files Browse the repository at this point in the history
* Meta functions & tests to archive items

* Test improvements

* Linting
  • Loading branch information
JBWilkie authored Nov 16, 2023
1 parent edd9c76 commit 9b98d8a
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 0 deletions.
13 changes: 13 additions & 0 deletions darwin/future/meta/objects/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from typing import Dict, List, Optional, Union, cast
from uuid import UUID

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.restore_items import restore_list_of_items
Expand Down Expand Up @@ -79,6 +80,18 @@ def restore(self) -> None:
filters = {"item_ids": [str(self.id)]}
restore_list_of_items(self.client, team_slug, dataset_id, filters)

def archive(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)]}
archive_list_of_items(self.client, team_slug, dataset_id, filters)

@property
def name(self) -> str:
return self._element.name
Expand Down
20 changes: 20 additions & 0 deletions darwin/future/meta/queries/item.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from functools import reduce
from typing import Dict

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.get import list_items
from darwin.future.core.items.move_items_to_folder import move_list_of_items_to_folder
Expand Down Expand Up @@ -101,3 +102,22 @@ def restore(self) -> None:
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)

def archive(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]}
archive_list_of_items(self.client, team_slug, dataset_ids, filters)
48 changes: 48 additions & 0 deletions darwin/future/tests/meta/objects/test_itemmeta.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ def test_delete(item: Item) -> None:
item.delete()


def test_delete_with_bad_team_slug(item: Item) -> None:
with pytest.raises(AssertionError):
item.meta_params["team_slug"] = 123
item.delete()


def test_move_to_folder(item: Item) -> None:
with responses.RequestsMock() as rsps:
team_slug = item.meta_params["team_slug"]
Expand Down Expand Up @@ -99,6 +105,13 @@ def test_move_to_folder_raises_on_incorrect_parameters(item: Item) -> None:
item.move_to_folder(path)


def test_move_to_folder_with_bad_team_slug(item: Item) -> None:
with pytest.raises(AssertionError):
path = "/new_folder"
item.meta_params["team_slug"] = 123
item.move_to_folder(path)


def test_restore(item: Item) -> None:
with responses.RequestsMock() as rsps:
team_slug = item.meta_params["team_slug"]
Expand All @@ -120,3 +133,38 @@ def test_restore(item: Item) -> None:
json={},
)
item.restore()


def test_restore_with_bad_team_slug(item: Item) -> None:
with pytest.raises(AssertionError):
item.meta_params["team_slug"] = 123
item.restore()


def test_archive(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/archive",
status=200,
match=[
json_params_matcher(
{
"filters": {
"item_ids": [str(item.id)],
"dataset_ids": [dataset_id],
}
}
)
],
json={},
)
item.archive()


def test_archive_with_bad_team_slug(item: Item) -> None:
with pytest.raises(AssertionError):
item.meta_params["team_slug"] = 123
item.archive()
35 changes: 35 additions & 0 deletions darwin/future/tests/meta/queries/test_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,38 @@ def test_restore(
json={},
)
item_query.restore()


def test_archive(
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/archive",
status=200,
match=[
json_params_matcher(
{
"filters": {
"item_ids": [str(item.id) for item in items],
"dataset_ids": [dataset_id],
}
}
)
],
json={},
)
item_query.archive()

0 comments on commit 9b98d8a

Please sign in to comment.