Skip to content

Commit

Permalink
Add tests for index schema validation and map check
Browse files Browse the repository at this point in the history
  • Loading branch information
nuric committed May 7, 2024
1 parent cc6a6df commit eb87e79
Showing 1 changed file with 109 additions and 0 deletions.
109 changes: 109 additions & 0 deletions models/index_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package models_test

import (
"testing"

"github.com/semafind/semadb/models"
"github.com/stretchr/testify/require"
)

func TestIndexSchema_Validate_Empty(t *testing.T) {
// These types require parameters to be provided
iTypes := []string{models.IndexTypeString, models.IndexTypeStringArray,
models.IndexTypeText, models.IndexTypeVectorFlat,
models.IndexTypeVectorVamana}
for _, iType := range iTypes {
schema := models.IndexSchema{
"prop": models.IndexSchemaValue{
Type: iType,
// We don't provide any parameters
},
}
err := schema.Validate()
// We should get an error
require.Error(t, err)
}
}

func TestIndexSchema_Validate_Haversine(t *testing.T) {
// The haversine distance requires a vector size of 2
iTypes := []string{models.IndexTypeVectorFlat, models.IndexTypeVectorVamana}
for _, iType := range iTypes {
schema := models.IndexSchema{
"prop": models.IndexSchemaValue{
Type: iType,
VectorFlat: &models.IndexVectorFlatParameters{
DistanceMetric: models.DistanceHaversine,
VectorSize: 1,
},
VectorVamana: &models.IndexVectorVamanaParameters{
DistanceMetric: models.DistanceHaversine,
VectorSize: 1,
},
},
}
err := schema.Validate()
require.Error(t, err)
}
}

func TestIndexSchema_CheckCompatibleMap(t *testing.T) {
// Check if the schema is compatible with a map
// ---------------------------
// Here is a kitchen sink schema
schema := models.IndexSchema{
"propVectorFlat": models.IndexSchemaValue{
Type: models.IndexTypeVectorFlat,
VectorFlat: &models.IndexVectorFlatParameters{
DistanceMetric: models.DistanceEuclidean,
VectorSize: 2,
},
},
"propVectorVamana": models.IndexSchemaValue{
Type: models.IndexTypeVectorVamana,
VectorVamana: &models.IndexVectorVamanaParameters{
DistanceMetric: models.DistanceEuclidean,
VectorSize: 2,
},
},
"propText": models.IndexSchemaValue{
Type: models.IndexTypeText,
Text: &models.IndexTextParameters{
Analyser: "standard",
},
},
"propString": models.IndexSchemaValue{
Type: models.IndexTypeString,
String: &models.IndexStringParameters{
CaseSensitive: false,
},
},
"propInteger": models.IndexSchemaValue{
Type: models.IndexTypeInteger,
},
"propFloat": models.IndexSchemaValue{
Type: models.IndexTypeFloat,
},
"propStringArray": models.IndexSchemaValue{
Type: models.IndexTypeStringArray,
StringArray: &models.IndexStringArrayParameters{
IndexStringParameters: models.IndexStringParameters{
CaseSensitive: false,
},
},
},
}
// ---------------------------
// Here is a matching map
m := models.PointAsMap{
"propVectorFlat": []any{1.0, 2.0},
"propVectorVamana": []any{1.0, 2.0},
"propText": "hello world",
"propString": "hello",
"propInteger": 1,
"propFloat": 1.1,
"propStringArray": []any{"hello", "world"},
}
err := schema.CheckCompatibleMap(m)
require.NoError(t, err)
}

0 comments on commit eb87e79

Please sign in to comment.