core[patch]: Add unit tests for Serializable (#25152)

Add a few test cases for serializable (many other test cases already
covered
throguh runnable tests).
This commit is contained in:
Eugene Yurtsev 2024-08-07 17:01:36 -04:00 committed by GitHub
parent df99b832a7
commit 7b1a132aff
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -0,0 +1,71 @@
from typing import Dict
from langchain_core.load import Serializable, dumpd
def test_simple_serialization() -> None:
class Foo(Serializable):
bar: int
baz: str
foo = Foo(bar=1, baz="hello")
assert dumpd(foo) == {
"id": ["tests", "unit_tests", "load", "test_serializable", "Foo"],
"lc": 1,
"repr": "Foo(bar=1, baz='hello')",
"type": "not_implemented",
}
def test_simple_serialization_is_serializable() -> None:
class Foo(Serializable):
bar: int
baz: str
@classmethod
def is_lc_serializable(cls) -> bool:
return True
foo = Foo(bar=1, baz="hello")
assert foo.lc_id() == ["tests", "unit_tests", "load", "test_serializable", "Foo"]
assert dumpd(foo) == {
"id": ["tests", "unit_tests", "load", "test_serializable", "Foo"],
"kwargs": {"bar": 1, "baz": "hello"},
"lc": 1,
"type": "constructor",
}
def test_simple_serialization_secret() -> None:
"""Test handling of secrets."""
from langchain_core.load import Serializable
from langchain_core.pydantic_v1 import SecretStr
class Foo(Serializable):
bar: int
baz: str
secret: SecretStr
secret_2: str
@classmethod
def is_lc_serializable(cls) -> bool:
return True
@property
def lc_secrets(self) -> Dict[str, str]:
return {"secret": "MASKED_SECRET", "secret_2": "MASKED_SECRET_2"}
foo = Foo(
bar=1, baz="baz", secret=SecretStr("SUPER_SECRET"), secret_2="SUPER_SECRET"
)
assert dumpd(foo) == {
"id": ["tests", "unit_tests", "load", "test_serializable", "Foo"],
"kwargs": {
"bar": 1,
"baz": "baz",
"secret": {"id": ["MASKED_SECRET"], "lc": 1, "type": "secret"},
"secret_2": {"id": ["MASKED_SECRET_2"], "lc": 1, "type": "secret"},
},
"lc": 1,
"type": "constructor",
}