langchain/libs/community/tests/unit_tests/callbacks/test_streamlit_callback.py
Bagatur ed58eeb9c5
community[major], core[patch], langchain[patch], experimental[patch]: Create langchain-community (#14463)
Moved the following modules to new package langchain-community in a backwards compatible fashion:

```
mv langchain/langchain/adapters community/langchain_community
mv langchain/langchain/callbacks community/langchain_community/callbacks
mv langchain/langchain/chat_loaders community/langchain_community
mv langchain/langchain/chat_models community/langchain_community
mv langchain/langchain/document_loaders community/langchain_community
mv langchain/langchain/docstore community/langchain_community
mv langchain/langchain/document_transformers community/langchain_community
mv langchain/langchain/embeddings community/langchain_community
mv langchain/langchain/graphs community/langchain_community
mv langchain/langchain/llms community/langchain_community
mv langchain/langchain/memory/chat_message_histories community/langchain_community
mv langchain/langchain/retrievers community/langchain_community
mv langchain/langchain/storage community/langchain_community
mv langchain/langchain/tools community/langchain_community
mv langchain/langchain/utilities community/langchain_community
mv langchain/langchain/vectorstores community/langchain_community
mv langchain/langchain/agents/agent_toolkits community/langchain_community
mv langchain/langchain/cache.py community/langchain_community
mv langchain/langchain/adapters community/langchain_community
mv langchain/langchain/callbacks community/langchain_community/callbacks
mv langchain/langchain/chat_loaders community/langchain_community
mv langchain/langchain/chat_models community/langchain_community
mv langchain/langchain/document_loaders community/langchain_community
mv langchain/langchain/docstore community/langchain_community
mv langchain/langchain/document_transformers community/langchain_community
mv langchain/langchain/embeddings community/langchain_community
mv langchain/langchain/graphs community/langchain_community
mv langchain/langchain/llms community/langchain_community
mv langchain/langchain/memory/chat_message_histories community/langchain_community
mv langchain/langchain/retrievers community/langchain_community
mv langchain/langchain/storage community/langchain_community
mv langchain/langchain/tools community/langchain_community
mv langchain/langchain/utilities community/langchain_community
mv langchain/langchain/vectorstores community/langchain_community
mv langchain/langchain/agents/agent_toolkits community/langchain_community
mv langchain/langchain/cache.py community/langchain_community
```

Moved the following to core
```
mv langchain/langchain/utils/json_schema.py core/langchain_core/utils
mv langchain/langchain/utils/html.py core/langchain_core/utils
mv langchain/langchain/utils/strings.py core/langchain_core/utils
cat langchain/langchain/utils/env.py >> core/langchain_core/utils/env.py
rm langchain/langchain/utils/env.py
```

See .scripts/community_split/script_integrations.sh for all changes
2023-12-11 13:53:30 -08:00

89 lines
3.0 KiB
Python

import builtins
import unittest
from typing import Any
from unittest import mock
from unittest.mock import MagicMock
from langchain_community.callbacks.streamlit import StreamlitCallbackHandler
class TestImport(unittest.TestCase):
"""Test the StreamlitCallbackHandler 'auto-updating' API"""
def setUp(self) -> None:
self.builtins_import = builtins.__import__
def tearDown(self) -> None:
builtins.__import__ = self.builtins_import
@mock.patch(
"langchain_community.callbacks.streamlit._InternalStreamlitCallbackHandler"
)
def test_create_internal_handler(self, mock_internal_handler: Any) -> None:
"""If we're using a Streamlit that does not expose its own
StreamlitCallbackHandler, use our own implementation.
"""
def external_import_error(
name: str, globals: Any, locals: Any, fromlist: Any, level: int
) -> Any:
if name == "streamlit.external.langchain":
raise ImportError
return self.builtins_import(name, globals, locals, fromlist, level)
builtins.__import__ = external_import_error # type: ignore[assignment]
parent_container = MagicMock()
thought_labeler = MagicMock()
StreamlitCallbackHandler(
parent_container,
max_thought_containers=1,
expand_new_thoughts=True,
collapse_completed_thoughts=False,
thought_labeler=thought_labeler,
)
# Our internal handler should be created
mock_internal_handler.assert_called_once_with(
parent_container,
max_thought_containers=1,
expand_new_thoughts=True,
collapse_completed_thoughts=False,
thought_labeler=thought_labeler,
)
def test_create_external_handler(self) -> None:
"""If we're using a Streamlit that *does* expose its own callback handler,
delegate to that implementation.
"""
mock_streamlit_module = MagicMock()
def external_import_success(
name: str, globals: Any, locals: Any, fromlist: Any, level: int
) -> Any:
if name == "streamlit.external.langchain":
return mock_streamlit_module
return self.builtins_import(name, globals, locals, fromlist, level)
builtins.__import__ = external_import_success # type: ignore[assignment]
parent_container = MagicMock()
thought_labeler = MagicMock()
StreamlitCallbackHandler(
parent_container,
max_thought_containers=1,
expand_new_thoughts=True,
collapse_completed_thoughts=False,
thought_labeler=thought_labeler,
)
# Streamlit's handler should be created
mock_streamlit_module.StreamlitCallbackHandler.assert_called_once_with(
parent_container,
max_thought_containers=1,
expand_new_thoughts=True,
collapse_completed_thoughts=False,
thought_labeler=thought_labeler,
)