test_query.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import os
  2. import unittest
  3. from unittest.mock import MagicMock, patch
  4. from embedchain import App
  5. from embedchain.config import AppConfig, QueryConfig
  6. class TestApp(unittest.TestCase):
  7. os.environ["OPENAI_API_KEY"] = "test_key"
  8. def setUp(self):
  9. self.app = App(config=AppConfig(collect_metrics=False))
  10. @patch("chromadb.api.models.Collection.Collection.add", MagicMock)
  11. def test_query(self):
  12. """
  13. This test checks the functionality of the 'query' method in the App class.
  14. It simulates a scenario where the 'retrieve_from_database' method returns a context list and
  15. 'get_llm_model_answer' returns an expected answer string.
  16. The 'query' method is expected to call 'retrieve_from_database' and 'get_llm_model_answer' methods
  17. appropriately and return the right answer.
  18. Key assumptions tested:
  19. - 'retrieve_from_database' method is called exactly once with arguments: "Test query" and an instance of
  20. QueryConfig.
  21. - 'get_llm_model_answer' is called exactly once. The specific arguments are not checked in this test.
  22. - 'query' method returns the value it received from 'get_llm_model_answer'.
  23. The test isolates the 'query' method behavior by mocking out 'retrieve_from_database' and
  24. 'get_llm_model_answer' methods.
  25. """
  26. with patch.object(self.app, "retrieve_from_database") as mock_retrieve:
  27. mock_retrieve.return_value = ["Test context"]
  28. with patch.object(self.app, "get_llm_model_answer") as mock_answer:
  29. mock_answer.return_value = "Test answer"
  30. answer = self.app.query("Test query")
  31. self.assertEqual(answer, "Test answer")
  32. self.assertEqual(mock_retrieve.call_args[0][0], "Test query")
  33. self.assertIsInstance(mock_retrieve.call_args[0][1], QueryConfig)
  34. mock_answer.assert_called_once()