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/
This commit is contained in:
Jib
2024-03-05 14:44:31 -05:00
committed by GitHub
parent 48e303ea10
commit fc35262356
3 changed files with 49 additions and 6 deletions

View File

@@ -169,18 +169,21 @@ class MockCollection(Collection):
[k["_id"] for k in mongodb_inserts], acknowledged=True
)
def insert_one(self, to_insert: Any, *args, **kwargs) -> Any: # type: ignore
return self.insert_many([to_insert])
def find_one(self, find_query: Dict[str, Any]) -> Optional[Dict[str, Any]]: # type: ignore
find = self.find(find_query) or [None] # type: ignore
return find[0]
def find(self, find_query: Dict[str, Any]) -> Optional[List[Dict[str, Any]]]: # type: ignore
def _is_match(item: Dict[str, Any]) -> bool:
for key, match_val in find_query.items():
if item.get(key) != match_val:
return False
return True
# Return the first element to match
for document in self._data:
if _is_match(document):
return document
return None
return [document for document in self._data if _is_match(document)]
def update_one( # type: ignore
self,