import pytest
from guardrails.schema.rail_schema import rail_string_to_schema
from guardrails.schema.validator import SchemaValidationError, validate_payload
from guardrails.utils.parsing_utils import coerce_types
# TODO: Make this an integration test instead.
@pytest.mark.parametrize(
"rail, payload, should_pass, should_coerce_types",
[
(
"""
""",
{
"my_list": [{"my_string": "string"}],
"my_integer": 1,
"my_string": "string",
"my_dict": {"my_string": "string"},
"my_dict2": {
"my_list": [
1.0,
2.0,
]
},
"my_list2": [],
},
True,
False,
),
(
"""
""",
{
"my_list": [{"my_string": "string"}],
"my_integer": 1,
"my_string": "string",
"my_dict": {"my_string": "string"},
"my_list2": [],
},
False,
False,
),
(
"""
""",
{
"action": {
"action_type": "fight",
"fight_move": "punch",
}
},
True,
False,
),
(
"""
""",
{
"my_list3": [
{
"action_type": "fight",
"fight": ["punch", "kick"],
},
{
"action_type": "flight",
"flight_direction": "north",
"flight_speed": 1,
},
],
},
True,
False,
),
(
"""
""",
{
"mychoices": {
"some random thing": "string",
"action": {
"action_type": "fight",
"fight_move": "punch",
},
},
},
True,
False,
),
(
"""
""",
{},
True,
False,
),
(
"""
""",
{},
True,
False,
),
(
"""
""",
{
"my_string": "e",
},
True,
True,
),
(
"""
""",
{
"my_string": ["a"],
},
False,
True,
),
(
"""
""",
{
"my_string": {"a": "a"},
},
False,
True,
),
],
)
def test_skeleton(rail, payload, should_pass, should_coerce_types):
payload = payload
processed_schema = rail_string_to_schema(rail)
json_schema = processed_schema.json_schema
if should_coerce_types:
payload = coerce_types(payload, json_schema)
if not should_pass:
with pytest.raises(SchemaValidationError):
validate_payload(payload, processed_schema.json_schema)
else:
validate_payload(payload, json_schema)