|
| 1 | +""" |
| 2 | +Test for IndexSchema type annotation issue. |
| 3 | +TDD test file for issue #361. |
| 4 | +
|
| 5 | +This test verifies that the IndexSchema fields type annotation |
| 6 | +matches what the validator actually expects. |
| 7 | +""" |
| 8 | + |
| 9 | +from typing import Dict, List |
| 10 | + |
| 11 | +import pytest |
| 12 | + |
| 13 | +from redisvl.schema import IndexSchema |
| 14 | +from redisvl.schema.fields import BaseField, TagField, TextField |
| 15 | + |
| 16 | + |
| 17 | +class TestIndexSchemaTypeIssue: |
| 18 | + """Test IndexSchema type annotation and validation consistency.""" |
| 19 | + |
| 20 | + def test_fields_as_list_works(self): |
| 21 | + """Test that fields as a list (what validator expects) works.""" |
| 22 | + schema_dict = { |
| 23 | + "index": { |
| 24 | + "name": "test_index", |
| 25 | + "prefix": "test", |
| 26 | + "storage_type": "hash", |
| 27 | + }, |
| 28 | + "fields": [ |
| 29 | + {"name": "text_field", "type": "text"}, |
| 30 | + {"name": "tag_field", "type": "tag"}, |
| 31 | + ], |
| 32 | + } |
| 33 | + |
| 34 | + # This should work since validator expects list |
| 35 | + schema = IndexSchema.from_dict(schema_dict) |
| 36 | + assert schema is not None |
| 37 | + assert isinstance(schema.fields, dict) # After processing, it becomes a dict |
| 38 | + assert "text_field" in schema.fields |
| 39 | + assert "tag_field" in schema.fields |
| 40 | + |
| 41 | + def test_fields_as_dict_should_work_per_type_annotation(self): |
| 42 | + """Test that fields as dict (per type annotation) now works after fix.""" |
| 43 | + schema_dict = { |
| 44 | + "index": { |
| 45 | + "name": "test_index", |
| 46 | + "prefix": "test", |
| 47 | + "storage_type": "hash", |
| 48 | + }, |
| 49 | + "fields": { |
| 50 | + "text_field": {"name": "text_field", "type": "text"}, |
| 51 | + "tag_field": {"name": "tag_field", "type": "tag"}, |
| 52 | + }, |
| 53 | + } |
| 54 | + |
| 55 | + # According to type annotation `fields: Dict[str, BaseField] = {}` |
| 56 | + # this should work, and now it does after fixing the validator |
| 57 | + schema = IndexSchema.from_dict(schema_dict) |
| 58 | + assert schema is not None |
| 59 | + assert isinstance(schema.fields, dict) |
| 60 | + assert "text_field" in schema.fields |
| 61 | + assert "tag_field" in schema.fields |
| 62 | + |
| 63 | + def test_type_annotation_matches_runtime_behavior(self): |
| 64 | + """Test that the type annotation should match what the code actually accepts.""" |
| 65 | + # Get the type annotation |
| 66 | + from typing import get_type_hints |
| 67 | + |
| 68 | + hints = get_type_hints(IndexSchema) |
| 69 | + fields_type = hints.get("fields") |
| 70 | + |
| 71 | + # The annotation says Dict[str, BaseField] |
| 72 | + assert fields_type == Dict[str, BaseField] |
| 73 | + |
| 74 | + # But the validator expects List and converts to Dict |
| 75 | + # This is the inconsistency reported in issue #361 |
| 76 | + |
| 77 | + def test_fields_empty_list_works(self): |
| 78 | + """Test that empty fields list works.""" |
| 79 | + schema_dict = { |
| 80 | + "index": { |
| 81 | + "name": "test_index", |
| 82 | + "prefix": "test", |
| 83 | + "storage_type": "hash", |
| 84 | + }, |
| 85 | + "fields": [], # Empty list should work |
| 86 | + } |
| 87 | + |
| 88 | + schema = IndexSchema.from_dict(schema_dict) |
| 89 | + assert schema is not None |
| 90 | + assert schema.fields == {} |
| 91 | + |
| 92 | + def test_fields_default_value(self): |
| 93 | + """Test default value for fields.""" |
| 94 | + schema_dict = { |
| 95 | + "index": { |
| 96 | + "name": "test_index", |
| 97 | + "prefix": "test", |
| 98 | + "storage_type": "hash", |
| 99 | + }, |
| 100 | + # No fields provided |
| 101 | + } |
| 102 | + |
| 103 | + schema = IndexSchema.from_dict(schema_dict) |
| 104 | + assert schema is not None |
| 105 | + assert schema.fields == {} # Default should be empty dict |
| 106 | + |
| 107 | + def test_direct_instantiation_with_dict_fields(self): |
| 108 | + """Test direct instantiation with dict fields (should work after fix).""" |
| 109 | + # After processing, fields are stored as Dict[str, BaseField] |
| 110 | + # So direct instantiation with proper dict should work |
| 111 | + |
| 112 | + # Create fields |
| 113 | + text_field = TextField(name="text_field") |
| 114 | + tag_field = TagField(name="tag_field") |
| 115 | + |
| 116 | + # Try to create schema with fields as dict |
| 117 | + # This is what the type annotation suggests should work |
| 118 | + schema = IndexSchema( |
| 119 | + index={ |
| 120 | + "name": "test_index", |
| 121 | + "prefix": "test", |
| 122 | + "storage_type": "hash", |
| 123 | + }, |
| 124 | + fields={"text_field": text_field, "tag_field": tag_field}, |
| 125 | + ) |
| 126 | + assert schema is not None |
| 127 | + assert isinstance(schema.fields, dict) |
| 128 | + assert "text_field" in schema.fields |
| 129 | + assert "tag_field" in schema.fields |
| 130 | + |
| 131 | + def test_yaml_format_fields_as_list(self): |
| 132 | + """Test that YAML format uses fields as list.""" |
| 133 | + yaml_content = """ |
| 134 | + index: |
| 135 | + name: test_index |
| 136 | + prefix: test |
| 137 | + storage_type: hash |
| 138 | + fields: |
| 139 | + - name: text_field |
| 140 | + type: text |
| 141 | + - name: tag_field |
| 142 | + type: tag |
| 143 | + """ |
| 144 | + |
| 145 | + # YAML format uses list, which is what validator expects |
| 146 | + # This test documents the expected YAML format |
| 147 | + import yaml |
| 148 | + |
| 149 | + schema_dict = yaml.safe_load(yaml_content) |
| 150 | + assert isinstance(schema_dict["fields"], list) |
| 151 | + |
| 152 | + schema = IndexSchema.from_dict(schema_dict) |
| 153 | + assert schema is not None |
| 154 | + assert isinstance(schema.fields, dict) |
| 155 | + |
| 156 | + def test_dict_fields_with_name_mismatch_fails(self): |
| 157 | + """Test that dict fields with mismatched names fail properly.""" |
| 158 | + schema_dict = { |
| 159 | + "index": { |
| 160 | + "name": "test_index", |
| 161 | + "prefix": "test", |
| 162 | + "storage_type": "hash", |
| 163 | + }, |
| 164 | + "fields": { |
| 165 | + "wrong_key": {"name": "correct_name", "type": "text"}, # Key != name |
| 166 | + }, |
| 167 | + } |
| 168 | + |
| 169 | + with pytest.raises(ValueError, match="Field name mismatch"): |
| 170 | + IndexSchema.from_dict(schema_dict) |
| 171 | + |
| 172 | + def test_dict_fields_with_invalid_field_type_fails(self): |
| 173 | + """Test that dict fields with invalid field types fail properly.""" |
| 174 | + schema_dict = { |
| 175 | + "index": { |
| 176 | + "name": "test_index", |
| 177 | + "prefix": "test", |
| 178 | + "storage_type": "hash", |
| 179 | + }, |
| 180 | + "fields": { |
| 181 | + "text_field": "invalid_field_type", # Should be dict or BaseField |
| 182 | + }, |
| 183 | + } |
| 184 | + |
| 185 | + with pytest.raises(ValueError, match="Invalid field type"): |
| 186 | + IndexSchema.from_dict(schema_dict) |
0 commit comments