Skip to content

Commit

Permalink
Python: Add commands FT.ALIASADD, FT.ALIASDEL, FT.ALIASUPDATE (v…
Browse files Browse the repository at this point in the history
…alkey-io#2471)

* Python: Add commands FT.ALIASADD, FT.ALIASDEL, FT.ALIASUPDATE

---------

Signed-off-by: Prateek Kumar <prateek.kumar@improving.com>
  • Loading branch information
prateek-kumar-improving authored and avifenesh committed Oct 21, 2024
1 parent 6fa6305 commit e472986
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#### Changes
* Python: Add commands FT.ALIASADD, FT.ALIASDEL, FT.ALIASUPDATE([#2471](https://github.com/valkey-io/valkey-glide/pull/2471))
* Python: Python FT.DROPINDEX command ([#2437](https://github.com/valkey-io/valkey-glide/pull/2437))
* Python: Python: Added FT.CREATE command([#2413](https://github.com/valkey-io/valkey-glide/pull/2413))
* Python: Add JSON.ARRLEN command ([#2403](https://github.com/valkey-io/valkey-glide/pull/2403))
Expand Down
66 changes: 66 additions & 0 deletions python/python/glide/async_commands/server_modules/ft.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,69 @@ async def dropindex(client: TGlideClient, indexName: TEncodable) -> TOK:
"""
args: List[TEncodable] = [CommandNames.FT_DROPINDEX, indexName]
return cast(TOK, await client.custom_command(args))


async def aliasadd(
client: TGlideClient, alias: TEncodable, indexName: TEncodable
) -> TOK:
"""
Add an alias for an index. The new alias name can be used anywhere that an index name is required.
Args:
client (TGlideClient): The client to execute the command.
alias (TEncodable): The alias to be added to an index.
indexName (TEncodable): The index name for which the alias has to be added.
Returns:
TOK: A simple "OK" response.
Examples:
>>> from glide.async_commands.server_modules import ft
>>> result = await ft.aliasadd(glide_client, "myalias", "myindex")
'OK' # Indicates the successful addition of the alias named "myalias" for the index.
"""
args: List[TEncodable] = [CommandNames.FT_ALIASADD, alias, indexName]
return cast(TOK, await client.custom_command(args))


async def aliasdel(client: TGlideClient, alias: TEncodable) -> TOK:
"""
Delete an existing alias for an index.
Args:
client (TGlideClient): The client to execute the command.
alias (TEncodable): The exisiting alias to be deleted for an index.
Returns:
TOK: A simple "OK" response.
Examples:
>>> from glide.async_commands.server_modules import ft
>>> result = await ft.aliasdel(glide_client, "myalias")
'OK' # Indicates the successful deletion of the alias named "myalias"
"""
args: List[TEncodable] = [CommandNames.FT_ALIASDEL, alias]
return cast(TOK, await client.custom_command(args))


async def aliasupdate(
client: TGlideClient, alias: TEncodable, indexName: TEncodable
) -> TOK:
"""
Update an existing alias to point to a different physical index. This command only affects future references to the alias.
Args:
client (TGlideClient): The client to execute the command.
alias (TEncodable): The alias name. This alias will now be pointed to a different index.
indexName (TEncodable): The index name for which an existing alias has to updated.
Returns:
TOK: A simple "OK" response.
Examples:
>>> from glide.async_commands.server_modules import ft
>>> result = await ft.aliasupdate(glide_client, "myalias", "myindex")
'OK' # Indicates the successful update of the alias to point to the index named "myindex"
"""
args: List[TEncodable] = [CommandNames.FT_ALIASUPDATE, alias, indexName]
return cast(TOK, await client.custom_command(args))
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ class CommandNames:

FT_CREATE = "FT.CREATE"
FT_DROPINDEX = "FT.DROPINDEX"
FT_ALIASADD = "FT.ALIASADD"
FT_ALIASDEL = "FT.ALIASDEL"
FT_ALIASUPDATE = "FT.ALIASUPDATE"


class FtCreateKeywords:
Expand Down
97 changes: 97 additions & 0 deletions python/python/tests/tests_server_modules/test_ft.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# Copyright Valkey GLIDE Project Contributors - SPDX Identifier: Apache-2.0
import uuid
from typing import List

import pytest
from glide.async_commands.server_modules import ft
from glide.async_commands.server_modules.ft_options.ft_create_options import (
DataType,
Field,
FtCreateOptions,
TextField,
)
from glide.config import ProtocolVersion
from glide.constants import OK, TEncodable
from glide.exceptions import RequestError
from glide.glide_client import GlideClusterClient


@pytest.mark.asyncio
class TestFt:
@pytest.mark.parametrize("cluster_mode", [True])
@pytest.mark.parametrize("protocol", [ProtocolVersion.RESP2, ProtocolVersion.RESP3])
async def test_ft_aliasadd(self, glide_client: GlideClusterClient):
indexName: str = str(uuid.uuid4())
alias: str = "alias"
# Test ft.aliasadd throws an error if index does not exist.
with pytest.raises(RequestError):
await ft.aliasadd(glide_client, alias, indexName)

# Test ft.aliasadd successfully adds an alias to an existing index.
await TestFt.create_test_index_hash_type(self, glide_client, indexName)
assert await ft.aliasadd(glide_client, alias, indexName) == OK

# Test ft.aliasadd for input of bytes type.
indexNameString = str(uuid.uuid4())
indexNameBytes = bytes(indexNameString, "utf-8")
aliasNameBytes = b"alias-bytes"
await TestFt.create_test_index_hash_type(self, glide_client, indexNameString)
assert await ft.aliasadd(glide_client, aliasNameBytes, indexNameBytes) == OK

@pytest.mark.parametrize("cluster_mode", [True])
@pytest.mark.parametrize("protocol", [ProtocolVersion.RESP2, ProtocolVersion.RESP3])
async def test_ft_aliasdel(self, glide_client: GlideClusterClient):
indexName: TEncodable = str(uuid.uuid4())
alias: str = "alias"
await TestFt.create_test_index_hash_type(self, glide_client, indexName)

# Test if deleting a non existent alias throws an error.
with pytest.raises(RequestError):
await ft.aliasdel(glide_client, alias)

# Test if an existing alias is deleted successfully.
assert await ft.aliasadd(glide_client, alias, indexName) == OK
assert await ft.aliasdel(glide_client, alias) == OK

# Test if an existing alias is deleted successfully for bytes type input.
assert await ft.aliasadd(glide_client, alias, indexName) == OK
assert await ft.aliasdel(glide_client, bytes(alias, "utf-8")) == OK

@pytest.mark.parametrize("cluster_mode", [True])
@pytest.mark.parametrize("protocol", [ProtocolVersion.RESP2, ProtocolVersion.RESP3])
async def test_ft_aliasupdate(self, glide_client: GlideClusterClient):
indexName: str = str(uuid.uuid4())
alias: str = "alias"
await TestFt.create_test_index_hash_type(self, glide_client, indexName)
assert await ft.aliasadd(glide_client, alias, indexName) == OK
newAliasName: str = "newAlias"
newIndexName: str = str(uuid.uuid4())

await TestFt.create_test_index_hash_type(self, glide_client, newIndexName)
assert await ft.aliasadd(glide_client, newAliasName, newIndexName) == OK

# Test if updating an already existing alias to point to an existing index returns "OK".
assert await ft.aliasupdate(glide_client, newAliasName, indexName) == OK
assert (
await ft.aliasupdate(
glide_client, bytes(alias, "utf-8"), bytes(newIndexName, "utf-8")
)
== OK
)

async def create_test_index_hash_type(
self, glide_client: GlideClusterClient, index_name: TEncodable
):
# Helper function used for creating a basic index with hash data type with one text field.
fields: List[Field] = []
text_field_title: TextField = TextField("$title")
fields.append(text_field_title)

prefix = "{json-search-" + str(uuid.uuid4()) + "}:"
prefixes: List[TEncodable] = []
prefixes.append(prefix)

result = await ft.create(
glide_client, index_name, fields, FtCreateOptions(DataType.HASH, prefixes)
)
assert result == OK

0 comments on commit e472986

Please sign in to comment.