|
2 | 2 |
|
3 | 3 | import pytest |
4 | 4 |
|
5 | | -from json_to_models.dynamic_typing import (DDict, DList, DOptional, FloatString, IntString, ModelMeta, compile_imports) |
| 5 | +from json_to_models.dynamic_typing import (DDict, DList, DOptional, DUnion, FloatString, IntString, ModelMeta, |
| 6 | + compile_imports) |
6 | 7 | from json_to_models.models.attr import AttrsModelCodeGenerator, DEFAULT_ORDER |
7 | 8 | from json_to_models.models.base import METADATA_FIELD_NAME, generate_code, sort_kwargs |
8 | 9 | from json_to_models.models.structure import sort_fields |
@@ -142,23 +143,53 @@ class Test: |
142 | 143 | }, |
143 | 144 | "generated": trim(f""" |
144 | 145 | import attr |
145 | | - from attr.converter import optional |
146 | 146 | from json_to_models.dynamic_typing import FloatString, IntString |
| 147 | + from json_to_models.models import ClassType |
| 148 | + from json_to_models.models.string_converters import convert_strings |
147 | 149 | from typing import Dict, List, Optional |
148 | 150 |
|
149 | 151 |
|
150 | 152 | @attr.s |
| 153 | + @convert_strings(['bar#O.S', 'qwerty'], class_type=ClassType.Attrs) |
151 | 154 | class Test: |
152 | 155 | foo: int = attr.ib() |
153 | | - qwerty: FloatString = attr.ib(converter=FloatString) |
| 156 | + qwerty: FloatString = attr.ib() |
154 | 157 | dict: Dict[str, int] = attr.ib() |
155 | 158 | not_: bool = attr.ib({field_meta('not')}) |
156 | 159 | one_day: int = attr.ib({field_meta('1day')}) |
157 | 160 | den_nedeli: str = attr.ib({field_meta('день_недели')}) |
158 | 161 | baz: Optional[List[List[str]]] = attr.ib(factory=list) |
159 | | - bar: Optional[IntString] = attr.ib(default=None, converter=optional(IntString)) |
| 162 | + bar: Optional[IntString] = attr.ib(default=None) |
160 | 163 | asdfg: Optional[int] = attr.ib(default=None) |
161 | 164 | """) |
| 165 | + }, |
| 166 | + "converters": { |
| 167 | + "model": ("Test", { |
| 168 | + "a": int, |
| 169 | + "b": IntString, |
| 170 | + "c": DOptional(FloatString), |
| 171 | + "d": DList(DList(DList(IntString))), |
| 172 | + "e": DDict(IntString), |
| 173 | + "u": DUnion(DDict(IntString), DList(DList(IntString))), |
| 174 | + }), |
| 175 | + "generated": trim(""" |
| 176 | + import attr |
| 177 | + from json_to_models.dynamic_typing import FloatString, IntString |
| 178 | + from json_to_models.models import ClassType |
| 179 | + from json_to_models.models.string_converters import convert_strings |
| 180 | + from typing import Dict, List, Optional, Union |
| 181 | +
|
| 182 | +
|
| 183 | + @attr.s |
| 184 | + @convert_strings(['b', 'c#O.S', 'd#L.L.L.S', 'e#D.S'], class_type=ClassType.Attrs) |
| 185 | + class Test: |
| 186 | + a: int = attr.ib() |
| 187 | + b: IntString = attr.ib() |
| 188 | + d: List[List[List[IntString]]] = attr.ib() |
| 189 | + e: Dict[str, IntString] = attr.ib() |
| 190 | + u: Union[Dict[str, IntString], List[List[IntString]]] = attr.ib() |
| 191 | + c: Optional[FloatString] = attr.ib(default=None) |
| 192 | + """) |
162 | 193 | } |
163 | 194 | } |
164 | 195 |
|
@@ -200,5 +231,5 @@ def test_fields_attr(value: ModelMeta, expected: dict): |
200 | 231 | @pytest.mark.parametrize("value,expected", test_data_unzip["generated"]) |
201 | 232 | def test_generated_attr(value: ModelMeta, expected: str): |
202 | 233 | generated = generate_code(([{"model": value, "nested": []}], {}), AttrsModelCodeGenerator, |
203 | | - class_generator_kwargs={'meta': True}) |
| 234 | + class_generator_kwargs={'meta': True, 'post_init_converters': True}) |
204 | 235 | assert generated.rstrip() == expected, generated |
0 commit comments