-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Fix python CI Signed-off-by: Yury-Fridlyand <[email protected]> Signed-off-by: Prateek Kumar <[email protected]> Co-authored-by: Prateek Kumar <[email protected]>
- Loading branch information
Showing
3 changed files
with
216 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
python/python/tests/tests_server_modules/search/test_ft_create.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
# 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, | ||
DistanceMetricType, | ||
Field, | ||
FtCreateOptions, | ||
NumericField, | ||
TextField, | ||
VectorAlgorithm, | ||
VectorField, | ||
VectorFieldAttributesHnsw, | ||
VectorType, | ||
) | ||
from glide.config import ProtocolVersion | ||
from glide.constants import OK, TEncodable | ||
from glide.glide_client import GlideClusterClient | ||
|
||
|
||
@pytest.mark.asyncio | ||
class TestFtCreate: | ||
@pytest.mark.parametrize("cluster_mode", [True]) | ||
@pytest.mark.parametrize("protocol", [ProtocolVersion.RESP2, ProtocolVersion.RESP3]) | ||
async def test_ft_create(self, glide_client: GlideClusterClient): | ||
fields: List[Field] = [] | ||
textFieldTitle: TextField = TextField("$title") | ||
numberField: NumericField = NumericField("$published_at") | ||
textFieldCategory: TextField = TextField("$category") | ||
fields.append(textFieldTitle) | ||
fields.append(numberField) | ||
fields.append(textFieldCategory) | ||
|
||
prefixes: List[TEncodable] = [] | ||
prefixes.append("blog:post:") | ||
|
||
# Create an index with multiple fields with Hash data type. | ||
index = str(uuid.uuid4()) | ||
result = await ft.create( | ||
glide_client, index, fields, FtCreateOptions(DataType.HASH, prefixes) | ||
) | ||
assert result == OK | ||
assert await ft.dropindex(glide_client, indexName=index) == OK | ||
|
||
# Create an index with multiple fields with JSON data type. | ||
index2 = str(uuid.uuid4()) | ||
result = await ft.create( | ||
glide_client, index2, fields, FtCreateOptions(DataType.JSON, prefixes) | ||
) | ||
assert result == OK | ||
assert await ft.dropindex(glide_client, indexName=index2) == OK | ||
|
||
# Create an index for vectors of size 2 | ||
# FT.CREATE hash_idx1 ON HASH PREFIX 1 hash: SCHEMA vec AS VEC VECTOR HNSW 6 DIM 2 TYPE FLOAT32 DISTANCE_METRIC L2 | ||
index3 = str(uuid.uuid4()) | ||
prefixes = [] | ||
prefixes.append("hash:") | ||
fields = [] | ||
vectorFieldHash: VectorField = VectorField( | ||
name="vec", | ||
algorithm=VectorAlgorithm.HNSW, | ||
attributes=VectorFieldAttributesHnsw( | ||
dim=2, distance_metric=DistanceMetricType.L2, type=VectorType.FLOAT32 | ||
), | ||
alias="VEC", | ||
) | ||
fields.append(vectorFieldHash) | ||
|
||
result = await ft.create( | ||
glide_client, index3, fields, FtCreateOptions(DataType.HASH, prefixes) | ||
) | ||
assert result == OK | ||
assert await ft.dropindex(glide_client, indexName=index3) == OK | ||
|
||
# Create a 6-dimensional JSON index using the HNSW algorithm | ||
# FT.CREATE json_idx1 ON JSON PREFIX 1 json: SCHEMA $.vec AS VEC VECTOR HNSW 6 DIM 6 TYPE FLOAT32 DISTANCE_METRIC L2 | ||
index4 = str(uuid.uuid4()) | ||
prefixes = [] | ||
prefixes.append("json:") | ||
fields = [] | ||
vectorFieldJson: VectorField = VectorField( | ||
name="$.vec", | ||
algorithm=VectorAlgorithm.HNSW, | ||
attributes=VectorFieldAttributesHnsw( | ||
dim=6, distance_metric=DistanceMetricType.L2, type=VectorType.FLOAT32 | ||
), | ||
alias="VEC", | ||
) | ||
fields.append(vectorFieldJson) | ||
|
||
result = await ft.create( | ||
glide_client, index4, fields, FtCreateOptions(DataType.JSON, prefixes) | ||
) | ||
assert result == OK | ||
assert await ft.dropindex(glide_client, indexName=index4) == OK | ||
|
||
# Create an index without FtCreateOptions | ||
|
||
index5 = str(uuid.uuid4()) | ||
result = await ft.create(glide_client, index5, fields, FtCreateOptions()) | ||
assert result == OK | ||
assert await ft.dropindex(glide_client, indexName=index5) == OK | ||
|
||
# TO-DO: | ||
# Add additional tests from VSS documentation that require a combination of commands to run. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
# 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 | ||
assert await ft.dropindex(glide_client, indexName=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 | ||
assert await ft.dropindex(glide_client, indexName=indexNameString) == 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 | ||
|
||
assert await ft.dropindex(glide_client, indexName=indexName) == 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 | ||
) | ||
|
||
assert await ft.dropindex(glide_client, indexName=indexName) == OK | ||
assert await ft.dropindex(glide_client, indexName=newIndexName) == 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 |