|
| 1 | +import json |
1 | 2 | import os |
2 | 3 |
|
3 | 4 | import pytest |
| 5 | +from pydantic import ValidationError |
4 | 6 |
|
5 | 7 | from scim2_models import BulkRequest |
6 | 8 | from scim2_models import BulkResponse |
|
19 | 21 | from scim2_models import get_model_by_schema |
20 | 22 |
|
21 | 23 |
|
| 24 | +def _error_summary(exc: ValidationError) -> list[tuple[str, tuple]]: |
| 25 | + return [(error["type"], error["loc"]) for error in exc.errors()] |
| 26 | + |
| 27 | + |
| 28 | +SAMPLE_MODELS = { |
| 29 | + "user": User, |
| 30 | + "enterprise_user": User[EnterpriseUser], |
| 31 | + "group": Group, |
| 32 | + "schema": Schema, |
| 33 | + "resource_type": ResourceType, |
| 34 | + "service_provider_configuration": ServiceProviderConfig, |
| 35 | + "list_response": ListResponse[User[EnterpriseUser] | Group | Schema | ResourceType], |
| 36 | + "patch_op": PatchOp[User], |
| 37 | + "bulk_request": BulkRequest, |
| 38 | + "bulk_response": BulkResponse, |
| 39 | + "search_request": SearchRequest, |
| 40 | + "error": Error, |
| 41 | +} |
| 42 | + |
| 43 | + |
22 | 44 | def test_parse_and_serialize_examples(load_sample): |
23 | 45 | samples = list(os.walk("samples"))[0][2] |
24 | | - models = { |
25 | | - "user": User, |
26 | | - "enterprise_user": User[EnterpriseUser], |
27 | | - "group": Group, |
28 | | - "schema": Schema, |
29 | | - "resource_type": ResourceType, |
30 | | - "service_provider_configuration": ServiceProviderConfig, |
31 | | - "list_response": ListResponse[ |
32 | | - User[EnterpriseUser] | Group | Schema | ResourceType |
33 | | - ], |
34 | | - "patch_op": PatchOp[User], |
35 | | - "bulk_request": BulkRequest, |
36 | | - "bulk_response": BulkResponse, |
37 | | - "search_request": SearchRequest, |
38 | | - "error": Error, |
39 | | - } |
40 | 46 |
|
41 | 47 | for sample in samples: |
42 | 48 | model_name = sample.replace(".json", "").split("-")[2] |
43 | | - model = models[model_name] |
| 49 | + model = SAMPLE_MODELS[model_name] |
44 | 50 |
|
45 | 51 | skipped = [ |
46 | 52 | # resources without schemas are not yet supported |
@@ -76,6 +82,32 @@ def test_parse_and_serialize_examples(load_sample): |
76 | 82 | assert obj.model_dump(exclude_unset=True) == payload |
77 | 83 |
|
78 | 84 |
|
| 85 | +def test_parse_json_and_decoded_examples(load_sample): |
| 86 | + """JSON payloads and already decoded payloads are validated the same way.""" |
| 87 | + samples = list(os.walk("samples"))[0][2] |
| 88 | + |
| 89 | + for sample in samples: |
| 90 | + model_name = sample.replace(".json", "").split("-")[2] |
| 91 | + model = SAMPLE_MODELS[model_name] |
| 92 | + |
| 93 | + payload = load_sample(sample) |
| 94 | + raw = json.dumps(payload) |
| 95 | + |
| 96 | + try: |
| 97 | + obj = model.model_validate(payload) |
| 98 | + except ValidationError as exc: |
| 99 | + with pytest.raises(ValidationError) as json_exc: |
| 100 | + model.model_validate_json(raw) |
| 101 | + assert _error_summary(json_exc.value) == _error_summary(exc) |
| 102 | + continue |
| 103 | + |
| 104 | + json_obj = model.model_validate_json(raw) |
| 105 | + assert obj == json_obj |
| 106 | + assert obj.model_dump(exclude_unset=True) == json_obj.model_dump( |
| 107 | + exclude_unset=True |
| 108 | + ) |
| 109 | + |
| 110 | + |
79 | 111 | def test_get_model_by_schema(): |
80 | 112 | resource_types = [Group, User[EnterpriseUser]] |
81 | 113 | assert ( |
|
0 commit comments