core[patch]: utils for adding/subtracting usage metadata (#27203)

This commit is contained in:
Bagatur
2024-10-08 13:15:33 -07:00
committed by GitHub
parent e3920f2320
commit e3e9ee8398
4 changed files with 296 additions and 11 deletions

View 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)