Skip to content

Commit bbb3dba

Browse files
committed
fixing database_id restriction
Signed-off-by: affonsov <67347924+affonsov@users.noreply.github.com>
1 parent 85540fc commit bbb3dba

File tree

3 files changed

+146
-12
lines changed

3 files changed

+146
-12
lines changed

python/glide-shared/glide_shared/config.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ class BaseClientConfiguration:
250250
connection failures.
251251
If not set, a default backoff strategy will be used.
252252
database_id (Optional[int]): Index of the logical database to connect to.
253+
Must be a non-negative integer. The valid range is determined by the server configuration.
253254
If not set, the client will connect to database 0.
254255
client_name (Optional[str]): Client name to be used for the client. Will be used with CLIENT SETNAME command
255256
during connection establishment.
@@ -322,8 +323,6 @@ def __init__(
322323
raise ValueError("database_id must be an integer")
323324
if database_id < 0:
324325
raise ValueError("database_id must be non-negative")
325-
if database_id > 15:
326-
raise ValueError("database_id must be less than or equal to 15")
327326

328327
if read_from == ReadFrom.AZ_AFFINITY and not client_az:
329328
raise ValueError(
@@ -462,6 +461,7 @@ class GlideClientConfiguration(BaseClientConfiguration):
462461
connection failures.
463462
If not set, a default backoff strategy will be used.
464463
database_id (Optional[int]): Index of the logical database to connect to.
464+
Must be a non-negative integer. The valid range is determined by the server configuration.
465465
If not set, the client will connect to database 0.
466466
client_name (Optional[str]): Client name to be used for the client. Will be used with CLIENT SETNAME command during
467467
connection establishment.
@@ -628,6 +628,7 @@ class GlideClusterClientConfiguration(BaseClientConfiguration):
628628
connection failures.
629629
If not set, a default backoff strategy will be used.
630630
database_id (Optional[int]): Index of the logical database to connect to.
631+
Must be a non-negative integer. The valid range is determined by the server configuration.
631632
If not set, the client will connect to database 0.
632633
client_name (Optional[str]): Client name to be used for the client. Will be used with CLIENT SETNAME command during
633634
connection establishment.

python/tests/test_config.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,13 @@ def test_database_id_validation_in_base_config():
210210
config = BaseClientConfiguration([NodeAddress("127.0.0.1")], database_id=15)
211211
assert config.database_id == 15
212212

213+
# Test broader range of database IDs
214+
config = BaseClientConfiguration([NodeAddress("127.0.0.1")], database_id=100)
215+
assert config.database_id == 100
216+
217+
config = BaseClientConfiguration([NodeAddress("127.0.0.1")], database_id=1000)
218+
assert config.database_id == 1000
219+
213220
# None should be allowed (defaults to 0)
214221
config = BaseClientConfiguration([NodeAddress("127.0.0.1")], database_id=None)
215222
assert config.database_id is None
@@ -218,11 +225,6 @@ def test_database_id_validation_in_base_config():
218225
with pytest.raises(ValueError, match="database_id must be non-negative"):
219226
BaseClientConfiguration([NodeAddress("127.0.0.1")], database_id=-1)
220227

221-
with pytest.raises(
222-
ValueError, match="database_id must be less than or equal to 15"
223-
):
224-
BaseClientConfiguration([NodeAddress("127.0.0.1")], database_id=16)
225-
226228
with pytest.raises(ValueError, match="database_id must be an integer"):
227229
BaseClientConfiguration([NodeAddress("127.0.0.1")], database_id="5")
228230

python/tests/test_database_id_integration.py

Lines changed: 136 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -244,11 +244,6 @@ async def test_invalid_database_id_configuration_error(self, request):
244244
with pytest.raises(ValueError, match="database_id must be non-negative"):
245245
await create_client(request, cluster_mode=False, database_id=-1)
246246

247-
with pytest.raises(
248-
ValueError, match="database_id must be less than or equal to 15"
249-
):
250-
await create_client(request, cluster_mode=False, database_id=16)
251-
252247
@pytest.mark.asyncio
253248
async def test_select_command_error_handling(self, request):
254249
"""Test error handling for SELECT command with invalid database numbers."""
@@ -326,3 +321,139 @@ async def test_explicit_database_zero_same_as_default(self, request):
326321
finally:
327322
await client_explicit.close()
328323
await client_default.close()
324+
325+
326+
class TestBroaderDatabaseIdRanges:
327+
"""Test database_id functionality with broader ranges beyond typical 0-15."""
328+
329+
@pytest.mark.asyncio
330+
async def test_standalone_client_with_higher_database_ids(self, request):
331+
"""Test creating standalone client with database IDs beyond typical range."""
332+
# Test with database_id = 50
333+
try:
334+
client = await create_client(request, cluster_mode=False, database_id=50)
335+
try:
336+
# Try to set a value - this may fail if server doesn't support DB 50
337+
await client.set("test_key_50", "test_value_50")
338+
result = await client.get("test_key_50")
339+
assert result == b"test_value_50"
340+
except RequestError as e:
341+
# Server-side validation should handle out-of-range database IDs
342+
assert "DB index is out of range" in str(
343+
e
344+
) or "invalid DB index" in str(e)
345+
finally:
346+
await client.close()
347+
except RequestError as e:
348+
# Connection-time error for invalid database
349+
assert "DB index is out of range" in str(e) or "invalid DB index" in str(e)
350+
351+
@pytest.mark.asyncio
352+
async def test_standalone_client_with_very_high_database_ids(self, request):
353+
"""Test creating standalone client with very high database IDs."""
354+
# Test with database_id = 999
355+
try:
356+
client = await create_client(request, cluster_mode=False, database_id=999)
357+
try:
358+
# Try to set a value - this should fail with server-side validation
359+
await client.set("test_key_999", "test_value_999")
360+
# If we get here, the server supports DB 999
361+
result = await client.get("test_key_999")
362+
assert result == b"test_value_999"
363+
except RequestError as e:
364+
# Expected: server-side validation should handle out-of-range database IDs
365+
assert "DB index is out of range" in str(
366+
e
367+
) or "invalid DB index" in str(e)
368+
finally:
369+
await client.close()
370+
except RequestError as e:
371+
# Connection-time error for invalid database
372+
assert "DB index is out of range" in str(e) or "invalid DB index" in str(e)
373+
374+
@pytest.mark.asyncio
375+
async def test_cluster_client_with_higher_database_ids(self, request):
376+
"""Test creating cluster client with database IDs beyond typical range."""
377+
# Check if cluster supports multi-DB
378+
supports_multi_db, skip_reason = await check_cluster_multi_db_support(request)
379+
if not supports_multi_db:
380+
pytest.skip(skip_reason)
381+
382+
# Test with database_id = 100
383+
try:
384+
client = await create_client(request, cluster_mode=True, database_id=100)
385+
try:
386+
# Try to set a value - this may fail if server doesn't support DB 100
387+
await client.set("test_key_100", "test_value_100")
388+
result = await client.get("test_key_100")
389+
assert result == b"test_value_100"
390+
except RequestError as e:
391+
# Server-side validation should handle out-of-range database IDs
392+
assert "DB index is out of range" in str(
393+
e
394+
) or "invalid DB index" in str(e)
395+
finally:
396+
await client.close()
397+
except RequestError as e:
398+
# Connection-time error for invalid database
399+
assert "DB index is out of range" in str(e) or "invalid DB index" in str(e)
400+
401+
402+
class TestServerSideValidation:
403+
"""Test that server-side validation is properly handled for out-of-range database IDs."""
404+
405+
@pytest.mark.asyncio
406+
async def test_server_side_validation_for_out_of_range_database_ids(self, request):
407+
"""Test that server-side validation errors are properly propagated."""
408+
# Try to connect with a database ID that's likely out of range
409+
very_high_db_id = 9999
410+
411+
try:
412+
client = await create_client(
413+
request, cluster_mode=False, database_id=very_high_db_id
414+
)
415+
try:
416+
# If connection succeeds, try an operation
417+
await client.set("test_key", "test_value")
418+
# If this succeeds, the server supports this database ID
419+
result = await client.get("test_key")
420+
assert result == b"test_value"
421+
except RequestError as e:
422+
# Expected: server should reject operations on invalid database
423+
assert "DB index is out of range" in str(
424+
e
425+
) or "invalid DB index" in str(e)
426+
finally:
427+
await client.close()
428+
except RequestError as e:
429+
# Expected: server should reject connection to invalid database
430+
assert "DB index is out of range" in str(e) or "invalid DB index" in str(e)
431+
432+
@pytest.mark.asyncio
433+
async def test_select_command_server_side_validation(self, request):
434+
"""Test that SELECT command with out-of-range database IDs is handled by server."""
435+
client = await create_client(request, cluster_mode=False, database_id=0)
436+
437+
try:
438+
# Try to select a database that's likely out of range
439+
with pytest.raises(RequestError, match="DB index is out of range"):
440+
await client.select(9999)
441+
finally:
442+
await client.close()
443+
444+
@pytest.mark.asyncio
445+
async def test_cluster_select_command_server_side_validation(self, request):
446+
"""Test that SELECT command in cluster mode with out-of-range database IDs is handled by server."""
447+
# Check if cluster supports multi-DB
448+
supports_multi_db, skip_reason = await check_cluster_multi_db_support(request)
449+
if not supports_multi_db:
450+
pytest.skip(skip_reason)
451+
452+
client = await create_client(request, cluster_mode=True, database_id=0)
453+
454+
try:
455+
# Try to select a database that's likely out of range
456+
with pytest.raises(RequestError, match="DB index is out of range"):
457+
await client.select(9999, route=AllNodes())
458+
finally:
459+
await client.close()

0 commit comments

Comments
 (0)