mirror of
				https://github.com/hwchase17/langchain.git
				synced 2025-11-04 10:10:09 +00:00 
			
		
		
		
	langchain: Use pytest.raises and pytest.fail to handle exceptions in tests (#31911)
Co-authored-by: Mason Daugherty <mason@langchain.dev>
This commit is contained in:
		
				
					committed by
					
						
						GitHub
					
				
			
			
				
	
			
			
			
						parent
						
							2c7eafffec
						
					
				
				
					commit
					a3e3fd20f2
				
			@@ -305,7 +305,7 @@ def test_agent_iterator_output_structure() -> None:
 | 
			
		||||
        elif "output" in step:
 | 
			
		||||
            assert isinstance(step["output"], str)
 | 
			
		||||
        else:
 | 
			
		||||
            assert False, "Unexpected output structure"
 | 
			
		||||
            pytest.fail("Unexpected output structure")
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
async def test_agent_async_iterator_output_structure() -> None:
 | 
			
		||||
@@ -321,7 +321,7 @@ async def test_agent_async_iterator_output_structure() -> None:
 | 
			
		||||
        elif "output" in step:
 | 
			
		||||
            assert isinstance(step["output"], str)
 | 
			
		||||
        else:
 | 
			
		||||
            assert False, "Unexpected output structure"
 | 
			
		||||
            pytest.fail("Unexpected output structure")
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_agent_iterator_empty_input() -> None:
 | 
			
		||||
 
 | 
			
		||||
@@ -1,5 +1,7 @@
 | 
			
		||||
from datetime import datetime
 | 
			
		||||
from time import sleep
 | 
			
		||||
 | 
			
		||||
import pytest
 | 
			
		||||
from langchain_core.exceptions import OutputParserException
 | 
			
		||||
 | 
			
		||||
from langchain.output_parsers.datetime import DatetimeOutputParser
 | 
			
		||||
 | 
			
		||||
@@ -39,11 +41,5 @@ def test_datetime_output_parser_parse() -> None:
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
    # Test invalid input
 | 
			
		||||
    try:
 | 
			
		||||
        sleep(0.001)
 | 
			
		||||
        datestr = date.strftime(parser.format)
 | 
			
		||||
        result = parser.parse(datestr)
 | 
			
		||||
        assert result == date
 | 
			
		||||
        assert False, "Should have raised AssertionError"
 | 
			
		||||
    except AssertionError:
 | 
			
		||||
        pass
 | 
			
		||||
    with pytest.raises(OutputParserException):
 | 
			
		||||
        parser.parse("Invalid date string")
 | 
			
		||||
 
 | 
			
		||||
@@ -1,5 +1,6 @@
 | 
			
		||||
from enum import Enum
 | 
			
		||||
 | 
			
		||||
import pytest
 | 
			
		||||
from langchain_core.exceptions import OutputParserException
 | 
			
		||||
 | 
			
		||||
from langchain.output_parsers.enum import EnumOutputParser
 | 
			
		||||
@@ -25,11 +26,8 @@ def test_enum_output_parser_parse() -> None:
 | 
			
		||||
    assert result == Colors.BLUE
 | 
			
		||||
 | 
			
		||||
    # Test invalid input
 | 
			
		||||
    try:
 | 
			
		||||
    with pytest.raises(OutputParserException):
 | 
			
		||||
        parser.parse("INVALID")
 | 
			
		||||
        assert False, "Should have raised OutputParserException"
 | 
			
		||||
    except OutputParserException:
 | 
			
		||||
        pass
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_enum_output_parser_output_type() -> None:
 | 
			
		||||
 
 | 
			
		||||
@@ -3,6 +3,7 @@
 | 
			
		||||
from typing import Any
 | 
			
		||||
 | 
			
		||||
import pandas as pd
 | 
			
		||||
import pytest
 | 
			
		||||
from langchain_core.exceptions import OutputParserException
 | 
			
		||||
 | 
			
		||||
from langchain.output_parsers.pandas_dataframe import PandasDataFrameOutputParser
 | 
			
		||||
@@ -20,20 +21,14 @@ parser = PandasDataFrameOutputParser(dataframe=df)
 | 
			
		||||
 | 
			
		||||
# Test Invalid Column
 | 
			
		||||
def test_pandas_output_parser_col_no_array() -> None:
 | 
			
		||||
    try:
 | 
			
		||||
    with pytest.raises(OutputParserException):
 | 
			
		||||
        parser.parse("column:num_legs")
 | 
			
		||||
        assert False, "Should have raised OutputParserException"
 | 
			
		||||
    except OutputParserException:
 | 
			
		||||
        assert True
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
# Test Column with invalid array (above DataFrame max index)
 | 
			
		||||
def test_pandas_output_parser_col_oob() -> None:
 | 
			
		||||
    try:
 | 
			
		||||
    with pytest.raises(OutputParserException):
 | 
			
		||||
        parser.parse("row:10")
 | 
			
		||||
        assert False, "Should have raised OutputParserException"
 | 
			
		||||
    except OutputParserException:
 | 
			
		||||
        assert True
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
# Test Column with array [x]
 | 
			
		||||
@@ -53,11 +48,8 @@ def test_pandas_output_parser_col_multi_elem() -> None:
 | 
			
		||||
 | 
			
		||||
# Test Row with invalid row entry
 | 
			
		||||
def test_pandas_output_parser_row_no_array() -> None:
 | 
			
		||||
    try:
 | 
			
		||||
    with pytest.raises(OutputParserException):
 | 
			
		||||
        parser.parse("row:5")
 | 
			
		||||
        assert False, "Should have raised OutputParserException"
 | 
			
		||||
    except OutputParserException:
 | 
			
		||||
        assert True
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
# Test Row with valid row entry
 | 
			
		||||
@@ -69,11 +61,8 @@ def test_pandas_output_parser_row_first() -> None:
 | 
			
		||||
 | 
			
		||||
# Test Row with invalid col entry
 | 
			
		||||
def test_pandas_output_parser_row_no_column() -> None:
 | 
			
		||||
    try:
 | 
			
		||||
    with pytest.raises(OutputParserException):
 | 
			
		||||
        parser.parse("row:1[num_legs]")
 | 
			
		||||
        assert False, "Should have raised OutputParserException"
 | 
			
		||||
    except OutputParserException:
 | 
			
		||||
        assert True
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
# Test Row with valid col entry
 | 
			
		||||
@@ -110,11 +99,8 @@ def test_pandas_output_parser_special_ops() -> None:
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_pandas_output_parser_invalid_special_op() -> None:
 | 
			
		||||
    try:
 | 
			
		||||
    with pytest.raises(OutputParserException):
 | 
			
		||||
        parser.parse("riemann_sum:chicken")
 | 
			
		||||
        assert False, "Should have raised OutputParserException"
 | 
			
		||||
    except OutputParserException:
 | 
			
		||||
        assert True
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_pandas_output_parser_output_type() -> None:
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user