forked from SierraSoftworks/sentry-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextra_test.go
140 lines (120 loc) · 3.34 KB
/
extra_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
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
package sentry
import (
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func ExampleExtra() {
cl := NewClient(
// You can define extra fields when you create your client
Extra(map[string]interface{}{
"redis": map[string]interface{}{
"host": "redis",
"port": 6379,
},
}),
)
cl.Capture(
// You can also define extra info when you send the event
// The extra object will be shallowly merged automatically,
// so this would send both `redis` and `cache`.
Extra(map[string]interface{}{
"cache": map[string]interface{}{
"key": "user.127.profile",
"hit": false,
},
}),
)
}
func TestExtra(t *testing.T) {
Convey("Extra", t, func() {
Convey("Extra()", func() {
data := map[string]interface{}{
"redis": map[string]interface{}{
"host": "redis",
"port": 6379,
},
}
Convey("Should return nil if the data is nil", func() {
So(Extra(nil), ShouldBeNil)
})
Convey("Should return an Option", func() {
So(Extra(data), ShouldImplement, (*Option)(nil))
})
Convey("Should use the correct Class()", func() {
So(Extra(data).Class(), ShouldEqual, "extra")
})
Convey("Should implement Merge()", func() {
So(Extra(data), ShouldImplement, (*MergeableOption)(nil))
})
})
Convey("Merge()", func() {
data1 := map[string]interface{}{
"redis": map[string]interface{}{
"host": "redis",
"port": 6379,
},
}
e1 := Extra(data1)
So(e1, ShouldNotBeNil)
e1m, ok := e1.(MergeableOption)
So(ok, ShouldBeTrue)
Convey("Should overwrite if it doesn't recognize the old option", func() {
So(e1m.Merge(&testOption{}), ShouldEqual, e1)
})
Convey("Should merge multiple extra entries", func() {
data2 := map[string]interface{}{
"cache": map[string]interface{}{
"key": "user.127.profile",
"hit": false,
},
}
e2 := Extra(data2)
So(e2, ShouldNotBeNil)
em := e1m.Merge(e2)
So(em, ShouldNotBeNil)
So(em, ShouldNotEqual, e1)
So(em, ShouldNotEqual, e2)
emm, ok := em.(*extraOption)
So(ok, ShouldBeTrue)
So(emm.extra, ShouldContainKey, "cache")
So(emm.extra, ShouldContainKey, "redis")
})
Convey("Should overwrite old entries with new ones", func() {
data2 := map[string]interface{}{
"redis": map[string]interface{}{
"host": "redis-dev",
"port": 6379,
},
}
e2 := Extra(data2)
So(e2, ShouldNotBeNil)
em := e1m.Merge(e2)
So(em, ShouldNotBeNil)
So(em, ShouldNotEqual, e1)
So(em, ShouldNotEqual, e2)
emm, ok := em.(*extraOption)
So(ok, ShouldBeTrue)
So(emm.extra, ShouldContainKey, "redis")
So(emm.extra["redis"], ShouldResemble, map[string]interface{}{
"host": "redis",
"port": 6379,
})
})
})
Convey("MarshalJSON", func() {
Convey("Should marshal the fields correctly", func() {
data := map[string]interface{}{
"redis": map[string]interface{}{
"host": "redis",
// Float mode required since we aren't deserializing into an int
"port": 6379.,
},
}
serialized := testOptionsSerialize(Extra(data))
So(serialized, ShouldNotBeNil)
So(serialized, ShouldHaveSameTypeAs, data)
So(serialized, ShouldResemble, data)
})
})
})
}