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

package objects

import (
	"context"
	"errors"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/weaviate/weaviate/entities/additional"
	"github.com/weaviate/weaviate/entities/models"
	"github.com/weaviate/weaviate/entities/schema"
	"github.com/weaviate/weaviate/entities/search"
)

func TestQuery(t *testing.T) {
	t.Parallel()
	var (
		cls    = "MyClass"
		m      = newFakeGetManager(schema.Schema{})
		errAny = errors.New("any")
	)
	params := QueryParams{
		Class: cls,
		Limit: ptInt64(10),
	}
	inputs := QueryInput{
		Class: cls,
		Limit: 10,
	}
	tests := []struct {
		class             string
		name              string
		param             QueryParams
		mockedErr         *Error
		authErr           error
		lockErr           error
		wantCode          int
		mockedDBResponse  []search.Result
		wantResponse      []*models.Object
		wantQueryInput    QueryInput
		wantUsageTracking bool
	}{
		{
			name:           "not found",
			class:          cls,
			param:          params,
			mockedErr:      &Error{Code: StatusNotFound},
			wantCode:       StatusNotFound,
			wantQueryInput: inputs,
		},
		{
			name:           "forbidden",
			class:          cls,
			param:          params,
			authErr:        errAny,
			wantCode:       StatusForbidden,
			wantQueryInput: inputs,
		},
		{
			name:           "internal error",
			class:          cls,
			param:          params,
			lockErr:        errAny,
			wantCode:       StatusInternalServerError,
			wantQueryInput: inputs,
		},
		{
			name:  "happy path",
			class: cls,
			param: params,
			mockedDBResponse: []search.Result{
				{
					ClassName: cls,
					Schema: map[string]interface{}{
						"foo": "bar",
					},
					Dims: 3,
					Dist: 0,
				},
			},
			wantResponse: []*models.Object{{
				Class:         cls,
				VectorWeights: map[string]string(nil),
				Properties: map[string]interface{}{
					"foo": "bar",
				},
			}},
			wantQueryInput: inputs,
		},
		{
			name:  "happy path with explicit vector requested",
			class: cls,
			param: QueryParams{
				Class:      cls,
				Limit:      ptInt64(10),
				Additional: additional.Properties{Vector: true},
			},
			mockedDBResponse: []search.Result{
				{
					ClassName: cls,
					Schema: map[string]interface{}{
						"foo": "bar",
					},
					Dims: 3,
				},
			},
			wantResponse: []*models.Object{{
				Class:         cls,
				VectorWeights: map[string]string(nil),
				Properties: map[string]interface{}{
					"foo": "bar",
				},
			}},
			wantQueryInput: QueryInput{
				Class:      cls,
				Limit:      10,
				Additional: additional.Properties{Vector: true},
			},
			wantUsageTracking: true,
		},
		{
			name:           "bad request",
			class:          cls,
			param:          QueryParams{Class: cls, Offset: ptInt64(1), Limit: &m.config.Config.QueryMaximumResults},
			wantCode:       StatusBadRequest,
			wantQueryInput: inputs,
		},
	}
	for i, tc := range tests {
		t.Run(tc.name, func(t *testing.T) {
			m.authorizer.Err = tc.authErr
			m.locks.Err = tc.lockErr
			if tc.authErr == nil && tc.lockErr == nil {
				m.repo.On("Query", &tc.wantQueryInput).Return(tc.mockedDBResponse, tc.mockedErr).Once()
			}
			if tc.wantUsageTracking {
				m.metrics.On("AddUsageDimensions", cls, "get_rest", "list_include_vector",
					tc.mockedDBResponse[0].Dims)
			}
			res, err := m.Manager.Query(context.Background(), nil, &tc.param)
			code := 0
			if err != nil {
				code = err.Code
			}
			if tc.wantCode != code {
				t.Errorf("case %d expected:%v got:%v", i+1, tc.wantCode, code)
			}

			assert.Equal(t, tc.wantResponse, res)
		})
	}
}