-
Notifications
You must be signed in to change notification settings - Fork 30
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
Added filtering option for entities listing #513
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
from typing import Optional | ||
from collections import defaultdict | ||
|
||
from medperf.entities.interface import Entity | ||
from medperf.enums import Status | ||
from medperf.exceptions import MedperfException | ||
from medperf.utils import format_errors_dict | ||
|
@@ -105,3 +106,7 @@ def default_status(cls, v): | |
if v is not None: | ||
status = Status(v) | ||
return status | ||
|
||
|
||
class DeployableEntity(DeployableSchema, Entity): | ||
pass | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This "implementation" is necessary at two places:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see your point. Maybe this could live in the interface file instead? I also think that, just for clarity, entities that apply to this description should inherit from this DeployableEntity class. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
from typing import Any | ||
|
||
import pytest | ||
|
||
from medperf.entities.cube import Cube | ||
from medperf.commands.list import EntityList | ||
|
||
PATCH_CUBE = "medperf.entities.cube.Cube.{}" | ||
|
||
|
||
def generate_cube(id: int, is_valid: bool, owner: int) -> dict[str, Any]: | ||
git_mlcube_url = f"{id}-{is_valid}-{owner}" | ||
name = git_mlcube_url | ||
return { | ||
'id': id, | ||
'is_valid': is_valid, | ||
'owner': owner, | ||
'git_mlcube_url': git_mlcube_url, | ||
'name': name | ||
} | ||
|
||
|
||
def cls_local_cubes(*args, **kwargs) -> list[Cube]: | ||
return [ | ||
Cube(**generate_cube(id=101, is_valid=True, owner=1)), | ||
Cube(**generate_cube(id=102, is_valid=False, owner=1)), | ||
# Intended: for local mlcubes owner is never checked. | ||
# All local cubes are supposed to be owned by current user | ||
|
||
# generate_cube(id=103, is_valid=True, owner=12345), | ||
# generate_cube(id=104, is_valid=False, owner=12345), | ||
] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The current behavior is not to filter local cubes by owner. I didn't change it, instead I anchored it in this test case. In future, once we elaborate & implement new |
||
|
||
|
||
def comms_remote_cubes_dict_mine_only() -> list[dict[str, Any]]: | ||
return [ | ||
generate_cube(id=201, is_valid=True, owner=1), | ||
generate_cube(id=202, is_valid=False, owner=1), | ||
] | ||
|
||
|
||
def comms_remote_cubes_dict() -> list[dict[str, Any]]: | ||
mine_only = comms_remote_cubes_dict_mine_only() | ||
someone_else = [ | ||
generate_cube(id=203, is_valid=True, owner=12345), | ||
generate_cube(id=204, is_valid=False, owner=12345), | ||
] | ||
return mine_only + someone_else | ||
|
||
|
||
def cls_remote_cubes(*args, **kwargs) -> list[Cube]: | ||
return [Cube(**d) for d in comms_remote_cubes_dict()] | ||
|
||
|
||
@pytest.mark.parametrize("local_only", [False, True]) | ||
@pytest.mark.parametrize("mine_only", [False, True]) | ||
@pytest.mark.parametrize("valid_only", [False, True]) | ||
def test_run_list_mlcubes(mocker, comms, ui, local_only, mine_only, valid_only): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test is way too complex. Is there a way we can split it into multiple tests? It is considered best practice to have atomic, DAMP unit tests |
||
# Arrange | ||
mocker.patch("medperf.commands.list.get_medperf_user_data", return_value={"id": 1}) | ||
mocker.patch("medperf.entities.cube.get_medperf_user_data", return_value={"id": 1}) | ||
|
||
# Implementation-specific: for local cubes there is a private classmethod. | ||
mocker.patch(PATCH_CUBE.format("_Cube__local_all"), new=cls_local_cubes) | ||
# For remote cubes there are two different endpoints - for all cubes and for mine only | ||
mocker.patch.object(comms, 'get_user_cubes', new=comms_remote_cubes_dict_mine_only) | ||
mocker.patch.object(comms, 'get_cubes', new=comms_remote_cubes_dict) | ||
|
||
tab_spy = mocker.patch("medperf.commands.list.tabulate", return_value="") | ||
|
||
local_cubes = cls_local_cubes() | ||
remote_cubes = cls_remote_cubes() | ||
cubes = local_cubes + remote_cubes | ||
|
||
# Act | ||
EntityList.run(Cube, fields=['UID'], local_only=local_only, mine_only=mine_only, valid_only=valid_only) | ||
|
||
# Assert | ||
tab_call = tab_spy.call_args_list[0] | ||
received_cubes: list[list[Any]] = tab_call[0][0] | ||
received_ids = {cube_fields[0] for cube_fields in received_cubes} | ||
|
||
local_ids = {c.id for c in local_cubes} | ||
|
||
expected_ids = set() | ||
for c in cubes: | ||
if local_only: | ||
if c.id not in local_ids: | ||
continue | ||
|
||
if mine_only: | ||
if c.owner != 1: | ||
continue | ||
|
||
if valid_only: | ||
if not c.is_valid: | ||
continue | ||
|
||
expected_ids.add(c.id) | ||
|
||
assert received_ids == expected_ids |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This fix was not connected to ls valid feature, just a bugfix: making abstract function signature relevant to a real implementations