File size: 3,544 Bytes
95d599c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | // _ _
// __ _____ __ ___ ___ __ _| |_ ___
// \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
// \ V V / __/ (_| |\ V /| | (_| | || __/
// \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
//
// Copyright © 2016 - 2025 Weaviate B.V. All rights reserved.
//
// CONTACT: hello@weaviate.io
//
package modelsext
import (
"testing"
"github.com/stretchr/testify/require"
"github.com/weaviate/weaviate/entities/models"
)
func TestClassHasLegacyVectorIndex(t *testing.T) {
for _, tt := range []struct {
name string
class *models.Class
want bool
}{
{
name: "all fields are empty or nil",
class: &models.Class{
Vectorizer: "",
VectorIndexConfig: nil,
VectorIndexType: "",
},
want: false,
},
{
name: "Vectorizer is not empty",
class: &models.Class{
Vectorizer: "some_vectorizer",
},
want: true,
},
{
name: "VectorIndexConfig is not nil",
class: &models.Class{
VectorIndexConfig: map[string]interface{}{"distance": "cosine"},
},
want: true,
},
{
name: "VectorIndexType is not empty",
class: &models.Class{
VectorIndexType: "hnsw",
},
want: true,
},
} {
t.Run(tt.name, func(t *testing.T) {
require.Equal(t, tt.want, ClassHasLegacyVectorIndex(tt.class))
})
}
}
func TestClassGetVectorConfig(t *testing.T) {
var (
customConfig = models.VectorConfig{
Vectorizer: "custom-vectorizer",
VectorIndexType: "flat",
VectorIndexConfig: map[string]interface{}{
"distance": "euclidean",
},
}
legacyConfig = models.VectorConfig{
Vectorizer: "legacy-vectorizer",
VectorIndexType: "hnsw",
VectorIndexConfig: map[string]interface{}{
"distance": "cosine",
},
}
mixedClass = &models.Class{
Vectorizer: "legacy-vectorizer",
VectorIndexType: "hnsw",
VectorIndexConfig: map[string]interface{}{
"distance": "cosine",
},
VectorConfig: map[string]models.VectorConfig{
"custom": customConfig,
},
}
)
for _, tt := range []struct {
name string
class *models.Class
targetVector string
expectConfig *models.VectorConfig
}{
{
name: "named vector not present",
class: mixedClass,
targetVector: "non-existent",
expectConfig: nil,
},
{
name: "legacy vector via empty string",
class: mixedClass,
targetVector: "",
expectConfig: &legacyConfig,
},
{
name: "legacy vector via default named target vector",
class: mixedClass,
targetVector: DefaultNamedVectorName,
expectConfig: &legacyConfig,
},
{
name: "named vector via its name",
class: mixedClass,
targetVector: "custom",
expectConfig: &customConfig,
},
{
name: "legacy vector without named vectors",
class: &models.Class{
Vectorizer: "legacy-vectorizer",
VectorIndexType: "hnsw",
VectorIndexConfig: map[string]interface{}{
"distance": "cosine",
},
},
targetVector: "",
expectConfig: &legacyConfig,
},
{
name: "named vector without legacy vectors",
class: &models.Class{
VectorConfig: map[string]models.VectorConfig{
"custom": customConfig,
},
},
targetVector: "custom",
expectConfig: &customConfig,
},
} {
t.Run(tt.name, func(t *testing.T) {
cfg, ok := ClassGetVectorConfig(tt.class, tt.targetVector)
if tt.expectConfig == nil {
require.False(t, ok)
} else {
require.True(t, ok)
require.Equal(t, *tt.expectConfig, cfg)
}
})
}
}
|