|
| 1 | +"""Unit tests for convert_index_info_to_schema function.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from redisvl.redis.connection import convert_index_info_to_schema |
| 6 | + |
| 7 | + |
| 8 | +def test_convert_index_info_single_prefix(): |
| 9 | + """Test converting index info with a single prefix. |
| 10 | +
|
| 11 | + Single-element prefix lists are normalized to strings for backward compatibility. |
| 12 | + """ |
| 13 | + index_info = { |
| 14 | + "index_name": "test_index", |
| 15 | + "index_definition": [ |
| 16 | + "key_type", |
| 17 | + "HASH", |
| 18 | + "prefixes", |
| 19 | + ["prefix_a"], |
| 20 | + ], |
| 21 | + "attributes": [], |
| 22 | + } |
| 23 | + |
| 24 | + result = convert_index_info_to_schema(index_info) |
| 25 | + |
| 26 | + assert result["index"]["name"] == "test_index" |
| 27 | + assert result["index"]["prefix"] == "prefix_a" # Normalized to string |
| 28 | + assert result["index"]["storage_type"] == "hash" |
| 29 | + |
| 30 | + |
| 31 | +def test_convert_index_info_multiple_prefixes(): |
| 32 | + """Test converting index info with multiple prefixes (issue #258).""" |
| 33 | + index_info = { |
| 34 | + "index_name": "test_index", |
| 35 | + "index_definition": [ |
| 36 | + "key_type", |
| 37 | + "HASH", |
| 38 | + "prefixes", |
| 39 | + ["prefix_a", "prefix_b", "prefix_c"], |
| 40 | + ], |
| 41 | + "attributes": [], |
| 42 | + } |
| 43 | + |
| 44 | + result = convert_index_info_to_schema(index_info) |
| 45 | + |
| 46 | + assert result["index"]["name"] == "test_index" |
| 47 | + assert result["index"]["prefix"] == ["prefix_a", "prefix_b", "prefix_c"] |
| 48 | + assert result["index"]["storage_type"] == "hash" |
| 49 | + |
| 50 | + |
| 51 | +def test_convert_index_info_json_storage(): |
| 52 | + """Test converting index info with JSON storage type. |
| 53 | +
|
| 54 | + Single-element prefix lists are normalized to strings for backward compatibility. |
| 55 | + """ |
| 56 | + index_info = { |
| 57 | + "index_name": "test_json_index", |
| 58 | + "index_definition": [ |
| 59 | + "key_type", |
| 60 | + "JSON", |
| 61 | + "prefixes", |
| 62 | + ["json_prefix"], |
| 63 | + ], |
| 64 | + "attributes": [], |
| 65 | + } |
| 66 | + |
| 67 | + result = convert_index_info_to_schema(index_info) |
| 68 | + |
| 69 | + assert result["index"]["name"] == "test_json_index" |
| 70 | + assert result["index"]["prefix"] == "json_prefix" # Normalized to string |
| 71 | + assert result["index"]["storage_type"] == "json" |
| 72 | + |
| 73 | + |
| 74 | +def test_convert_index_info_with_fields(): |
| 75 | + """Test converting index info with field definitions.""" |
| 76 | + index_info = { |
| 77 | + "index_name": "test_index", |
| 78 | + "index_definition": [ |
| 79 | + "key_type", |
| 80 | + "HASH", |
| 81 | + "prefixes", |
| 82 | + ["prefix_a", "prefix_b"], |
| 83 | + ], |
| 84 | + "attributes": [ |
| 85 | + [ |
| 86 | + "identifier", |
| 87 | + "user", |
| 88 | + "attribute", |
| 89 | + "user", |
| 90 | + "type", |
| 91 | + "TAG", |
| 92 | + ], |
| 93 | + [ |
| 94 | + "identifier", |
| 95 | + "text", |
| 96 | + "attribute", |
| 97 | + "text", |
| 98 | + "type", |
| 99 | + "TEXT", |
| 100 | + ], |
| 101 | + ], |
| 102 | + } |
| 103 | + |
| 104 | + result = convert_index_info_to_schema(index_info) |
| 105 | + |
| 106 | + assert result["index"]["name"] == "test_index" |
| 107 | + assert result["index"]["prefix"] == ["prefix_a", "prefix_b"] |
| 108 | + assert len(result["fields"]) == 2 |
| 109 | + assert result["fields"][0]["name"] == "user" |
| 110 | + assert result["fields"][0]["type"] == "tag" |
| 111 | + assert result["fields"][1]["name"] == "text" |
| 112 | + assert result["fields"][1]["type"] == "text" |
0 commit comments