|
| 1 | +import pytest |
| 2 | +from pydantic import ValidationError |
| 3 | +from redis.commands.search.field import ( |
| 4 | + GeoField, |
| 5 | + NumericField, |
| 6 | + TagField, |
| 7 | + TextField, |
| 8 | + VectorField, |
| 9 | +) |
| 10 | + |
| 11 | +from redisvl.schema import ( |
| 12 | + FlatVectorField, |
| 13 | + GeoFieldSchema, |
| 14 | + HNSWVectorField, |
| 15 | + NumericFieldSchema, |
| 16 | + SchemaModel, |
| 17 | + TagFieldSchema, |
| 18 | + TextFieldSchema, |
| 19 | + read_schema, |
| 20 | +) |
| 21 | + |
| 22 | + |
| 23 | +# Utility functions to create schema instances with default values |
| 24 | +def create_text_field_schema(**kwargs): |
| 25 | + defaults = {"name": "example_textfield", "sortable": False, "weight": 1.0} |
| 26 | + defaults.update(kwargs) |
| 27 | + return TextFieldSchema(**defaults) |
| 28 | + |
| 29 | + |
| 30 | +def create_tag_field_schema(**kwargs): |
| 31 | + defaults = {"name": "example_tagfield", "sortable": False, "separator": ","} |
| 32 | + defaults.update(kwargs) |
| 33 | + return TagFieldSchema(**defaults) |
| 34 | + |
| 35 | + |
| 36 | +def create_numeric_field_schema(**kwargs): |
| 37 | + defaults = {"name": "example_numericfield", "sortable": False} |
| 38 | + defaults.update(kwargs) |
| 39 | + return NumericFieldSchema(**defaults) |
| 40 | + |
| 41 | + |
| 42 | +def create_geo_field_schema(**kwargs): |
| 43 | + defaults = {"name": "example_geofield", "sortable": False} |
| 44 | + defaults.update(kwargs) |
| 45 | + return GeoFieldSchema(**defaults) |
| 46 | + |
| 47 | + |
| 48 | +def create_flat_vector_field(**kwargs): |
| 49 | + defaults = {"name": "example_flatvectorfield", "dims": 128, "algorithm": "FLAT"} |
| 50 | + defaults.update(kwargs) |
| 51 | + return FlatVectorField(**defaults) |
| 52 | + |
| 53 | + |
| 54 | +def create_hnsw_vector_field(**kwargs): |
| 55 | + defaults = { |
| 56 | + "name": "example_hnswvectorfield", |
| 57 | + "dims": 128, |
| 58 | + "algorithm": "HNSW", |
| 59 | + "m": 16, |
| 60 | + "ef_construction": 200, |
| 61 | + "ef_runtime": 10, |
| 62 | + "epsilon": 0.01, |
| 63 | + } |
| 64 | + defaults.update(kwargs) |
| 65 | + return HNSWVectorField(**defaults) |
| 66 | + |
| 67 | + |
| 68 | +# Tests for field schema creation and validation |
| 69 | +@pytest.mark.parametrize( |
| 70 | + "schema_func,field_class", |
| 71 | + [ |
| 72 | + (create_text_field_schema, TextField), |
| 73 | + (create_tag_field_schema, TagField), |
| 74 | + (create_numeric_field_schema, NumericField), |
| 75 | + (create_geo_field_schema, GeoField), |
| 76 | + ], |
| 77 | +) |
| 78 | +def test_field_schema_as_field(schema_func, field_class): |
| 79 | + schema = schema_func() |
| 80 | + field = schema.as_field() |
| 81 | + assert isinstance(field, field_class) |
| 82 | + assert field.name == f"example_{field_class.__name__.lower()}" |
| 83 | + |
| 84 | + |
| 85 | +def test_vector_fields_as_field(): |
| 86 | + flat_vector_schema = create_flat_vector_field() |
| 87 | + flat_vector_field = flat_vector_schema.as_field() |
| 88 | + assert isinstance(flat_vector_field, VectorField) |
| 89 | + assert flat_vector_field.name == "example_flatvectorfield" |
| 90 | + |
| 91 | + hnsw_vector_schema = create_hnsw_vector_field() |
| 92 | + hnsw_vector_field = hnsw_vector_schema.as_field() |
| 93 | + assert isinstance(hnsw_vector_field, VectorField) |
| 94 | + assert hnsw_vector_field.name == "example_hnswvectorfield" |
| 95 | + |
| 96 | + |
| 97 | +@pytest.mark.parametrize( |
| 98 | + "vector_schema_func,extra_params", |
| 99 | + [ |
| 100 | + (create_flat_vector_field, {"block_size": 100}), |
| 101 | + (create_hnsw_vector_field, {"m": 24, "ef_construction": 300}), |
| 102 | + ], |
| 103 | +) |
| 104 | +def test_vector_fields_with_optional_params(vector_schema_func, extra_params): |
| 105 | + # Create a vector schema with additional parameters set. |
| 106 | + vector_schema = vector_schema_func(**extra_params) |
| 107 | + vector_field = vector_schema.as_field() |
| 108 | + |
| 109 | + # Assert that the field is correctly created and the optional parameters are set. |
| 110 | + assert isinstance(vector_field, VectorField) |
| 111 | + for param, value in extra_params.items(): |
| 112 | + assert param.upper() in vector_field.args |
| 113 | + i = vector_field.args.index(param.upper()) |
| 114 | + assert vector_field.args[i + 1] == value |
| 115 | + |
| 116 | + |
| 117 | +def test_hnsw_vector_field_optional_params_not_set(): |
| 118 | + # Create HNSW vector field without setting optional params |
| 119 | + hnsw_field = HNSWVectorField(name="example_vector", dims=128, algorithm="HNSW") |
| 120 | + |
| 121 | + assert hnsw_field.m == 16 # default value |
| 122 | + assert hnsw_field.ef_construction == 200 # default value |
| 123 | + assert hnsw_field.ef_runtime == 10 # default value |
| 124 | + assert hnsw_field.epsilon == 0.01 # default value |
| 125 | + |
| 126 | + field_exported = hnsw_field.as_field() |
| 127 | + |
| 128 | + # Check the default values are correctly applied in the exported object |
| 129 | + assert field_exported.args[field_exported.args.index("M") + 1] == 16 |
| 130 | + assert field_exported.args[field_exported.args.index("EF_CONSTRUCTION") + 1] == 200 |
| 131 | + assert field_exported.args[field_exported.args.index("EF_RUNTIME") + 1] == 10 |
| 132 | + assert field_exported.args[field_exported.args.index("EPSILON") + 1] == 0.01 |
| 133 | + |
| 134 | + |
| 135 | +def test_flat_vector_field_block_size_not_set(): |
| 136 | + # Create Flat vector field without setting block_size |
| 137 | + flat_field = FlatVectorField(name="example_vector", dims=128, algorithm="FLAT") |
| 138 | + field_exported = flat_field.as_field() |
| 139 | + |
| 140 | + # block_size and initial_cap should not be in the exported field if it was not set |
| 141 | + assert "BLOCK_SIZE" not in field_exported.args |
| 142 | + assert "INITIAL_CAP" not in field_exported.args |
| 143 | + |
| 144 | + |
| 145 | +# Test for schema model validation |
| 146 | +def test_schema_model_validation_success(): |
| 147 | + valid_index = {"name": "test_index", "storage_type": "hash"} |
| 148 | + valid_fields = {"text": [create_text_field_schema()]} |
| 149 | + schema_model = SchemaModel(index=valid_index, fields=valid_fields) |
| 150 | + |
| 151 | + assert schema_model.index.name == "test_index" |
| 152 | + assert schema_model.index.storage_type == "hash" |
| 153 | + assert len(schema_model.fields.text) == 1 |
| 154 | + |
| 155 | + |
| 156 | +def test_schema_model_validation_failures(): |
| 157 | + # Invalid storage type |
| 158 | + with pytest.raises(ValueError): |
| 159 | + invalid_index = {"name": "test_index", "storage_type": "unsupported"} |
| 160 | + SchemaModel(index=invalid_index, fields={}) |
| 161 | + |
| 162 | + # Missing required field |
| 163 | + with pytest.raises(ValidationError): |
| 164 | + SchemaModel(index={}, fields={}) |
| 165 | + |
| 166 | + |
| 167 | +def test_read_schema_file_not_found(): |
| 168 | + with pytest.raises(FileNotFoundError): |
| 169 | + read_schema("non_existent_file.yaml") |
0 commit comments