|
1 | 1 | import time |
2 | | -from typing import Callable, Generator |
| 2 | +from typing import Callable, Dict, Generator |
3 | 3 | from unittest.mock import patch |
4 | 4 |
|
5 | 5 | from httpx import URL |
@@ -551,3 +551,79 @@ def use_engine_callback_counter(request, **kwargs): |
551 | 551 | assert ( |
552 | 552 | use_engine_call_counter == 2 |
553 | 553 | ), "Use engine URL was not called after cache expiry" |
| 554 | + |
| 555 | + |
| 556 | +async def test_use_engine_parameters_caching( |
| 557 | + db_name: str, |
| 558 | + engine_name: str, |
| 559 | + auth_url: str, |
| 560 | + api_endpoint: str, |
| 561 | + auth: Auth, |
| 562 | + account_name: str, |
| 563 | + httpx_mock: HTTPXMock, |
| 564 | + system_engine_no_db_query_url: str, |
| 565 | + system_engine_query_url: str, |
| 566 | + use_database_callback: Callable, |
| 567 | + use_engine_with_params_callback: Callable, |
| 568 | + test_update_parameters: Dict[str, str], |
| 569 | + mock_system_engine_connection_flow: Callable, |
| 570 | +): |
| 571 | + """Test that USE ENGINE parameters are cached and correctly retrieved.""" |
| 572 | + mock_system_engine_connection_flow() |
| 573 | + |
| 574 | + use_engine_call_counter = 0 |
| 575 | + |
| 576 | + def use_engine_callback_counter(request, **kwargs): |
| 577 | + nonlocal use_engine_call_counter |
| 578 | + use_engine_call_counter += 1 |
| 579 | + return use_engine_with_params_callback(request, **kwargs) |
| 580 | + |
| 581 | + # Add the missing USE DATABASE callback |
| 582 | + httpx_mock.add_callback( |
| 583 | + use_database_callback, |
| 584 | + url=system_engine_no_db_query_url, |
| 585 | + match_content=f'USE DATABASE "{db_name}"'.encode("utf-8"), |
| 586 | + is_reusable=True, |
| 587 | + ) |
| 588 | + |
| 589 | + # Add USE ENGINE callback with parameters |
| 590 | + httpx_mock.add_callback( |
| 591 | + use_engine_callback_counter, |
| 592 | + url=system_engine_query_url, |
| 593 | + match_content=f'USE ENGINE "{engine_name}"'.encode("utf-8"), |
| 594 | + is_reusable=True, |
| 595 | + ) |
| 596 | + |
| 597 | + # First connection - should populate cache with parameters |
| 598 | + async with await connect( |
| 599 | + database=db_name, |
| 600 | + engine_name=engine_name, |
| 601 | + auth=auth, |
| 602 | + account_name=account_name, |
| 603 | + api_endpoint=api_endpoint, |
| 604 | + ) as connection: |
| 605 | + cursor = connection.cursor() |
| 606 | + # Verify parameters are set in cursor from USE ENGINE response |
| 607 | + for param_name, expected_value in test_update_parameters.items(): |
| 608 | + assert param_name in cursor._set_parameters |
| 609 | + assert cursor._set_parameters[param_name] == expected_value |
| 610 | + |
| 611 | + # Verify USE ENGINE was called once |
| 612 | + assert use_engine_call_counter == 1, "USE ENGINE was not called on first connection" |
| 613 | + |
| 614 | + # Second connection - should use cache and not call USE ENGINE again |
| 615 | + async with await connect( |
| 616 | + database=db_name, |
| 617 | + engine_name=engine_name, |
| 618 | + auth=auth, |
| 619 | + account_name=account_name, |
| 620 | + api_endpoint=api_endpoint, |
| 621 | + ) as connection: |
| 622 | + cursor = connection.cursor() |
| 623 | + # Verify cached parameters are correctly applied |
| 624 | + for param_name, expected_value in test_update_parameters.items(): |
| 625 | + assert param_name in cursor._set_parameters |
| 626 | + assert cursor._set_parameters[param_name] == expected_value |
| 627 | + |
| 628 | + # Verify USE ENGINE was not called again (cache hit) |
| 629 | + assert use_engine_call_counter == 1, "USE ENGINE was called when cache should hit" |
0 commit comments