Skip to content

Commit 7ae7b30

Browse files
committed
Provide test for adding enum with extra kwargs specified
1 parent 712bed7 commit 7ae7b30

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

tests/test_utils/test_schema_builder.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,3 +199,66 @@ def test_add_enum_with_extra_permissible_values(
199199
else:
200200
builder.add_enum(enum_def, permissible_values=permissible_values)
201201
assert builder.schema.enums[enum_def.name] == expected_added_enum
202+
203+
204+
# === Tests for `SchemaBuilder.add_enum` ===
205+
@pytest.mark.parametrize(
206+
("enum_def", "extra_kwargs", "expected_added_enum"),
207+
[
208+
("Color", {}, EnumDefinition(name="Color")),
209+
(42, {}, None), # Invalid type for `enum_def`
210+
(
211+
"Color",
212+
{"description": "What meets the eyes"},
213+
EnumDefinition(name="Color", description="What meets the eyes"),
214+
),
215+
(
216+
{"name": "Color", "description": "It's obvious"},
217+
{"description": "What meets the eyes"},
218+
EnumDefinition(name="Color", description="What meets the eyes"),
219+
),
220+
(
221+
EnumDefinition("Color"),
222+
{"description": "What meets the eyes"},
223+
EnumDefinition(name="Color"),
224+
),
225+
(
226+
"Color",
227+
{"description": "What meets the eyes", "ijk": True}, # Invalid extra kwarg
228+
None,
229+
),
230+
],
231+
)
232+
def test_add_enum_with_extra_kwargs(
233+
enum_def: Union[EnumDefinition, dict, str],
234+
extra_kwargs: Dict[str, Any],
235+
expected_added_enum: Optional[EnumDefinition],
236+
):
237+
"""
238+
Test adding an enum with extra kwargs
239+
"""
240+
enum_meta_slots = {f.name for f in fields(EnumDefinition)}
241+
242+
builder = SchemaBuilder()
243+
244+
if not isinstance(enum_def, (str, dict, EnumDefinition)):
245+
with pytest.raises(TypeError, match="enum_def must be"):
246+
builder.add_enum(enum_def, **extra_kwargs)
247+
elif extra_kwargs.keys() - enum_meta_slots:
248+
# Handle the case of extra kwargs include a key that is not a meta slot of
249+
# `EnumDefinition`
250+
with pytest.raises(ValueError):
251+
builder.add_enum(enum_def, **extra_kwargs)
252+
else:
253+
builder.add_enum(enum_def, **extra_kwargs)
254+
255+
if isinstance(enum_def, str):
256+
enum_name = enum_def
257+
elif isinstance(enum_def, dict):
258+
enum_name = enum_def["name"]
259+
else:
260+
enum_name = enum_def.name
261+
262+
added_enum = builder.schema.enums[enum_name]
263+
264+
assert added_enum == expected_added_enum

0 commit comments

Comments
 (0)