|
3 | 3 | import pytest
|
4 | 4 | from typing_extensions import Literal
|
5 | 5 |
|
6 |
| -from json_to_models.dynamic_typing import (AbsoluteModelRef, BaseType, DDict, DList, DOptional, IntString, IsoDateString, ModelMeta, ModelPtr, StringLiteral, StringSerializable, Unknown, compile_imports) |
| 6 | +from json_to_models.dynamic_typing import ( |
| 7 | + AbsoluteModelRef, |
| 8 | + BaseType, |
| 9 | + DDict, |
| 10 | + DList, |
| 11 | + DOptional, |
| 12 | + IntString, |
| 13 | + IsoDateString, |
| 14 | + ModelMeta, |
| 15 | + ModelPtr, |
| 16 | + StringLiteral, |
| 17 | + StringSerializable, |
| 18 | + Unknown, |
| 19 | + compile_imports, |
| 20 | +) |
7 | 21 | from json_to_models.models.base import GenericModelCodeGenerator, generate_code
|
8 | 22 | from json_to_models.models.structure import sort_fields
|
9 | 23 | from json_to_models.models.utils import indent
|
@@ -146,6 +160,39 @@ class Test:
|
146 | 160 | d: Dict[str, Any]
|
147 | 161 | baz: Optional[List[List[str]]]
|
148 | 162 | """)
|
| 163 | + }, |
| 164 | + "literals": { |
| 165 | + "model": ("Test", { |
| 166 | + "a": StringLiteral({'basic'}), |
| 167 | + "b": StringLiteral({'with space'}), |
| 168 | + "c": StringLiteral({'with\ttab'}), |
| 169 | + "d": StringLiteral({'with\nnew_line'}), |
| 170 | + "e": StringLiteral({'with \'"qoutes"\''}), |
| 171 | + "f": StringLiteral({'with \\ // slash'}), |
| 172 | + }), |
| 173 | + "fields": { |
| 174 | + "imports": f"{LITERAL_SOURCE} import Literal", |
| 175 | + "fields": [ |
| 176 | + 'a: Literal["basic"]', |
| 177 | + 'b: Literal["with space"]', |
| 178 | + 'c: Literal["with\\ttab"]', |
| 179 | + 'd: Literal["with\\nnew_line"]', |
| 180 | + 'e: Literal["with \'\\"qoutes\\"\'"]', |
| 181 | + 'f: Literal["with \\\\ // slash"]' |
| 182 | + ] |
| 183 | + }, |
| 184 | + "generated": trim(f""" |
| 185 | + {LITERAL_SOURCE} import Literal |
| 186 | +
|
| 187 | +
|
| 188 | + class Test: |
| 189 | + a: Literal["basic"] |
| 190 | + b: Literal["with space"] |
| 191 | + c: Literal["with\\ttab"] |
| 192 | + d: Literal["with\\nnew_line"] |
| 193 | + e: Literal["with \'\\"qoutes\\"\'"] |
| 194 | + f: Literal["with \\\\ // slash"] |
| 195 | + """) |
149 | 196 | }
|
150 | 197 | }
|
151 | 198 |
|
|
0 commit comments