test_query.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import os
  2. import unittest
  3. from unittest.mock import MagicMock, patch
  4. from embedchain import App
  5. from embedchain.config import AppConfig, BaseLlmConfig
  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. LlmConfig.
  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.llm, "get_llm_model_answer") as mock_answer:
  29. mock_answer.return_value = "Test answer"
  30. _answer = self.app.query(input_query="Test query")
  31. # Ensure retrieve_from_database was called
  32. mock_retrieve.assert_called_once()
  33. # Check the call arguments
  34. args, kwargs = mock_retrieve.call_args
  35. input_query_arg = kwargs.get("input_query")
  36. self.assertEqual(input_query_arg, "Test query")
  37. mock_answer.assert_called_once()
  38. @patch("embedchain.llm.openai.OpenAILlm._get_answer")
  39. def test_query_config_app_passing(self, mock_get_answer):
  40. mock_get_answer.return_value = MagicMock()
  41. mock_get_answer.return_value.content = "Test answer"
  42. config = AppConfig(collect_metrics=False)
  43. chat_config = BaseLlmConfig(system_prompt="Test system prompt")
  44. app = App(config=config, llm_config=chat_config)
  45. answer = app.llm.get_llm_model_answer("Test query")
  46. self.assertEqual(app.llm.config.system_prompt, "Test system prompt")
  47. self.assertEqual(answer, "Test answer")
  48. @patch("embedchain.llm.openai.OpenAILlm._get_answer")
  49. def test_app_passing(self, mock_get_answer):
  50. mock_get_answer.return_value = MagicMock()
  51. mock_get_answer.return_value.content = "Test answer"
  52. config = AppConfig(collect_metrics=False)
  53. chat_config = BaseLlmConfig()
  54. app = App(config=config, llm_config=chat_config, system_prompt="Test system prompt")
  55. answer = app.llm.get_llm_model_answer("Test query")
  56. self.assertEqual(app.llm.config.system_prompt, "Test system prompt")
  57. self.assertEqual(answer, "Test answer")
  58. @patch("chromadb.api.models.Collection.Collection.add", MagicMock)
  59. def test_query_with_where_in_params(self):
  60. """
  61. This test checks the functionality of the 'query' method in the App class.
  62. It simulates a scenario where the 'retrieve_from_database' method returns a context list based on
  63. a where filter and 'get_llm_model_answer' returns an expected answer string.
  64. The 'query' method is expected to call 'retrieve_from_database' with the where filter and
  65. 'get_llm_model_answer' methods appropriately and return the right answer.
  66. Key assumptions tested:
  67. - 'retrieve_from_database' method is called exactly once with arguments: "Test query" and an instance of
  68. LlmConfig.
  69. - 'get_llm_model_answer' is called exactly once. The specific arguments are not checked in this test.
  70. - 'query' method returns the value it received from 'get_llm_model_answer'.
  71. The test isolates the 'query' method behavior by mocking out 'retrieve_from_database' and
  72. 'get_llm_model_answer' methods.
  73. """
  74. with patch.object(self.app, "retrieve_from_database") as mock_retrieve:
  75. mock_retrieve.return_value = ["Test context"]
  76. with patch.object(self.app.llm, "get_llm_model_answer") as mock_answer:
  77. mock_answer.return_value = "Test answer"
  78. answer = self.app.query("Test query", where={"attribute": "value"})
  79. self.assertEqual(answer, "Test answer")
  80. _args, kwargs = mock_retrieve.call_args
  81. self.assertEqual(kwargs.get("input_query"), "Test query")
  82. self.assertEqual(kwargs.get("where"), {"attribute": "value"})
  83. mock_answer.assert_called_once()
  84. @patch("chromadb.api.models.Collection.Collection.add", MagicMock)
  85. def test_query_with_where_in_query_config(self):
  86. """
  87. This test checks the functionality of the 'query' method in the App class.
  88. It simulates a scenario where the 'retrieve_from_database' method returns a context list based on
  89. a where filter and 'get_llm_model_answer' returns an expected answer string.
  90. The 'query' method is expected to call 'retrieve_from_database' with the where filter and
  91. 'get_llm_model_answer' methods appropriately and return the right answer.
  92. Key assumptions tested:
  93. - 'retrieve_from_database' method is called exactly once with arguments: "Test query" and an instance of
  94. LlmConfig.
  95. - 'get_llm_model_answer' is called exactly once. The specific arguments are not checked in this test.
  96. - 'query' method returns the value it received from 'get_llm_model_answer'.
  97. The test isolates the 'query' method behavior by mocking out 'retrieve_from_database' and
  98. 'get_llm_model_answer' methods.
  99. """
  100. with patch.object(self.app.llm, "get_llm_model_answer") as mock_answer:
  101. mock_answer.return_value = "Test answer"
  102. with patch.object(self.app.db, "query") as mock_database_query:
  103. mock_database_query.return_value = ["Test context"]
  104. llm_config = BaseLlmConfig(where={"attribute": "value"})
  105. answer = self.app.query("Test query", llm_config)
  106. self.assertEqual(answer, "Test answer")
  107. _args, kwargs = mock_database_query.call_args
  108. self.assertEqual(kwargs.get("input_query"), "Test query")
  109. self.assertEqual(kwargs.get("where"), {"attribute": "value"})
  110. mock_answer.assert_called_once()