diff --git a/python/python/glide/async_commands/server_modules/vss.py b/python/python/glide/async_commands/server_modules/vss.py index f122cf11a9..01df029ab8 100644 --- a/python/python/glide/async_commands/server_modules/vss.py +++ b/python/python/glide/async_commands/server_modules/vss.py @@ -1,39 +1,21 @@ -from typing import List, Optional, cast +from typing import List, cast from glide.glide_client import TGlideClient -from glide.constants import TEncodable +from glide.constants import TOK, TEncodable from glide.protobuf.command_request_pb2 import RequestType - -class CreateOptions: - def __init__( - self, - optionName: str, - optionValue: Optional[str] = None - ): - self.optionName = optionName - self.optionValue = optionValue - - def getCreateOptions(self) -> str: - args = [] - if self.optionName: - args.extend(self.optionName) - if self.optionValue: - args.extend(self.optionValue) - +from glide.async_commands.server_modules.vss_options import FtCreateOptions async def create( client: TGlideClient, indexName: TEncodable, - options: List[CreateOptions] + options: FtCreateOptions ): args: List[TEncodable] = [indexName] - for createOption in options: - args.extend(createOption.getCreateOptions()) - return cast(Optional[bytes], await client._execute_command(RequestType.FtCreate, args)) - + args.extend(options.getCreateOptions()) + return cast(TOK, await client._execute_command(RequestType.FtCreate, args)) async def info( client: TGlideClient, indexName: TEncodable, ): args: List[TEncodable] = [indexName] - return cast(Optional[bytes], await client._execute_command(RequestType.FtInfo, args)) + return cast(List[TEncodable], await client._execute_command(RequestType.FtInfo, args)) diff --git a/python/python/glide/async_commands/server_modules/vss_options.py b/python/python/glide/async_commands/server_modules/vss_options.py new file mode 100644 index 0000000000..0c7ef90404 --- /dev/null +++ b/python/python/glide/async_commands/server_modules/vss_options.py @@ -0,0 +1,69 @@ +from typing import Optional, List +from enum import Enum + +class FieldType(Enum): + TEXT = 1 + TAG = 2 + NUMERIC = 3 + GEO = 4 + VECTOR = 5 + GEOSHAPE = 6 + +class DataType(Enum): + HASH = 1 + JSON = 2 + +class FieldInfo: + def __init__( + self, + identifier: str, + alias: Optional[str], + type: FieldType, + isSortable: Optional[bool] = None, + isUnnormalized: Optional[bool] = None, + isNoIndex: Optional[bool] = None + ): + self.identifier = identifier + self.alias = alias + self.type = type + self.isSortable = isSortable + self.isUnnormalized = isUnnormalized + self.isNoIndex = isNoIndex + + def getFieldInfo(self) -> List[str]: + args = [] + if self.identifier: + args.append(self.identifier) + if self.alias: + args.append("AS") + args.append(self.alias) + if self.type: + args.append(self.type.name) + if self.isSortable: + args.append("SORTABLE") + if self.isUnnormalized: + args.append("UNF") + if self.isNoIndex: + args.append("NOINDEX") + return args + + +class FtCreateOptions: + def __init__( + self, + dataType: Optional[DataType], + fields: List[FieldInfo], + ): + self.dataType = dataType + self.fields = fields + + def getCreateOptions(self) -> List[str]: + args = [] + if self.dataType: + args.append("ON") + args.append(self.dataType.name) + if self.fields: + args.append("SCHEMA") + for fieldInfo in self.fields: + args = args + fieldInfo.getFieldInfo() + return args diff --git a/python/python/tests/tests_server_modules/test_json.py b/python/python/tests/tests_server_modules/test_json.py index 9a297ad143..41a4229c34 100644 --- a/python/python/tests/tests_server_modules/test_json.py +++ b/python/python/tests/tests_server_modules/test_json.py @@ -12,18 +12,8 @@ from glide.glide_client import TGlideClient from tests.test_async_client import get_random_string, parse_info_response -from glide.async_commands.server_modules import vss - @pytest.mark.asyncio class TestJson: - - @pytest.mark.parametrize("cluster_mode", [True, False]) - @pytest.mark.parametrize("protocol", [ProtocolVersion.RESP2, ProtocolVersion.RESP3]) - async def test_vss_info(self, glide_client: TGlideClient): - - index = "idx" - print("---------"+await vss.info(glide_client, index)) - @pytest.mark.parametrize("cluster_mode", [True, False]) @pytest.mark.parametrize("protocol", [ProtocolVersion.RESP2, ProtocolVersion.RESP3]) async def test_json_set_get(self, glide_client: TGlideClient): diff --git a/python/python/tests/tests_server_modules/test_vss.py b/python/python/tests/tests_server_modules/test_vss.py new file mode 100644 index 0000000000..63acab0b0c --- /dev/null +++ b/python/python/tests/tests_server_modules/test_vss.py @@ -0,0 +1,27 @@ +import pytest +from typing import List +from glide.async_commands.server_modules import vss +from glide.config import ProtocolVersion +from glide.glide_client import TGlideClient +from glide.async_commands.server_modules.vss_options import FtCreateOptions, DataType, FieldInfo, FieldType +from glide.constants import OK + +@pytest.mark.asyncio +class TestVss: + @pytest.mark.parametrize("cluster_mode", [True, False]) + @pytest.mark.parametrize("protocol", [ProtocolVersion.RESP2, ProtocolVersion.RESP3]) + async def test_vss_create(self, glide_client: TGlideClient): + index = "idx" + fields: List[FieldInfo] = [] + fieldInfo1: FieldInfo = FieldInfo("title", None, FieldType.TEXT, True) + fieldInfo2: FieldInfo = FieldInfo("published_at", None, FieldType.NUMERIC, True) + fieldInfo3: FieldInfo = FieldInfo("category", None, FieldType.TAG, True) + fields.append(fieldInfo1) + fields.append(fieldInfo2) + fields.append(fieldInfo3) + options: FtCreateOptions = FtCreateOptions(DataType.HASH, fields) + result = await vss.create(glide_client, index, options) + assert result == OK + + # print info command result + print(await vss.info(glide_client, index))