-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathput_change_test.go
99 lines (81 loc) · 2.05 KB
/
put_change_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
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
package changeset
import (
"reflect"
"testing"
"github.com/go-rel/changeset/params"
"github.com/go-rel/rel"
"github.com/stretchr/testify/assert"
)
func TestPutChange(t *testing.T) {
ch := &Changeset{
changes: make(map[string]interface{}),
values: map[string]interface{}{
"field1": 0,
},
types: map[string]reflect.Type{
"field1": reflect.TypeOf(0),
},
}
assert.Nil(t, ch.Error())
assert.Nil(t, ch.Errors())
assert.Equal(t, 0, len(ch.Changes()))
// normal put changes
PutChange(ch, "field1", 10)
assert.Nil(t, ch.Error())
assert.Nil(t, ch.Errors())
assert.Equal(t, 1, len(ch.Changes()))
assert.Equal(t, 10, ch.Changes()["field1"])
// put changes not valid and not allowed to error
PutChange(ch, "field1", "10")
assert.NotNil(t, ch.Error())
assert.NotNil(t, ch.Errors())
assert.Equal(t, 1, len(ch.Errors()))
assert.Equal(t, "field1 is invalid", ch.Error().Error())
assert.Equal(t, 1, len(ch.Changes()))
assert.Equal(t, 10, ch.Changes()["field1"])
}
func TestPutChange_ptr(t *testing.T) {
var (
nullable = false
a struct {
ID int
Nullable *bool
}
)
ch := Cast(a, params.Map{}, []string{})
PutChange(ch, "nullable", &nullable)
assert.Nil(t, ch.Error())
assert.Equal(t, &nullable, ch.Changes()["nullable"])
assert.NotPanics(t, func() {
rel.Apply(rel.NewDocument(&a), ch)
assert.Equal(t, &nullable, a.Nullable)
})
}
func TestPutChange_nil(t *testing.T) {
var a struct {
ID int
Nullable *bool
}
ch := Cast(a, params.Map{}, []string{})
PutChange(ch, "nullable", nil)
assert.Nil(t, ch.Error())
assert.Equal(t, nil, ch.Changes()["nullable"])
assert.NotPanics(t, func() {
rel.Apply(rel.NewDocument(&a), ch)
assert.Nil(t, a.Nullable)
})
}
func TestPutChange_typedNil(t *testing.T) {
var a struct {
ID int
Nullable *bool
}
ch := Cast(a, params.Map{}, []string{})
PutChange(ch, "nullable", (*bool)(nil))
assert.Nil(t, ch.Error())
assert.Equal(t, nil, ch.Changes()["nullable"])
assert.NotPanics(t, func() {
rel.Apply(rel.NewDocument(&a), ch)
assert.Nil(t, a.Nullable)
})
}