-
Notifications
You must be signed in to change notification settings - Fork 13.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
langchain-mongodb: add unit tests for MongoDBChatMessageHistory (#18599)
## Description Adding in Unit Test variation for `MongoDBChatMessageHistory` package Follow-up to #18590 - [x] **Add tests and docs**: Unit test is what's being added - [x] **Lint and test**: Run `make format`, `make lint` and `make test` from the root of the package(s) you've modified. See contribution guidelines for more: https://python.langchain.com/docs/contributing/
- Loading branch information
Showing
3 changed files
with
49 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
libs/partners/mongodb/tests/unit_tests/test_chat_message_histories.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import json | ||
|
||
from langchain.memory import ConversationBufferMemory | ||
from langchain_core.messages import message_to_dict | ||
|
||
from langchain_mongodb.chat_message_histories import MongoDBChatMessageHistory | ||
from tests.utils import MockCollection | ||
|
||
|
||
class PatchedMongoDBChatMessageHistory(MongoDBChatMessageHistory): | ||
def __init__(self) -> None: | ||
self.session_id = "test-session" | ||
self.database_name = "test-database" | ||
self.collection_name = "test-collection" | ||
self.collection = MockCollection() | ||
|
||
|
||
def test_memory_with_message_store() -> None: | ||
"""Test the memory with a message store.""" | ||
# setup MongoDB as a message store | ||
message_history = PatchedMongoDBChatMessageHistory() | ||
memory = ConversationBufferMemory( | ||
memory_key="baz", chat_memory=message_history, return_messages=True | ||
) | ||
|
||
# add some messages | ||
memory.chat_memory.add_ai_message("This is me, the AI") | ||
memory.chat_memory.add_user_message("This is me, the human") | ||
|
||
# get the message history from the memory store and turn it into a json | ||
messages = memory.chat_memory.messages | ||
messages_json = json.dumps([message_to_dict(msg) for msg in messages]) | ||
|
||
assert "This is me, the AI" in messages_json | ||
assert "This is me, the human" in messages_json | ||
|
||
# remove the record from MongoDB, so the next test run won't pick it up | ||
memory.chat_memory.clear() | ||
|
||
assert memory.chat_memory.messages == [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters