|
1 | 1 | import json
|
2 | 2 | from uuid import UUID
|
3 | 3 |
|
4 |
| -from more_itertools import first |
5 | 4 | from pydantic.config import JsonDict
|
6 | 5 | import pytest
|
7 | 6 | from pydantic import BaseModel, ValidationError
|
8 | 7 |
|
9 | 8 | from pydantic_forms.core import FormPage
|
10 | 9 | from pydantic_forms.types import strEnum
|
11 | 10 | from pydantic_forms.validators import read_only_field, read_only_list, LongText, OrganisationId
|
12 |
| -from pydantic_forms.utils.schema import merge_json_schema, _get_field_info_with_schema |
| 11 | +from pydantic_forms.utils.schema import merge_json_schema |
13 | 12 |
|
14 | 13 |
|
15 | 14 | class TestEnum(strEnum):
|
@@ -108,10 +107,20 @@ class Form(FormPage):
|
108 | 107 |
|
109 | 108 |
|
110 | 109 | def test_read_only_field_list_with_empty_default_raises_error():
|
111 |
| - with pytest.raises(ValueError, match="Default list object must not be empty"): |
| 110 | + with pytest.raises(ValueError, match="Default argument must be a list"): |
112 | 111 |
|
113 | 112 | class Form(FormPage):
|
114 |
| - read_only: read_only_list([]) |
| 113 | + read_only: read_only_list() |
| 114 | + |
| 115 | + |
| 116 | +def test_read_only_list_empty_sets_default_to_array_string(): |
| 117 | + class Form(FormPage): |
| 118 | + read_only_list: read_only_list([]) |
| 119 | + |
| 120 | + validated = Form(read_only_list=[]) |
| 121 | + read_only_schema = validated.model_json_schema()["properties"]["read_only_list"] |
| 122 | + assert read_only_schema["items"]["type"] == "string" |
| 123 | + assert read_only_schema["default"] == [] |
115 | 124 |
|
116 | 125 |
|
117 | 126 | def test_read_only_field_list_with_mixed_types_raises_error():
|
@@ -227,3 +236,15 @@ def test_merge_json_schema():
|
227 | 236 |
|
228 | 237 | with pytest.raises(TypeError, match="Target type has no json_schema_extra"):
|
229 | 238 | merge_json_schema(OrganisationId, test_uuid1)
|
| 239 | + |
| 240 | + |
| 241 | +def test_stringy_types_import_works(): |
| 242 | + import importlib |
| 243 | + |
| 244 | + with pytest.MonkeyPatch.context() as mock: |
| 245 | + from pydantic_forms.validators.components import read_only |
| 246 | + |
| 247 | + mock.setattr(read_only.sys, "version_info", (3, 9)) |
| 248 | + importlib.reload(read_only) |
| 249 | + |
| 250 | + assert read_only.STRINGY_TYPES == (strEnum, UUID) |
0 commit comments