Skip to content

Commit

Permalink
Ft.Create command test added
Browse files Browse the repository at this point in the history
Signed-off-by: Prateek Kumar <prateek.kumar@improving.com>
  • Loading branch information
prateek-kumar-improving committed Sep 27, 2024
1 parent 915d0cb commit cfa2ce3
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 35 deletions.
32 changes: 7 additions & 25 deletions python/python/glide/async_commands/server_modules/vss.py
Original file line number Diff line number Diff line change
@@ -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))
69 changes: 69 additions & 0 deletions python/python/glide/async_commands/server_modules/vss_options.py
Original file line number Diff line number Diff line change
@@ -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
10 changes: 0 additions & 10 deletions python/python/tests/tests_server_modules/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
27 changes: 27 additions & 0 deletions python/python/tests/tests_server_modules/test_vss.py
Original file line number Diff line number Diff line change
@@ -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))

0 comments on commit cfa2ce3

Please sign in to comment.