|
| 1 | +""" |
| 2 | +Tests ClusterPipeline |
| 3 | +""" |
| 4 | + |
| 5 | +import pytest |
| 6 | +from redis.cluster import RedisCluster |
| 7 | +from redis.commands.helpers import get_protocol_version |
| 8 | + |
| 9 | +from redisvl.index import SearchIndex |
| 10 | +from redisvl.schema import IndexSchema |
| 11 | + |
| 12 | + |
| 13 | +@pytest.mark.requires_cluster |
| 14 | +def test_real_cluster_pipeline_get_protocol_version(redis_cluster_url): |
| 15 | + """ |
| 16 | + Test that get_protocol_version works with ClusterPipeline |
| 17 | + """ |
| 18 | + # Create REAL Redis Cluster client |
| 19 | + cluster_client = RedisCluster.from_url(redis_cluster_url) |
| 20 | + |
| 21 | + # Create REAL pipeline from cluster |
| 22 | + pipeline = cluster_client.pipeline() |
| 23 | + |
| 24 | + # This is the actual line that was failing in issue #365 |
| 25 | + # If our fix works, this should NOT raise AttributeError |
| 26 | + protocol = get_protocol_version(pipeline) |
| 27 | + |
| 28 | + # Protocol should be a string ("2" or "3") or None |
| 29 | + assert protocol in [None, "2", "3", 2, 3], f"Unexpected protocol: {protocol}" |
| 30 | + |
| 31 | + # Clean up |
| 32 | + cluster_client.close() |
| 33 | + |
| 34 | + |
| 35 | +@pytest.mark.requires_cluster |
| 36 | +def test_real_searchindex_with_cluster_batch_operations(redis_cluster_url): |
| 37 | + """ |
| 38 | + Test SearchIndex.load() with Redis Cluster. |
| 39 | + """ |
| 40 | + # Create schema like the user had |
| 41 | + schema_dict = { |
| 42 | + "index": {"name": "test-real-365", "prefix": "doc", "storage_type": "hash"}, |
| 43 | + "fields": [ |
| 44 | + {"name": "id", "type": "tag"}, |
| 45 | + {"name": "text", "type": "text"}, |
| 46 | + ], |
| 47 | + } |
| 48 | + |
| 49 | + schema = IndexSchema.from_dict(schema_dict) |
| 50 | + |
| 51 | + # Create SearchIndex with REAL cluster URL |
| 52 | + index = SearchIndex(schema, redis_url=redis_cluster_url) |
| 53 | + |
| 54 | + # Create the index |
| 55 | + index.create(overwrite=True) |
| 56 | + |
| 57 | + try: |
| 58 | + # Test data like user had |
| 59 | + test_data = [{"id": f"item{i}", "text": f"Document {i}"} for i in range(10)] |
| 60 | + |
| 61 | + # See issue #365 |
| 62 | + # index.load() with batch_size triggers pipeline operations internally |
| 63 | + keys = index.load( |
| 64 | + data=test_data, |
| 65 | + id_field="id", |
| 66 | + batch_size=3, # Forces multiple pipeline operations |
| 67 | + ) |
| 68 | + |
| 69 | + assert len(keys) == 10 |
| 70 | + assert all(k.startswith("doc:") for k in keys) |
| 71 | + |
| 72 | + finally: |
| 73 | + # Clean up |
| 74 | + index.delete() |
| 75 | + |
| 76 | + |
| 77 | +@pytest.mark.requires_cluster |
| 78 | +def test_cluster_pipeline_protocol_version_directly(): |
| 79 | + """ |
| 80 | + Test get_protocol_version with various cluster configurations. |
| 81 | + """ |
| 82 | + import os |
| 83 | + |
| 84 | + # Skip if no cluster available |
| 85 | + cluster_url = os.getenv("REDIS_CLUSTER_URL", "redis://localhost:7000") |
| 86 | + |
| 87 | + try: |
| 88 | + # Test with default protocol |
| 89 | + cluster = RedisCluster.from_url(cluster_url) |
| 90 | + pipeline = cluster.pipeline() |
| 91 | + |
| 92 | + # This should work without AttributeError |
| 93 | + protocol = get_protocol_version(pipeline) |
| 94 | + print(f"Protocol version from real cluster pipeline: {protocol}") |
| 95 | + |
| 96 | + cluster.close() |
| 97 | + |
| 98 | + # Test with explicit RESP2 |
| 99 | + cluster2 = RedisCluster.from_url(cluster_url, protocol=2) |
| 100 | + pipeline2 = cluster2.pipeline() |
| 101 | + protocol2 = get_protocol_version(pipeline2) |
| 102 | + assert protocol2 in [2, "2", None] |
| 103 | + cluster2.close() |
| 104 | + |
| 105 | + # Test with explicit RESP3 |
| 106 | + cluster3 = RedisCluster.from_url(cluster_url, protocol=3) |
| 107 | + pipeline3 = cluster3.pipeline() |
| 108 | + protocol3 = get_protocol_version(pipeline3) |
| 109 | + assert protocol3 in [3, "3", None] |
| 110 | + cluster3.close() |
| 111 | + |
| 112 | + except Exception as e: |
| 113 | + pytest.skip(f"Redis Cluster not available: {e}") |
| 114 | + |
| 115 | + |
| 116 | +@pytest.mark.requires_cluster |
| 117 | +def test_batch_search_with_real_cluster(redis_cluster_url): |
| 118 | + """ |
| 119 | + Test batch_search which uses get_protocol_version internally. |
| 120 | + """ |
| 121 | + from redisvl.query import FilterQuery |
| 122 | + |
| 123 | + schema_dict = { |
| 124 | + "index": {"name": "test-batch-365", "prefix": "batch", "storage_type": "json"}, |
| 125 | + "fields": [ |
| 126 | + {"name": "id", "type": "tag"}, |
| 127 | + {"name": "category", "type": "tag"}, |
| 128 | + ], |
| 129 | + } |
| 130 | + |
| 131 | + schema = IndexSchema.from_dict(schema_dict) |
| 132 | + index = SearchIndex(schema, redis_url=redis_cluster_url) |
| 133 | + |
| 134 | + index.create(overwrite=True) |
| 135 | + |
| 136 | + try: |
| 137 | + # Load test data |
| 138 | + data = [{"id": f"doc{i}", "category": f"cat{i % 3}"} for i in range(15)] |
| 139 | + index.load(data=data, id_field="id") |
| 140 | + |
| 141 | + # Create multiple queries |
| 142 | + queries = [ |
| 143 | + FilterQuery(filter_expression=f"@category:{{cat{i}}}") for i in range(3) |
| 144 | + ] |
| 145 | + |
| 146 | + # batch_search internally uses get_protocol_version on pipelines |
| 147 | + results = index.batch_search( |
| 148 | + [(q.query, q.params) for q in queries], batch_size=2 |
| 149 | + ) |
| 150 | + |
| 151 | + assert len(results) == 3 |
| 152 | + |
| 153 | + finally: |
| 154 | + index.delete() |
0 commit comments