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