aboutsummaryrefslogtreecommitdiffhomepage
path: root/vendor/google.golang.org/appengine/search/struct_test.go
blob: 4e5b5d1b8f1b665ec27084ab7e954d3e41747af8 (plain)
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// Copyright 2015 Google Inc. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.

package search

import (
	"reflect"
	"testing"
)

func TestLoadingStruct(t *testing.T) {
	testCases := []struct {
		desc    string
		fields  []Field
		meta    *DocumentMetadata
		want    interface{}
		wantErr bool
	}{
		{
			desc: "Basic struct",
			fields: []Field{
				{Name: "Name", Value: "Gopher"},
				{Name: "Legs", Value: float64(4)},
			},
			want: &struct {
				Name string
				Legs float64
			}{"Gopher", 4},
		},
		{
			desc: "Struct with tags",
			fields: []Field{
				{Name: "Name", Value: "Gopher"},
				{Name: "about", Value: "Likes slide rules."},
			},
			meta: &DocumentMetadata{Facets: []Facet{
				{Name: "Legs", Value: float64(4)},
				{Name: "Fur", Value: Atom("furry")},
			}},
			want: &struct {
				Name string
				Info string  `search:"about"`
				Legs float64 `search:",facet"`
				Fuzz Atom    `search:"Fur,facet"`
			}{"Gopher", "Likes slide rules.", 4, Atom("furry")},
		},
		{
			desc: "Bad field from tag",
			want: &struct {
				AlphaBeta string `search:"αβ"`
			}{},
			wantErr: true,
		},
		{
			desc: "Ignore missing field",
			fields: []Field{
				{Name: "Meaning", Value: float64(42)},
			},
			want:    &struct{}{},
			wantErr: true,
		},
		{
			desc: "Ignore unsettable field",
			fields: []Field{
				{Name: "meaning", Value: float64(42)},
			},
			want:    &struct{ meaning float64 }{}, // field not populated.
			wantErr: true,
		},
		{
			desc: "Error on missing facet",
			meta: &DocumentMetadata{Facets: []Facet{
				{Name: "Set", Value: Atom("yes")},
				{Name: "Missing", Value: Atom("no")},
			}},
			want: &struct {
				Set Atom `search:",facet"`
			}{Atom("yes")},
			wantErr: true,
		},
		{
			desc: "Error on unsettable facet",
			meta: &DocumentMetadata{Facets: []Facet{
				{Name: "Set", Value: Atom("yes")},
				{Name: "unset", Value: Atom("no")},
			}},
			want: &struct {
				Set Atom `search:",facet"`
			}{Atom("yes")},
			wantErr: true,
		},
		{
			desc: "Error setting ignored field",
			fields: []Field{
				{Name: "Set", Value: "yes"},
				{Name: "Ignored", Value: "no"},
			},
			want: &struct {
				Set     string
				Ignored string `search:"-"`
			}{Set: "yes"},
			wantErr: true,
		},
		{
			desc: "Error setting ignored facet",
			meta: &DocumentMetadata{Facets: []Facet{
				{Name: "Set", Value: Atom("yes")},
				{Name: "Ignored", Value: Atom("no")},
			}},
			want: &struct {
				Set     Atom `search:",facet"`
				Ignored Atom `search:"-,facet"`
			}{Set: Atom("yes")},
			wantErr: true,
		},
	}

	for _, tt := range testCases {
		// Make a pointer to an empty version of what want points to.
		dst := reflect.New(reflect.TypeOf(tt.want).Elem()).Interface()
		err := loadStructWithMeta(dst, tt.fields, tt.meta)
		if err != nil != tt.wantErr {
			t.Errorf("%s: got err %v; want err %t", tt.desc, err, tt.wantErr)
			continue
		}
		if !reflect.DeepEqual(dst, tt.want) {
			t.Errorf("%s: doesn't match\ngot:  %v\nwant: %v", tt.desc, dst, tt.want)
		}
	}
}

func TestSavingStruct(t *testing.T) {
	testCases := []struct {
		desc       string
		doc        interface{}
		wantFields []Field
		wantFacets []Facet
	}{
		{
			desc: "Basic struct",
			doc: &struct {
				Name string
				Legs float64
			}{"Gopher", 4},
			wantFields: []Field{
				{Name: "Name", Value: "Gopher"},
				{Name: "Legs", Value: float64(4)},
			},
		},
		{
			desc: "Struct with tags",
			doc: &struct {
				Name string
				Info string  `search:"about"`
				Legs float64 `search:",facet"`
				Fuzz Atom    `search:"Fur,facet"`
			}{"Gopher", "Likes slide rules.", 4, Atom("furry")},
			wantFields: []Field{
				{Name: "Name", Value: "Gopher"},
				{Name: "about", Value: "Likes slide rules."},
			},
			wantFacets: []Facet{
				{Name: "Legs", Value: float64(4)},
				{Name: "Fur", Value: Atom("furry")},
			},
		},
		{
			desc: "Ignore unexported struct fields",
			doc: &struct {
				Name string
				info string
				Legs float64 `search:",facet"`
				fuzz Atom    `search:",facet"`
			}{"Gopher", "Likes slide rules.", 4, Atom("furry")},
			wantFields: []Field{
				{Name: "Name", Value: "Gopher"},
			},
			wantFacets: []Facet{
				{Name: "Legs", Value: float64(4)},
			},
		},
		{
			desc: "Ignore fields marked -",
			doc: &struct {
				Name string
				Info string  `search:"-"`
				Legs float64 `search:",facet"`
				Fuzz Atom    `search:"-,facet"`
			}{"Gopher", "Likes slide rules.", 4, Atom("furry")},
			wantFields: []Field{
				{Name: "Name", Value: "Gopher"},
			},
			wantFacets: []Facet{
				{Name: "Legs", Value: float64(4)},
			},
		},
	}

	for _, tt := range testCases {
		fields, meta, err := saveStructWithMeta(tt.doc)
		if err != nil {
			t.Errorf("%s: got err %v; want nil", tt.desc, err)
			continue
		}
		if !reflect.DeepEqual(fields, tt.wantFields) {
			t.Errorf("%s: fields don't match\ngot:  %v\nwant: %v", tt.desc, fields, tt.wantFields)
		}
		if facets := meta.Facets; !reflect.DeepEqual(facets, tt.wantFacets) {
			t.Errorf("%s: facets don't match\ngot:  %v\nwant: %v", tt.desc, facets, tt.wantFacets)
		}
	}
}