This repository has been archived by the owner on Oct 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
initialize_index_test.go
114 lines (96 loc) · 3.2 KB
/
initialize_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
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
package storage
import (
"testing"
"github.com/Worldcoin/hubble-commander/db"
"github.com/Worldcoin/hubble-commander/models"
"github.com/Worldcoin/hubble-commander/testutils"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
bh "github.com/timshannon/badgerhold/v4"
)
type InitializeIndexTestSuite struct {
*require.Assertions
suite.Suite
storage *TestStorage
}
func (s *InitializeIndexTestSuite) SetupSuite() {
s.Assertions = require.New(s.T())
}
func (s *InitializeIndexTestSuite) SetupTest() {
var err error
s.storage, err = NewTestStorage()
s.NoError(err)
}
func (s *InitializeIndexTestSuite) TearDownTest() {
err := s.storage.Teardown()
s.NoError(err)
}
func (s *InitializeIndexTestSuite) TestAccountLeaf_PublicKey_IndexWorks() {
pk1 := models.PublicKey{1, 2, 3}
pk2 := models.PublicKey{4, 5, 6}
_, err := s.storage.AccountTree.unsafeSet(&models.AccountLeaf{
PubKeyID: 0,
PublicKey: pk1,
})
s.NoError(err)
_, err = s.storage.AccountTree.unsafeSet(&models.AccountLeaf{
PubKeyID: 1,
PublicKey: pk1,
})
s.NoError(err)
_, err = s.storage.AccountTree.unsafeSet(&models.AccountLeaf{
PubKeyID: 2,
PublicKey: pk2,
})
s.NoError(err)
indexValues := s.getPublicKeyIndexValues(models.AccountLeafName)
s.Len(indexValues, 3)
s.Len(indexValues[models.ZeroPublicKey], 0) // value set due to index initialization, see NewAccountTree
s.Len(indexValues[pk1], 2)
s.Len(indexValues[pk2], 1)
}
func (s *InitializeIndexTestSuite) TestAccountLeaf_PublicKey_AccountsWithZeroPublicKeysAreNotIndexed() {
_, err := s.storage.AccountTree.unsafeSet(&models.AccountLeaf{
PubKeyID: 0,
PublicKey: models.ZeroPublicKey,
})
s.NoError(err)
indexValues := s.getPublicKeyIndexValues(models.AccountLeafName)
s.Len(indexValues, 1)
s.Len(indexValues[models.ZeroPublicKey], 0) // value set due to index initialization, see NewAccountTree
}
// This test checks an edge case that we introduced by not indexing AccountLeaf structs with zero public keys.
func (s *InitializeIndexTestSuite) TestAccountLeaf_PublicKey_FindUsingIndexWorksWhenThereAreOnlyAccountsWithZeroPublicKey() {
_, err := s.storage.AccountTree.unsafeSet(&models.AccountLeaf{
PubKeyID: 1,
PublicKey: models.ZeroPublicKey, // zero public key values are not indexed
})
s.NoError(err)
accounts := make([]models.AccountLeaf, 0, 1)
err = s.storage.database.Badger.Find(
&accounts,
bh.Where("PublicKey").Ge(models.ZeroPublicKey).Index("PublicKey"),
)
s.NoError(err)
s.Len(accounts, 0)
}
func (s *InitializeIndexTestSuite) getPublicKeyIndexValues(typeName []byte) map[models.PublicKey]bh.KeyList {
indexValues := make(map[models.PublicKey]bh.KeyList)
s.iterateIndex(typeName, "PublicKey", func(encodedKey []byte, keyList bh.KeyList) {
var publicKey models.PublicKey
err := db.Decode(encodedKey, &publicKey)
s.NoError(err)
indexValues[publicKey] = keyList
})
return indexValues
}
func (s *InitializeIndexTestSuite) iterateIndex(
typeName []byte,
indexName string,
handleIndex func(encodedKey []byte, keyList bh.KeyList),
) {
testutils.IterateIndex(s.Assertions, s.storage.database.Badger, typeName, indexName, handleIndex)
}
func TestInitializeIndexTestSuite(t *testing.T) {
suite.Run(t, new(InitializeIndexTestSuite))
}