weaviate
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
12package vectorizer13
14import (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
24func TestVectorizingObjects(t *testing.T) {25type testCase struct {26name string27input *models.Object28expectedClientCall string29noindex string30excludedProperty string // to simulate a schema where property names aren't vectorized31excludedClass string // to simulate a schema where class names aren't vectorized32}33
34tests := []testCase{35{36name: "empty object",37input: &models.Object{38Class: "Car",39},40expectedClientCall: "car",41},42{43name: "object with one string prop",44input: &models.Object{45Class: "Car",46Properties: map[string]interface{}{47"brand": "Mercedes",48},49},50expectedClientCall: "car brand mercedes",51},52{53name: "object with one non-string prop",54input: &models.Object{55Class: "Car",56Properties: map[string]interface{}{57"power": 300,58},59},60expectedClientCall: "car",61},62{63name: "object with a mix of props",64input: &models.Object{65Class: "Car",66Properties: map[string]interface{}{67"brand": "best brand",68"power": 300,69"review": "a very great car",70},71},72expectedClientCall: "car brand best brand review a very great car",73},74{75name: "with a noindexed property",76noindex: "review",77input: &models.Object{78Class: "Car",79Properties: map[string]interface{}{80"brand": "best brand",81"power": 300,82"review": "a very great car",83},84},85expectedClientCall: "car brand best brand",86},87
88{89name: "with the class name not vectorized",90excludedClass: "Car",91input: &models.Object{92Class: "Car",93Properties: map[string]interface{}{94"brand": "best brand",95"power": 300,96"review": "a very great car",97},98},99expectedClientCall: "brand best brand review a very great car",100},101{102name: "with a property name not vectorized",103excludedProperty: "review",104input: &models.Object{105Class: "Car",106Properties: map[string]interface{}{107"brand": "best brand",108"power": 300,109"review": "a very great car",110},111},112expectedClientCall: "car brand best brand a very great car",113},114{115name: "with no schema labels vectorized",116excludedProperty: "review",117excludedClass: "Car",118input: &models.Object{119Class: "Car",120Properties: map[string]interface{}{121"review": "a very great car",122},123},124expectedClientCall: "a very great car",125},126{127name: "with string/text arrays without propname or classname",128excludedProperty: "reviews",129excludedClass: "Car",130input: &models.Object{131Class: "Car",132Properties: map[string]interface{}{133"reviews": []string{134"a very great car",135"you should consider buying one",136},137},138},139expectedClientCall: "a very great car you should consider buying one",140},141{142name: "with string/text arrays with propname and classname",143input: &models.Object{144Class: "Car",145Properties: map[string]interface{}{146"reviews": []string{147"a very great car",148"you should consider buying one",149},150},151},152expectedClientCall: "car reviews a very great car reviews you should consider buying one",153},154{155name: "with compound class and prop names",156input: &models.Object{157Class: "SuperCar",158Properties: map[string]interface{}{159"brandOfTheCar": "best brand",160"power": 300,161"review": "a very great car",162},163},164expectedClientCall: "super car brand of the car best brand review a very great car",165},166}167
168for _, test := range tests {169t.Run(test.name, func(t *testing.T) {170client := &fakeClient{}171
172v := New(client)173
174ic := &fakeClassConfig{175excludedProperty: test.excludedProperty,176skippedProperty: test.noindex,177vectorizeClassName: test.excludedClass != "Car",178vectorizePropertyName: true,179}180vector, _, err := v.Object(context.Background(), test.input, ic)181
182require.Nil(t, err)183assert.Equal(t, []float32{0, 1, 2, 3}, vector)184expected := strings.Split(test.expectedClientCall, " ")185actual := strings.Split(client.lastInput, " ")186assert.Equal(t, expected, actual)187})188}189}
190