-
Notifications
You must be signed in to change notification settings - Fork 1
/
stores_test.go
192 lines (153 loc) · 4.72 KB
/
stores_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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package raccoon
import (
"github.com/stretchr/testify/assert"
"testing"
)
// Simple implementation of Edge interface
type record struct {
Data string
Source uint8
Dest uint8
}
func (r record) GetSource() uint8 {
return r.Source
}
func (r record) GetDest() uint8 {
return r.Dest
}
func newRecord(data string, source, dest uint8) record {
return record{
Data: data,
Source: source,
Dest: dest,
}
}
type recordKeyer struct{}
func (k *recordKeyer) Key(node uint8) []byte {
return []byte{node}
}
func (k *recordKeyer) MinKey() []byte {
return []byte{0}
}
func (k *recordKeyer) MaxKey() []byte {
return []byte{255}
}
var _ Edge[uint8] = (*record)(nil)
var _ NodeKeyer[uint8] = (*recordKeyer)(nil)
func TestEdgeStoreSetThenGet(t *testing.T) {
kv := NewMemKV()
var keyer NodeKeyer[uint8] = &recordKeyer{}
marshaler := &Json[record]{}
var store EdgeStore[record, uint8] = NewEdgeStore[record, uint8](kv, nil, keyer, marshaler)
record := newRecord("data", 1, 2)
err := store.Set(record)
assert.Nil(t, err)
opt, err := store.GetEdg(1, 2)
assert.Nil(t, err)
assert.False(t, opt.IsEmpty())
assert.Equal(t, opt.Value(), record)
}
func TestEdgeStoreCannotGetADeleteValue(t *testing.T) {
kv := NewMemKV()
var keyer NodeKeyer[uint8] = &recordKeyer{}
marshaler := &Json[record]{}
var store EdgeStore[record, uint8] = NewEdgeStore[record, uint8](kv, nil, keyer, marshaler)
record := newRecord("data", 1, 2)
err := store.Set(record)
assert.Nil(t, err)
err = store.Delete(record)
assert.Nil(t, err)
opt, err := store.GetEdg(1, 2)
assert.Nil(t, err)
assert.True(t, opt.IsEmpty())
}
func TestEdgeStoreGetSucessorsReturnDirectSucessors(t *testing.T) {
kv := NewMemKV()
var keyer NodeKeyer[uint8] = &recordKeyer{}
marshaler := &Json[record]{}
var store EdgeStore[record, uint8] = NewEdgeStore[record, uint8](kv, nil, keyer, marshaler)
rec1 := newRecord("data", 1, 2)
err := store.Set(rec1)
assert.Nil(t, err)
rec2 := newRecord("data", 1, 3)
err = store.Set(rec2)
assert.Nil(t, err)
sucessors, err := store.GetSucessors(1)
assert.Nil(t, err)
assert.Equal(t, 2, len(sucessors))
assert.Contains(t, sucessors, rec1)
assert.Contains(t, sucessors, rec2)
}
func TestEdgeStoreGetSucessorsReturnDirectAncestors(t *testing.T) {
kv := NewMemKV()
var keyer NodeKeyer[uint8] = &recordKeyer{}
marshaler := &Json[record]{}
var store EdgeStore[record, uint8] = NewEdgeStore[record, uint8](kv, nil, keyer, marshaler)
rec1 := newRecord("data", 1, 2)
err := store.Set(rec1)
assert.Nil(t, err)
rec2 := newRecord("data", 3, 2)
err = store.Set(rec2)
assert.Nil(t, err)
rec3 := newRecord("data", 9, 5)
err = store.Set(rec3)
assert.Nil(t, err)
sucessors, err := store.GetAncestors(2)
assert.Nil(t, err)
assert.Equal(t, 2, len(sucessors))
assert.Contains(t, sucessors, rec1)
assert.Contains(t, sucessors, rec2)
}
func TestSecondaryIdxStoreSetThenGet(t *testing.T) {
kv := NewMemKV()
var keyer NodeKeyer[uint8] = &recordKeyer{}
marshaler := &Json[record]{}
mapper := func(r record) []byte { return []byte(r.Data) }
var store SecondaryIndexStore[record, uint8] = NewSecondaryIdxStore[record, uint8](kv, nil, "index", keyer, mapper, marshaler)
rec1 := newRecord("data", 1, 2)
err := store.Set(rec1)
assert.Nil(t, err)
rec2 := newRecord("data", 2, 3)
err = store.Set(rec2)
assert.Nil(t, err)
records, err := store.GetByIdx([]byte("data"))
assert.Nil(t, err)
assert.Equal(t, 2, len(records))
assert.Contains(t, records, rec1)
assert.Contains(t, records, rec2)
}
func TestSecondaryIdxStoreSetThenGetFromUnknwonIdxReturnsEmptySlice(t *testing.T) {
kv := NewMemKV()
var keyer NodeKeyer[uint8] = &recordKeyer{}
marshaler := &Json[record]{}
mapper := func(r record) []byte { return []byte(r.Data) }
var store SecondaryIndexStore[record, uint8] = NewSecondaryIdxStore[record, uint8](kv, nil, "index", keyer, mapper, marshaler)
rec1 := newRecord("data", 1, 2)
err := store.Set(rec1)
assert.Nil(t, err)
rec2 := newRecord("data", 2, 3)
err = store.Set(rec2)
assert.Nil(t, err)
println("BAD")
records, err := store.GetByIdx([]byte("404"))
assert.Nil(t, err)
assert.Equal(t, 0, len(records))
}
func TestSecondaryIdxStoreDelete(t *testing.T) {
kv := NewMemKV()
var keyer NodeKeyer[uint8] = &recordKeyer{}
marshaler := &Json[record]{}
mapper := func(r record) []byte { return []byte(r.Data) }
var store SecondaryIndexStore[record, uint8] = NewSecondaryIdxStore[record, uint8](kv, nil, "index", keyer, mapper, marshaler)
rec1 := newRecord("data", 1, 2)
store.Set(rec1)
rec2 := newRecord("data", 2, 3)
store.Set(rec2)
err := store.Delete(rec1)
assert.Nil(t, err)
records, err := store.GetByIdx([]byte("data"))
assert.Nil(t, err)
assert.Equal(t, 1, len(records))
assert.NotContains(t, records, rec1)
assert.Contains(t, records, rec2)
}