Skip to content

Commit d00d78e

Browse files
committed
more group examples
1 parent 6836ff2 commit d00d78e

File tree

3 files changed

+76
-39
lines changed

3 files changed

+76
-39
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,12 @@ class MyGroupsGet(OutputSchema):
187187
organizations: list[GroupGet] | None = None
188188
all: GroupGet
189189
product: GroupGet | None = None
190+
support: Annotated[
191+
GroupGet | None,
192+
Field(
193+
description="Group ID of the app support team or None if no support is defined for this product"
194+
),
195+
] = None
190196

191197
model_config = ConfigDict(
192198
json_schema_extra={
@@ -225,6 +231,12 @@ class MyGroupsGet(OutputSchema):
225231
"description": "Open to all users",
226232
"accessRights": {"read": True, "write": False, "delete": False},
227233
},
234+
"support": {
235+
"gid": "2",
236+
"label": "Support Team",
237+
"description": "The support team of the application",
238+
"accessRights": {"read": False, "write": False, "delete": False},
239+
},
228240
}
229241
}
230242
)
@@ -234,6 +246,7 @@ def from_domain_model(
234246
cls,
235247
groups_by_type: GroupsByTypeTuple,
236248
my_product_group: tuple[Group, AccessRightsDict] | None,
249+
my_support_group: tuple[Group, AccessRightsDict] | None = None,
237250
) -> Self:
238251
assert groups_by_type.primary # nosec
239252
assert groups_by_type.everyone # nosec
@@ -249,6 +262,11 @@ def from_domain_model(
249262
if my_product_group
250263
else None
251264
),
265+
support=(
266+
GroupGet.from_domain_model(*my_support_group)
267+
if my_support_group
268+
else None
269+
),
252270
)
253271

254272

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

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -39,41 +39,44 @@ class Group(BaseModel):
3939

4040
@staticmethod
4141
def _update_json_schema_extra(schema: JsonDict) -> None:
42-
schema.update(
43-
{
44-
"examples": [
45-
{
46-
"gid": 1,
47-
"name": "Everyone",
48-
"type": "everyone",
49-
"description": "all users",
50-
"thumbnail": None,
51-
},
52-
{
53-
"gid": 2,
54-
"name": "User",
55-
"description": "primary group",
56-
"type": "primary",
57-
"thumbnail": None,
58-
},
59-
{
60-
"gid": 3,
61-
"name": "Organization",
62-
"description": "standard group",
63-
"type": "standard",
64-
"thumbnail": None,
65-
"inclusionRules": {},
66-
},
67-
{
68-
"gid": 4,
69-
"name": "Product",
70-
"description": "standard group for products",
71-
"type": "standard",
72-
"thumbnail": None,
73-
},
74-
]
75-
}
76-
)
42+
everyone = {
43+
"gid": 1,
44+
"name": "Everyone",
45+
"type": "everyone",
46+
"description": "all users",
47+
"thumbnail": None,
48+
}
49+
user = {
50+
"gid": 2,
51+
"name": "User",
52+
"description": "primary group",
53+
"type": "primary",
54+
"thumbnail": None,
55+
}
56+
organization = {
57+
"gid": 3,
58+
"name": "Organization",
59+
"description": "standard group",
60+
"type": "standard",
61+
"thumbnail": None,
62+
"inclusionRules": {},
63+
}
64+
product = {
65+
"gid": 4,
66+
"name": "Product",
67+
"description": "standard group for products",
68+
"type": "standard",
69+
"thumbnail": None,
70+
}
71+
support = {
72+
"gid": 5,
73+
"name": "Support",
74+
"description": "support group",
75+
"type": "standard",
76+
"thumbnail": None,
77+
}
78+
79+
schema.update({"examples": [everyone, user, organization, product, support]})
7780

7881
model_config = ConfigDict(
7982
populate_by_name=True, json_schema_extra=_update_json_schema_extra
Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import pytest
12
from models_library.api_schemas_webserver.users import (
23
MyProfileRestGet,
34
)
@@ -7,7 +8,11 @@
78
from pydantic import TypeAdapter
89

910

10-
def test_adapter_from_model_to_schema():
11+
@pytest.mark.parametrize("with_support_group", [True, False])
12+
@pytest.mark.parametrize("with_standard_groups", [True, False])
13+
def test_adapter_from_model_to_schema(
14+
with_support_group: bool, with_standard_groups: bool
15+
):
1116
my_profile = MyProfile.model_validate(MyProfile.model_json_schema()["example"])
1217

1318
groups = TypeAdapter(list[Group]).validate_python(
@@ -17,13 +22,24 @@ def test_adapter_from_model_to_schema():
1722
ar = AccessRightsDict(read=False, write=False, delete=False)
1823

1924
my_groups_by_type = GroupsByTypeTuple(
20-
primary=(groups[1], ar), standard=[(groups[2], ar)], everyone=(groups[0], ar)
25+
primary=(groups[1], ar),
26+
standard=[(groups[2], ar)] if with_standard_groups else [],
27+
everyone=(groups[0], ar),
2128
)
22-
my_product_group = groups[-1], AccessRightsDict(
29+
my_product_group = groups[3], AccessRightsDict(
2330
read=False, write=False, delete=False
2431
)
32+
33+
my_support_group = groups[4], AccessRightsDict(
34+
read=False, write=False, delete=False
35+
)
36+
2537
my_preferences = {"foo": Preference(default_value=3, value=1)}
2638

2739
MyProfileRestGet.from_domain_model(
28-
my_profile, my_groups_by_type, my_product_group, my_preferences
40+
my_profile,
41+
my_groups_by_type,
42+
my_product_group,
43+
my_preferences,
44+
my_support_group if with_support_group else None,
2945
)

0 commit comments

Comments
 (0)