forked from openapi-generators/openapi-python-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_utils.py
121 lines (90 loc) · 3.61 KB
/
test_utils.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
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
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"
@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"]),
],
)
def test_split_words(before, after):
assert utils.split_words(before) == after
@pytest.mark.parametrize(
"reserved_word, expected",
[
("self", "self_"),
("int", "int_"),
("dict", "dict_"),
("not_reserved", "not_reserved"),
("type", "type"),
("id", "id"),
("None", "none"),
],
)
def test_snake_case_fix_reserved_words(reserved_word: str, expected: str):
assert utils.snake_case(reserved_word) == expected
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("something*~with lots_- of weird things}=") == "somethingwith 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