Skip to content

Commit 8eb6811

Browse files
committed
Fix test
1 parent 61d0b94 commit 8eb6811

File tree

3 files changed

+48
-29
lines changed

3 files changed

+48
-29
lines changed

services/web/server/src/simcore_service_webserver/functions/_functions_repository.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -453,10 +453,9 @@ async def list_functions(
453453
search_by_function_title=search_by_function_title,
454454
)
455455

456-
# Build the base query with join to access rights table
456+
# Use GROUP BY on the primary key to ensure unique functions
457457
base_query = (
458458
functions_table.select()
459-
.distinct()
460459
.join(
461460
functions_access_rights_table,
462461
functions_table.c.uuid == functions_access_rights_table.c.function_uuid,
@@ -467,6 +466,7 @@ async def list_functions(
467466
functions_access_rights_table.c.read,
468467
*attributes_filters,
469468
)
469+
.group_by(functions_table.c.uuid)
470470
)
471471

472472
# Get total count

services/web/server/tests/unit/with_dbs/04/functions/conftest.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55

66
from collections.abc import AsyncIterator, Awaitable, Callable
7-
from contextlib import AsyncExitStack
7+
from contextlib import AsyncExitStack, suppress
88
from typing import Any
99
from uuid import UUID, uuid4
1010

@@ -18,6 +18,7 @@
1818
ProjectFunction,
1919
)
2020
from models_library.functions import FunctionClass, SolverFunction
21+
from models_library.functions_errors import FunctionWriteAccessDeniedError
2122
from models_library.products import ProductName
2223
from models_library.rabbitmq_basic_types import RPCNamespace
2324
from pydantic import TypeAdapter
@@ -152,23 +153,26 @@ async def clean_functions(
152153
client: TestClient,
153154
webserver_rpc_client: WebServerRpcClient,
154155
logged_user: UserInfoDict,
156+
other_logged_user: UserInfoDict,
155157
osparc_product_name: ProductName,
156158
) -> None:
157159
assert client.app
158160

159-
functions, _ = await webserver_rpc_client.functions.list_functions(
160-
pagination_limit=100,
161-
pagination_offset=0,
162-
user_id=logged_user["id"],
163-
product_name=osparc_product_name,
164-
)
165-
for function in functions:
166-
assert function.uid is not None
167-
await webserver_rpc_client.functions.delete_function(
168-
function_id=function.uid,
169-
user_id=logged_user["id"],
161+
for user_id in (logged_user["id"], other_logged_user["id"]):
162+
functions, _ = await webserver_rpc_client.functions.list_functions(
163+
pagination_limit=100,
164+
pagination_offset=0,
165+
user_id=user_id,
170166
product_name=osparc_product_name,
171167
)
168+
for function in functions:
169+
assert function.uid is not None
170+
with suppress(FunctionWriteAccessDeniedError):
171+
await webserver_rpc_client.functions.delete_function(
172+
function_id=function.uid,
173+
user_id=user_id,
174+
product_name=osparc_product_name,
175+
)
172176

173177

174178
@pytest.fixture

services/web/server/tests/unit/with_dbs/04/functions/wb-api-server/test_functions_controller_rpc.py

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
from models_library.groups import EVERYONE_GROUP_ID
3131
from models_library.products import ProductName
3232
from models_library.rest_ordering import OrderBy, OrderDirection
33+
from models_library.rest_pagination import PageMetaInfoLimitOffset
3334
from pytest_simcore.helpers.webserver_users import UserInfoDict
3435
from servicelib.rabbitmq.rpc_interfaces.webserver.v1 import WebServerRpcClient
3536

@@ -294,6 +295,7 @@ async def test_list_functions_mixed_user(
294295
user_id=logged_user["id"],
295296
product_name=osparc_product_name,
296297
read=True,
298+
write=True,
297299
)
298300
other_functions, _ = await webserver_rpc_client.functions.list_functions(
299301
pagination_limit=10,
@@ -314,38 +316,48 @@ async def test_list_functions_mixed_user(
314316
],
315317
)
316318
@pytest.mark.parametrize(
317-
"test_pagination_limit, test_pagination_offset",
319+
"test_pagination_limit, test_pagination_offset, total_number_functions",
318320
[
319-
(5, 0),
320-
(2, 2),
321-
(12, 4),
321+
(5, 0, 10),
322+
(2, 2, 10),
323+
(12, 4, 10),
322324
],
323325
)
324326
async def test_list_functions_with_pagination_ordering(
325327
client: TestClient,
326328
add_user_function_api_access_rights: None,
327329
webserver_rpc_client: WebServerRpcClient,
328330
create_fake_function_obj: Callable[[FunctionClass], ProjectFunction],
329-
clean_functions: None,
330331
osparc_product_name: ProductName,
331332
logged_user: UserInfoDict,
332333
order_by: OrderBy | None,
333334
test_pagination_limit: int,
334335
test_pagination_offset: int,
336+
total_number_functions: int,
337+
clean_functions: None,
335338
):
339+
# Making sure functions are empty before we start
340+
assert await webserver_rpc_client.functions.list_functions(
341+
pagination_limit=1,
342+
pagination_offset=0,
343+
user_id=logged_user["id"],
344+
product_name=osparc_product_name,
345+
) == (
346+
[],
347+
PageMetaInfoLimitOffset(limit=1, total=0, offset=0, count=0),
348+
)
336349
# Register multiple functions
337-
TOTAL_FUNCTIONS = 10
338350
registered_functions = [
339351
await webserver_rpc_client.functions.register_function(
340352
function=create_fake_function_obj(FunctionClass.PROJECT),
341353
user_id=logged_user["id"],
342354
product_name=osparc_product_name,
343355
)
344-
for _ in range(TOTAL_FUNCTIONS)
356+
for _ in range(total_number_functions)
345357
]
346358

347359
# List functions with pagination
348-
functions, page_info = await webserver_rpc_client.functions.list_functions(
360+
listed_functions, page_info = await webserver_rpc_client.functions.list_functions(
349361
pagination_limit=test_pagination_limit,
350362
pagination_offset=test_pagination_offset,
351363
user_id=logged_user["id"],
@@ -354,23 +366,26 @@ async def test_list_functions_with_pagination_ordering(
354366
)
355367

356368
# Assert the list contains the correct number of functions
357-
assert len(functions) == min(
358-
test_pagination_limit, max(0, TOTAL_FUNCTIONS - test_pagination_offset)
369+
assert len(listed_functions) == min(
370+
test_pagination_limit, max(0, total_number_functions - test_pagination_offset)
359371
)
360-
assert all(f.uid in [rf.uid for rf in registered_functions] for f in functions)
361-
assert page_info.count == len(functions)
362-
assert page_info.total == TOTAL_FUNCTIONS
372+
373+
assert all(
374+
f.uid in [rf.uid for rf in registered_functions] for f in listed_functions
375+
)
376+
assert page_info.count == len(listed_functions)
377+
assert page_info.total == total_number_functions
363378

364379
# Verify the functions are sorted correctly based on the order_by parameter
365380
if order_by:
366381
field = order_by.field
367382
direction = order_by.direction
368383
sorted_functions = sorted(
369-
functions,
384+
listed_functions,
370385
key=lambda f: getattr(f, field),
371386
reverse=(direction == OrderDirection.DESC),
372387
)
373-
assert functions == sorted_functions
388+
assert listed_functions == sorted_functions
374389

375390

376391
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)