mirror of
https://github.com/hwchase17/langchain.git
synced 2025-09-15 22:44:36 +00:00
core[patch]: utils for adding/subtracting usage metadata (#27203)
This commit is contained in:
38
libs/core/tests/unit_tests/utils/test_usage.py
Normal file
38
libs/core/tests/unit_tests/utils/test_usage.py
Normal file
@@ -0,0 +1,38 @@
|
||||
import pytest
|
||||
|
||||
from langchain_core.utils.usage import _dict_int_op
|
||||
|
||||
|
||||
def test_dict_int_op_add() -> None:
|
||||
left = {"a": 1, "b": 2}
|
||||
right = {"b": 3, "c": 4}
|
||||
result = _dict_int_op(left, right, lambda x, y: x + y)
|
||||
assert result == {"a": 1, "b": 5, "c": 4}
|
||||
|
||||
|
||||
def test_dict_int_op_subtract() -> None:
|
||||
left = {"a": 5, "b": 10}
|
||||
right = {"a": 2, "b": 3, "c": 1}
|
||||
result = _dict_int_op(left, right, lambda x, y: max(x - y, 0))
|
||||
assert result == {"a": 3, "b": 7, "c": 0}
|
||||
|
||||
|
||||
def test_dict_int_op_nested() -> None:
|
||||
left = {"a": 1, "b": {"c": 2, "d": 3}}
|
||||
right = {"a": 2, "b": {"c": 1, "e": 4}}
|
||||
result = _dict_int_op(left, right, lambda x, y: x + y)
|
||||
assert result == {"a": 3, "b": {"c": 3, "d": 3, "e": 4}}
|
||||
|
||||
|
||||
def test_dict_int_op_max_depth_exceeded() -> None:
|
||||
left = {"a": {"b": {"c": 1}}}
|
||||
right = {"a": {"b": {"c": 2}}}
|
||||
with pytest.raises(ValueError):
|
||||
_dict_int_op(left, right, lambda x, y: x + y, max_depth=2)
|
||||
|
||||
|
||||
def test_dict_int_op_invalid_types() -> None:
|
||||
left = {"a": 1, "b": "string"}
|
||||
right = {"a": 2, "b": 3}
|
||||
with pytest.raises(ValueError):
|
||||
_dict_int_op(left, right, lambda x, y: x + y)
|
Reference in New Issue
Block a user