1 package server
2
3 import (
4 "reflect"
5 "testing"
6
7 "google.golang.org/genproto/protobuf/field_mask"
8 )
9
10 func TestApplyFieldMask(t *testing.T) {
11 for _, test := range []struct {
12 name string
13 patchee interface{}
14 patcher interface{}
15 fieldMask *field_mask.FieldMask
16 expected interface{}
17 }{
18 {"nil fieldMask", &a{E: 64}, &a{E: 42}, nil, &a{E: 64}},
19 {"empty paths", &a{E: 63}, &a{E: 42}, &field_mask.FieldMask{}, &a{E: 63}},
20 {"simple path",
21 &a{E: 23, Foo: "test"},
22 &a{BeefCake: &b{}, E: 42},
23 &field_mask.FieldMask{Paths: []string{"e"}},
24 &a{E: 42, Foo: "test"}},
25 {"nested",
26 &a{BeefCake: &b{CowCount: 85}},
27 &a{BeefCake: &b{CowCount: 58, Data: nil}},
28 &field_mask.FieldMask{Paths: []string{"beef_cake.cow_count"}},
29 &a{BeefCake: &b{CowCount: 58}}},
30 {"multiple paths",
31 &a{BeefCake: &b{CowCount: 40, Data: []int{1, 2, 3}}, E: 34, Foo: "catapult"},
32 &a{BeefCake: &b{CowCount: 56}, Foo: "lettuce"},
33 &field_mask.FieldMask{Paths: []string{"beef_cake.cow_count", "foo"}},
34 &a{BeefCake: &b{CowCount: 56, Data: []int{1, 2, 3}}, E: 34, Foo: "lettuce"}},
35 } {
36 t.Run(test.name, func(t *testing.T) {
37 applyFieldMask(test.patchee, test.patcher, test.fieldMask)
38 if !reflect.DeepEqual(test.patchee, test.expected) {
39 t.Errorf("expected %v, but was %v", test.expected, test.patchee)
40 }
41 })
42 }
43 }
44
45 func TestGetValue(t *testing.T) {
46 for _, test := range []struct {
47 name string
48 input interface{}
49 path string
50 expected interface{}
51 }{
52 {"empty", &a{E: 45, Foo: "test"}, "", &a{E: 45, Foo: "test"}},
53 {"pointer-simple", &a{E: 45}, "E", 45},
54 {"pointer-nested", &a{BeefCake: &b{CowCount: 42}}, "beef_cake.cow_count", 42},
55 {"pointer-complex type", &a{BeefCake: &b{Data: []int{1, 2}}}, "beef_cake.data", []int{1, 2}},
56 {"pointer-invalid path", &a{Foo: "test"}, "x.y", nil},
57 {"simple", a{E: 45}, "E", 45},
58 {"nested", a{BeefCake: &b{CowCount: 42}}, "beef_cake.cow_count", 42},
59 {"complex type", a{BeefCake: &b{Data: []int{1, 2}}}, "beef_cake.data", []int{1, 2}},
60 {"invalid path", a{Foo: "test"}, "X.Y", nil},
61 } {
62 t.Run(test.name, func(t *testing.T) {
63 if actual := getField(test.input, test.path); actual.IsValid() {
64 if !reflect.DeepEqual(test.expected, actual.Interface()) {
65 t.Errorf("expected %v, but got %v", test.expected, actual)
66 }
67 } else if test.expected != nil {
68 t.Errorf("expected nil, but was %v", actual)
69 }
70 })
71 }
72 }
73
74 func TestSetValue(t *testing.T) {
75 for _, test := range []struct {
76 name string
77 obj interface{}
78 newValue interface{}
79 path string
80 expected interface{}
81 }{
82 {"simple", &a{E: 45}, 34, "e", 34},
83 {"nested", &a{BeefCake: &b{CowCount: 54}}, 43, "beef_cake.cow_count", 43},
84 {"complex type", &a{BeefCake: &b{Data: []int{1, 2}}}, []int{3, 4}, "beef_cake.data", []int{3, 4}},
85 } {
86 t.Run(test.name, func(t *testing.T) {
87 setValue(test.obj, reflect.ValueOf(test.newValue), test.path)
88 if actual := getField(test.obj, test.path).Interface(); !reflect.DeepEqual(actual, test.expected) {
89 t.Errorf("expected %v, but got %v", test.newValue, actual)
90 }
91 })
92 }
93 }
94
95 type a struct {
96 BeefCake *b
97 E int
98 Foo string
99 }
100
101 type b struct {
102 CowCount int
103 Data []int
104 }
105
View as plain text