Skip to content

Commit 568f7fd

Browse files
adding unit test
1 parent 62e80b7 commit 568f7fd

File tree

5 files changed

+35
-13
lines changed

5 files changed

+35
-13
lines changed

services/web/server/src/simcore_service_webserver/director_v2/_computations_service.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
check_user_project_permission,
2323
get_project_dict_legacy,
2424
)
25-
from ..projects.projects_metadata_service import get_project_custom_metadata
25+
from ..projects.projects_metadata_service import (
26+
get_project_custom_metadata_or_empty_dict,
27+
)
2628
from ..rabbitmq import get_rabbitmq_rpc_client
2729

2830

@@ -50,10 +52,12 @@ async def list_computations_latest_iteration(
5052
order_by=order_by,
5153
)
5254

53-
# Get projects metadata
55+
# Get projects metadata (NOTE: MD: can be improved with a single batch call)
5456
_projects_metadata = await limited_gather(
5557
*[
56-
get_project_custom_metadata(app, project_uuid=item.project_uuid)
58+
get_project_custom_metadata_or_empty_dict(
59+
app, project_uuid=item.project_uuid
60+
)
5761
for item in _runs_get.items
5862
],
5963
limit=20,

services/web/server/src/simcore_service_webserver/projects/_metadata_service.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from ..db.plugin import get_database_engine
1212
from . import _metadata_repository
1313
from ._access_rights_service import validate_project_ownership
14+
from .exceptions import ProjectNotFoundError
1415

1516
_logger = logging.getLogger(__name__)
1617

@@ -28,11 +29,26 @@ async def get_project_custom_metadata_for_user(
2829
async def get_project_custom_metadata(
2930
app: web.Application, project_uuid: ProjectID
3031
) -> MetadataDict:
32+
"""Can Raise ProjectNotFoundError"""
3133
return await _metadata_repository.get_project_custom_metadata(
3234
engine=get_database_engine(app), project_uuid=project_uuid
3335
)
3436

3537

38+
async def get_project_custom_metadata_or_empty_dict(
39+
app: web.Application, project_uuid: ProjectID
40+
) -> MetadataDict:
41+
try:
42+
output = await _metadata_repository.get_project_custom_metadata(
43+
engine=get_database_engine(app), project_uuid=project_uuid
44+
)
45+
except ProjectNotFoundError:
46+
# This is a valid case when the project is not found
47+
# but we still want to return an empty dict
48+
output = {}
49+
return output
50+
51+
3652
async def set_project_custom_metadata(
3753
app: web.Application,
3854
user_id: UserID,

services/web/server/src/simcore_service_webserver/projects/_projects_repository.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ async def batch_get_project_name(
128128

129129
query = (
130130
sql.select(
131+
projects.c.uuid,
131132
projects.c.name,
132133
)
133134
.select_from(projects)
@@ -145,7 +146,7 @@ async def batch_get_project_name(
145146
)
146147
async with pass_or_acquire_connection(get_asyncpg_engine(app), connection) as conn:
147148
result = await conn.stream(query)
148-
rows = {row.uuid: row.trashed_by_primary_gid async for row in result}
149+
rows = {row.uuid: row.name async for row in result}
149150

150151
return [rows.get(project_uuid) for project_uuid in projects_uuids_str]
151152

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
from ._metadata_service import get_project_custom_metadata
1+
from ._metadata_service import get_project_custom_metadata_or_empty_dict
22

3-
__all__: tuple[str, ...] = ("get_project_custom_metadata",)
3+
__all__: tuple[str, ...] = ("get_project_custom_metadata_or_empty_dict",)
44

55

66
# nopycln: file

services/web/server/tests/unit/with_dbs/01/test_director_v2_handlers.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -134,18 +134,18 @@ async def test_stop_computation(
134134
@pytest.fixture
135135
def mock_rpc_list_computations_latest_iteration_tasks(
136136
mocker: MockerFixture,
137+
user_project: ProjectDict,
137138
) -> ComputationRunRpcGetPage:
139+
project_uuid = user_project["uuid"]
140+
example = ComputationRunRpcGet.model_config["json_schema_extra"]["examples"][0]
141+
example["project_uuid"] = project_uuid
142+
example["info"]["project_metadata"]["root_parent_project_id"] = project_uuid
143+
138144
return mocker.patch(
139145
"simcore_service_webserver.director_v2._computations_service.computations.list_computations_latest_iteration_page",
140146
spec=True,
141147
return_value=ComputationRunRpcGetPage(
142-
items=[
143-
ComputationRunRpcGet.model_validate(
144-
ComputationRunRpcGet.model_config["json_schema_extra"]["examples"][
145-
0
146-
]
147-
)
148-
],
148+
items=[ComputationRunRpcGet.model_validate(example)],
149149
total=1,
150150
),
151151
)
@@ -189,6 +189,7 @@ async def test_list_computations_latest_iteration(
189189
)
190190
if user_role != UserRole.ANONYMOUS:
191191
assert ComputationRunRestGet.model_validate(data[0])
192+
assert data[0]["rootProjectName"] == user_project["name"]
192193

193194
url = client.app.router["list_computations_latest_iteration_tasks"].url_for(
194195
project_id=f"{user_project['uuid']}"

0 commit comments

Comments
 (0)