Skip to content

Commit ce104a4

Browse files
tests: add deletion tests
1 parent 59a4d50 commit ce104a4

File tree

2 files changed

+117
-2
lines changed

2 files changed

+117
-2
lines changed

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

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from collections.abc import AsyncIterator, Awaitable, Callable
77
from contextlib import AsyncExitStack
88
from typing import Any
9-
from uuid import uuid4
9+
from uuid import UUID, uuid4
1010

1111
import pytest
1212
import sqlalchemy
@@ -31,6 +31,13 @@
3131
from simcore_postgres_database.models.funcapi_api_access_rights_table import (
3232
funcapi_api_access_rights_table,
3333
)
34+
from simcore_postgres_database.models.funcapi_function_jobs_table import (
35+
function_jobs_table,
36+
)
37+
from simcore_postgres_database.models.funcapi_functions_access_rights_table import (
38+
functions_access_rights_table,
39+
)
40+
from simcore_postgres_database.models.funcapi_functions_table import functions_table
3441
from simcore_service_webserver.application_settings import ApplicationSettings
3542
from simcore_service_webserver.statics._constants import FRONTEND_APP_DEFAULT
3643
from sqlalchemy.ext.asyncio import AsyncEngine
@@ -253,3 +260,71 @@ async def logged_user_function_api_access_rights(
253260
async with AsyncExitStack() as stack:
254261
row = await stack.enter_async_context(cm)
255262
yield row
263+
264+
265+
@pytest.fixture
266+
async def fake_function_with_associated_job(
267+
asyncpg_engine: AsyncEngine,
268+
logged_user: UserInfoDict,
269+
) -> AsyncIterator[UUID]:
270+
async with AsyncExitStack() as stack:
271+
function_row = await stack.enter_async_context(
272+
insert_and_get_row_lifespan(
273+
asyncpg_engine,
274+
table=functions_table,
275+
values={
276+
"title": "Test Function",
277+
"function_class": FunctionClass.PROJECT.value,
278+
"description": "A test function",
279+
"input_schema": {
280+
"schema_class": "application/schema+json",
281+
"schema_content": {
282+
"type": "object",
283+
"properties": {"input1": {"type": "string"}},
284+
},
285+
},
286+
"output_schema": {
287+
"schema_class": "application/schema+json",
288+
"schema_content": {
289+
"type": "object",
290+
"properties": {"output1": {"type": "string"}},
291+
},
292+
},
293+
"class_specific_data": {"project_id": f"{uuid4()}"},
294+
},
295+
pk_col=functions_table.c.uuid,
296+
)
297+
)
298+
299+
await stack.enter_async_context(
300+
insert_and_get_row_lifespan(
301+
asyncpg_engine,
302+
table=functions_access_rights_table,
303+
values={
304+
"function_uuid": function_row["uuid"],
305+
"group_id": logged_user["primary_gid"],
306+
"product_name": "osparc", # Default product name
307+
"read": True,
308+
"write": True,
309+
"execute": True,
310+
},
311+
pk_cols=[
312+
functions_access_rights_table.c.function_uuid,
313+
functions_access_rights_table.c.group_id,
314+
functions_access_rights_table.c.product_name,
315+
],
316+
)
317+
)
318+
319+
await stack.enter_async_context(
320+
insert_and_get_row_lifespan(
321+
asyncpg_engine,
322+
table=function_jobs_table,
323+
values={
324+
"function_uuid": function_row["uuid"],
325+
"status": "pending",
326+
},
327+
pk_col=function_jobs_table.c.uuid,
328+
)
329+
)
330+
yield function_row["uuid"]

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

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from collections.abc import AsyncIterator
88
from http import HTTPStatus
99
from typing import Any
10-
from uuid import uuid4
10+
from uuid import UUID, uuid4
1111

1212
import pytest
1313
from aiohttp.test_utils import TestClient
@@ -306,3 +306,43 @@ async def test_list_user_functions_permissions(
306306
function_permissions = MyFunctionPermissionsGet.model_validate(data)
307307
assert function_permissions.read_functions == expected_read_functions
308308
assert function_permissions.write_functions == expected_write_functions
309+
310+
311+
@pytest.mark.parametrize(
312+
"user_role,expected_read_functions,expected_write_functions",
313+
[(UserRole.USER, True, True)],
314+
)
315+
async def test_delete_function_with_associated_jobs(
316+
client: TestClient,
317+
logged_user: UserInfoDict,
318+
fake_function_with_associated_job: UUID,
319+
logged_user_function_api_access_rights: dict[str, Any],
320+
) -> None:
321+
function_id = fake_function_with_associated_job
322+
323+
url = client.app.router["get_function"].url_for(function_id=f"{function_id}")
324+
response = await client.get(url)
325+
data, error = await assert_status(response, status.HTTP_200_OK)
326+
assert not error
327+
function = TypeAdapter(RegisteredFunctionGet).validate_python(data)
328+
assert function.uid == function_id
329+
330+
url = client.app.router["delete_function"].url_for(function_id=f"{function_id}")
331+
response = await client.delete(url)
332+
data, error = await assert_status(response, status.HTTP_409_CONFLICT)
333+
assert error is not None
334+
335+
url = client.app.router["get_function"].url_for(function_id=f"{function_id}")
336+
response = await client.get(url)
337+
data, error = await assert_status(response, status.HTTP_200_OK)
338+
assert not error
339+
340+
url = client.app.router["delete_function"].url_for(function_id=f"{function_id}")
341+
response = await client.delete(url, params={"force": "true"})
342+
data, error = await assert_status(response, status.HTTP_204_NO_CONTENT)
343+
assert not error
344+
345+
url = client.app.router["get_function"].url_for(function_id=f"{function_id}")
346+
response = await client.get(url)
347+
data, error = await assert_status(response, status.HTTP_404_NOT_FOUND)
348+
assert error is not None

0 commit comments

Comments
 (0)