forked from openapi-generators/openapi-python-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_utils.py
More file actions
136 lines (99 loc) · 4.4 KB
/
test_utils.py
File metadata and controls
136 lines (99 loc) · 4.4 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import pytest
from openapi_python_client import utils
class TestPythonIdentifier:
def test_valid_identifier_is_not_changed(self):
assert utils.PythonIdentifier(value="valid_field", prefix="field") == "valid_field"
def test_numbers_are_prefixed(self):
assert utils.PythonIdentifier(value="1", prefix="field") == "field1"
def test_invalid_symbols_are_stripped(self):
assert utils.PythonIdentifier(value="$abc", prefix="prefix") == "abc"
def test_keywords_are_postfixed(self):
assert utils.PythonIdentifier(value="for", prefix="prefix") == "for_"
def test_empty_is_prefixed(self):
assert utils.PythonIdentifier(value="", prefix="something") == "something"
class TestClassName:
def test_valid_is_not_changed(self):
assert utils.ClassName(value="ValidClass", prefix="field") == "ValidClass"
def test_numbers_are_prefixed(self):
assert utils.ClassName(value="1", prefix="field") == "Field1"
def test_invalid_symbols_are_stripped(self):
assert utils.ClassName(value="$abc", prefix="prefix") == "Abc"
def test_keywords_are_postfixed(self):
assert utils.ClassName(value="none", prefix="prefix") == "None_"
def test_empty_is_prefixed(self):
assert utils.ClassName(value="", prefix="something") == "Something"
@pytest.mark.parametrize(
"before, after",
[
("connectionID", ["connection", "ID"]),
("connection_id", ["connection", "id"]),
("connection-id", ["connection", "id"]),
("Response200", ["Response", "200"]),
("Response200Okay", ["Response", "200", "Okay"]),
("S3Config", ["S3", "Config"]),
("s3config", ["s3config"]),
("fully.qualified.Name", ["fully", "qualified", "Name"]),
],
)
def test_split_words(before, after):
assert utils.split_words(before) == after
def test_snake_case_uppercase_str():
assert utils.snake_case("HTTP") == "http"
assert utils.snake_case("HTTP RESPONSE") == "http_response"
def test_snake_case_from_pascal_with_acronyms():
assert utils.snake_case("HTTPResponse") == "http_response"
assert utils.snake_case("APIClientHTTPResponse") == "api_client_http_response"
assert utils.snake_case("OAuthClientHTTPResponse") == "o_auth_client_http_response"
assert utils.snake_case("S3Config") == "s3_config"
def test_snake_case_from_pascal_with_numbers():
assert utils.snake_case("Response200") == "response_200"
assert utils.snake_case("Response200WithContent") == "response_200_with_content"
def test_snake_case_from_pascal():
assert utils.snake_case("HttpResponsePascalCase") == "http_response_pascal_case"
def test_snake_case_from_camel():
assert utils.snake_case("httpResponseLowerCamel") == "http_response_lower_camel"
assert utils.snake_case("connectionID") == "connection_id"
def test_kebab_case():
assert utils.kebab_case("keep_alive") == "keep-alive"
def test_sanitize():
assert utils.sanitize("some.thing*~with lots_- of weird things}=") == "some.thingwith lots_- of weird things"
def test_no_string_escapes():
assert utils.remove_string_escapes('an "evil" string') == 'an \\"evil\\" string'
@pytest.mark.parametrize(
"reserved_word, expected",
[
("self", "self_"),
("int", "int_"),
("dict", "dict_"),
("not_reserved", "not_reserved"),
("type", "type"),
("id", "id"),
("None", "None_"),
],
)
def test__fix_reserved_words(reserved_word: str, expected: str):
assert utils.fix_reserved_words(reserved_word) == expected
@pytest.mark.parametrize(
"before, after",
[
("PascalCase", "PascalCase"),
("snake_case", "SnakeCase"),
("TLAClass", "TLAClass"),
("Title Case", "TitleCase"),
("s3_config", "S3Config"),
("__LeadingUnderscore", "LeadingUnderscore"),
],
)
def test_pascalcase(before, after):
assert utils.pascal_case(before) == after
@pytest.mark.parametrize(
"content_type, expected",
[
pytest.param("application/json", "application/json"),
pytest.param("application/vnd.api+json", "application/vnd.api+json"),
pytest.param("application/json;charset=utf-8", "application/json"),
pytest.param("application/vnd.api+json;charset=utf-8", "application/vnd.api+json"),
],
)
def test_get_content_type(content_type: str, expected: str, config) -> None:
assert utils.get_content_type(content_type, config) == expected