-
Notifications
You must be signed in to change notification settings - Fork 4
/
singlet_test.go
162 lines (135 loc) · 4.69 KB
/
singlet_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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package fluxdb
import (
"encoding/hex"
"errors"
"math"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestKeyForSinglet(t *testing.T) {
tests := []struct {
name string
singlet Singlet
expectedHex string
}{
{"standard", testSinglet("abc"), "fff1616263"},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
assert.Equal(t, test.expectedHex, hex.EncodeToString(KeyForSinglet(test.singlet)))
})
}
}
func TestKeyForSingletAt(t *testing.T) {
tests := []struct {
name string
singlet Singlet
height uint64
expectedHex string
}{
{"zero", testSinglet("abc"), 0, "fff1616263ffffffffffffffff"},
{"standard", testSinglet("abc"), 10, "fff1616263fffffffffffffff5"},
{"max", testSinglet("abc"), math.MaxUint64, "fff16162630000000000000000"},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
assert.Equal(t, test.expectedHex, hex.EncodeToString(KeyForSingletAt(test.singlet, test.height)))
})
}
}
func TestKeyForSingletRow(t *testing.T) {
tests := []struct {
name string
entry SingletEntry
expectedHex string
}{
{"zero", testSinglet("abc").entry(t, 0, "ghi"), "fff1616263ffffffffffffffff"},
{"standard", testSinglet("abc").entry(t, 10, "ghi"), "fff1616263fffffffffffffff5"},
{"max", testSinglet("abc").entry(t, math.MaxUint64, "ghi"), "fff16162630000000000000000"},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
assert.Equal(t, test.expectedHex, hex.EncodeToString(KeyForSingletEntry(test.entry)))
})
}
}
func TestRegisterSingletFactory(t *testing.T) {
tests := []struct {
name string
inCollection uint16
inCollectionName string
expectedPanic error
}{
// Beware, some collection are already injected by the system, so be sure to not use one of them.
// - IndexSinglet 0xFFFF
// - testSinglet 0xFFF1
// - testSinglet 0xFFF2
{"non-reserved, smallest", 0x0000, "smallest", nil},
{"non-reserved, highest", 0xEEEF, "highest", nil},
{"reserved, smallest", 0xFFF0, "reserved_smallest", errors.New("collections identifier 0xFFF0 is reserved, libraries identifiers must be within 0x0000 and 0xEEEF")},
{"reserved, highest", 0xFFFF, "reserved_highest", errors.New("collections identifier 0xFFFF is reserved, libraries identifiers must be within 0x0000 and 0xEEEF")},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
defer func() {
delete(collections, test.inCollection)
delete(singletFactories, test.inCollection)
}()
factory := func(identifier []byte) (Singlet, error) {
return testSinglet(string(identifier)), nil
}
if test.expectedPanic != nil {
panicked, value := didPanic(func() { RegisterSingletFactory(test.inCollection, test.inCollectionName, factory) })
require.True(t, panicked, "The func should have panic but did not")
require.Equal(t, test.expectedPanic, value)
// The collection 0xFFFF is already registered, so we cannnot check if it's not present anymore
if test.inCollection != 0xFFFF {
assert.NotContains(t, collections, test.inCollection, "collections")
assert.NotContains(t, singletFactories, test.inCollection, "singlet factories")
}
return
}
RegisterSingletFactory(test.inCollection, test.inCollectionName, factory)
assert.Equal(t, Collection{Identifier: test.inCollection, Name: test.inCollectionName}, collections[test.inCollection])
singlet, err := singletFactories[test.inCollection]([]byte("test"))
require.NoError(t, err)
assert.Equal(t, testSinglet("test"), singlet)
})
}
}
var testSingletCollection uint16 = 0xFFF1
var testSingletCollectionName string = "sts"
type testSinglet string
func init() {
registerSingletFactory(testSingletCollection, "sts", func(identifier []byte) (Singlet, error) {
return newTestSinglet(string(identifier[0:3])), nil
})
}
func newTestSinglet(elementName string) testSinglet {
if len(elementName) != 3 {
panic("test singlet name must always be 3 characters long")
}
return testSinglet(elementName)
}
func (s testSinglet) Collection() uint16 {
return testSingletCollection
}
func (s testSinglet) Identifier() []byte {
return []byte(s)
}
func (s testSinglet) Entry(height uint64, data []byte) (SingletEntry, error) {
return testSingletEntry{NewBaseSingletEntry(s, height, data)}, nil
}
func (s testSinglet) entry(tt *testing.T, height uint64, data string) SingletEntry {
return testSingletEntry{NewBaseSingletEntry(s, height, []byte(data))}
}
func (s testSinglet) String() string {
return testSingletCollectionName + ":" + string(s)
}
type testSingletEntry struct {
BaseSingletEntry
}
func (e testSingletEntry) data() string {
return string(e.value)
}