Skip to content

Commit

Permalink
Final touchups
Browse files Browse the repository at this point in the history
  • Loading branch information
vblagoje committed Jul 29, 2024
1 parent 762c938 commit e212123
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
@component
class ChatMessageRetriever:
"""
Retrieves chat messages.
Retrieves chat messages from the underlying ChatMessageStore.
Usage example:
```python
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
@component
class ChatMessageWriter:
"""
Writes chat messages to a ChatMessageStore.
Writes chat messages to an underlying ChatMessageStore.
Usage example:
```python
Expand Down Expand Up @@ -99,7 +99,7 @@ def run(self, messages: List[ChatMessage]):
:param messages:
A list of chat messages to write to the store.
:returns:
- `messages_written`: Number of messages written
- `messages_written`: Number of messages written to the ChatMessageStore.
:raises ValueError:
If the specified message store is not found.
Expand Down
18 changes: 18 additions & 0 deletions test/chat_message_stores/in_memory/test_chat_message_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,29 @@
class TestChatMessageStore:

def test_init(self):
"""
Test that the InMemoryChatMessageStore can be initialized and that it works as expected.
"""
store = InMemoryChatMessageStore()
assert store.count_messages() == 0
assert store.retrieve() == []
assert store.write_messages([]) == 0
assert not store.delete_messages()

def test_to_dict(self):
"""
Test that the InMemoryChatMessageStore can be serialized to a dictionary.
"""
store = InMemoryChatMessageStore()
assert store.to_dict() == {
"init_parameters": {},
"type": "haystack_experimental.chat_message_stores.in_memory.chat_message_store.InMemoryChatMessageStore"
}

def test_from_dict(self):
"""
Test that the InMemoryChatMessageStore can be deserialized from a dictionary.
"""
data = {
"init_parameters": {},
"type": "haystack_experimental.chat_message_stores.in_memory.chat_message_store.InMemoryChatMessageStore"
Expand All @@ -28,6 +37,9 @@ def test_from_dict(self):
assert store.to_dict() == data

def test_count_messages(self):
"""
Test that the InMemoryChatMessageStore can count the number of messages in the store correctly.
"""
store = InMemoryChatMessageStore()
assert store.count_messages() == 0
store.write_messages(messages=[ChatMessage.from_user(content="Hello, how can I help you?")])
Expand All @@ -38,6 +50,9 @@ def test_count_messages(self):
assert store.count_messages() == 3

def test_retrieve(self):
"""
Test that the InMemoryChatMessageStore can retrieve all messages from the store correctly.
"""
store = InMemoryChatMessageStore()
assert store.retrieve() == []
store.write_messages(messages=[ChatMessage.from_user(content="Hello, how can I help you?")])
Expand All @@ -55,6 +70,9 @@ def test_retrieve(self):
]

def test_delete_messages(self):
"""
Test that the InMemoryChatMessageStore can delete all messages from the store correctly.
"""
store = InMemoryChatMessageStore()
assert store.count_messages() == 0
store.write_messages(messages=[ChatMessage.from_user(content="Hello, how can I help you?")])
Expand Down
13 changes: 12 additions & 1 deletion test/components/retrievers/test_chat_message_retriever.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@

class TestChatMessageRetriever:
def test_init(self):

"""
Test that the ChatMessageRetriever component can be initialized with a valid message store.
"""
messages = [
ChatMessage.from_user(content="Hello, how can I help you?"),
ChatMessage.from_user(content="Hallo, wie kann ich Ihnen helfen?")
Expand All @@ -22,6 +24,9 @@ def test_init(self):
assert retriever.run() == {"messages": messages}

def test_to_dict(self):
"""
Test that the ChatMessageRetriever component can be serialized to a dictionary.
"""
message_store = InMemoryChatMessageStore()
retriever = ChatMessageRetriever(message_store)

Expand All @@ -37,6 +42,9 @@ def test_to_dict(self):
}

def test_from_dict(self):
"""
Test that the ChatMessageRetriever component can be deserialized from a dictionary.
"""
data = {
"type": "haystack_experimental.components.retrievers.chat_message_retriever.ChatMessageRetriever",
"init_parameters": {
Expand All @@ -53,6 +61,9 @@ def test_from_dict(self):
}

def test_chat_message_retriever_pipeline(self):
"""
Test that the ChatMessageRetriever can be used in a pipeline and that it works as expected.
"""
store = InMemoryChatMessageStore()
store.write_messages([ChatMessage.from_assistant("Hello, how can I help you?")])

Expand Down
12 changes: 12 additions & 0 deletions test/components/writers/test_chat_message_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

class TestChatMessageRetriever:
def test_init(self):
"""
Test that the ChatMessageWriter component can be initialized with a valid message store.
"""
messages = [
ChatMessage.from_user(content="Hello, how can I help you?"),
ChatMessage.from_user(content="Hallo, wie kann ich Ihnen helfen?")
Expand All @@ -21,6 +24,9 @@ def test_init(self):
assert retriever.run(messages=messages) == {"messages_written": 2}

def test_to_dict(self):
"""
Test that the ChatMessageWriter component can be serialized to a dictionary.
"""
message_store = InMemoryChatMessageStore()
retriever = ChatMessageWriter(message_store)

Expand All @@ -36,6 +42,9 @@ def test_to_dict(self):
}

def test_from_dict(self):
"""
Test that the ChatMessageWriter component can be deserialized from a dictionary.
"""
data = {
"type": "haystack_experimental.components.writers.chat_message_writer.ChatMessageWriter",
"init_parameters": {
Expand All @@ -52,6 +61,9 @@ def test_from_dict(self):
}

def test_chat_message_writer_pipeline(self):
"""
Test that the ChatMessageWriter can be used in a pipeline and that it works as expected.
"""
store = InMemoryChatMessageStore()

pipe = Pipeline()
Expand Down

0 comments on commit e212123

Please sign in to comment.