Skip to content

Commit 4cebf3a

Browse files
committed
fix unit tests
1 parent 436a240 commit 4cebf3a

2 files changed

Lines changed: 38 additions & 27 deletions

File tree

tests/unit/async_db/test_caching.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,7 @@ async def test_token_cache_expiry_forces_reauthentication(
648648

649649
with patch("time.time", return_value=fixed_time):
650650
# First connection should cache token
651-
async with aconnect(
651+
async with await connect(
652652
database=db_name,
653653
engine_name=engine_name,
654654
auth=auth,
@@ -661,7 +661,7 @@ async def test_token_cache_expiry_forces_reauthentication(
661661

662662
# Simulate time passing within token validity
663663
with patch("time.time", return_value=fixed_time + 1800): # 30 minutes later
664-
async with aconnect(
664+
async with await connect(
665665
database=db_name,
666666
engine_name=engine_name,
667667
auth=auth,
@@ -678,7 +678,7 @@ async def test_token_cache_expiry_forces_reauthentication(
678678
auth._token = None
679679
auth._expires = fixed_time # Set to expired time
680680

681-
async with aconnect(
681+
async with await connect(
682682
database=db_name,
683683
engine_name=engine_name,
684684
auth=auth,
@@ -791,7 +791,7 @@ def system_engine_callback_counter(request, **kwargs):
791791
)
792792

793793
# Connect with first credentials
794-
async with connect(
794+
async with await connect(
795795
database=db_name,
796796
engine_name=engine_name,
797797
auth=auth1,
@@ -804,7 +804,7 @@ def system_engine_callback_counter(request, **kwargs):
804804
first_call_count = system_engine_call_counter
805805

806806
# Connect with second credentials
807-
async with connect(
807+
async with await connect(
808808
database=db_name,
809809
engine_name=engine_name,
810810
auth=auth2,
@@ -817,7 +817,7 @@ def system_engine_callback_counter(request, **kwargs):
817817
second_call_count = system_engine_call_counter
818818

819819
# Connect again with first credentials
820-
async with connect(
820+
async with await connect(
821821
database=db_name,
822822
engine_name=engine_name,
823823
auth=auth1,
@@ -1068,7 +1068,7 @@ def system_engine_callback_counter(request, **kwargs):
10681068
)
10691069

10701070
# Connect to first account
1071-
async with connect(
1071+
async with await connect(
10721072
database=db_name,
10731073
engine_name=engine_name,
10741074
auth=auth,
@@ -1080,7 +1080,7 @@ def system_engine_callback_counter(request, **kwargs):
10801080
first_account_calls = system_engine_call_counter
10811081

10821082
# Connect to second account with same credentials
1083-
async with connect(
1083+
async with await connect(
10841084
database=db_name,
10851085
engine_name=engine_name,
10861086
auth=auth,
@@ -1092,7 +1092,7 @@ def system_engine_callback_counter(request, **kwargs):
10921092
second_account_calls = system_engine_call_counter
10931093

10941094
# Connect again to first account
1095-
async with connect(
1095+
async with await connect(
10961096
database=db_name,
10971097
engine_name=engine_name,
10981098
auth=auth,
@@ -1117,7 +1117,6 @@ def system_engine_callback_counter(request, **kwargs):
11171117

11181118
async def test_database_switching_with_same_engine(
11191119
db_name: str,
1120-
second_db_name: str,
11211120
engine_name: str,
11221121
auth_url: str,
11231122
httpx_mock: HTTPXMock,
@@ -1126,18 +1125,20 @@ async def test_database_switching_with_same_engine(
11261125
get_system_engine_callback: Callable,
11271126
system_engine_query_url: str,
11281127
system_engine_no_db_query_url: str,
1129-
use_db_callback: Callable,
1130-
use_engine_callback: Callable,
11311128
query_callback: Callable,
11321129
query_url: str,
11331130
auth: Auth,
11341131
account_name: str,
11351132
api_endpoint: str,
1133+
use_engine_callback: Callable,
1134+
dynamic_use_database_callback: Callable,
11361135
):
11371136
"""
11381137
Test that we can switch between different databases using the same engine
11391138
while maintaining proper database context for each cursor.
11401139
"""
1140+
second_db_name = f"{db_name}_second"
1141+
11411142
# Mock HTTP calls
11421143
httpx_mock.add_callback(check_credentials_callback, url=auth_url, is_reusable=True)
11431144
httpx_mock.add_callback(
@@ -1146,6 +1147,9 @@ async def test_database_switching_with_same_engine(
11461147
is_reusable=True,
11471148
)
11481149

1150+
# Create dynamic callback for both databases
1151+
use_db_callback = dynamic_use_database_callback(db_name, second_db_name)
1152+
11491153
# Add USE DATABASE callbacks for both databases
11501154
httpx_mock.add_callback(
11511155
use_db_callback,

tests/unit/db/test_caching.py

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1259,12 +1259,16 @@ def system_engine_callback_counter(request, **kwargs):
12591259
is_reusable=True,
12601260
)
12611261

1262-
# Set environment variable to disable cache
1263-
env_patch = {}
1264-
if cache_disabled:
1265-
env_patch["FIREBOLT_SDK_DISABLE_CACHE"] = "true"
1262+
# Store original cache state
1263+
original_cache_disabled = _firebolt_cache.disabled
1264+
1265+
try:
1266+
# Set cache disabled state directly
1267+
if cache_disabled:
1268+
_firebolt_cache.disable()
1269+
else:
1270+
_firebolt_cache.enable()
12661271

1267-
with patch.dict(os.environ, env_patch):
12681272
# Create multiple connections to test caching behavior
12691273
for _ in range(3):
12701274
with connect(
@@ -1276,13 +1280,16 @@ def system_engine_callback_counter(request, **kwargs):
12761280
) as connection:
12771281
connection.cursor().execute("SELECT 1")
12781282

1279-
if cache_disabled:
1280-
# Each connection should call system engine when cache is disabled
1281-
assert (
1282-
system_engine_call_counter == 3
1283-
), "Cache disabled should not reuse system engine"
1284-
else:
1285-
# Only first connection should call system engine when cache is enabled
1286-
assert (
1287-
system_engine_call_counter == 1
1288-
), "Cache enabled should reuse system engine"
1283+
if cache_disabled:
1284+
# Each connection should call system engine when cache is disabled
1285+
assert (
1286+
system_engine_call_counter == 3
1287+
), "Cache disabled should not reuse system engine"
1288+
else:
1289+
# Only first connection should call system engine when cache is enabled
1290+
assert (
1291+
system_engine_call_counter == 1
1292+
), "Cache enabled should reuse system engine"
1293+
finally:
1294+
# Restore original cache state
1295+
_firebolt_cache.disabled = original_cache_disabled

0 commit comments

Comments
 (0)