Skip to content

Commit 6849b11

Browse files
fix: updated tests for optionally keyed by
1 parent ab07a7e commit 6849b11

File tree

1 file changed

+47
-18
lines changed

1 file changed

+47
-18
lines changed

test/test_util_schema.py

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -253,29 +253,58 @@ def test_no_key(self):
253253

254254

255255
def test_optionally_keyed_by():
256-
validator = optionally_keyed_by("foo", str)
257-
assert validator("baz") == "baz"
258-
assert validator({"by-foo": {"a": "b", "c": "d"}}) == {"a": "b", "c": "d"}
256+
# optionally_keyed_by now returns a type annotation for msgspec
257+
type_annotation = optionally_keyed_by("foo", str)
259258

260-
with pytest.raises((TypeError, ValueError)):
261-
validator({"by-foo": {"a": 1, "c": "d"}})
259+
# Create a struct with this type annotation to test validation
260+
class TestSchema(Schema):
261+
value: type_annotation
262262

263-
with pytest.raises(ValueError):
264-
validator({"by-bar": {"a": "b"}})
263+
# Test that a simple string is accepted
264+
result = msgspec.convert({"value": "baz"}, TestSchema)
265+
assert result.value == "baz"
266+
267+
# Test that keyed-by structure is accepted and works
268+
result = msgspec.convert({"value": {"by-foo": {"a": "b", "c": "d"}}}, TestSchema)
269+
assert result.value == {"by-foo": {"a": "b", "c": "d"}}
270+
271+
# Test that invalid value types are rejected
272+
with pytest.raises(msgspec.ValidationError):
273+
msgspec.convert({"value": {"by-foo": {"a": 1, "c": "d"}}}, TestSchema)
274+
275+
# Test that unknown by-keys are rejected due to Literal constraint
276+
with pytest.raises(msgspec.ValidationError):
277+
msgspec.convert({"value": {"by-bar": {"a": "b"}}}, TestSchema)
265278

266279

267280
def test_optionally_keyed_by_mulitple_keys():
268-
validator = optionally_keyed_by("foo", "bar", str)
269-
assert validator("baz") == "baz"
270-
assert validator({"by-foo": {"a": "b", "c": "d"}}) == {"a": "b", "c": "d"}
271-
assert validator({"by-bar": {"x": "y"}}) == {"x": "y"}
272-
assert validator({"by-foo": {"a": {"by-bar": {"x": "y"}}}}) == {"a": {"x": "y"}}
281+
# optionally_keyed_by now returns a type annotation for msgspec
282+
type_annotation = optionally_keyed_by("foo", "bar", str)
283+
284+
# Create a struct with this type annotation to test validation
285+
class TestSchema(Schema):
286+
value: type_annotation
287+
288+
# Test that a simple string is accepted
289+
result = msgspec.convert({"value": "baz"}, TestSchema)
290+
assert result.value == "baz"
291+
292+
# Test that keyed-by with "foo" is accepted
293+
result = msgspec.convert({"value": {"by-foo": {"a": "b", "c": "d"}}}, TestSchema)
294+
assert result.value == {"by-foo": {"a": "b", "c": "d"}}
295+
296+
# Test that keyed-by with "bar" is accepted
297+
result = msgspec.convert({"value": {"by-bar": {"x": "y"}}}, TestSchema)
298+
assert result.value == {"by-bar": {"x": "y"}}
273299

274-
with pytest.raises((TypeError, ValueError)):
275-
validator({"by-foo": {"a": 123, "c": "d"}})
300+
# Test that invalid value types in by-foo are rejected
301+
with pytest.raises(msgspec.ValidationError):
302+
msgspec.convert({"value": {"by-foo": {"a": 123, "c": "d"}}}, TestSchema)
276303

277-
with pytest.raises((TypeError, ValueError)):
278-
validator({"by-bar": {"a": 1}})
304+
# Test that invalid value types in by-bar are rejected
305+
with pytest.raises(msgspec.ValidationError):
306+
msgspec.convert({"value": {"by-bar": {"a": 1}}}, TestSchema)
279307

280-
with pytest.raises(ValueError):
281-
validator({"by-unknown": {"a": "b"}})
308+
# Test that unknown by-keys are rejected due to Literal constraint
309+
with pytest.raises(msgspec.ValidationError):
310+
msgspec.convert({"value": {"by-unknown": {"a": "b"}}}, TestSchema)

0 commit comments

Comments
 (0)