weaviate

Форк
0
189 строк · 4.7 Кб
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 vectorizer
13

14
import (
15
	"context"
16
	"strings"
17
	"testing"
18

19
	"github.com/stretchr/testify/assert"
20
	"github.com/stretchr/testify/require"
21
	"github.com/weaviate/weaviate/entities/models"
22
)
23

24
func TestVectorizingObjects(t *testing.T) {
25
	type testCase struct {
26
		name               string
27
		input              *models.Object
28
		expectedClientCall string
29
		noindex            string
30
		excludedProperty   string // to simulate a schema where property names aren't vectorized
31
		excludedClass      string // to simulate a schema where class names aren't vectorized
32
	}
33

34
	tests := []testCase{
35
		{
36
			name: "empty object",
37
			input: &models.Object{
38
				Class: "Car",
39
			},
40
			expectedClientCall: "car",
41
		},
42
		{
43
			name: "object with one string prop",
44
			input: &models.Object{
45
				Class: "Car",
46
				Properties: map[string]interface{}{
47
					"brand": "Mercedes",
48
				},
49
			},
50
			expectedClientCall: "car brand mercedes",
51
		},
52
		{
53
			name: "object with one non-string prop",
54
			input: &models.Object{
55
				Class: "Car",
56
				Properties: map[string]interface{}{
57
					"power": 300,
58
				},
59
			},
60
			expectedClientCall: "car",
61
		},
62
		{
63
			name: "object with a mix of props",
64
			input: &models.Object{
65
				Class: "Car",
66
				Properties: map[string]interface{}{
67
					"brand":  "best brand",
68
					"power":  300,
69
					"review": "a very great car",
70
				},
71
			},
72
			expectedClientCall: "car brand best brand review a very great car",
73
		},
74
		{
75
			name:    "with a noindexed property",
76
			noindex: "review",
77
			input: &models.Object{
78
				Class: "Car",
79
				Properties: map[string]interface{}{
80
					"brand":  "best brand",
81
					"power":  300,
82
					"review": "a very great car",
83
				},
84
			},
85
			expectedClientCall: "car brand best brand",
86
		},
87

88
		{
89
			name:          "with the class name not vectorized",
90
			excludedClass: "Car",
91
			input: &models.Object{
92
				Class: "Car",
93
				Properties: map[string]interface{}{
94
					"brand":  "best brand",
95
					"power":  300,
96
					"review": "a very great car",
97
				},
98
			},
99
			expectedClientCall: "brand best brand review a very great car",
100
		},
101
		{
102
			name:             "with a property name not vectorized",
103
			excludedProperty: "review",
104
			input: &models.Object{
105
				Class: "Car",
106
				Properties: map[string]interface{}{
107
					"brand":  "best brand",
108
					"power":  300,
109
					"review": "a very great car",
110
				},
111
			},
112
			expectedClientCall: "car brand best brand a very great car",
113
		},
114
		{
115
			name:             "with no schema labels vectorized",
116
			excludedProperty: "review",
117
			excludedClass:    "Car",
118
			input: &models.Object{
119
				Class: "Car",
120
				Properties: map[string]interface{}{
121
					"review": "a very great car",
122
				},
123
			},
124
			expectedClientCall: "a very great car",
125
		},
126
		{
127
			name:             "with string/text arrays without propname or classname",
128
			excludedProperty: "reviews",
129
			excludedClass:    "Car",
130
			input: &models.Object{
131
				Class: "Car",
132
				Properties: map[string]interface{}{
133
					"reviews": []string{
134
						"a very great car",
135
						"you should consider buying one",
136
					},
137
				},
138
			},
139
			expectedClientCall: "a very great car you should consider buying one",
140
		},
141
		{
142
			name: "with string/text arrays with propname and classname",
143
			input: &models.Object{
144
				Class: "Car",
145
				Properties: map[string]interface{}{
146
					"reviews": []string{
147
						"a very great car",
148
						"you should consider buying one",
149
					},
150
				},
151
			},
152
			expectedClientCall: "car reviews a very great car reviews you should consider buying one",
153
		},
154
		{
155
			name: "with compound class and prop names",
156
			input: &models.Object{
157
				Class: "SuperCar",
158
				Properties: map[string]interface{}{
159
					"brandOfTheCar": "best brand",
160
					"power":         300,
161
					"review":        "a very great car",
162
				},
163
			},
164
			expectedClientCall: "super car brand of the car best brand review a very great car",
165
		},
166
	}
167

168
	for _, test := range tests {
169
		t.Run(test.name, func(t *testing.T) {
170
			client := &fakeClient{}
171

172
			v := New(client)
173

174
			ic := &fakeClassConfig{
175
				excludedProperty:      test.excludedProperty,
176
				skippedProperty:       test.noindex,
177
				vectorizeClassName:    test.excludedClass != "Car",
178
				vectorizePropertyName: true,
179
			}
180
			vector, _, err := v.Object(context.Background(), test.input, ic)
181

182
			require.Nil(t, err)
183
			assert.Equal(t, []float32{0, 1, 2, 3}, vector)
184
			expected := strings.Split(test.expectedClientCall, " ")
185
			actual := strings.Split(client.lastInput, " ")
186
			assert.Equal(t, expected, actual)
187
		})
188
	}
189
}
190

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.