Files
AI/참고/guardrails-main/tests/unit_tests/utils/test_castring_utils.py
2026-05-12 19:40:31 +09:00

63 lines
1.4 KiB
Python

import pytest
from guardrails.utils.casting_utils import to_float, to_int, to_string
# NOTE: These tests are _currently_ (August 31, 2023) descriptive not prescriptive.
# We can change the functionality of these methods to make them more flexible
# if desired, but we should _NOT_ make them more strict than what
# basic casting allows.
@pytest.mark.parametrize(
"input,expected_output",
[
(1, 1),
(1.1, 1),
("1", 1),
("1.1", None),
("abc", None),
("None", None),
(None, None),
],
)
def test_to_int(input, expected_output):
actual_output = to_int(input)
assert actual_output == expected_output
@pytest.mark.parametrize(
"input,expected_output",
[
(1, 1.0),
(1.1, 1.1),
("1", 1.0),
("1.1", 1.1),
("abc", None),
("None", None),
(None, None),
],
)
def test_to_float(input, expected_output):
actual_output = to_float(input)
assert actual_output == expected_output
@pytest.mark.parametrize(
"input,expected_output",
[
("abc", "abc"),
(1, "1"),
(1.1, "1.1"),
(True, "True"),
(["a", "b", "c"], "['a', 'b', 'c']"),
({"a": 1}, "{'a': 1}"),
(None, "None"),
],
)
def test_to_string(input, expected_output):
actual_output = to_string(input)
assert actual_output == expected_output