github.com/weaviate/weaviate@v1.24.6/usecases/modulecomponents/settings/base_class_settings_test.go (about) 1 // _ _ 2 // __ _____ __ ___ ___ __ _| |_ ___ 3 // \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \ 4 // \ V V / __/ (_| |\ V /| | (_| | || __/ 5 // \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___| 6 // 7 // Copyright © 2016 - 2024 Weaviate B.V. All rights reserved. 8 // 9 // CONTACT: hello@weaviate.io 10 // 11 12 package settings 13 14 import ( 15 "errors" 16 "testing" 17 18 "github.com/stretchr/testify/assert" 19 "github.com/stretchr/testify/require" 20 "github.com/weaviate/weaviate/entities/models" 21 "github.com/weaviate/weaviate/usecases/modules" 22 ) 23 24 func Test_BaseClassSettings(t *testing.T) { 25 targetVector := "targetVector" 26 propertyToIndex := "someProp" 27 class := &models.Class{ 28 Class: "MyClass", 29 VectorConfig: map[string]models.VectorConfig{ 30 targetVector: { 31 Vectorizer: map[string]interface{}{ 32 "my-module": map[string]interface{}{ 33 "vectorizeClassName": false, 34 "properties": []interface{}{propertyToIndex}, 35 }, 36 }, 37 VectorIndexType: "hnsw", 38 }, 39 }, 40 Properties: []*models.Property{ 41 { 42 Name: propertyToIndex, 43 ModuleConfig: map[string]interface{}{ 44 "my-module": map[string]interface{}{ 45 "skip": true, 46 "vectorizePropertyName": true, 47 }, 48 }, 49 }, 50 { 51 Name: "otherProp", 52 }, 53 }, 54 } 55 56 cfg := modules.NewClassBasedModuleConfig(class, "my-module", "tenant", targetVector) 57 ic := NewBaseClassSettings(cfg) 58 59 assert.True(t, ic.PropertyIndexed(propertyToIndex)) 60 assert.True(t, ic.VectorizePropertyName(propertyToIndex)) 61 assert.False(t, ic.PropertyIndexed("otherProp")) 62 assert.False(t, ic.VectorizePropertyName("otherProp")) 63 assert.False(t, ic.VectorizeClassName()) 64 } 65 66 func Test_BaseClassSettings_Validate(t *testing.T) { 67 targetVector := "targetVector" 68 getClass := func(moduleSettings map[string]interface{}) *models.Class { 69 settings := map[string]interface{}{ 70 "vectorizeClassName": false, 71 } 72 for k, v := range moduleSettings { 73 settings[k] = v 74 } 75 return &models.Class{ 76 Class: "MyClass", 77 VectorConfig: map[string]models.VectorConfig{ 78 targetVector: { 79 Vectorizer: map[string]interface{}{ 80 "my-module": settings, 81 }, 82 VectorIndexType: "hnsw", 83 }, 84 }, 85 Properties: []*models.Property{ 86 { 87 Name: "prop1", 88 }, 89 { 90 Name: "otherProp", 91 }, 92 }, 93 } 94 } 95 tests := []struct { 96 name string 97 settings map[string]interface{} 98 wantErr error 99 }{ 100 { 101 name: "without properties", 102 settings: nil, 103 wantErr: nil, 104 }, 105 { 106 name: "proper properties", 107 settings: map[string]interface{}{ 108 "properties": []interface{}{"prop1"}, 109 }, 110 wantErr: nil, 111 }, 112 { 113 name: "nil properties", 114 settings: map[string]interface{}{ 115 "properties": nil, 116 }, 117 wantErr: errors.New("properties field needs to be of array type, got: <nil>"), 118 }, 119 { 120 name: "at least 1 property", 121 settings: map[string]interface{}{ 122 "properties": []interface{}{}, 123 }, 124 wantErr: errors.New("properties field needs to have at least 1 property defined"), 125 }, 126 { 127 name: "at least 1 property with []string", 128 settings: map[string]interface{}{ 129 "properties": []string{}, 130 }, 131 wantErr: errors.New("properties field needs to have at least 1 property defined"), 132 }, 133 { 134 name: "must be an array", 135 settings: map[string]interface{}{ 136 "properties": "string", 137 }, 138 wantErr: errors.New("properties field needs to be of array type, got: string"), 139 }, 140 { 141 name: "properties values need to be string", 142 settings: map[string]interface{}{ 143 "properties": []interface{}{"string", 1}, 144 }, 145 wantErr: errors.New("properties field value: 1 must be a string"), 146 }, 147 } 148 for _, tt := range tests { 149 t.Run(tt.name, func(t *testing.T) { 150 class := getClass(tt.settings) 151 cfg := modules.NewClassBasedModuleConfig(class, "my-module", "tenant", targetVector) 152 s := NewBaseClassSettings(cfg) 153 err := s.Validate() 154 if tt.wantErr != nil { 155 require.Error(t, err) 156 assert.Equal(t, err.Error(), tt.wantErr.Error()) 157 } else { 158 assert.NoError(t, err) 159 } 160 }) 161 } 162 }