-
Notifications
You must be signed in to change notification settings - Fork 115
/
fieldmask_test.go
67 lines (57 loc) · 2.16 KB
/
fieldmask_test.go
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
package gorm
import (
"errors"
"testing"
"github.com/stretchr/testify/assert"
"google.golang.org/genproto/protobuf/field_mask"
)
type childTest struct {
FieldOne int
FieldTwo string
FieldThree *int
FieldFour []int
}
type topTest struct {
FieldA childTest
FieldB *childTest
}
func wrapInt(x int) *int {
return &x
}
func TestMergeWithMask(t *testing.T) {
source := &topTest{
FieldA: childTest{FieldOne: 22, FieldTwo: "catch", FieldThree: wrapInt(2), FieldFour: []int{1, 2, 3}},
FieldB: &childTest{FieldOne: 3, FieldTwo: "string", FieldThree: wrapInt(1), FieldFour: []int{3, 2, 1}},
}
dest := &topTest{}
err := MergeWithMask(source, dest, &field_mask.FieldMask{Paths: []string{"FieldB.FieldOne", "FieldA.FieldTwo", "FieldA.FieldThree", "FieldB.FieldFour"}})
assert.Equal(t, &topTest{
FieldA: childTest{FieldTwo: "catch", FieldThree: wrapInt(2)},
FieldB: &childTest{FieldOne: 3, FieldFour: []int{3, 2, 1}},
}, dest)
assert.Nil(t, err)
err = MergeWithMask(source, dest, &field_mask.FieldMask{Paths: []string{"FieldB.FieldDNE", "FieldA.FieldTwo"}})
assert.Equal(t, errors.New("Field path \"FieldB.FieldDNE\" doesn't exist in type *gorm.topTest"), err)
err = MergeWithMask(nil, dest, &field_mask.FieldMask{Paths: []string{"FieldB.FieldDNE"}})
assert.Equal(t, errors.New("Source object is nil"), err)
for _, fm := range []*field_mask.FieldMask{nil, {}} {
err = MergeWithMask(nil, nil, fm)
assert.Nil(t, err)
err = MergeWithMask(nil, dest, fm)
assert.Nil(t, err)
err = MergeWithMask(source, nil, fm)
assert.Nil(t, err)
err = MergeWithMask(source, dest.FieldA, fm)
assert.Nil(t, err)
}
err = MergeWithMask(source, nil, &field_mask.FieldMask{Paths: []string{"FieldB"}})
assert.Equal(t, errors.New("Destination object is nil"), err)
err = MergeWithMask(source, dest.FieldA, &field_mask.FieldMask{Paths: []string{"FieldB"}})
assert.Equal(t, errors.New("Types of source and destination objects do not match"), err)
dest = &topTest{}
err = MergeWithMask(source, dest, &field_mask.FieldMask{Paths: []string{"FieldA.FieldTwo", "FieldA.FieldFour.Anything"}})
assert.Equal(t, &topTest{
FieldA: childTest{FieldTwo: "catch"},
}, dest)
assert.Nil(t, err)
}