|
7 | 7 |
|
8 | 8 |
|
9 | 9 | class TestApp(unittest.TestCase):
|
10 |
| - os.environ["OPENAI_API_KEY"] = "test_key" |
11 |
| - |
12 | 10 | def setUp(self):
|
| 11 | + os.environ["OPENAI_API_KEY"] = "test_key" |
13 | 12 | self.app = App(config=AppConfig(collect_metrics=False))
|
14 | 13 |
|
15 |
| - @patch("embedchain.embedchain.memory", autospec=True) |
16 | 14 | @patch.object(App, "retrieve_from_database", return_value=["Test context"])
|
17 | 15 | @patch.object(App, "get_answer_from_llm", return_value="Test answer")
|
18 |
| - def test_chat_with_memory(self, mock_answer, mock_retrieve, mock_memory): |
| 16 | + def test_chat_with_memory(self, mock_get_answer, mock_retrieve): |
19 | 17 | """
|
20 | 18 | This test checks the functionality of the 'chat' method in the App class with respect to the chat history
|
21 | 19 | memory.
|
22 | 20 | The 'chat' method is called twice. The first call initializes the chat history memory.
|
23 | 21 | The second call is expected to use the chat history from the first call.
|
24 | 22 |
|
25 | 23 | Key assumptions tested:
|
26 |
| - - After the first call, 'memory.chat_memory.add_user_message' and 'memory.chat_memory.add_ai_message' are |
27 | 24 | called with correct arguments, adding the correct chat history.
|
| 25 | + - After the first call, 'memory.chat_memory.add_user_message' and 'memory.chat_memory.add_ai_message' are |
28 | 26 | - During the second call, the 'chat' method uses the chat history from the first call.
|
29 | 27 |
|
30 | 28 | The test isolates the 'chat' method behavior by mocking out 'retrieve_from_database', 'get_answer_from_llm' and
|
31 | 29 | 'memory' methods.
|
32 | 30 | """
|
33 |
| - mock_memory.load_memory_variables.return_value = {"history": []} |
34 | 31 | app = App()
|
35 |
| - |
36 |
| - # First call to chat |
37 | 32 | first_answer = app.chat("Test query 1")
|
38 | 33 | self.assertEqual(first_answer, "Test answer")
|
39 |
| - mock_memory.chat_memory.add_user_message.assert_called_once_with("Test query 1") |
40 |
| - mock_memory.chat_memory.add_ai_message.assert_called_once_with("Test answer") |
41 |
| - |
42 |
| - mock_memory.chat_memory.add_user_message.reset_mock() |
43 |
| - mock_memory.chat_memory.add_ai_message.reset_mock() |
44 |
| - |
45 |
| - # Second call to chat |
| 34 | + self.assertEqual(len(app.memory.chat_memory.messages), 2) |
46 | 35 | second_answer = app.chat("Test query 2")
|
47 | 36 | self.assertEqual(second_answer, "Test answer")
|
48 |
| - mock_memory.chat_memory.add_user_message.assert_called_once_with("Test query 2") |
49 |
| - mock_memory.chat_memory.add_ai_message.assert_called_once_with("Test answer") |
| 37 | + self.assertEqual(len(app.memory.chat_memory.messages), 4) |
0 commit comments