Skip to content

Commit 00077c4

Browse files
Copilotshuchenliu
andcommitted
test: mock clear_cache and run_single_query in test_cache_bypass
Co-authored-by: shuchenliu <5944967+shuchenliu@users.noreply.github.com>
1 parent 59bc320 commit 00077c4

1 file changed

Lines changed: 21 additions & 8 deletions

File tree

tests/data_tiers/tier_1/elasticsearch_tests/test_tier1_driver.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -233,23 +233,36 @@ async def test_end_to_end(qgraph, expected_hits):
233233

234234
@pytest.mark.usefixtures("mock_elasticsearch_config")
235235
@pytest.mark.asyncio
236-
async def test_cache_bypass():
236+
async def test_cache_bypass(monkeypatch: pytest.MonkeyPatch):
237+
from unittest.mock import AsyncMock
238+
237239
transpiler = ElasticsearchTranspiler()
238240
payload = _convert_triple(transpiler, DINGO_QGRAPH)
239241

240242
driver: driver_mod.ElasticSearchDriver = driver_mod.ElasticSearchDriver()
241243

242-
try:
243-
await driver.connect()
244-
assert driver.es_connection is not None
245-
except Exception:
246-
pytest.skip("skipping es driver connection test: cannot connect")
244+
# Inject a mock ES connection so the test doesn't require a live ES instance
245+
mock_es = AsyncMock()
246+
mock_es.indices.clear_cache.return_value = {"_shards": {"successful": 1, "total": 1}}
247+
driver.es_connection = mock_es
248+
249+
# Mock run_single_query to avoid actual ES queries
250+
mock_run_single = AsyncMock(return_value=[])
251+
monkeypatch.setattr(driver_mod, "run_single_query", mock_run_single)
247252

248253
hits = await driver.run_query(payload, bypass_cache=True)
249-
assert len(hits) == 8
250254

251-
await driver.close()
255+
mock_es.indices.clear_cache.assert_called_once_with(
256+
index=driver_mod.CONFIG.tier1.elasticsearch.index_name
257+
)
258+
mock_run_single.assert_called_once_with(
259+
es_connection=mock_es,
260+
index_name=driver_mod.CONFIG.tier1.elasticsearch.index_name,
261+
query=payload,
262+
)
263+
assert isinstance(hits, list)
252264

265+
await driver.close()
253266

254267

255268
@pytest.mark.usefixtures("mock_elasticsearch_config")

0 commit comments

Comments
 (0)