You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import os
  2. import pytest
  3. from _pytest.monkeypatch import MonkeyPatch
  4. from elasticsearch import Elasticsearch
  5. from core.rag.datasource.vdb.field import Field
  6. class MockIndicesClient:
  7. def __init__(self):
  8. pass
  9. def create(self, index, mappings, settings):
  10. return {"acknowledge": True}
  11. def refresh(self, index):
  12. return {"acknowledge": True}
  13. def delete(self, index):
  14. return {"acknowledge": True}
  15. def exists(self, index):
  16. return True
  17. class MockClient:
  18. def __init__(self, **kwargs):
  19. self.indices = MockIndicesClient()
  20. def index(self, **kwargs):
  21. return {"acknowledge": True}
  22. def exists(self, **kwargs):
  23. return True
  24. def delete(self, **kwargs):
  25. return {"acknowledge": True}
  26. def search(self, **kwargs):
  27. return {
  28. "took": 1,
  29. "hits": {
  30. "hits": [
  31. {
  32. "_source": {
  33. Field.CONTENT_KEY.value: "abcdef",
  34. Field.VECTOR.value: [1, 2],
  35. Field.METADATA_KEY.value: {},
  36. },
  37. "_score": 1.0,
  38. },
  39. {
  40. "_source": {
  41. Field.CONTENT_KEY.value: "123456",
  42. Field.VECTOR.value: [2, 2],
  43. Field.METADATA_KEY.value: {},
  44. },
  45. "_score": 0.9,
  46. },
  47. {
  48. "_source": {
  49. Field.CONTENT_KEY.value: "a1b2c3",
  50. Field.VECTOR.value: [3, 2],
  51. Field.METADATA_KEY.value: {},
  52. },
  53. "_score": 0.8,
  54. },
  55. ]
  56. },
  57. }
  58. MOCK = os.getenv("MOCK_SWITCH", "false").lower() == "true"
  59. @pytest.fixture
  60. def setup_client_mock(request, monkeypatch: MonkeyPatch):
  61. if MOCK:
  62. monkeypatch.setattr(Elasticsearch, "__init__", MockClient.__init__)
  63. monkeypatch.setattr(Elasticsearch, "index", MockClient.index)
  64. monkeypatch.setattr(Elasticsearch, "exists", MockClient.exists)
  65. monkeypatch.setattr(Elasticsearch, "delete", MockClient.delete)
  66. monkeypatch.setattr(Elasticsearch, "search", MockClient.search)
  67. yield
  68. if MOCK:
  69. monkeypatch.undo()