File size: 4,000 Bytes
b110593
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
//                           _       _
// __      _____  __ ___   ___  __ _| |_ ___
// \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
//  \ V  V /  __/ (_| |\ V /| | (_| | ||  __/
//   \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
//
//  Copyright © 2016 - 2024 Weaviate B.V. All rights reserved.
//
//  CONTACT: [email protected]
//

package acceptance_with_go_client

import (
	"context"
	"fmt"
	"math/rand"
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	client "github.com/weaviate/weaviate-go-client/v4/weaviate"
	"github.com/weaviate/weaviate-go-client/v4/weaviate/graphql"
	"github.com/weaviate/weaviate/entities/models"
	"github.com/weaviate/weaviate/entities/schema"
)

func TestEdgeVectorDimensions(t *testing.T) {
	r := rand.New(rand.NewSource(time.Now().UnixNano()))
	c := client.New(client.Config{Scheme: "http", Host: "localhost:8080"})
	ctx := context.Background()

	objID1 := "00000000-0000-0000-0000-000000000001"
	objID2 := "00000000-0000-0000-0000-000000000002"
	className := "VectorDimensions65k"
	propName := "title"

	// 65535 is the max value for uint16
	maxUint16 := uint16(65535)

	for _, vectorLength := range []uint16{1, 50000, maxUint16} {
		t.Run(fmt.Sprintf("%v vector dimensions", vectorLength), func(t *testing.T) {
			class := &models.Class{
				Class: className,
				Properties: []*models.Property{
					{Name: propName, DataType: []string{string(schema.DataTypeText)}, IndexInverted: &vTrue},
				},
				Vectorizer: "none",
			}

			// delete class if exists and cleanup after test
			c.Schema().ClassDeleter().WithClassName(className).Do(ctx)
			require.Nil(t, c.Schema().ClassCreator().WithClass(class).Do(ctx))
			defer c.Schema().ClassDeleter().WithClassName(className).Do(ctx)

			t.Run("insert vectors", func(t *testing.T) {
				generateVector := func(dims uint16) []float32 {
					vector := make([]float32, dims)
					for i := range vector {
						vector[i] = r.Float32()
					}
					return vector
				}
				for i, objID := range []string{objID1, objID2} {
					_, err := c.Data().Creator().
						WithClassName(className).WithID(objID).
						WithProperties(map[string]interface{}{
							propName: fmt.Sprintf("title %v", i),
						}).
						WithVector(generateVector(vectorLength)).
						Do(ctx)
					require.Nil(t, err)
				}
			})

			t.Run("check Aggregate", func(t *testing.T) {
				getCount := func(t *testing.T, result *models.GraphQLResponse) int {
					aggregate, ok := result.Data["Aggregate"].(map[string]interface{})
					require.True(t, ok)
					require.NotNil(t, aggregate)
					class, ok := aggregate[className].([]interface{})
					require.True(t, ok)
					require.Len(t, class, 1)
					title, ok := class[0].(map[string]interface{})
					require.True(t, ok)
					require.NotNil(t, title)
					count, ok := title["title"].(map[string]interface{})
					require.True(t, ok)
					require.NotNil(t, count)
					titleCount, ok := count["count"].(float64)
					require.True(t, ok)
					return int(titleCount)
				}
				result, err := c.GraphQL().Aggregate().WithClassName(className).WithFields(graphql.Field{
					Name: propName, Fields: []graphql.Field{
						{Name: "count"},
					},
				}).Do(ctx)
				require.Nil(t, err)
				require.Empty(t, result.Errors)
				assert.Equal(t, 2, getCount(t, result))
			})

			t.Run("check nearObject", func(t *testing.T) {
				nearObject := c.GraphQL().NearObjectArgBuilder().WithID(objID1)
				result, err := c.GraphQL().Get().
					WithClassName(className).
					WithNearObject(nearObject).
					WithFields(graphql.Field{Name: propName}).
					Do(ctx)
				require.Nil(t, err)
				require.Empty(t, result.Errors)
				get, ok := result.Data["Get"].(map[string]interface{})
				require.True(t, ok)
				require.NotNil(t, get)
				class, ok := get[className].([]interface{})
				require.True(t, ok)
				require.Len(t, class, 2)
			})
		})
	}
}