forked from go-bongo/bongo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
difftracker_test.go
113 lines (94 loc) · 2.49 KB
/
difftracker_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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package bongo
import (
. "github.com/smartystreets/goconvey/convey"
"reflect"
"testing"
"time"
)
type FooChangeTest struct {
DocumentBase `bson:",inline"`
StringVal string
IntVal int
Timestamp time.Time
diffTracker *DiffTracker
Arr []string
}
func (f *FooChangeTest) GetDiffTracker() *DiffTracker {
v := reflect.ValueOf(f.diffTracker)
if !v.IsValid() || v.IsNil() {
f.diffTracker = NewDiffTracker(f)
}
return f.diffTracker
}
type FooBarChangeTest struct {
FooVal *FooChangeTest
BarVal string
}
func TestDiffTracker(t *testing.T) {
Convey("DiffTracker", t, func() {
Convey("should get changed fields when comparing two structs", func() {
foo1 := &FooChangeTest{
StringVal: "foo",
IntVal: 1,
Arr: []string{},
}
foo2 := &FooChangeTest{
StringVal: "bar",
IntVal: 2,
Arr: []string{},
}
diffs, err := GetChangedFields(foo1, foo2, false)
So(err, ShouldEqual, nil)
So(len(diffs), ShouldEqual, 2)
So(diffs[0], ShouldEqual, "StringVal")
So(diffs[1], ShouldEqual, "IntVal")
})
Convey("should get changed fields when comparing two structs with pointers", func() {
foobar1 := &FooBarChangeTest{
FooVal: &FooChangeTest{
StringVal: "foo",
IntVal: 5,
},
BarVal: "bar",
}
foobar2 := &FooBarChangeTest{
FooVal: &FooChangeTest{
StringVal: "foo",
IntVal: 10,
Timestamp: time.Now(),
},
BarVal: "BAR",
}
diffs, err := GetChangedFields(foobar1, foobar2, false)
So(err, ShouldEqual, nil)
So(len(diffs), ShouldEqual, 3)
So(diffs[0], ShouldEqual, "FooVal.IntVal")
So(diffs[1], ShouldEqual, "FooVal.Timestamp")
So(diffs[2], ShouldEqual, "BarVal")
})
Convey("should get changed fields when comparing two structs with nil pointers", func() {
foobar1 := &FooBarChangeTest{
BarVal: "bar",
}
foobar2 := &FooBarChangeTest{
BarVal: "BAR",
}
diffs, err := GetChangedFields(foobar1, foobar2, false)
So(err, ShouldEqual, nil)
So(len(diffs), ShouldEqual, 1)
So(diffs[0], ShouldEqual, "BarVal")
})
Convey("should get fields modified since difftracker reset", func() {
foo1 := &FooChangeTest{
StringVal: "foo",
IntVal: 1,
}
foo1.GetDiffTracker().Reset()
sess, _ := foo1.GetDiffTracker().NewSession(false)
So(sess.Modified("StringVal"), ShouldEqual, false)
foo1.StringVal = "bar"
sess, _ = foo1.GetDiffTracker().NewSession(false)
So(sess.Modified("StringVal"), ShouldEqual, true)
})
})
}