Skip to content

Commit 0d25c17

Browse files
committed
fix: resolve VectorField syntax error in JsonModel and add unit tests
1 parent d0e3a68 commit 0d25c17

File tree

2 files changed

+85
-1
lines changed

2 files changed

+85
-1
lines changed

aredis_om/model/model.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2162,7 +2162,10 @@ def schema_for_type(
21622162
# a proper type, we can pull the type information from the origin of the first argument.
21632163
if not isinstance(typ, type):
21642164
type_args = typing_get_args(field_info.annotation)
2165-
typ = type_args[0].__origin__
2165+
if type_args and hasattr(type_args[0], "__origin__"):
2166+
typ = type_args[0].__origin__
2167+
else:
2168+
typ = type_args[0] if type_args else typ
21662169

21672170
# TODO: GEO field
21682171
if is_vector and vector_options:

tests/test_json_model.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import pytest
1414
import pytest_asyncio
15+
from redis.exceptions import ResponseError
1516

1617
from aredis_om import (
1718
EmbeddedJsonModel,
@@ -21,6 +22,7 @@
2122
NotFoundError,
2223
QueryNotSupportedError,
2324
RedisModelError,
25+
VectorFieldOptions,
2426
)
2527

2628
# We need to run this check as sync code (during tests) even in async mode
@@ -1173,3 +1175,82 @@ class Game(JsonModel):
11731175
)
11741176
print(q.query)
11751177
assert q.query == "(@player1_username:{username})| (@player2_username:{username})"
1178+
1179+
1180+
@py_test_mark_asyncio
1181+
def test_vector_field_definition(redis):
1182+
"""
1183+
Test the definition and behavior of a vector field in a JsonModel.
1184+
This test verifies:
1185+
1. The model schema includes "VECTOR" for a vector field with specified options.
1186+
2. Instances with vector fields can be saved and retrieved accurately.
1187+
3. Vector field values remain consistent after persistence.
1188+
1189+
Args:
1190+
redis: Redis connection fixture for testing.
1191+
"""
1192+
1193+
class Group(JsonModel):
1194+
articles: List[str]
1195+
tender_text: str = Field(index=False)
1196+
tender_embedding: List[float] = Field(
1197+
index=True,
1198+
vector_options=VectorFieldOptions(
1199+
algorithm=VectorFieldOptions.ALGORITHM.FLAT,
1200+
type=VectorFieldOptions.TYPE.FLOAT32,
1201+
dimension=3,
1202+
distance_metric=VectorFieldOptions.DISTANCE_METRIC.COSINE,
1203+
),
1204+
)
1205+
1206+
schema = Group.redisearch_schema()
1207+
assert "VECTOR" in schema
1208+
1209+
group = Group(
1210+
articles=["article_1", "article_2"],
1211+
tender_text="Sample text",
1212+
tender_embedding=[0.1, 0.2, 0.3],
1213+
)
1214+
group.save()
1215+
1216+
retrieved_group = await Group.get(group.pk)
1217+
assert retrieved_group.tender_embedding == [0.1, 0.2, 0.3]
1218+
1219+
retrieved_group = Group.get(group.pk)
1220+
assert retrieved_group.tender_embedding == [0.1, 0.2, 0.3]
1221+
1222+
1223+
def test_vector_field_schema_debug(redis):
1224+
"""
1225+
Test and debug the schema definition for a vector field in a JsonModel.
1226+
1227+
This test ensures:
1228+
1. The schema for a vector field is generated correctly.
1229+
2. No syntax errors occur when saving a model instance.
1230+
3. The Redis schema syntax is valid and debugged if issues arise.
1231+
1232+
Steps:
1233+
- Define a `TestModel` with a vector field `embedding`.
1234+
- Attempt to save an instance and print the schema.
1235+
- Handle and fail gracefully if Redis raises a ResponseError.
1236+
1237+
Args:
1238+
redis: Redis connection fixture for testing.
1239+
"""
1240+
1241+
class TestModel(JsonModel):
1242+
embedding: List[float] = Field(
1243+
index=True,
1244+
vector_options=VectorFieldOptions(
1245+
algorithm=VectorFieldOptions.ALGORITHM.FLAT,
1246+
type=VectorFieldOptions.TYPE.FLOAT32,
1247+
dimension=3,
1248+
distance_metric=VectorFieldOptions.DISTANCE_METRIC.COSINE,
1249+
),
1250+
)
1251+
1252+
try:
1253+
TestModel(embedding=[0.1, 0.2, 0.3]).save()
1254+
print(TestModel.redisearch_schema())
1255+
except ResponseError as e:
1256+
pytest.fail(f"Redis rejected the schema with error: {e}")

0 commit comments

Comments
 (0)