Skip to content

Commit 401282b

Browse files
committed
adds tests
1 parent c02392a commit 401282b

File tree

3 files changed

+147
-9
lines changed

3 files changed

+147
-9
lines changed

packages/postgres-database/src/simcore_postgres_database/utils_groups_extra_properties.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import logging
33
import warnings
44
from dataclasses import dataclass, fields
5-
from typing import Any
5+
from typing import Any, Callable
66

77
import sqlalchemy as sa
88
from aiopg.sa.connection import SAConnection
@@ -135,10 +135,10 @@ async def get_v2(
135135
raise GroupExtraPropertiesNotFoundError(msg)
136136

137137
@staticmethod
138-
def _aggregate(rows, user_id, product_name):
138+
def _aggregate(rows, user_id, product_name, from_row: Callable):
139139
merged_standard_extra_properties = None
140140
for row in rows:
141-
group_extra_properties = GroupExtraProperties.from_row_proxy(row)
141+
group_extra_properties = from_row(row)
142142
match row.type:
143143
case GroupType.PRIMARY:
144144
# this always has highest priority
@@ -198,7 +198,9 @@ async def get_aggregated_properties_for_user(
198198
rows: list[RowProxy] | None = await result.fetchall()
199199
assert rows is not None # nosec
200200

201-
return GroupExtraPropertiesRepo._aggregate(rows, user_id, product_name)
201+
return GroupExtraPropertiesRepo._aggregate(
202+
rows, user_id, product_name, GroupExtraProperties.from_row_proxy
203+
)
202204

203205
@staticmethod
204206
async def get_aggregated_properties_for_user_v2(
@@ -217,4 +219,6 @@ async def get_aggregated_properties_for_user_v2(
217219
sa.select(list_stmt).order_by(list_stmt.c.type_order)
218220
)
219221
rows = [row async for row in result]
220-
return GroupExtraPropertiesRepo._aggregate(rows, user_id, product_name)
222+
return GroupExtraPropertiesRepo._aggregate(
223+
rows, user_id, product_name, GroupExtraProperties.from_orm
224+
)

packages/postgres-database/tests/test_utils_groups_extra_properties.py

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
GroupExtraPropertiesRepo,
2222
)
2323
from sqlalchemy import literal_column
24+
from sqlalchemy.ext.asyncio import AsyncEngine
2425

2526

2627
async def test_get_raises_if_not_found(
@@ -101,6 +102,28 @@ async def test_get(
101102
assert created_extra_properties == received_extra_properties
102103

103104

105+
async def test_get_v2(
106+
asyncpg_engine: AsyncEngine,
107+
registered_user: RowProxy,
108+
product_name: str,
109+
create_fake_product: Callable[..., Awaitable[RowProxy]],
110+
create_fake_group_extra_properties: Callable[..., Awaitable[GroupExtraProperties]],
111+
):
112+
with pytest.raises(GroupExtraPropertiesNotFoundError):
113+
await GroupExtraPropertiesRepo.get_v2(
114+
asyncpg_engine, gid=registered_user.primary_gid, product_name=product_name
115+
)
116+
117+
await create_fake_product(product_name)
118+
created_extra_properties = await create_fake_group_extra_properties(
119+
registered_user.primary_gid, product_name
120+
)
121+
received_extra_properties = await GroupExtraPropertiesRepo.get_v2(
122+
asyncpg_engine, gid=registered_user.primary_gid, product_name=product_name
123+
)
124+
assert created_extra_properties == received_extra_properties
125+
126+
104127
@pytest.fixture
105128
async def everyone_group_id(connection: aiopg.sa.connection.SAConnection) -> int:
106129
result = await connection.scalar(
@@ -355,3 +378,114 @@ async def test_get_aggregated_properties_for_user_returns_property_values_as_tru
355378
assert aggregated_group_properties.internet_access is False
356379
assert aggregated_group_properties.override_services_specifications is False
357380
assert aggregated_group_properties.use_on_demand_clusters is True
381+
382+
383+
async def test_get_aggregated_properties_for_user_returns_property_values_as_truthy_if_one_of_them_is_v2(
384+
asyncpg_engine: AsyncEngine,
385+
connection: aiopg.sa.connection.SAConnection,
386+
product_name: str,
387+
registered_user: RowProxy,
388+
create_fake_product: Callable[..., Awaitable[RowProxy]],
389+
create_fake_group: Callable[..., Awaitable[RowProxy]],
390+
create_fake_group_extra_properties: Callable[..., Awaitable[GroupExtraProperties]],
391+
everyone_group_id: int,
392+
):
393+
await create_fake_product(product_name)
394+
await create_fake_product(f"{product_name}_additional_just_for_fun")
395+
396+
# create a specific extra properties for group that disallow everything
397+
everyone_group_extra_properties = await create_fake_group_extra_properties(
398+
everyone_group_id,
399+
product_name,
400+
internet_access=False,
401+
override_services_specifications=False,
402+
use_on_demand_clusters=False,
403+
)
404+
# this should return the everyone group properties
405+
aggregated_group_properties = (
406+
await GroupExtraPropertiesRepo.get_aggregated_properties_for_user_v2(
407+
asyncpg_engine, user_id=registered_user.id, product_name=product_name
408+
)
409+
)
410+
assert aggregated_group_properties == everyone_group_extra_properties
411+
412+
# now we create some standard groups and add the user to them and make everything false for now
413+
standard_groups = [await create_fake_group(connection) for _ in range(5)]
414+
for group in standard_groups:
415+
await create_fake_group_extra_properties(
416+
group.gid,
417+
product_name,
418+
internet_access=False,
419+
override_services_specifications=False,
420+
use_on_demand_clusters=False,
421+
)
422+
await _add_user_to_group(
423+
connection, user_id=registered_user.id, group_id=group.gid
424+
)
425+
426+
# now we still should not have any of these value Truthy
427+
aggregated_group_properties = (
428+
await GroupExtraPropertiesRepo.get_aggregated_properties_for_user_v2(
429+
asyncpg_engine, user_id=registered_user.id, product_name=product_name
430+
)
431+
)
432+
assert aggregated_group_properties.internet_access is False
433+
assert aggregated_group_properties.override_services_specifications is False
434+
assert aggregated_group_properties.use_on_demand_clusters is False
435+
436+
# let's change one of these standard groups
437+
random_standard_group = random.choice(standard_groups) # noqa: S311
438+
result = await connection.execute(
439+
groups_extra_properties.update()
440+
.where(groups_extra_properties.c.group_id == random_standard_group.gid)
441+
.values(internet_access=True)
442+
)
443+
assert result.rowcount == 1
444+
445+
# now we should have internet access
446+
aggregated_group_properties = (
447+
await GroupExtraPropertiesRepo.get_aggregated_properties_for_user_v2(
448+
asyncpg_engine, user_id=registered_user.id, product_name=product_name
449+
)
450+
)
451+
assert aggregated_group_properties.internet_access is True
452+
assert aggregated_group_properties.override_services_specifications is False
453+
assert aggregated_group_properties.use_on_demand_clusters is False
454+
455+
# let's change another one of these standard groups
456+
random_standard_group = random.choice(standard_groups) # noqa: S311
457+
result = await connection.execute(
458+
groups_extra_properties.update()
459+
.where(groups_extra_properties.c.group_id == random_standard_group.gid)
460+
.values(override_services_specifications=True)
461+
)
462+
assert result.rowcount == 1
463+
464+
# now we should have internet access and service override
465+
aggregated_group_properties = (
466+
await GroupExtraPropertiesRepo.get_aggregated_properties_for_user_v2(
467+
asyncpg_engine, user_id=registered_user.id, product_name=product_name
468+
)
469+
)
470+
assert aggregated_group_properties.internet_access is True
471+
assert aggregated_group_properties.override_services_specifications is True
472+
assert aggregated_group_properties.use_on_demand_clusters is False
473+
474+
# and we can deny it again by setting a primary extra property
475+
# now create some personal extra properties
476+
personal_group_extra_properties = await create_fake_group_extra_properties(
477+
registered_user.primary_gid,
478+
product_name,
479+
internet_access=False,
480+
use_on_demand_clusters=True,
481+
)
482+
assert personal_group_extra_properties
483+
484+
aggregated_group_properties = (
485+
await GroupExtraPropertiesRepo.get_aggregated_properties_for_user_v2(
486+
asyncpg_engine, user_id=registered_user.id, product_name=product_name
487+
)
488+
)
489+
assert aggregated_group_properties.internet_access is False
490+
assert aggregated_group_properties.override_services_specifications is False
491+
assert aggregated_group_properties.use_on_demand_clusters is True

packages/postgres-database/tests/test_utils_projects.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
# pylint: disable=unused-variable
44
# pylint: disable=too-many-arguments
55
import uuid
6-
from collections.abc import Awaitable, Callable
7-
from datetime import datetime, timezone
8-
from typing import Any, AsyncIterator
6+
from collections.abc import AsyncIterator, Awaitable, Callable
7+
from datetime import UTC, datetime
8+
from typing import Any
99

1010
import pytest
1111
import sqlalchemy as sa
@@ -53,7 +53,7 @@ async def registered_project(
5353
await _delete_project(connection, project["uuid"])
5454

5555

56-
@pytest.mark.parametrize("expected", (datetime.now(tz=timezone.utc), None))
56+
@pytest.mark.parametrize("expected", (datetime.now(tz=UTC), None))
5757
async def test_get_project_trashed_at_column_can_be_converted_to_datetime(
5858
asyncpg_engine: AsyncEngine, registered_project: dict, expected: datetime | None
5959
):

0 commit comments

Comments
 (0)