File size: 6,345 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
//                           _       _
// __      _____  __ ___   ___  __ _| |_ ___
// \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
//  \ V  V /  __/ (_| |\ V /| | (_| | ||  __/
//   \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
//
//  Copyright © 2016 - 2024 Weaviate B.V. All rights reserved.
//
//  CONTACT: [email protected]
//

package v1

import (
	"testing"

	"github.com/weaviate/weaviate/entities/models"

	"github.com/stretchr/testify/require"
	"github.com/weaviate/weaviate/entities/schema"
	pb "github.com/weaviate/weaviate/grpc/generated/protocol/v1"
)

type innerTest struct {
	datatype    schema.DataType
	out         *pb.Value
	shouldError bool
}

func makeTestList(succeedingInnerTests map[schema.DataType]*pb.Value) []innerTest {
	dtypes := append(schema.PrimitiveDataTypes, schema.DeprecatedPrimitiveDataTypes...)
	list := make([]innerTest, len(dtypes))
	for idx := range dtypes {
		out, ok := succeedingInnerTests[dtypes[idx]]
		if ok {
			list[idx] = innerTest{
				datatype:    dtypes[idx],
				out:         out,
				shouldError: false,
			}
		} else {
			list[idx] = innerTest{
				datatype:    dtypes[idx],
				out:         nil,
				shouldError: true,
			}
		}
	}
	return list
}

func TestNewPrimitiveValue(t *testing.T) {
	float_val := float32(1.1)

	tests := []struct {
		name  string
		in    any
		tests []innerTest
	}{
		{
			name: "bools",
			in:   []bool{true, false},
			tests: makeTestList(map[schema.DataType]*pb.Value{
				schema.DataTypeBooleanArray: {Kind: &pb.Value_ListValue{ListValue: &pb.ListValue{Values: []*pb.Value{
					{Kind: &pb.Value_BoolValue{BoolValue: true}},
					{Kind: &pb.Value_BoolValue{BoolValue: false}},
				}}}},
			}),
		},
		{
			name: "strings",
			in:   []string{"a string", "another string"},
			tests: makeTestList(map[schema.DataType]*pb.Value{
				schema.DataTypeDateArray: {Kind: &pb.Value_ListValue{ListValue: &pb.ListValue{Values: []*pb.Value{
					{Kind: &pb.Value_DateValue{DateValue: "a string"}},
					{Kind: &pb.Value_DateValue{DateValue: "another string"}},
				}}}},
				schema.DataTypeStringArray: {Kind: &pb.Value_ListValue{ListValue: &pb.ListValue{Values: []*pb.Value{
					{Kind: &pb.Value_StringValue{StringValue: "a string"}},
					{Kind: &pb.Value_StringValue{StringValue: "another string"}},
				}}}},
				schema.DataTypeTextArray: {Kind: &pb.Value_ListValue{ListValue: &pb.ListValue{Values: []*pb.Value{
					{Kind: &pb.Value_StringValue{StringValue: "a string"}},
					{Kind: &pb.Value_StringValue{StringValue: "another string"}},
				}}}},
				schema.DataTypeUUIDArray: {Kind: &pb.Value_ListValue{ListValue: &pb.ListValue{Values: []*pb.Value{
					{Kind: &pb.Value_UuidValue{UuidValue: "a string"}},
					{Kind: &pb.Value_UuidValue{UuidValue: "another string"}},
				}}}},
			}),
		},
		{
			name: "float64s",
			in:   []float64{1.1, 2.2, 3.3},
			tests: makeTestList(map[schema.DataType]*pb.Value{
				schema.DataTypeNumberArray: {Kind: &pb.Value_ListValue{ListValue: &pb.ListValue{Values: []*pb.Value{
					{Kind: &pb.Value_NumberValue{NumberValue: 1.1}},
					{Kind: &pb.Value_NumberValue{NumberValue: 2.2}},
					{Kind: &pb.Value_NumberValue{NumberValue: 3.3}},
				}}}},
				schema.DataTypeIntArray: {Kind: &pb.Value_ListValue{ListValue: &pb.ListValue{Values: []*pb.Value{
					{Kind: &pb.Value_IntValue{IntValue: 1}},
					{Kind: &pb.Value_IntValue{IntValue: 2}},
					{Kind: &pb.Value_IntValue{IntValue: 3}},
				}}}},
			}),
		},
		{
			name: "empty array",
			in:   []interface{}{},
			tests: makeTestList(map[schema.DataType]*pb.Value{
				schema.DataTypeBooleanArray: {Kind: &pb.Value_ListValue{ListValue: &pb.ListValue{Values: []*pb.Value{}}}},
				schema.DataTypeDateArray:    {Kind: &pb.Value_ListValue{ListValue: &pb.ListValue{Values: []*pb.Value{}}}},
				schema.DataTypeNumberArray:  {Kind: &pb.Value_ListValue{ListValue: &pb.ListValue{Values: []*pb.Value{}}}},
				schema.DataTypeIntArray:     {Kind: &pb.Value_ListValue{ListValue: &pb.ListValue{Values: []*pb.Value{}}}},
				schema.DataTypeStringArray:  {Kind: &pb.Value_ListValue{ListValue: &pb.ListValue{Values: []*pb.Value{}}}},
				schema.DataTypeTextArray:    {Kind: &pb.Value_ListValue{ListValue: &pb.ListValue{Values: []*pb.Value{}}}},
				schema.DataTypeUUIDArray:    {Kind: &pb.Value_ListValue{ListValue: &pb.ListValue{Values: []*pb.Value{}}}},
			}),
		},
		{
			name: "bool",
			in:   true,
			tests: makeTestList(map[schema.DataType]*pb.Value{
				schema.DataTypeBoolean: {Kind: &pb.Value_BoolValue{BoolValue: true}},
			}),
		},
		{
			name: "string",
			in:   "a string",
			tests: makeTestList(map[schema.DataType]*pb.Value{
				schema.DataTypeDate:   {Kind: &pb.Value_DateValue{DateValue: "a string"}},
				schema.DataTypeString: {Kind: &pb.Value_StringValue{StringValue: "a string"}},
				schema.DataTypeText:   {Kind: &pb.Value_StringValue{StringValue: "a string"}},
				schema.DataTypeUUID:   {Kind: &pb.Value_UuidValue{UuidValue: "a string"}},
				schema.DataTypeBlob:   {Kind: &pb.Value_BlobValue{BlobValue: "a string"}},
			}),
		},
		{
			name: "float64",
			in:   1.1,
			tests: makeTestList(map[schema.DataType]*pb.Value{
				schema.DataTypeNumber: {Kind: &pb.Value_NumberValue{NumberValue: 1.1}},
				schema.DataTypeInt:    {Kind: &pb.Value_IntValue{IntValue: 1}},
			}),
		},
		{
			name: "geo",
			in:   &models.GeoCoordinates{Longitude: &float_val, Latitude: &float_val},
			tests: makeTestList(map[schema.DataType]*pb.Value{
				schema.DataTypeGeoCoordinates: {Kind: &pb.Value_GeoValue{GeoValue: &pb.GeoCoordinate{Latitude: float_val, Longitude: float_val}}},
			}),
		},
		{
			name: "phone number",
			in:   &models.PhoneNumber{Input: "1234567890"},
			tests: makeTestList(map[schema.DataType]*pb.Value{
				schema.DataTypePhoneNumber: {Kind: &pb.Value_PhoneValue{PhoneValue: &pb.PhoneNumber{Input: "1234567890"}}},
			}),
		},
	}

	for _, tt := range tests {
		for _, test := range tt.tests {
			out, err := NewPrimitiveValue(tt.in, test.datatype)
			if test.shouldError {
				if err == nil {
					t.Logf("expected an error for %v and %s", tt.in, test.datatype)
				}
				require.Error(t, err)
			} else {
				require.NoError(t, err)
				require.Equal(t, test.out, out)
			}
		}
	}
}