-
Notifications
You must be signed in to change notification settings - Fork 9
/
indexers_test.go
81 lines (68 loc) · 1.8 KB
/
indexers_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
package collections
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestMultiIndex(t *testing.T) {
sk, ctx, _ := deps()
im := NewMultiIndex[string, uint64, person](
sk, 0,
StringKeyEncoder, Uint64KeyEncoder,
func(v person) string { return v.City },
)
// test insertions
persons := []person{
{
ID: 0,
City: "milan",
},
{
ID: 1,
City: "milan",
},
{
ID: 2,
City: "new york",
},
}
for _, p := range persons {
im.Insert(ctx, p.ID, p)
}
// test iterations and matches alongside PrimaryKeys ( and indirectly FullKeys )
// test ExactMatch
ks := im.ExactMatch(ctx, "milan").PrimaryKeys()
require.Equal(t, []uint64{0, 1}, ks)
// test ReverseExactMatch
ks = im.ReverseExactMatch(ctx, "milan").PrimaryKeys()
require.Equal(t, []uint64{1, 0}, ks)
// test after removal it is not present
im.Delete(ctx, persons[0].ID, persons[0])
ks = im.ExactMatch(ctx, "milan").PrimaryKeys()
require.Equal(t, []uint64{1}, ks)
// test iteration
iter := im.Iterate(ctx, PairRange[string, uint64]{}.Descending())
fk := iter.FullKey()
require.Equal(t, fk.K1(), "new york")
require.Equal(t, fk.K2(), uint64(2))
}
func TestIndexerIterator(t *testing.T) {
sk, ctx, _ := deps()
// test insertions
im := NewMultiIndex[string, uint64, person](
sk, 0,
StringKeyEncoder, Uint64KeyEncoder,
func(v person) string { return v.City },
)
im.Insert(ctx, 0, person{ID: 0, City: "milan"})
im.Insert(ctx, 1, person{ID: 1, City: "milan"})
iter := im.Iterate(ctx, PairRange[string, uint64]{})
defer iter.Close()
require.Equal(t, Join[string, uint64]("milan", 0), iter.FullKey())
require.Equal(t, uint64(0), iter.PrimaryKey())
// test next
iter.Next()
require.Equal(t, uint64(1), iter.PrimaryKey())
require.Equal(t, iter.Valid(), true)
iter.Next()
require.False(t, iter.Valid())
}