Repository navigation
Expand file tree
/
Copy pathordereddict_test.go
More file actions
230 lines (184 loc) · 5.66 KB
/
Copy pathordereddict_test.go
File metadata and controls
230 lines (184 loc) · 5.66 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package ordereddict
import (
"encoding/json"
"fmt"
"reflect"
"sync/atomic"
"testing"
"time"
"github.com/alecthomas/repr"
"github.com/stretchr/testify/assert"
)
type dictSerializationTest struct {
dict *Dict
serialized string
}
var (
dictSerializationTests = []dictSerializationTest{
{NewDict().Set("Foo", "Bar"), `{"Foo":"Bar"}`},
// Test an unserilizable member - This should not prevent the
// entire dict from serializing - only that member should be
// ignored.
{NewDict().Set("Foo", "Bar").
Set("Time", time.Unix(3000000000000000, 0)),
`{"Foo":"Bar","Time":null}`},
// Recursive dict
{NewDict().Set("Foo",
NewDict().Set("Bar", 2).
Set("Time", time.Unix(3000000000000000, 0))),
`{"Foo":{"Bar":2,"Time":null}}`},
// Ensure key order is preserved.
{NewDict().Set("A", 1).Set("B", 2), `{"A":1,"B":2}`},
{NewDict().Set("B", 1).Set("A", 2), `{"B":1,"A":2}`},
// Serialize with quotes
{NewDict().Set("foo\\'s quote", 1), `{"foo\\'s quote":1}`},
}
// Check that serialization decodes to the object.
dictUnserializationTest = []dictSerializationTest{
// Preserve order of keys on deserialization.
{NewDict().Set("A", uint64(1)).Set("B", uint64(2)), `{"A":1,"B":2}`},
{NewDict().Set("B", uint64(1)).Set("A", uint64(2)), `{"B":1,"A":2}`},
// Handle arrays, ints floats and bools
{NewDict().Set("B", uint64(1)).Set("A", uint64(2)), `{"B":1,"A":2}`},
{NewDict().Set("B", float64(1)).Set("A", uint64(2)), `{"B":1.0,"A":2}`},
{NewDict().Set("B", []interface{}{uint64(1)}).Set("A", uint64(2)), `{"B":[1],"A":2}`},
{NewDict().Set("B", true).Set("A", uint64(2)), `{"B":true,"A":2}`},
{NewDict().Set("B", nil).Set("A", uint64(2)), `{"B":null,"A":2}`},
// Embedded dicts decode into ordered dicts.
{NewDict().
Set("B", NewDict().Set("Zoo", "X").Set("Baz", "Y")).
Set("A", "Z"), `{"B":{"Zoo":"X","Baz":"Y"},"A":"Z"}`},
// Make sure we properly preserve uint64 (overflows int64)
{NewDict().
Set("Uint64", uint64(9223372036854775808)),
`{"Uint64": 9223372036854775808}`},
// We prefer uint64 but int64 is needed for negative numbers
{NewDict().
Set("Int64", int64(-500)),
`{"Int64": -500}`},
}
)
func TestDictSerialization(t *testing.T) {
for _, test := range dictSerializationTests {
serialized, err := json.Marshal(test.dict)
if err != nil {
t.Fatalf("Failed to serialize %v: %v", repr.String(test.dict), err)
}
assert.Equal(t, test.serialized, string(serialized))
}
}
func TestDictDeserialization(t *testing.T) {
for _, test := range dictUnserializationTest {
value := NewDict()
err := json.Unmarshal([]byte(test.serialized), value)
if err != nil {
t.Fatalf("Failed to serialize %v: %v", test.serialized, err)
}
// Make sure the keys are the same.
assert.Equal(t, test.dict.Keys(), value.Keys())
assert.Equal(t, test.dict.ToMap(), value.ToMap())
assert.True(t, reflect.DeepEqual(test.dict, value))
}
}
func TestTimestampDeserialization(t *testing.T) {
serialized := `{"Time":"2022-08-21T03:08:45.076884Z", "Time2":"2022-08-21T03:08:45.076884+07:00"}`
value := NewDict()
err := json.Unmarshal([]byte(serialized), value)
assert.NoError(t, err)
t1_any, pres := value.Get("Time")
assert.True(t, pres)
t1, ok := t1_any.(time.Time)
assert.True(t, ok)
assert.Equal(t, int64(1661051325), t1.Unix())
t2_any, pres := value.Get("Time2")
assert.True(t, pres)
t2, ok := t2_any.(time.Time)
assert.True(t, ok)
assert.Equal(t, int64(1661026125), t2.Unix())
}
func TestOrder(t *testing.T) {
test := NewDict().
Set("A", 1).
Set("B", 2)
assert.Equal(t, []string{"A", "B"}, test.Keys())
test = NewDict().
Set("B", 1).
Set("A", 2)
assert.Equal(t, []string{"B", "A"}, test.Keys())
}
func TestDelete(t *testing.T) {
test := NewDict().
Set("A", 1).
Set("B", 2).
Set("C", 3)
test.Delete("B")
assert.Equal(t, []string{"A", "C"}, test.Keys())
assert.Equal(t, 2, test.Len())
}
func TestDeleteCompactions(t *testing.T) {
clearCompactions()
CompactionThreashold = 10
test := NewDict()
for i := 0; i < 20; i++ {
key := fmt.Sprintf("Key%d", i)
test.Set(key, i)
}
// No compactions when setting fresh keys
assert.Equal(t, uint64(0), getCompactions())
// Setting always reorders the dict, since Set must update the
// key's order. This is equivalent to Delete() +
// Update(). Repeated setting of the same key will cause a lot of
// compactions - consider using Update() instead.
for i := 0; i < 20; i++ {
key := fmt.Sprintf("Key%d", i)
test.Set(key, i)
}
// Two compactions cycles every 10 keys
assert.Equal(t, uint64(2), getCompactions())
// Deleting keys will also cause compactions
for i := 0; i < 20; i++ {
key := fmt.Sprintf("Key%d", i)
test.Delete(key)
}
// We should rebalance twice - every 10 deletions.
assert.Equal(t, uint64(4), getCompactions())
}
func TestAliases(t *testing.T) {
test := NewDict().
Set("A", 1).
Set("B", 2).
Set("C", 3)
test.AliasField("Foo", "A")
assert.Equal(t, []string{"A", "B", "C"}, test.Keys())
assert.Equal(t, 3, test.Len())
value, ok := test.Get("Foo")
assert.True(t, ok)
assert.Equal(t, 1, value)
}
func TestDefault(t *testing.T) {
test := NewDict().
SetDefault("Default").
SetCaseInsensitive().
Set("A", 1).
Set("B", 2).
Set("C", 3)
value, pres := test.Get("a")
assert.True(t, pres)
assert.Equal(t, 1, value)
}
func TestCaseInsensitive(t *testing.T) {
test := NewDict().SetCaseInsensitive()
test.Set("FOO", 1)
value, pres := test.Get("foo")
assert.True(t, pres)
assert.Equal(t, 1, value)
test = NewDict().Set("FOO", 1)
value, pres = test.Get("foo")
assert.False(t, pres)
}
func clearCompactions() {
atomic.StoreUint64(&Compactions, 0)
}
func getCompactions() uint64 {
return atomic.LoadUint64(&Compactions)
}