/
test_custom_get_name.py
78 lines (68 loc) · 2.18 KB
/
test_custom_get_name.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
from typing import Callable, Optional, Union
from jsonschema_gentypes import jsonschema_draft_04, jsonschema_draft_2019_09_meta_data, normalize
from jsonschema_gentypes.cli import process_config
from jsonschema_gentypes.configuration import Configuration
GetNameFunction = Callable[
[
Optional[
Union[
jsonschema_draft_04.JSONSchemaD4,
jsonschema_draft_2019_09_meta_data.JSONSchemaItemD2019,
]
],
Optional[str],
bool,
],
str,
]
def custom_get_name(
schema: Optional[
Union[
jsonschema_draft_04.JSONSchemaD4,
jsonschema_draft_2019_09_meta_data.JSONSchemaItemD2019,
]
],
proposed_name: Optional[str] = None,
upper: bool = False,
) -> str:
"""
Custom get the name for an element.
Just capitalize first Letter, don't do `.title`
Parameter:
schema: the concerned schema
proposed_name: a name that we will use it the schema hasn't any title
upper: should we use an upper case (For constants)
"""
# Get the base name
has_title = isinstance(schema, dict) and "title" in schema
name = schema["title"] if has_title else proposed_name # type: ignore
assert name is not None
name = normalize(name)
prefix = "" if has_title else "_"
if upper:
# Upper case
name = name.upper()
# Remove spaces
return prefix + "".join(["_" if char.isspace() else char for char in name])
else:
# Title case
name = name[0].upper() + name[1:]
# Remove spaces
return prefix + "".join([char for char in name if not char.isspace()])
def test_empty_array() -> None:
config: Configuration = Configuration(
generate=[
{
"source": "tests/custom_get_name.json",
"destination": "tests/custom_get_name.py",
"api_arguments": {"custom_get_name": custom_get_name},
}
],
)
process_config(
config,
["tests/custom_get_name.json"],
)
with open("tests/custom_get_name.py") as f:
content = f.read()
assert "class SubresourceUris" in content