Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -161,42 +161,6 @@ def exec(
timeout=timeout,
)

def select(self, index: int) -> TOK:
"""
Change the currently selected database.

**WARNING**: This command is NOT RECOMMENDED for production use.
Upon reconnection, the client will revert to the database_id specified
in the client configuration (default: 0), NOT the database selected
via this command.

**RECOMMENDED APPROACH**: Use the database_id parameter in client
configuration instead:

```python
client = GlideClient.create_client(
GlideClientConfiguration(
addresses=[NodeAddress("localhost", 6379)],
database_id=5 # Recommended: persists across reconnections
)
)
```

**RECONNECTION BEHAVIOR**: After any reconnection (due to network issues,
timeouts, etc.), the client will automatically revert to the database_id
specified during client creation, losing any database selection made via
this SELECT command.

See [valkey.io](https://valkey.io/commands/select/) for details.

Args:
index (int): The index of the database to select.

Returns:
A simple OK response.
"""
return cast(TOK, self._execute_command(RequestType.Select, [str(index)]))

def config_resetstat(self) -> TOK:
"""
Resets the statistics reported by the server using the INFO and LATENCY HISTOGRAM commands.
Expand Down
57 changes: 22 additions & 35 deletions python/tests/sync_tests/test_sync_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -613,34 +613,21 @@ def test_sync_info_default(self, glide_sync_client: TGlideClient):
info_result = get_first_result(info_result)
assert b"# Memory" in info_result

@pytest.mark.parametrize("cluster_mode", [False])
@pytest.mark.parametrize("protocol", [ProtocolVersion.RESP2, ProtocolVersion.RESP3])
def test_sync_select(self, glide_sync_client: GlideClient):
assert glide_sync_client.select(0) == OK
key = get_random_string(10)
value = get_random_string(10)
assert glide_sync_client.set(key, value) == OK
assert glide_sync_client.get(key) == value.encode()
assert glide_sync_client.select(1) == OK
assert glide_sync_client.get(key) is None
assert glide_sync_client.select(0) == OK
assert glide_sync_client.get(key) == value.encode()

@pytest.mark.parametrize("cluster_mode", [False])
@pytest.mark.parametrize("protocol", [ProtocolVersion.RESP2, ProtocolVersion.RESP3])
def test_sync_move(self, glide_sync_client: GlideClient):
key = get_random_string(10)
value = get_random_string(10)

assert glide_sync_client.select(0) == OK
assert glide_sync_client.custom_command(["SELECT", "0"]) == OK
assert glide_sync_client.move(key, 1) is False

assert glide_sync_client.set(key, value) == OK
assert glide_sync_client.get(key) == value.encode()

assert glide_sync_client.move(key, 1) is True
assert glide_sync_client.get(key) is None
assert glide_sync_client.select(1) == OK
assert glide_sync_client.custom_command(["SELECT", "1"]) == OK
assert glide_sync_client.get(key) == value.encode()

with pytest.raises(RequestError):
Expand All @@ -652,15 +639,15 @@ def test_sync_move_with_bytes(self, glide_sync_client: GlideClient):
key = get_random_string(10)
value = get_random_string(10)

assert glide_sync_client.select(0) == OK
assert glide_sync_client.custom_command(["SELECT", "0"]) == OK

assert glide_sync_client.set(key, value) == OK
assert glide_sync_client.get(key.encode()) == value.encode()

assert glide_sync_client.move(key.encode(), 1) is True
assert glide_sync_client.get(key) is None
assert glide_sync_client.get(key.encode()) is None
assert glide_sync_client.select(1) == OK
assert glide_sync_client.custom_command(["SELECT", "1"]) == OK
assert glide_sync_client.get(key) == value.encode()

@pytest.mark.parametrize("cluster_mode", [True, False])
Expand Down Expand Up @@ -5210,7 +5197,7 @@ def test_sync_dbsize(self, glide_sync_client: TGlideClient):
assert glide_sync_client.set(key, value) == OK
assert glide_sync_client.dbsize(SlotKeyRoute(SlotType.PRIMARY, key)) == 1
else:
assert glide_sync_client.select(1) == OK
assert glide_sync_client.custom_command(["SELECT", "1"]) == OK
assert glide_sync_client.dbsize() == 0

@pytest.mark.parametrize("cluster_mode", [True, False])
Expand Down Expand Up @@ -8996,12 +8983,12 @@ def test_sync_standalone_flushdb(self, glide_sync_client: GlideClient):
value = get_random_string(5)

# fill DB 0 and check size non-empty
assert glide_sync_client.select(0) == OK
assert glide_sync_client.custom_command(["SELECT", "0"]) == OK
glide_sync_client.set(key1, value)
assert glide_sync_client.dbsize() > 0

# fill DB 1 and check size non-empty
assert glide_sync_client.select(1) == OK
assert glide_sync_client.custom_command(["SELECT", "1"]) == OK
glide_sync_client.set(key2, value)
assert glide_sync_client.dbsize() > 0

Expand All @@ -9010,7 +8997,7 @@ def test_sync_standalone_flushdb(self, glide_sync_client: GlideClient):
assert glide_sync_client.dbsize() == 0

# swith to DB 0, flush, and check
assert glide_sync_client.select(0) == OK
assert glide_sync_client.custom_command(["SELECT", "0"]) == OK
assert glide_sync_client.dbsize() > 0
assert glide_sync_client.flushdb(FlushMode.ASYNC) == OK
assert glide_sync_client.dbsize() == 0
Expand Down Expand Up @@ -9096,12 +9083,12 @@ def test_sync_copy_database(self, glide_sync_client: GlideClient):
value2 = get_random_string(5)
value1_encoded = value1.encode()
value2_encoded = value2.encode()
index0 = 0
index1 = 1
index2 = 2
index0 = "0"
index1 = "1"
index2 = "2"

try:
assert glide_sync_client.select(index0) == OK
assert glide_sync_client.custom_command(["SELECT", index0]) == OK

# neither key exists
assert (
Expand All @@ -9115,11 +9102,11 @@ def test_sync_copy_database(self, glide_sync_client: GlideClient):
glide_sync_client.copy(source, destination, index1, replace=False)
is True
)
assert glide_sync_client.select(index1) == OK
assert glide_sync_client.custom_command(["SELECT", index1]) == OK
assert glide_sync_client.get(destination) == value1_encoded

# new value for source key
assert glide_sync_client.select(index0) == OK
assert glide_sync_client.custom_command(["SELECT", index0]) == OK
glide_sync_client.set(source, value2)

# no REPLACE, copying to existing key on DB 0 & 1, non-existing key on DB 2
Expand All @@ -9133,25 +9120,25 @@ def test_sync_copy_database(self, glide_sync_client: GlideClient):
)

# new value only gets copied to DB 2
assert glide_sync_client.select(index1) == OK
assert glide_sync_client.custom_command(["SELECT", index1]) == OK
assert glide_sync_client.get(destination) == value1_encoded
assert glide_sync_client.select(index2) == OK
assert glide_sync_client.custom_command(["SELECT", index2]) == OK
assert glide_sync_client.get(destination) == value2_encoded

# both exists, with REPLACE, when value isn't the same, source always get copied to destination
assert glide_sync_client.select(index0) == OK
assert glide_sync_client.custom_command(["SELECT", index0]) == OK
assert (
glide_sync_client.copy(source, destination, index1, replace=True)
is True
)
assert glide_sync_client.select(index1) == OK
assert glide_sync_client.custom_command(["SELECT", index1]) == OK
assert glide_sync_client.get(destination) == value2_encoded

# invalid DB index
with pytest.raises(RequestError):
glide_sync_client.copy(source, destination, -1, replace=True)
finally:
assert glide_sync_client.select(0) == OK
assert glide_sync_client.custom_command(["SELECT", "0"]) == OK

@pytest.mark.parametrize("cluster_mode", [True, False])
@pytest.mark.parametrize("protocol", [ProtocolVersion.RESP2, ProtocolVersion.RESP3])
Expand Down Expand Up @@ -9240,9 +9227,9 @@ def test_sync_standalone_client_random_key(self, glide_sync_client: GlideClient)
key = get_random_string(10)

# setup: delete all keys in DB 0 and DB 1
assert glide_sync_client.select(0) == OK
assert glide_sync_client.custom_command(["SELECT", "0"]) == OK
assert glide_sync_client.flushdb(FlushMode.SYNC) == OK
assert glide_sync_client.select(1) == OK
assert glide_sync_client.custom_command(["SELECT", "1"]) == OK
assert glide_sync_client.flushdb(FlushMode.SYNC) == OK

# no keys exist so random_key returns None
Expand All @@ -9253,7 +9240,7 @@ def test_sync_standalone_client_random_key(self, glide_sync_client: GlideClient)
assert glide_sync_client.random_key() == key.encode()

# switch back to DB 0
assert glide_sync_client.select(0) == OK
assert glide_sync_client.custom_command(["SELECT", "0"]) == OK
# DB 0 should still have no keys, so random_key should still return None
assert glide_sync_client.random_key() is None

Expand Down
2 changes: 2 additions & 0 deletions python/tests/test_api_consistency.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"create_leaked_value",
"start_socket_listener_external",
"value_from_pointer",
"select",
],
"sync_only": [],
}
Expand All @@ -67,6 +68,7 @@
"async_only": [
"test_inflight_request_limit",
"test_statistics",
"test_select",
],
"sync_only": ["test_sync_fork"],
}
Expand Down
4 changes: 2 additions & 2 deletions python/tests/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ def create_client_config(
addresses=seed_nodes if addresses is None else addresses,
use_tls=use_tls,
credentials=credentials,
database_id=database_id, # Add database_id parameter
database_id=database_id,
client_name=client_name,
protocol=protocol,
request_timeout=timeout,
Expand Down Expand Up @@ -626,7 +626,7 @@ def create_sync_client_config(
addresses=seed_nodes if addresses is None else addresses,
use_tls=use_tls,
credentials=credentials,
database_id=database_id, # Add database_id parameter
database_id=database_id,
client_name=client_name,
protocol=protocol,
request_timeout=timeout,
Expand Down
Loading