-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathindexed_map_test.go
106 lines (87 loc) · 2.82 KB
/
indexed_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
package collections_test
import (
"testing"
"github.com/stretchr/testify/require"
"cosmossdk.io/collections"
"cosmossdk.io/collections/colltest"
"cosmossdk.io/collections/indexes"
)
type company struct {
City string
Vat uint64
}
type companyIndexes struct {
// City is an index of the company indexed map. It indexes a company
// given its city. The index is multi, meaning that there can be multiple
// companies from the same city.
City *indexes.Multi[string, string, company]
// Vat is an index of the company indexed map. It indexes a company
// given its VAT number. The index is unique, meaning that there can be
// only one VAT number for a company.
Vat *indexes.Unique[uint64, string, company]
}
func (c companyIndexes) IndexesList() []collections.Index[string, company] {
return []collections.Index[string, company]{c.City, c.Vat}
}
func newTestIndexedMap(schema *collections.SchemaBuilder) *collections.IndexedMap[string, company, companyIndexes] {
return collections.NewIndexedMap(schema, collections.NewPrefix(0), "companies", collections.StringKey, colltest.MockValueCodec[company](),
companyIndexes{
City: indexes.NewMulti(schema, collections.NewPrefix(1), "companies_by_city", collections.StringKey, collections.StringKey, func(pk string, value company) (string, error) {
return value.City, nil
}),
Vat: indexes.NewUnique(schema, collections.NewPrefix(2), "companies_by_vat", collections.Uint64Key, collections.StringKey, func(pk string, value company) (uint64, error) {
return value.Vat, nil
}),
},
)
}
func TestIndexedMap(t *testing.T) {
sk, ctx := colltest.MockStore()
schema := collections.NewSchemaBuilder(sk)
im := newTestIndexedMap(schema)
// test insertion
err := im.Set(ctx, "1", company{
City: "milan",
Vat: 0,
})
require.NoError(t, err)
err = im.Set(ctx, "2", company{
City: "milan",
Vat: 1,
})
require.NoError(t, err)
err = im.Set(ctx, "3", company{
City: "milan",
Vat: 4,
})
require.NoError(t, err)
pk, err := im.Indexes.Vat.MatchExact(ctx, 1)
require.NoError(t, err)
require.Equal(t, "2", pk)
// test a set which updates the indexes
err = im.Set(ctx, "2", company{
City: "milan",
Vat: 2,
})
require.NoError(t, err)
pk, err = im.Indexes.Vat.MatchExact(ctx, 2)
require.NoError(t, err)
require.Equal(t, "2", pk)
_, err = im.Indexes.Vat.MatchExact(ctx, 1)
require.ErrorIs(t, err, collections.ErrNotFound)
// test removal
err = im.Remove(ctx, "2")
require.NoError(t, err)
_, err = im.Indexes.Vat.MatchExact(ctx, 2)
require.ErrorIs(t, err, collections.ErrNotFound)
// test iteration
iter, err := im.Iterate(ctx, nil)
require.NoError(t, err)
keys, err := iter.Keys()
require.NoError(t, err)
require.Equal(t, []string{"1", "3"}, keys)
// test get
v, err := im.Get(ctx, "3")
require.NoError(t, err)
require.Equal(t, company{"milan", 4}, v)
}