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-401][external] Restore Meta Item & ItemQuery functions #718

Merged
merged 5 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 13 additions & 0 deletions darwin/future/meta/objects/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
JBWilkie marked this conversation as resolved.
Show resolved Hide resolved
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
Expand Down
20 changes: 20 additions & 0 deletions darwin/future/meta/queries/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
23 changes: 23 additions & 0 deletions darwin/future/tests/meta/objects/test_itemmeta.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As with the other one, I would sad path these, but in these, they are small enough, that it isn't a massive deal. Red/Green/Refactor is a good approach.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quick Q on this - Since neither item.restore() or item_query.restore() take any parameters nor do they return anything, what could we consider the sad path?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@owencjones I discussed these tests with Nathan - Since they don't return anything or take any arguments, there should only be one path these functions can actually take

Error responses from the API are covered in the underlying Core method tests. Given this, I'll proceed to merge but let me know if you disagree or have any other thoughts

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 @@ -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()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And with this one ideally, but again, small ticket, not the end of the world.

The thing I would aim to be testing is each path through the function