From 7b1a132affb609a617d208ef57f8d431346c516a Mon Sep 17 00:00:00 2001 From: Eugene Yurtsev Date: Wed, 7 Aug 2024 17:01:36 -0400 Subject: [PATCH] core[patch]: Add unit tests for Serializable (#25152) Add a few test cases for serializable (many other test cases already covered throguh runnable tests). --- .../unit_tests/load/test_serializable.py | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 libs/core/tests/unit_tests/load/test_serializable.py diff --git a/libs/core/tests/unit_tests/load/test_serializable.py b/libs/core/tests/unit_tests/load/test_serializable.py new file mode 100644 index 00000000000..ce6964933f8 --- /dev/null +++ b/libs/core/tests/unit_tests/load/test_serializable.py @@ -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", + }