Skip to content

Commit d4697e2

Browse files
fix version checking and module checking logic
1 parent d73ce19 commit d4697e2

File tree

8 files changed

+139
-104
lines changed

8 files changed

+139
-104
lines changed

tests/conftest.py

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
import pytest
55
from testcontainers.compose import DockerCompose
66

7+
from redisvl.exceptions import RedisModuleVersionError
78
from redisvl.index.index import AsyncSearchIndex, SearchIndex
8-
from redisvl.redis.connection import RedisConnectionFactory
9+
from redisvl.redis.connection import RedisConnectionFactory, compare_versions
910
from redisvl.redis.utils import array_to_buffer
1011
from redisvl.utils.vectorize import HFTextVectorizer
1112

@@ -424,3 +425,77 @@ def hash_preprocess(item: dict) -> dict:
424425

425426
# run the test
426427
yield index
428+
429+
430+
# Version checking utilities
431+
def get_redis_version(client):
432+
"""Get Redis version from client info."""
433+
return client.info()["redis_version"]
434+
435+
436+
async def get_redis_version_async(client):
437+
"""Get Redis version from async client info."""
438+
info = await client.info()
439+
return info["redis_version"]
440+
441+
442+
def skip_if_redis_version_below(client, min_version: str, message: str = None):
443+
"""
444+
Skip test if Redis version is below minimum required.
445+
446+
Args:
447+
client: Redis client instance
448+
min_version: Minimum required Redis version
449+
message: Custom skip message
450+
"""
451+
redis_version = get_redis_version(client)
452+
if not compare_versions(redis_version, min_version):
453+
skip_msg = message or f"Redis version {redis_version} < {min_version} required"
454+
pytest.skip(skip_msg)
455+
456+
457+
async def skip_if_redis_version_below_async(
458+
client, min_version: str, message: str = None
459+
):
460+
"""
461+
Skip test if Redis version is below minimum required (async version).
462+
463+
Args:
464+
client: Async Redis client instance
465+
min_version: Minimum required Redis version
466+
message: Custom skip message
467+
"""
468+
redis_version = await get_redis_version_async(client)
469+
if not compare_versions(redis_version, min_version):
470+
skip_msg = message or f"Redis version {redis_version} < {min_version} required"
471+
pytest.skip(skip_msg)
472+
473+
474+
def skip_if_module_version_error(func, *args, **kwargs):
475+
"""
476+
Execute function and skip test if RedisModuleVersionError is raised.
477+
478+
Args:
479+
func: Function to execute
480+
*args: Arguments for the function
481+
**kwargs: Keyword arguments for the function
482+
"""
483+
try:
484+
return func(*args, **kwargs)
485+
except RedisModuleVersionError:
486+
pytest.skip("Required Redis modules not available or version too low")
487+
488+
489+
async def skip_if_module_version_error_async(func, *args, **kwargs):
490+
"""
491+
Execute async function and skip test if RedisModuleVersionError is raised.
492+
493+
Args:
494+
func: Async function to execute
495+
*args: Arguments for the function
496+
**kwargs: Keyword arguments for the function
497+
"""
498+
try:
499+
return await func(*args, **kwargs)
500+
except RedisModuleVersionError:
501+
pytest.skip("Required Redis modules not available or version too low")

tests/integration/test_aggregation.py

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
from redisvl.index import SearchIndex
66
from redisvl.query import HybridQuery
77
from redisvl.query.filter import FilterExpression, Geo, GeoRadius, Num, Tag, Text
8-
from redisvl.redis.connection import compare_versions
98
from redisvl.redis.utils import array_to_buffer
9+
from tests.conftest import skip_if_redis_version_below
1010

1111

1212
@pytest.fixture
@@ -60,9 +60,7 @@ def hash_preprocess(item: dict) -> dict:
6060

6161

6262
def test_aggregation_query(index):
63-
redis_version = index.client.info()["redis_version"]
64-
if not compare_versions(redis_version, "7.2.0"):
65-
pytest.skip("Not using a late enough version of Redis")
63+
skip_if_redis_version_below(index.client, "7.2.0")
6664

6765
text = "a medical professional with expertise in lung cancer"
6866
text_field = "description"
@@ -141,9 +139,7 @@ def test_empty_query_string():
141139

142140

143141
def test_aggregation_query_with_filter(index):
144-
redis_version = index.client.info()["redis_version"]
145-
if not compare_versions(redis_version, "7.2.0"):
146-
pytest.skip("Not using a late enough version of Redis")
142+
skip_if_redis_version_below(index.client, "7.2.0")
147143

148144
text = "a medical professional with expertise in lung cancer"
149145
text_field = "description"
@@ -169,9 +165,7 @@ def test_aggregation_query_with_filter(index):
169165

170166

171167
def test_aggregation_query_with_geo_filter(index):
172-
redis_version = index.client.info()["redis_version"]
173-
if not compare_versions(redis_version, "7.2.0"):
174-
pytest.skip("Not using a late enough version of Redis")
168+
skip_if_redis_version_below(index.client, "7.2.0")
175169

176170
text = "a medical professional with expertise in lung cancer"
177171
text_field = "description"
@@ -197,9 +191,7 @@ def test_aggregation_query_with_geo_filter(index):
197191

198192
@pytest.mark.parametrize("alpha", [0.1, 0.5, 0.9])
199193
def test_aggregate_query_alpha(index, alpha):
200-
redis_version = index.client.info()["redis_version"]
201-
if not compare_versions(redis_version, "7.2.0"):
202-
pytest.skip("Not using a late enough version of Redis")
194+
skip_if_redis_version_below(index.client, "7.2.0")
203195

204196
text = "a medical professional with expertise in lung cancer"
205197
text_field = "description"
@@ -226,9 +218,7 @@ def test_aggregate_query_alpha(index, alpha):
226218

227219

228220
def test_aggregate_query_stopwords(index):
229-
redis_version = index.client.info()["redis_version"]
230-
if not compare_versions(redis_version, "7.2.0"):
231-
pytest.skip("Not using a late enough version of Redis")
221+
skip_if_redis_version_below(index.client, "7.2.0")
232222

233223
text = "a medical professional with expertise in lung cancer"
234224
text_field = "description"
@@ -262,9 +252,7 @@ def test_aggregate_query_stopwords(index):
262252

263253

264254
def test_aggregate_query_with_text_filter(index):
265-
redis_version = index.client.info()["redis_version"]
266-
if not compare_versions(redis_version, "7.2.0"):
267-
pytest.skip("Not using a late enough version of Redis")
255+
skip_if_redis_version_below(index.client, "7.2.0")
268256

269257
text = "a medical professional with expertise in lung cancer"
270258
text_field = "description"

tests/integration/test_connection.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
)
1616
from redisvl.schema import IndexSchema
1717
from redisvl.version import __version__
18+
from tests.conftest import (
19+
skip_if_redis_version_below,
20+
skip_if_redis_version_below_async,
21+
)
1822

1923
EXPECTED_LIB_NAME = f"redis-py(redisvl_v{__version__})"
2024

@@ -166,38 +170,30 @@ def test_unknown_redis(self):
166170

167171

168172
def test_validate_redis(client):
169-
redis_version = client.info()["redis_version"]
170-
if not compare_versions(redis_version, "7.2.0"):
171-
pytest.skip("Not using a late enough version of Redis")
173+
skip_if_redis_version_below(client, "7.2.0")
172174
RedisConnectionFactory.validate_sync_redis(client)
173175
lib_name = client.client_info()
174176
assert lib_name["lib-name"] == EXPECTED_LIB_NAME
175177

176178

177179
@pytest.mark.asyncio
178180
async def test_validate_async_redis(async_client):
179-
redis_version = (await async_client.info())["redis_version"]
180-
if not compare_versions(redis_version, "7.2.0"):
181-
pytest.skip("Not using a late enough version of Redis")
181+
await skip_if_redis_version_below_async(async_client, "7.2.0")
182182
await RedisConnectionFactory.validate_async_redis(async_client)
183183
lib_name = await async_client.client_info()
184184
assert lib_name["lib-name"] == EXPECTED_LIB_NAME
185185

186186

187187
def test_validate_redis_custom_lib_name(client):
188-
redis_version = client.info()["redis_version"]
189-
if not compare_versions(redis_version, "7.2.0"):
190-
pytest.skip("Not using a late enough version of Redis")
188+
skip_if_redis_version_below(client, "7.2.0")
191189
RedisConnectionFactory.validate_sync_redis(client, "langchain_v0.1.0")
192190
lib_name = client.client_info()
193191
assert lib_name["lib-name"] == f"redis-py(redisvl_v{__version__};langchain_v0.1.0)"
194192

195193

196194
@pytest.mark.asyncio
197195
async def test_validate_async_redis_custom_lib_name(async_client):
198-
redis_version = (await async_client.info())["redis_version"]
199-
if not compare_versions(redis_version, "7.2.0"):
200-
pytest.skip("Not using a late enough version of Redis")
196+
await skip_if_redis_version_below_async(async_client, "7.2.0")
201197
await RedisConnectionFactory.validate_async_redis(async_client, "langchain_v0.1.0")
202198
lib_name = await async_client.client_info()
203199
assert lib_name["lib-name"] == f"redis-py(redisvl_v{__version__};langchain_v0.1.0)"

tests/integration/test_llmcache.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from redisvl.index.index import AsyncSearchIndex, SearchIndex
1414
from redisvl.query.filter import Num, Tag, Text
1515
from redisvl.utils.vectorize import HFTextVectorizer
16+
from tests.conftest import skip_if_module_version_error
1617

1718

1819
@pytest.fixture
@@ -935,21 +936,24 @@ def test_create_cache_with_different_vector_types(worker_id):
935936
for cache in [bfloat_cache, float16_cache, float32_cache, float64_cache]:
936937
cache.set_threshold(0.6)
937938
assert len(cache.check("float prompt", num_results=5)) == 1
938-
except:
939-
pytest.skip("Not using a late enough version of Redis")
939+
except RedisModuleVersionError:
940+
pytest.skip("Required Redis modules not available or version too low")
940941

941942

942943
def test_bad_dtype_connecting_to_existing_cache(redis_url, worker_id):
943-
try:
944-
cache = SemanticCache(
944+
def create_cache():
945+
return SemanticCache(
945946
name=f"float64_cache_{worker_id}", dtype="float64", redis_url=redis_url
946947
)
947-
same_type = SemanticCache(
948+
949+
def create_same_type():
950+
return SemanticCache(
948951
name=f"float64_cache_{worker_id}", dtype="float64", redis_url=redis_url
949952
)
950-
# under the hood uses from_existing
951-
except RedisModuleVersionError:
952-
pytest.skip("Not using a late enough version of Redis")
953+
954+
cache = skip_if_module_version_error(create_cache)
955+
same_type = skip_if_module_version_error(create_same_type)
956+
# under the hood uses from_existing
953957

954958
with pytest.raises(ValueError):
955959
bad_type = SemanticCache(

tests/integration/test_message_history.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from redisvl.exceptions import RedisModuleVersionError
77
from redisvl.extensions.constants import ID_FIELD_NAME
88
from redisvl.extensions.message_history import MessageHistory, SemanticMessageHistory
9-
from redisvl.utils.vectorize.text.huggingface import HFTextVectorizer
9+
from tests.conftest import skip_if_module_version_error
1010

1111

1212
@pytest.fixture
@@ -569,21 +569,24 @@ def test_different_vector_dtypes():
569569
for sess in [bfloat_sess, float16_sess, float32_sess, float64_sess]:
570570
sess.set_distance_threshold(0.7)
571571
assert len(sess.get_relevant("float message")) == 1
572-
except:
573-
pytest.skip("Not using a late enough version of Redis")
572+
except RedisModuleVersionError:
573+
pytest.skip("Required Redis modules not available or version too low")
574574

575575

576576
def test_bad_dtype_connecting_to_exiting_history(redis_url):
577-
try:
578-
history = SemanticMessageHistory(
577+
def create_history():
578+
return SemanticMessageHistory(
579579
name="float64 history", dtype="float64", redis_url=redis_url
580580
)
581-
same_type = SemanticMessageHistory(
581+
582+
def create_same_type():
583+
return SemanticMessageHistory(
582584
name="float64 history", dtype="float64", redis_url=redis_url
583585
)
584-
# under the hood uses from_existing
585-
except RedisModuleVersionError:
586-
pytest.skip("Not using a late enough version of Redis")
586+
587+
history = skip_if_module_version_error(create_history)
588+
same_type = skip_if_module_version_error(create_same_type)
589+
# under the hood uses from_existing
587590

588591
with pytest.raises(ValueError):
589592
bad_type = SemanticMessageHistory(

tests/integration/test_query.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
)
2525
from redisvl.redis.connection import compare_versions
2626
from redisvl.redis.utils import array_to_buffer
27+
from tests.conftest import skip_if_redis_version_below
2728

2829

2930
@pytest.fixture
@@ -962,12 +963,11 @@ def missing_fields_index(worker_id, redis_url):
962963
missing_index.create(overwrite=True)
963964

964965
# Skip all missing field tests if Redis version doesn't support INDEXMISSING/INDEXEMPTY (requires Redis 7.4+)
965-
redis_version = missing_index.client.info()["redis_version"]
966-
if not compare_versions(redis_version, "7.4.0"):
967-
missing_index.delete(drop=True) # Clean up before skipping
968-
pytest.skip(
969-
"INDEXMISSING/INDEXEMPTY features require Redis 7.4+ (RediSearch 2.10+)"
970-
)
966+
skip_if_redis_version_below(
967+
missing_index.client,
968+
"7.4.0",
969+
"INDEXMISSING/INDEXEMPTY features require Redis 7.4+ (RediSearch 2.10+)",
970+
)
971971

972972
# Load test data with different missing field scenarios
973973
test_data = [

0 commit comments

Comments
 (0)