-
Notifications
You must be signed in to change notification settings - Fork 1
/
index_test.go
73 lines (56 loc) · 1.5 KB
/
index_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
package indexmap
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestPrimaryIndex(t *testing.T) {
index := NewPrimaryIndex(func(value *Person) int64 {
return value.ID
})
persons := GenPersons()
for _, person := range persons {
index.insert(person)
}
for _, person := range persons {
assert.Equal(t,
person, index.get(person.ID))
}
// Insert overwrite
overwritePerson := &Person{1, "Tracer", 23, "London", []string{"Bob"}}
index.insert(overwritePerson)
assert.Equal(t, overwritePerson, index.get(overwritePerson.ID))
// Remove get nil
index.remove(overwritePerson.ID)
assert.Nil(t, index.get(overwritePerson.ID))
}
func TestSecondaryIndex(t *testing.T) {
// Many-to-One index
index := NewSecondaryIndex(func(value *Person) []any {
return []any{value.Name, value.City}
})
persons := GenPersons()
for _, person := range persons {
index.insert(person)
}
for _, person := range persons {
result := index.get(person.Name)
assert.Equal(t, 1, len(result))
assert.Contains(t,
result, person)
result = index.get(person.City)
assert.Contains(t,
result, person)
}
// Insert makes One-to-Many, Many-to-Many
ashe2 := &Person{4, "Ashe", 83, "Chengdu", nil}
index.insert(ashe2)
result := index.get(ashe2.Name)
assert.Equal(t, 2, len(result))
assert.Contains(t, result, ashe2)
assert.Contains(t, result, persons[0])
// Remove
index.remove(ashe2)
result = index.get(persons[0].Name)
assert.Equal(t, 1, len(result))
assert.Contains(t, result, persons[0])
}