Add unit test to output parsers (#3911)

This pull request adds unit tests for various output parsers
(BooleanOutputParser, CommaSeparatedListOutputParser, and
StructuredOutputParser) to ensure their correct functionality and to
increase code reliability and maintainability. The tests cover both
valid and invalid input cases.

Changes:

Added unit tests for BooleanOutputParser.
Added unit tests for CommaSeparatedListOutputParser.
Added unit tests for StructuredOutputParser.

Testing:

All new unit tests have been executed, and they pass successfully.
The overall test suite has been run, and all tests pass.
Notes:

These tests cover both successful parsing scenarios and error handling
for invalid inputs.
If any new output parsers are added in the future, corresponding unit
tests should also be created to maintain coverage.
fix_agent_callbacks
Roma 1 year ago committed by GitHub
parent 9c89ff8bd9
commit d15f481352
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,20 @@
from langchain.output_parsers.boolean import BooleanOutputParser
def test_boolean_output_parser_parse() -> None:
parser = BooleanOutputParser()
# Test valid input
result = parser.parse("YES")
assert result is True
# Test valid input
result = parser.parse("NO")
assert result is False
# Test invalid input
try:
parser.parse("INVALID")
assert False, "Should have raised ValueError"
except ValueError:
pass

@ -0,0 +1,13 @@
from langchain.output_parsers.list import CommaSeparatedListOutputParser
def test_single_item() -> None:
"""Test that a string with a single item is parsed to a list with that item."""
parser = CommaSeparatedListOutputParser()
assert parser.parse("foo") == ["foo"]
def test_multiple_items() -> None:
"""Test that a string with multiple comma-separated items is parsed to a list."""
parser = CommaSeparatedListOutputParser()
assert parser.parse("foo, bar, baz") == ["foo", "bar", "baz"]

@ -0,0 +1,25 @@
from langchain.output_parsers import ResponseSchema, StructuredOutputParser
from langchain.schema import OutputParserException
def test_parse() -> None:
response_schemas = [
ResponseSchema(name="name", description="desc"),
ResponseSchema(name="age", description="desc"),
]
parser = StructuredOutputParser.from_response_schemas(response_schemas)
# Test valid JSON input
text = '```json\n{"name": "John", "age": 30}\n```'
expected_result = {"name": "John", "age": 30}
result = parser.parse(text)
assert result == expected_result, f"Expected {expected_result}, but got {result}"
# Test invalid JSON input
text = '```json\n{"name": "John"}\n```'
try:
parser.parse(text)
except OutputParserException:
pass # Test passes if OutputParserException is raised
else:
assert False, f"Expected OutputParserException, but got {parser.parse(text)}"
Loading…
Cancel
Save