-
Notifications
You must be signed in to change notification settings - Fork 88
/
entry_test.go
164 lines (135 loc) · 4.5 KB
/
entry_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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package gonx
import (
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func TestEntry(t *testing.T) {
Convey("Test Entry", t, func() {
Convey("Test get Entry fields", func() {
entry := NewEntry(Fields{"foo": "1", "bar": "not a number"})
Convey("Get all fields", func() {
// Get all fields
fields := entry.Fields()
So(fields, ShouldContainKey, "foo")
So(fields, ShouldContainKey, "bar")
})
Convey("Get raw string value", func() {
// Get existings field
val, err := entry.Field("foo")
So(err, ShouldBeNil)
So(val, ShouldEqual, "1")
// Get field that does not exist
val, err = entry.Field("baz")
So(err, ShouldNotBeNil)
So(val, ShouldEqual, "")
})
Convey("Get float values", func() {
// Get existings field
val, err := entry.FloatField("foo")
So(err, ShouldBeNil)
So(val, ShouldEqual, 1.0)
// Type casting eror
val, err = entry.FloatField("bar")
So(err, ShouldNotBeNil)
So(val, ShouldEqual, 0.0)
// Get field that does not exist
val, err = entry.FloatField("baz")
So(err, ShouldNotBeNil)
So(val, ShouldEqual, 0.0)
})
Convey("Get int64 values", func() {
// Get existings field
val, err := entry.IntField64("foo")
So(err, ShouldBeNil)
So(val, ShouldEqual, 1)
// Type casting eror
val, err = entry.IntField64("bar")
So(err, ShouldNotBeNil)
So(val, ShouldEqual, 0)
// Get field that does not exist
val, err = entry.IntField64("baz")
So(err, ShouldNotBeNil)
So(val, ShouldEqual, 0)
})
Convey("Get int values", func() {
// Get existings field
val, err := entry.IntField("foo")
So(err, ShouldBeNil)
So(val, ShouldEqual, 1)
// Type casting eror
val, err = entry.IntField("bar")
So(err, ShouldNotBeNil)
So(val, ShouldEqual, 0)
// Get field that does not exist
val, err = entry.IntField("baz")
So(err, ShouldNotBeNil)
So(val, ShouldEqual, 0)
})
})
Convey("Test set Entry fields", func() {
entry := NewEmptyEntry()
Convey("Set raw string value", func() {
// Set field value
entry.SetField("foo", "123")
val, err := entry.Field("foo")
So(err, ShouldBeNil)
So(val, ShouldEqual, "123")
// Ovewrite value
entry.SetField("foo", "234")
val, err = entry.Field("foo")
So(err, ShouldBeNil)
So(val, ShouldEqual, "234")
})
Convey("Test set float Entry fields", func() {
entry.SetFloatField("foo", 123.4567)
val, err := entry.Field("foo")
So(err, ShouldBeNil)
So(val, ShouldEqual, "123.46")
})
Convey("Test set uint Entry fields", func() {
entry.SetUintField("foo", 123)
val, err := entry.Field("foo")
So(err, ShouldBeNil)
So(val, ShouldEqual, "123")
})
})
Convey("Test Entries merge", func() {
entry1 := NewEntry(Fields{"foo": "1", "bar": "hello"})
entry2 := NewEntry(Fields{"foo": "2", "bar": "hello", "name": "alpha"})
entry1.Merge(entry2)
val, err := entry1.Field("foo")
So(err, ShouldBeNil)
So(val, ShouldEqual, "2")
val, err = entry1.Field("bar")
So(err, ShouldBeNil)
So(val, ShouldEqual, "hello")
val, err = entry1.Field("name")
So(err, ShouldBeNil)
So(val, ShouldEqual, "alpha")
})
Convey("Test Entry fields hash", func() {
entry1 := NewEntry(Fields{"foo": "1", "bar": "Hello world #1", "name": "alpha"})
entry2 := NewEntry(Fields{"foo": "2", "bar": "Hello world #2", "name": "alpha"})
entry3 := NewEntry(Fields{"foo": "2", "bar": "Hello world #3", "name": "alpha"})
entry4 := NewEntry(Fields{"foo": "3", "bar": "Hello world #4", "name": "beta"})
fields := []string{"name"}
So(entry1.FieldsHash(fields), ShouldEqual, entry2.FieldsHash(fields))
So(entry1.FieldsHash(fields), ShouldEqual, entry3.FieldsHash(fields))
So(entry1.FieldsHash(fields), ShouldNotEqual, entry4.FieldsHash(fields))
fields = []string{"name", "foo"}
So(entry1.FieldsHash(fields), ShouldNotEqual, entry2.FieldsHash(fields))
So(entry2.FieldsHash(fields), ShouldEqual, entry3.FieldsHash(fields))
So(entry1.FieldsHash(fields), ShouldNotEqual, entry4.FieldsHash(fields))
So(entry2.FieldsHash(fields), ShouldNotEqual, entry4.FieldsHash(fields))
})
Convey("Test partial Entry", func() {
entry := NewEntry(Fields{"foo": "1", "bar": "Hello world #1", "name": "alpha"})
partial := entry.Partial([]string{"name", "foo"})
So(len(partial.fields), ShouldEqual, 2)
val, _ := partial.Field("name")
So(val, ShouldEqual, "alpha")
val, _ = partial.Field("foo")
So(val, ShouldEqual, "1")
})
})
}