From d15f481352557657b6009d6b93881285c3bd1ac5 Mon Sep 17 00:00:00 2001 From: Roma Date: Mon, 1 May 2023 18:53:08 -0300 Subject: [PATCH] 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. --- .../output_parsers/test_boolean_parser.py | 20 +++++++++++++++ .../output_parsers/test_list_parser.py | 13 ++++++++++ .../output_parsers/test_structured_parser.py | 25 +++++++++++++++++++ 3 files changed, 58 insertions(+) create mode 100644 tests/unit_tests/output_parsers/test_boolean_parser.py create mode 100644 tests/unit_tests/output_parsers/test_list_parser.py create mode 100644 tests/unit_tests/output_parsers/test_structured_parser.py diff --git a/tests/unit_tests/output_parsers/test_boolean_parser.py b/tests/unit_tests/output_parsers/test_boolean_parser.py new file mode 100644 index 00000000..3daaf610 --- /dev/null +++ b/tests/unit_tests/output_parsers/test_boolean_parser.py @@ -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 diff --git a/tests/unit_tests/output_parsers/test_list_parser.py b/tests/unit_tests/output_parsers/test_list_parser.py new file mode 100644 index 00000000..84be4db9 --- /dev/null +++ b/tests/unit_tests/output_parsers/test_list_parser.py @@ -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"] diff --git a/tests/unit_tests/output_parsers/test_structured_parser.py b/tests/unit_tests/output_parsers/test_structured_parser.py new file mode 100644 index 00000000..abf19a7c --- /dev/null +++ b/tests/unit_tests/output_parsers/test_structured_parser.py @@ -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)}"