-
Notifications
You must be signed in to change notification settings - Fork 1
/
map_test.go
113 lines (90 loc) · 2.77 KB
/
map_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 indexmap
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestIndexMap(t *testing.T) {
imap := NewIndexMap(NewPrimaryIndex(func(value *Person) int64 {
return value.ID
}))
ok := imap.AddIndex(NameIndex, NewSecondaryIndex(func(value *Person) []any {
return []any{value.Name}
}))
assert.True(t, ok)
persons := GenPersons()
InsertData(imap, persons)
for _, person := range persons {
assert.True(t, imap.Contain(person.ID))
assert.Equal(t,
person, imap.Get(person.ID))
assert.Equal(t,
person, imap.GetBy(NameIndex, person.Name))
assert.Nil(t, imap.GetBy(InvalidIndex, person.Name))
result := imap.GetAllBy(NameIndex, person.Name)
assert.Equal(t, 1, len(result))
assert.Contains(t, result, person)
assert.Nil(t, imap.getAllBy(InvalidIndex, person.Name))
}
// Add index after inserting data
ok = imap.AddIndex(CityIndex, NewSecondaryIndex(func(value *Person) []any {
return []any{value.City}
}))
assert.True(t, ok)
for _, person := range persons {
assert.Equal(t,
person, imap.GetBy(NameIndex, person.Name))
result := imap.GetAllBy(CityIndex, person.City)
assert.Contains(t, result, person)
}
// Remove
imap.Remove(persons[0].ID)
assert.Nil(t, imap.Get(persons[0].ID))
assert.Nil(t, imap.GetBy(NameIndex, persons[0].Name))
assert.Empty(t, imap.GetAllBy(NameIndex, persons[0].Name))
imap.RemoveBy(CityIndex, "San Francisco")
assert.Empty(t, imap.GetAllBy(CityIndex, "San Francisco"))
assert.Equal(t, 1, len(imap.GetAllBy(CityIndex, "Shanghai")))
// Update
imap.Clear()
InsertData(imap, persons)
imap.Update(persons[0].ID, func(value *Person) (*Person, bool) {
value.Name = "Tracer"
return value, true
})
assert.Equal(t, "Tracer", imap.Get(persons[0].ID).Name)
count := len(imap.GetAllBy(CityIndex, "Shanghai"))
imap.UpdateBy(CityIndex, "Shanghai", func(value *Person) (*Person, bool) {
value.City = "Beijing"
return value, true
})
assert.Empty(t, imap.GetAllBy(CityIndex, "Shanghai"))
assert.Equal(t, count, len(imap.GetAllBy(CityIndex, "Beijing")))
// Collect
keys, values := imap.Collect()
assert.Equal(t, imap.Len(), len(keys))
assert.Equal(t, imap.Len(), len(values))
for i := range keys {
assert.Equal(t, values[i], imap.Get(keys[i]))
}
// Range
count = 0
imap.Range(func(key int64, value *Person) bool {
count++
assert.Equal(t, value, imap.Get(key))
return true
})
assert.Equal(t, imap.Len(), count)
}
func TestAddExistedIndex(t *testing.T) {
imap := NewIndexMap(NewPrimaryIndex(func(value *Person) int64 {
return value.ID
}))
ok := imap.AddIndex(NameIndex, NewSecondaryIndex(func(value *Person) []any {
return []any{value.Name}
}))
assert.True(t, ok)
ok = imap.AddIndex(NameIndex, NewSecondaryIndex(func(value *Person) []any {
return []any{value.City}
}))
assert.False(t, ok)
}