Skip to content

Commit 8bb03e6

Browse files
committed
adds tests for conversions
1 parent f0d9cbb commit 8bb03e6

File tree

4 files changed

+70
-9
lines changed

4 files changed

+70
-9
lines changed

packages/models-library/src/models_library/api_schemas_webserver/groups.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ def from_model(cls, group: Group, access_rights: AccessRightsDict) -> Self:
8686
"thumbnail",
8787
"inclusion_rules",
8888
},
89+
exclude={"access_rights"},
8990
exclude_unset=True,
9091
by_alias=False,
9192
),

packages/models-library/src/models_library/groups.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,14 @@ class GroupMember(BaseModel):
8080

8181
class OrganizationCreate(BaseModel):
8282
name: str
83-
description: str | None
84-
thumbnail: str | None
83+
description: str | None = None
84+
thumbnail: str | None = None
8585

8686

8787
class OrganizationUpdate(BaseModel):
88-
name: str | None
89-
description: str | None
90-
thumbnail: str | None
88+
name: str | None = None
89+
description: str | None = None
90+
thumbnail: str | None = None
9191

9292

9393
class GroupAtDB(Group):

packages/pytest-simcore/src/pytest_simcore/simcore_webserver_groups_fixtures.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ async def standard_groups_owner(
7171
app=client.app,
7272
user_id=owner_user["id"],
7373
new_group={
74-
"label": "SPARC",
74+
"name": "SPARC",
7575
"description": "Stimulating Peripheral Activity to Relieve Conditions",
7676
"thumbnail": "https://commonfund.nih.gov/sites/default/files/sparc-image-homepage500px.png",
7777
"inclusionRules": {"email": r"@(sparc)+\.(io|com)$"},
@@ -81,8 +81,7 @@ async def standard_groups_owner(
8181
app=client.app,
8282
user_id=owner_user["id"],
8383
new_group={
84-
"gid": "5", # this will be replaced
85-
"label": "team Black",
84+
"name": "team Black",
8685
"description": "THE incredible black team",
8786
"thumbnail": None,
8887
"inclusionRules": {"email": r"@(black)+\.(io|com)$"},

services/web/server/tests/unit/isolated/test_groups_models.py

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
11
import models_library.groups
22
import simcore_postgres_database.models.groups
3-
from models_library.api_schemas_webserver.groups import GroupGet
3+
from faker import Faker
4+
from models_library.api_schemas_webserver._base import OutputSchema
5+
from models_library.api_schemas_webserver.groups import (
6+
GroupCreate,
7+
GroupGet,
8+
GroupUpdate,
9+
GroupUserGet,
10+
)
11+
from models_library.groups import (
12+
AccessRightsDict,
13+
Group,
14+
GroupMember,
15+
GroupTypeInModel,
16+
OrganizationCreate,
17+
OrganizationUpdate,
18+
)
419
from models_library.utils.enums import enum_to_dict
520

621

@@ -39,3 +54,49 @@ def test_sanitize_legacy_data():
3954
assert users_group_2.thumbnail is None
4055

4156
assert users_group_1 == users_group_2
57+
58+
59+
def test_output_schemas_from_models(faker: Faker):
60+
# output : schema <- model
61+
assert issubclass(GroupGet, OutputSchema)
62+
domain_model = Group(
63+
gid=1,
64+
name=faker.word(),
65+
description=faker.sentence(),
66+
group_type=GroupTypeInModel.STANDARD,
67+
thumbnail=None,
68+
)
69+
output_schema = GroupGet.from_model(
70+
domain_model,
71+
access_rights=AccessRightsDict(read=True, write=False, delete=False),
72+
)
73+
assert output_schema.label == domain_model.name
74+
75+
# output : schema <- model
76+
domain_model = GroupMember(
77+
id=12,
78+
name=faker.user_name(),
79+
email=None,
80+
first_name=None,
81+
last_name=None,
82+
primary_gid=13,
83+
access_rights=AccessRightsDict(read=True, write=False, delete=False),
84+
)
85+
output_schema = GroupUserGet.from_model(user=domain_model)
86+
assert output_schema.user_name == domain_model.name
87+
88+
89+
def test_input_schemas_to_models(faker: Faker):
90+
# input : scheam -> model
91+
input_schema = GroupCreate(
92+
label=faker.word(), description=faker.sentence(), thumbnail=faker.url()
93+
)
94+
domain_model = input_schema.to_model()
95+
assert isinstance(domain_model, OrganizationCreate)
96+
assert domain_model.name == input_schema.label
97+
98+
# input : scheam -> model
99+
input_schema = GroupUpdate(label=faker.word())
100+
domain_model = input_schema.to_model()
101+
assert isinstance(domain_model, OrganizationUpdate)
102+
assert domain_model.name == input_schema.label

0 commit comments

Comments
 (0)