test_add.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536
  1. import os
  2. import unittest
  3. from unittest.mock import MagicMock, patch
  4. from embedchain import App
  5. from embedchain.config import AppConfig
  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_add(self):
  12. """
  13. This test checks the functionality of the 'add' method in the App class.
  14. It begins by simulating the addition of a web page with a specific URL to the application instance.
  15. The 'add' method is expected to append the input type and URL to the 'user_asks' attribute of the App instance.
  16. By asserting that 'user_asks' is updated correctly after the 'add' method is called, we can confirm that the
  17. method is working as intended.
  18. The Collection.add method from the chromadb library is mocked during this test to isolate the behavior of the
  19. 'add' method.
  20. """
  21. self.app.add("https://example.com", metadata={"meta": "meta-data"})
  22. self.assertEqual(self.app.user_asks, [["https://example.com", "web_page", {"meta": "meta-data"}]])
  23. @patch("chromadb.api.models.Collection.Collection.add", MagicMock)
  24. def test_add_forced_type(self):
  25. """
  26. Test that you can also force a data_type with `add`.
  27. """
  28. data_type = "text"
  29. self.app.add("https://example.com", data_type=data_type, metadata={"meta": "meta-data"})
  30. self.assertEqual(self.app.user_asks, [["https://example.com", data_type, {"meta": "meta-data"}]])