|
1 | 1 | import base64
|
| 2 | +import os |
| 3 | +import shutil |
2 | 4 | from urllib.parse import quote_plus, urlparse
|
3 | 5 |
|
4 | 6 | import orjson
|
|
10 | 12 | from testcontainers.postgres import PostgresContainer
|
11 | 13 |
|
12 | 14 | from app.model.validator import rules
|
| 15 | +from app.query_cache import QueryCacheManager |
13 | 16 | from tests.conftest import file_path
|
14 | 17 |
|
15 | 18 | pytestmark = pytest.mark.postgres
|
@@ -124,6 +127,15 @@ def postgres(request) -> PostgresContainer:
|
124 | 127 | return pg
|
125 | 128 |
|
126 | 129 |
|
| 130 | +@pytest.fixture(scope="function") |
| 131 | +def cache_dir(): |
| 132 | + temp_dir = "/tmp/wren-engine-test" |
| 133 | + os.makedirs(temp_dir, exist_ok=True) |
| 134 | + yield temp_dir |
| 135 | + # Clean up after the test |
| 136 | + shutil.rmtree(temp_dir, ignore_errors=True) |
| 137 | + |
| 138 | + |
127 | 139 | async def test_query(client, manifest_str, postgres: PostgresContainer):
|
128 | 140 | connection_info = _to_connection_info(postgres)
|
129 | 141 | response = await client.post(
|
@@ -164,6 +176,51 @@ async def test_query(client, manifest_str, postgres: PostgresContainer):
|
164 | 176 | }
|
165 | 177 |
|
166 | 178 |
|
| 179 | +async def test_query_with_cache( |
| 180 | + client, manifest_str, postgres: PostgresContainer, cache_dir, monkeypatch |
| 181 | +): |
| 182 | + # Override the cache path to use our test directory |
| 183 | + monkeypatch.setattr( |
| 184 | + QueryCacheManager, |
| 185 | + "_get_cache_path", |
| 186 | + lambda self, key: f"{cache_dir}/{key}.cache", |
| 187 | + ) |
| 188 | + |
| 189 | + connection_info = _to_connection_info(postgres) |
| 190 | + |
| 191 | + # First request - should miss cache |
| 192 | + response1 = await client.post( |
| 193 | + url=f"{base_url}/query?cacheEnable=true", # Enable cache |
| 194 | + json={ |
| 195 | + "connectionInfo": connection_info, |
| 196 | + "manifestStr": manifest_str, |
| 197 | + "sql": 'SELECT * FROM "Orders" LIMIT 10', |
| 198 | + }, |
| 199 | + ) |
| 200 | + |
| 201 | + assert response1.status_code == 200 |
| 202 | + assert response1.headers["X-Cache-Hit"] == "false" |
| 203 | + result1 = response1.json() |
| 204 | + |
| 205 | + # Second request with same SQL - should hit cache |
| 206 | + response2 = await client.post( |
| 207 | + url=f"{base_url}/query?cacheEnable=true", # Enable cache |
| 208 | + json={ |
| 209 | + "connectionInfo": connection_info, |
| 210 | + "manifestStr": manifest_str, |
| 211 | + "sql": 'SELECT * FROM "Orders" LIMIT 10', |
| 212 | + }, |
| 213 | + ) |
| 214 | + assert response2.status_code == 200 |
| 215 | + assert response2.headers["X-Cache-Hit"] == "true" |
| 216 | + result2 = response2.json() |
| 217 | + |
| 218 | + # Verify results are identical |
| 219 | + assert result1["data"] == result2["data"] |
| 220 | + assert result1["columns"] == result2["columns"] |
| 221 | + assert result1["dtypes"] == result2["dtypes"] |
| 222 | + |
| 223 | + |
167 | 224 | async def test_query_with_connection_url(
|
168 | 225 | client, manifest_str, postgres: PostgresContainer
|
169 | 226 | ):
|
|
0 commit comments