test_chroma_db.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. # ruff: noqa: E501
  2. import unittest
  3. from unittest.mock import patch
  4. from chromadb.config import Settings
  5. from embedchain import App
  6. from embedchain.config import AppConfig
  7. from embedchain.vectordb.chroma_db import ChromaDB, chromadb
  8. class TestChromaDbHosts(unittest.TestCase):
  9. def test_init_with_host_and_port(self):
  10. """
  11. Test if the `ChromaDB` instance is initialized with the correct host and port values.
  12. """
  13. host = "test-host"
  14. port = "1234"
  15. with patch.object(chromadb, "HttpClient") as mock_client:
  16. _db = ChromaDB(host=host, port=port, embedding_fn=len)
  17. expected_settings = Settings(
  18. chroma_server_host="test-host",
  19. chroma_server_http_port="1234",
  20. )
  21. mock_client.assert_called_once_with(expected_settings)
  22. # Review this test
  23. class TestChromaDbHostsInit(unittest.TestCase):
  24. @patch("embedchain.vectordb.chroma_db.chromadb.Client")
  25. def test_init_with_host_and_port(self, mock_client):
  26. """
  27. Test if the `App` instance is initialized with the correct host and port values.
  28. """
  29. host = "test-host"
  30. port = "1234"
  31. config = AppConfig(host=host, port=port)
  32. _app = App(config)
  33. # self.assertEqual(mock_client.call_args[0][0].chroma_server_host, host)
  34. # self.assertEqual(mock_client.call_args[0][0].chroma_server_http_port, port)
  35. class TestChromaDbHostsNone(unittest.TestCase):
  36. @patch("embedchain.vectordb.chroma_db.chromadb.Client")
  37. def test_init_with_host_and_port(self, mock_client):
  38. """
  39. Test if the `App` instance is initialized without default hosts and ports.
  40. """
  41. _app = App()
  42. self.assertEqual(mock_client.call_args[0][0].chroma_server_host, None)
  43. self.assertEqual(mock_client.call_args[0][0].chroma_server_http_port, None)
  44. class TestChromaDbHostsLoglevel(unittest.TestCase):
  45. @patch("embedchain.vectordb.chroma_db.chromadb.Client")
  46. def test_init_with_host_and_port(self, mock_client):
  47. """
  48. Test if the `App` instance is initialized without a config that does not contain default hosts and ports.
  49. """
  50. config = AppConfig(log_level="DEBUG")
  51. _app = App(config)
  52. self.assertEqual(mock_client.call_args[0][0].chroma_server_host, None)
  53. self.assertEqual(mock_client.call_args[0][0].chroma_server_http_port, None)