Skip to content

Commit 743d6e1

Browse files
committed
split tests
1 parent bb6415c commit 743d6e1

File tree

2 files changed

+81
-104
lines changed

2 files changed

+81
-104
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# pylint: disable=redefined-outer-name
2+
# pylint: disable=unused-argument
3+
# pylint: disable=unused-variable
4+
# pylint: disable=too-many-arguments
5+
6+
from copy import deepcopy
7+
8+
import pytest
9+
from common_library.users_enums import UserRole
10+
from models_library.api_schemas_webserver.users import (
11+
MyProfileGet,
12+
MyProfilePatch,
13+
)
14+
from pydantic import ValidationError
15+
16+
17+
@pytest.mark.parametrize("user_role", [u.name for u in UserRole])
18+
def test_profile_get_role(user_role: str):
19+
for example in MyProfileGet.model_json_schema()["examples"]:
20+
data = deepcopy(example)
21+
data["role"] = user_role
22+
m1 = MyProfileGet(**data)
23+
24+
data["role"] = UserRole(user_role)
25+
m2 = MyProfileGet(**data)
26+
assert m1 == m2
27+
28+
29+
def test_my_profile_patch_username_min_len():
30+
# minimum length username is 4
31+
with pytest.raises(ValidationError) as err_info:
32+
MyProfilePatch.model_validate({"userName": "abc"})
33+
34+
assert err_info.value.error_count() == 1
35+
assert err_info.value.errors()[0]["type"] == "too_short"
36+
37+
MyProfilePatch.model_validate({"userName": "abcd"}) # OK
38+
39+
40+
def test_my_profile_patch_username_valid_characters():
41+
# Ensure valid characters (alphanumeric + . _ -)
42+
with pytest.raises(ValidationError, match="start with a letter") as err_info:
43+
MyProfilePatch.model_validate({"userName": "1234"})
44+
45+
assert err_info.value.error_count() == 1
46+
assert err_info.value.errors()[0]["type"] == "value_error"
47+
48+
MyProfilePatch.model_validate({"userName": "u1234"}) # OK
49+
50+
51+
def test_my_profile_patch_username_special_characters():
52+
# Ensure no consecutive special characters
53+
with pytest.raises(
54+
ValidationError, match="consecutive special characters"
55+
) as err_info:
56+
MyProfilePatch.model_validate({"userName": "u1__234"})
57+
58+
assert err_info.value.error_count() == 1
59+
assert err_info.value.errors()[0]["type"] == "value_error"
60+
61+
MyProfilePatch.model_validate({"userName": "u1_234"}) # OK
62+
63+
# Ensure it doesn't end with a special character
64+
with pytest.raises(ValidationError, match="end with") as err_info:
65+
MyProfilePatch.model_validate({"userName": "u1234_"})
66+
67+
assert err_info.value.error_count() == 1
68+
assert err_info.value.errors()[0]["type"] == "value_error"
69+
70+
MyProfilePatch.model_validate({"userName": "u1_234"}) # OK
71+
72+
73+
def test_my_profile_patch_username_reserved_words():
74+
# Check reserved words (example list; extend as needed)
75+
with pytest.raises(ValidationError, match="cannot be used") as err_info:
76+
MyProfilePatch.model_validate({"userName": "admin"})
77+
78+
assert err_info.value.error_count() == 1
79+
assert err_info.value.errors()[0]["type"] == "value_error"
80+
81+
MyProfilePatch.model_validate({"userName": "midas"}) # OK

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

Lines changed: 0 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
# pylint: disable=unused-variable
44
# pylint: disable=too-many-arguments
55

6-
import itertools
7-
from copy import deepcopy
86
from datetime import UTC, datetime
97
from typing import Any
108

@@ -16,46 +14,11 @@
1614
MyProfilePrivacyGet,
1715
)
1816
from models_library.generics import Envelope
19-
from models_library.users import UserThirdPartyToken
2017
from models_library.utils.fastapi_encoders import jsonable_encoder
21-
from pydantic import BaseModel, ValidationError
22-
from pytest_simcore.pydantic_models import (
23-
assert_validation_model,
24-
iter_model_examples_in_class,
25-
)
2618
from servicelib.rest_constants import RESPONSE_MODEL_POLICY
27-
from simcore_postgres_database.models.users import UserRole
2819
from simcore_service_webserver.users._common.models import ToUserUpdateDB
2920

3021

31-
@pytest.mark.parametrize(
32-
"model_cls, example_name, example_data",
33-
itertools.chain(
34-
iter_model_examples_in_class(MyProfileGet),
35-
iter_model_examples_in_class(UserThirdPartyToken),
36-
),
37-
)
38-
def test_user_models_examples(
39-
model_cls: type[BaseModel], example_name: str, example_data: Any
40-
):
41-
model_instance = assert_validation_model(
42-
model_cls, example_name=example_name, example_data=example_data
43-
)
44-
45-
model_enveloped = Envelope[model_cls].from_data(
46-
model_instance.model_dump(by_alias=True)
47-
)
48-
model_array_enveloped = Envelope[list[model_cls]].from_data(
49-
[
50-
model_instance.model_dump(by_alias=True),
51-
model_instance.model_dump(by_alias=True),
52-
]
53-
)
54-
55-
assert model_enveloped.error is None
56-
assert model_array_enveloped.error is None
57-
58-
5922
@pytest.fixture
6023
def fake_profile_get(faker: Faker) -> MyProfileGet:
6124
fake_profile: dict[str, Any] = faker.simple_profile()
@@ -104,18 +67,6 @@ def test_auto_compute_gravatar__deprecated(fake_profile_get: MyProfileGet):
10467
assert data["preferences"] == profile.preferences
10568

10669

107-
@pytest.mark.parametrize("user_role", [u.name for u in UserRole])
108-
def test_profile_get_role(user_role: str):
109-
for example in MyProfileGet.model_json_schema()["examples"]:
110-
data = deepcopy(example)
111-
data["role"] = user_role
112-
m1 = MyProfileGet(**data)
113-
114-
data["role"] = UserRole(user_role)
115-
m2 = MyProfileGet(**data)
116-
assert m1 == m2
117-
118-
11970
def test_parsing_output_of_get_user_profile():
12071
result_from_db_query_and_composition = {
12172
"id": 1,
@@ -185,58 +136,3 @@ def test_mapping_update_models_from_rest_to_db():
185136
"name": "foo1234",
186137
"privacy_hide_fullname": False,
187138
}
188-
189-
190-
def test_my_profile_patch_username_min_len():
191-
# minimum length username is 4
192-
with pytest.raises(ValidationError) as err_info:
193-
MyProfilePatch.model_validate({"userName": "abc"})
194-
195-
assert err_info.value.error_count() == 1
196-
assert err_info.value.errors()[0]["type"] == "too_short"
197-
198-
MyProfilePatch.model_validate({"userName": "abcd"}) # OK
199-
200-
201-
def test_my_profile_patch_username_valid_characters():
202-
# Ensure valid characters (alphanumeric + . _ -)
203-
with pytest.raises(ValidationError, match="start with a letter") as err_info:
204-
MyProfilePatch.model_validate({"userName": "1234"})
205-
206-
assert err_info.value.error_count() == 1
207-
assert err_info.value.errors()[0]["type"] == "value_error"
208-
209-
MyProfilePatch.model_validate({"userName": "u1234"}) # OK
210-
211-
212-
def test_my_profile_patch_username_special_characters():
213-
# Ensure no consecutive special characters
214-
with pytest.raises(
215-
ValidationError, match="consecutive special characters"
216-
) as err_info:
217-
MyProfilePatch.model_validate({"userName": "u1__234"})
218-
219-
assert err_info.value.error_count() == 1
220-
assert err_info.value.errors()[0]["type"] == "value_error"
221-
222-
MyProfilePatch.model_validate({"userName": "u1_234"}) # OK
223-
224-
# Ensure it doesn't end with a special character
225-
with pytest.raises(ValidationError, match="end with") as err_info:
226-
MyProfilePatch.model_validate({"userName": "u1234_"})
227-
228-
assert err_info.value.error_count() == 1
229-
assert err_info.value.errors()[0]["type"] == "value_error"
230-
231-
MyProfilePatch.model_validate({"userName": "u1_234"}) # OK
232-
233-
234-
def test_my_profile_patch_username_reserved_words():
235-
# Check reserved words (example list; extend as needed)
236-
with pytest.raises(ValidationError, match="cannot be used") as err_info:
237-
MyProfilePatch.model_validate({"userName": "admin"})
238-
239-
assert err_info.value.error_count() == 1
240-
assert err_info.value.errors()[0]["type"] == "value_error"
241-
242-
MyProfilePatch.model_validate({"userName": "midas"}) # OK

0 commit comments

Comments
 (0)