-
-
Notifications
You must be signed in to change notification settings - Fork 227
/
Copy pathtest_enum_property.py
109 lines (80 loc) · 3.48 KB
/
test_enum_property.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
from typing import Union
import pytest
import openapi_python_client.schema as oai
from openapi_python_client import Config
from openapi_python_client.parser.errors import PropertyError
from openapi_python_client.parser.properties import LiteralEnumProperty, Schemas
from openapi_python_client.parser.properties.enum_property import EnumProperty
PropertyClass = Union[type[EnumProperty], type[LiteralEnumProperty]]
@pytest.fixture(params=[EnumProperty, LiteralEnumProperty])
def property_class(request) -> PropertyClass:
return request.param
def test_conflict(config: Config, property_class: PropertyClass) -> None:
schemas = Schemas()
_, schemas = property_class.build(
data=oai.Schema(enum=["a"]), name="Existing", required=True, schemas=schemas, parent_name="", config=config
)
err, new_schemas = property_class.build(
data=oai.Schema(enum=["a", "b"]),
name="Existing",
required=True,
schemas=schemas,
parent_name="",
config=config,
)
assert schemas == new_schemas
assert err.detail == "Found conflicting enums named Existing with incompatible values."
def test_avoids_false_conflict(config: Config, property_class: PropertyClass) -> None:
schemas = Schemas()
_, schemas = property_class.build(
data=oai.Schema(enum=["a"]), name="Friend", required=True, schemas=schemas, parent_name="", config=config
)
_, schemas = property_class.build(
data=oai.Schema(enum=["a", "b"]),
name="FriendShips",
required=True,
schemas=schemas,
parent_name="",
config=config,
)
prop, new_schemas = property_class.build(
data=oai.Schema(enum=["c"]),
name="ships",
required=True,
schemas=schemas,
parent_name="Friend",
config=config,
)
assert sorted([n for n in schemas.classes_by_name]) == ["Friend", "FriendShips"]
assert sorted([n for n in new_schemas.classes_by_name]) == ["Friend", "FriendShips", "FriendShipsPropertyEnum1"]
assert prop.name == "ships"
def test_bad_default_value(config: Config, property_class: PropertyClass) -> None:
data = oai.Schema(default="B", enum=["A"])
schemas = Schemas()
err, new_schemas = property_class.build(
data=data, name="Existing", required=True, schemas=schemas, parent_name="parent", config=config
)
assert schemas == new_schemas
assert err == PropertyError(detail="Value B is not valid for enum Existing", data=data)
def test_bad_default_type(config: Config, property_class: PropertyClass) -> None:
data = oai.Schema(default=123, enum=["A"])
schemas = Schemas()
err, new_schemas = property_class.build(
data=data, name="Existing", required=True, schemas=schemas, parent_name="parent", config=config
)
assert schemas == new_schemas
assert isinstance(err, PropertyError)
def test_mixed_types(config: Config, property_class: PropertyClass) -> None:
data = oai.Schema(enum=["A", 1])
schemas = Schemas()
err, _ = property_class.build(
data=data, name="Enum", required=True, schemas=schemas, parent_name="parent", config=config
)
assert isinstance(err, PropertyError)
def test_unsupported_type(config: Config, property_class: PropertyClass) -> None:
data = oai.Schema(enum=[1.4, 1.5])
schemas = Schemas()
err, _ = property_class.build(
data=data, name="Enum", required=True, schemas=schemas, parent_name="parent", config=config
)
assert isinstance(err, PropertyError)