|
| 1 | +import pytest |
1 | 2 | from packaging import version |
2 | 3 |
|
| 4 | +from arango.errno import FORBIDDEN |
3 | 5 | from arango.exceptions import ( |
4 | 6 | AQLCacheClearError, |
5 | 7 | AQLCacheConfigureError, |
@@ -346,6 +348,86 @@ def test_aql_function_management(db, bad_db): |
346 | 348 | assert db.aql.functions() == [] |
347 | 349 |
|
348 | 350 |
|
| 351 | +def test_cache_results_management(db, bad_db, col, docs, cluster): |
| 352 | + if cluster: |
| 353 | + pytest.skip("Cluster mode does not support query result cache management") |
| 354 | + |
| 355 | + aql = db.aql |
| 356 | + cache = aql.cache |
| 357 | + |
| 358 | + # Sanity check, just see if the response is OK. |
| 359 | + _ = cache.properties() |
| 360 | + with pytest.raises(AQLCachePropertiesError) as err: |
| 361 | + _ = bad_db.aql.cache.properties() |
| 362 | + assert err.value.error_code == FORBIDDEN |
| 363 | + |
| 364 | + # Turn on caching |
| 365 | + result = cache.configure(mode="on") |
| 366 | + assert result["mode"] == "on" |
| 367 | + result = cache.properties() |
| 368 | + assert result["mode"] == "on" |
| 369 | + with pytest.raises(AQLCacheConfigureError) as err: |
| 370 | + _ = bad_db.aql.cache.configure(mode="on") |
| 371 | + assert err.value.error_code == FORBIDDEN |
| 372 | + |
| 373 | + # Run a simple query to use the cache |
| 374 | + col.insert(docs[0]) |
| 375 | + _ = aql.execute( |
| 376 | + query="FOR doc IN @@collection RETURN doc", |
| 377 | + bind_vars={"@collection": col.name}, |
| 378 | + cache=True, |
| 379 | + ) |
| 380 | + |
| 381 | + # Check the entries |
| 382 | + entries = cache.entries() |
| 383 | + assert isinstance(entries, list) |
| 384 | + assert len(entries) > 0 |
| 385 | + |
| 386 | + with pytest.raises(AQLCacheEntriesError) as err: |
| 387 | + _ = bad_db.aql.cache.entries() |
| 388 | + assert err.value.error_code == FORBIDDEN |
| 389 | + |
| 390 | + # Clear the cache |
| 391 | + cache.clear() |
| 392 | + entries = cache.entries() |
| 393 | + assert len(entries) == 0 |
| 394 | + with pytest.raises(AQLCacheClearError) as err: |
| 395 | + bad_db.aql.cache.clear() |
| 396 | + assert err.value.error_code == FORBIDDEN |
| 397 | + |
| 398 | + |
| 399 | +def test_cache_plan_management(db, bad_db, col, docs, db_version): |
| 400 | + if db_version < version.parse("3.12.4"): |
| 401 | + pytest.skip("Query plan cache is supported in ArangoDB 3.12.4+") |
| 402 | + |
| 403 | + aql = db.aql |
| 404 | + cache = aql.cache |
| 405 | + |
| 406 | + # Run a simple query to use the cache |
| 407 | + col.insert(docs[0]) |
| 408 | + _ = aql.execute( |
| 409 | + query="FOR doc IN @@collection RETURN doc", |
| 410 | + bind_vars={"@collection": col.name}, |
| 411 | + use_plan_cache=True, |
| 412 | + ) |
| 413 | + |
| 414 | + # Check the entries |
| 415 | + entries = cache.plan_entries() |
| 416 | + assert isinstance(entries, list) |
| 417 | + assert len(entries) > 0 |
| 418 | + with pytest.raises(AQLCacheEntriesError) as err: |
| 419 | + _ = bad_db.aql.cache.plan_entries() |
| 420 | + assert err.value.error_code == FORBIDDEN |
| 421 | + |
| 422 | + # Clear the cache |
| 423 | + cache.clear_plan() |
| 424 | + entries = cache.plan_entries() |
| 425 | + assert len(entries) == 0 |
| 426 | + with pytest.raises(AQLCacheClearError) as err: |
| 427 | + bad_db.aql.cache.clear_plan() |
| 428 | + assert err.value.error_code == FORBIDDEN |
| 429 | + |
| 430 | + |
349 | 431 | def test_aql_cache_management(db, bad_db): |
350 | 432 | # Test get AQL cache properties |
351 | 433 | properties = db.aql.cache.properties() |
|
0 commit comments