aboutsummaryrefslogtreecommitdiffhomepage
path: root/vendor/google.golang.org/appengine/datastore/prop_test.go
blob: 646a18f00dd2b4acdbc9ba6a20d0553a2c624633 (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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
// Copyright 2011 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 datastore

import (
	"reflect"
	"sort"
	"testing"
	"time"

	"google.golang.org/appengine"
)

func TestValidPropertyName(t *testing.T) {
	testCases := []struct {
		name string
		want bool
	}{
		// Invalid names.
		{"", false},
		{"'", false},
		{".", false},
		{"..", false},
		{".foo", false},
		{"0", false},
		{"00", false},
		{"X.X.4.X.X", false},
		{"\n", false},
		{"\x00", false},
		{"abc\xffz", false},
		{"foo.", false},
		{"foo..", false},
		{"foo..bar", false},
		{"☃", false},
		{`"`, false},
		// Valid names.
		{"AB", true},
		{"Abc", true},
		{"X.X.X.X.X", true},
		{"_", true},
		{"_0", true},
		{"a", true},
		{"a_B", true},
		{"f00", true},
		{"f0o", true},
		{"fo0", true},
		{"foo", true},
		{"foo.bar", true},
		{"foo.bar.baz", true},
		{"世界", true},
	}
	for _, tc := range testCases {
		got := validPropertyName(tc.name)
		if got != tc.want {
			t.Errorf("%q: got %v, want %v", tc.name, got, tc.want)
		}
	}
}

func TestStructCodec(t *testing.T) {
	type oStruct struct {
		O int
	}
	type pStruct struct {
		P int
		Q int
	}
	type rStruct struct {
		R int
		S pStruct
		T oStruct
		oStruct
	}
	type uStruct struct {
		U int
		v int
	}
	type vStruct struct {
		V string `datastore:",noindex"`
	}
	oStructCodec := &structCodec{
		fields: map[string]fieldCodec{
			"O": {path: []int{0}},
		},
		complete: true,
	}
	pStructCodec := &structCodec{
		fields: map[string]fieldCodec{
			"P": {path: []int{0}},
			"Q": {path: []int{1}},
		},
		complete: true,
	}
	rStructCodec := &structCodec{
		fields: map[string]fieldCodec{
			"R": {path: []int{0}},
			"S": {path: []int{1}, structCodec: pStructCodec},
			"T": {path: []int{2}, structCodec: oStructCodec},
			"O": {path: []int{3, 0}},
		},
		complete: true,
	}
	uStructCodec := &structCodec{
		fields: map[string]fieldCodec{
			"U": {path: []int{0}},
		},
		complete: true,
	}
	vStructCodec := &structCodec{
		fields: map[string]fieldCodec{
			"V": {path: []int{0}, noIndex: true},
		},
		complete: true,
	}

	testCases := []struct {
		desc        string
		structValue interface{}
		want        *structCodec
	}{
		{
			"oStruct",
			oStruct{},
			oStructCodec,
		},
		{
			"pStruct",
			pStruct{},
			pStructCodec,
		},
		{
			"rStruct",
			rStruct{},
			rStructCodec,
		},
		{
			"uStruct",
			uStruct{},
			uStructCodec,
		},
		{
			"non-basic fields",
			struct {
				B appengine.BlobKey
				K *Key
				T time.Time
			}{},
			&structCodec{
				fields: map[string]fieldCodec{
					"B": {path: []int{0}},
					"K": {path: []int{1}},
					"T": {path: []int{2}},
				},
				complete: true,
			},
		},
		{
			"struct tags with ignored embed",
			struct {
				A       int `datastore:"a,noindex"`
				B       int `datastore:"b"`
				C       int `datastore:",noindex"`
				D       int `datastore:""`
				E       int
				I       int `datastore:"-"`
				J       int `datastore:",noindex" json:"j"`
				oStruct `datastore:"-"`
			}{},
			&structCodec{
				fields: map[string]fieldCodec{
					"a": {path: []int{0}, noIndex: true},
					"b": {path: []int{1}},
					"C": {path: []int{2}, noIndex: true},
					"D": {path: []int{3}},
					"E": {path: []int{4}},
					"J": {path: []int{6}, noIndex: true},
				},
				complete: true,
			},
		},
		{
			"unexported fields",
			struct {
				A int
				b int
				C int `datastore:"x"`
				d int `datastore:"Y"`
			}{},
			&structCodec{
				fields: map[string]fieldCodec{
					"A": {path: []int{0}},
					"x": {path: []int{2}},
				},
				complete: true,
			},
		},
		{
			"nested and embedded structs",
			struct {
				A   int
				B   int
				CC  oStruct
				DDD rStruct
				oStruct
			}{},
			&structCodec{
				fields: map[string]fieldCodec{
					"A":   {path: []int{0}},
					"B":   {path: []int{1}},
					"CC":  {path: []int{2}, structCodec: oStructCodec},
					"DDD": {path: []int{3}, structCodec: rStructCodec},
					"O":   {path: []int{4, 0}},
				},
				complete: true,
			},
		},
		{
			"struct tags with nested and embedded structs",
			struct {
				A       int     `datastore:"-"`
				B       int     `datastore:"w"`
				C       oStruct `datastore:"xx"`
				D       rStruct `datastore:"y"`
				oStruct `datastore:"z"`
			}{},
			&structCodec{
				fields: map[string]fieldCodec{
					"w":   {path: []int{1}},
					"xx":  {path: []int{2}, structCodec: oStructCodec},
					"y":   {path: []int{3}, structCodec: rStructCodec},
					"z.O": {path: []int{4, 0}},
				},
				complete: true,
			},
		},
		{
			"unexported nested and embedded structs",
			struct {
				a int
				B int
				c uStruct
				D uStruct
				uStruct
			}{},
			&structCodec{
				fields: map[string]fieldCodec{
					"B": {path: []int{1}},
					"D": {path: []int{3}, structCodec: uStructCodec},
					"U": {path: []int{4, 0}},
				},
				complete: true,
			},
		},
		{
			"noindex nested struct",
			struct {
				A oStruct `datastore:",noindex"`
			}{},
			&structCodec{
				fields: map[string]fieldCodec{
					"A": {path: []int{0}, structCodec: oStructCodec, noIndex: true},
				},
				complete: true,
			},
		},
		{
			"noindex slice",
			struct {
				A []string `datastore:",noindex"`
			}{},
			&structCodec{
				fields: map[string]fieldCodec{
					"A": {path: []int{0}, noIndex: true},
				},
				hasSlice: true,
				complete: true,
			},
		},
		{
			"noindex embedded struct slice",
			struct {
				// vStruct has a single field, V, also with noindex.
				A []vStruct `datastore:",noindex"`
			}{},
			&structCodec{
				fields: map[string]fieldCodec{
					"A": {path: []int{0}, structCodec: vStructCodec, noIndex: true},
				},
				hasSlice: true,
				complete: true,
			},
		},
	}

	for _, tc := range testCases {
		got, err := getStructCodec(reflect.TypeOf(tc.structValue))
		if err != nil {
			t.Errorf("%s: getStructCodec: %v", tc.desc, err)
			continue
		}
		// can't reflect.DeepEqual b/c element order in fields map may differ
		if !isEqualStructCodec(got, tc.want) {
			t.Errorf("%s\ngot  %+v\nwant %+v\n", tc.desc, got, tc.want)
		}
	}
}

func isEqualStructCodec(got, want *structCodec) bool {
	if got.complete != want.complete {
		return false
	}
	if got.hasSlice != want.hasSlice {
		return false
	}
	if len(got.fields) != len(want.fields) {
		return false
	}
	for name, wantF := range want.fields {
		gotF := got.fields[name]
		if !reflect.DeepEqual(wantF.path, gotF.path) {
			return false
		}
		if wantF.noIndex != gotF.noIndex {
			return false
		}
		if wantF.structCodec != nil {
			if gotF.structCodec == nil {
				return false
			}
			if !isEqualStructCodec(gotF.structCodec, wantF.structCodec) {
				return false
			}
		}
	}

	return true
}

func TestRepeatedPropertyName(t *testing.T) {
	good := []interface{}{
		struct {
			A int `datastore:"-"`
		}{},
		struct {
			A int `datastore:"b"`
			B int
		}{},
		struct {
			A int
			B int `datastore:"B"`
		}{},
		struct {
			A int `datastore:"B"`
			B int `datastore:"-"`
		}{},
		struct {
			A int `datastore:"-"`
			B int `datastore:"A"`
		}{},
		struct {
			A int `datastore:"B"`
			B int `datastore:"A"`
		}{},
		struct {
			A int `datastore:"B"`
			B int `datastore:"C"`
			C int `datastore:"A"`
		}{},
		struct {
			A int `datastore:"B"`
			B int `datastore:"C"`
			C int `datastore:"D"`
		}{},
	}
	bad := []interface{}{
		struct {
			A int `datastore:"B"`
			B int
		}{},
		struct {
			A int
			B int `datastore:"A"`
		}{},
		struct {
			A int `datastore:"C"`
			B int `datastore:"C"`
		}{},
		struct {
			A int `datastore:"B"`
			B int `datastore:"C"`
			C int `datastore:"B"`
		}{},
	}
	testGetStructCodec(t, good, bad)
}

func TestFlatteningNestedStructs(t *testing.T) {
	type DeepGood struct {
		A struct {
			B []struct {
				C struct {
					D int
				}
			}
		}
	}
	type DeepBad struct {
		A struct {
			B []struct {
				C struct {
					D []int
				}
			}
		}
	}
	type ISay struct {
		Tomato int
	}
	type YouSay struct {
		Tomato int
	}
	type Tweedledee struct {
		Dee int `datastore:"D"`
	}
	type Tweedledum struct {
		Dum int `datastore:"D"`
	}

	good := []interface{}{
		struct {
			X []struct {
				Y string
			}
		}{},
		struct {
			X []struct {
				Y []byte
			}
		}{},
		struct {
			P []int
			X struct {
				Y []int
			}
		}{},
		struct {
			X struct {
				Y []int
			}
			Q []int
		}{},
		struct {
			P []int
			X struct {
				Y []int
			}
			Q []int
		}{},
		struct {
			DeepGood
		}{},
		struct {
			DG DeepGood
		}{},
		struct {
			Foo struct {
				Z int
			} `datastore:"A"`
			Bar struct {
				Z int
			} `datastore:"B"`
		}{},
	}
	bad := []interface{}{
		struct {
			X []struct {
				Y []string
			}
		}{},
		struct {
			X []struct {
				Y []int
			}
		}{},
		struct {
			DeepBad
		}{},
		struct {
			DB DeepBad
		}{},
		struct {
			ISay
			YouSay
		}{},
		struct {
			Tweedledee
			Tweedledum
		}{},
		struct {
			Foo struct {
				Z int
			} `datastore:"A"`
			Bar struct {
				Z int
			} `datastore:"A"`
		}{},
	}
	testGetStructCodec(t, good, bad)
}

func testGetStructCodec(t *testing.T, good []interface{}, bad []interface{}) {
	for _, x := range good {
		if _, err := getStructCodec(reflect.TypeOf(x)); err != nil {
			t.Errorf("type %T: got non-nil error (%s), want nil", x, err)
		}
	}
	for _, x := range bad {
		if _, err := getStructCodec(reflect.TypeOf(x)); err == nil {
			t.Errorf("type %T: got nil error, want non-nil", x)
		}
	}
}

func TestNilKeyIsStored(t *testing.T) {
	x := struct {
		K *Key
		I int
	}{}
	p := PropertyList{}
	// Save x as properties.
	p1, _ := SaveStruct(&x)
	p.Load(p1)
	// Set x's fields to non-zero.
	x.K = &Key{}
	x.I = 2
	// Load x from properties.
	p2, _ := p.Save()
	LoadStruct(&x, p2)
	// Check that x's fields were set to zero.
	if x.K != nil {
		t.Errorf("K field was not zero")
	}
	if x.I != 0 {
		t.Errorf("I field was not zero")
	}
}

func TestSaveStructOmitEmpty(t *testing.T) {
	// Expected props names are sorted alphabetically
	expectedPropNamesForSingles := []string{"EmptyValue", "NonEmptyValue", "OmitEmptyWithValue"}
	expectedPropNamesForSlices := []string{"NonEmptyValue", "NonEmptyValue", "OmitEmptyWithValue", "OmitEmptyWithValue"}

	testOmitted := func(expectedPropNames []string, src interface{}) {
		// t.Helper() - this is available from Go version 1.9, but we also support Go versions 1.6, 1.7, 1.8
		if props, err := SaveStruct(src); err != nil {
			t.Fatal(err)
		} else {
			// Collect names for reporting if diffs from expected and for easier sorting
			actualPropNames := make([]string, len(props))
			for i := range props {
				actualPropNames[i] = props[i].Name
			}
			// Sort actuals for comparing with already sorted expected names
			sort.Sort(sort.StringSlice(actualPropNames))
			if !reflect.DeepEqual(actualPropNames, expectedPropNames) {
				t.Errorf("Expected this properties: %v, got: %v", expectedPropNames, actualPropNames)
			}
		}
	}

	testOmitted(expectedPropNamesForSingles, &struct {
		EmptyValue         int
		NonEmptyValue      int
		OmitEmptyNoValue   int `datastore:",omitempty"`
		OmitEmptyWithValue int `datastore:",omitempty"`
	}{
		NonEmptyValue:      1,
		OmitEmptyWithValue: 2,
	})

	testOmitted(expectedPropNamesForSlices, &struct {
		EmptyValue         []int
		NonEmptyValue      []int
		OmitEmptyNoValue   []int `datastore:",omitempty"`
		OmitEmptyWithValue []int `datastore:",omitempty"`
	}{
		NonEmptyValue:      []int{1, 2},
		OmitEmptyWithValue: []int{3, 4},
	})

	testOmitted(expectedPropNamesForSingles, &struct {
		EmptyValue         bool
		NonEmptyValue      bool
		OmitEmptyNoValue   bool `datastore:",omitempty"`
		OmitEmptyWithValue bool `datastore:",omitempty"`
	}{
		NonEmptyValue:      true,
		OmitEmptyWithValue: true,
	})

	testOmitted(expectedPropNamesForSlices, &struct {
		EmptyValue         []bool
		NonEmptyValue      []bool
		OmitEmptyNoValue   []bool `datastore:",omitempty"`
		OmitEmptyWithValue []bool `datastore:",omitempty"`
	}{
		NonEmptyValue:      []bool{true, true},
		OmitEmptyWithValue: []bool{true, true},
	})

	testOmitted(expectedPropNamesForSingles, &struct {
		EmptyValue         string
		NonEmptyValue      string
		OmitEmptyNoValue   string `datastore:",omitempty"`
		OmitEmptyWithValue string `datastore:",omitempty"`
	}{
		NonEmptyValue:      "s",
		OmitEmptyWithValue: "s",
	})

	testOmitted(expectedPropNamesForSlices, &struct {
		EmptyValue         []string
		NonEmptyValue      []string
		OmitEmptyNoValue   []string `datastore:",omitempty"`
		OmitEmptyWithValue []string `datastore:",omitempty"`
	}{
		NonEmptyValue:      []string{"s1", "s2"},
		OmitEmptyWithValue: []string{"s3", "s4"},
	})

	testOmitted(expectedPropNamesForSingles, &struct {
		EmptyValue         float32
		NonEmptyValue      float32
		OmitEmptyNoValue   float32 `datastore:",omitempty"`
		OmitEmptyWithValue float32 `datastore:",omitempty"`
	}{
		NonEmptyValue:      1.1,
		OmitEmptyWithValue: 1.2,
	})

	testOmitted(expectedPropNamesForSlices, &struct {
		EmptyValue         []float32
		NonEmptyValue      []float32
		OmitEmptyNoValue   []float32 `datastore:",omitempty"`
		OmitEmptyWithValue []float32 `datastore:",omitempty"`
	}{
		NonEmptyValue:      []float32{1.1, 2.2},
		OmitEmptyWithValue: []float32{3.3, 4.4},
	})

	testOmitted(expectedPropNamesForSingles, &struct {
		EmptyValue         time.Time
		NonEmptyValue      time.Time
		OmitEmptyNoValue   time.Time `datastore:",omitempty"`
		OmitEmptyWithValue time.Time `datastore:",omitempty"`
	}{
		NonEmptyValue:      now,
		OmitEmptyWithValue: now,
	})

	testOmitted(expectedPropNamesForSlices, &struct {
		EmptyValue         []time.Time
		NonEmptyValue      []time.Time
		OmitEmptyNoValue   []time.Time `datastore:",omitempty"`
		OmitEmptyWithValue []time.Time `datastore:",omitempty"`
	}{
		NonEmptyValue:      []time.Time{now, now},
		OmitEmptyWithValue: []time.Time{now, now},
	})
}