-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathspace_test.go
108 lines (85 loc) · 2.27 KB
/
space_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
package esdb
import (
"bytes"
"reflect"
"testing"
)
func create(id []byte) *Space {
buffer := bytes.NewBuffer([]byte{})
writer := newSpace(buffer, []byte("a"))
populateSpace(writer)
writer.write()
return openSpace(bytes.NewReader(buffer.Bytes()), []byte("a"), 0, int64(buffer.Len()))
}
func populateSpace(space *spaceWriter) {
es := events{
newEvent([]byte("1"), 2),
newEvent([]byte("2"), 3),
newEvent([]byte("3"), 1),
}
space.add(es[0], "a", map[string]string{"ts": "", "i": "i1"})
space.add(es[1], "b", map[string]string{"ts": "", "i": "i2"})
space.add(es[2], "b", map[string]string{"ts": "", "i": "i1"})
}
func fetchPrimary(space *Space, primary string) []string {
found := make([]string, 0)
space.Scan(primary, func(event *Event) bool {
found = append(found, string(event.Data))
return true
})
return found
}
func fetchIndex(space *Space, index, value string) []string {
found := make([]string, 0)
space.ScanIndex(index, value, func(event *Event) bool {
found = append(found, string(event.Data))
return true
})
return found
}
func TestSpaceIndexScanning(t *testing.T) {
space := create([]byte("a"))
var tests = []struct {
index string
value string
want []string
}{
{"ts", "", []string{"2", "1", "3"}},
{"i", "i1", []string{"1", "3"}},
{"i", "i2", []string{"2"}},
{"i", "i3", []string{}},
}
for i, test := range tests {
found := fetchIndex(space, test.index, test.value)
if !reflect.DeepEqual(test.want, found) {
t.Errorf("Case #%v: wanted: %v, found: %v", i, test.want, found)
}
}
}
func TestSpacePrimaryScanning(t *testing.T) {
space := create([]byte("a"))
var tests = []struct {
primary string
want []string
}{
{"a", []string{"1"}},
{"b", []string{"2", "3"}},
}
for i, test := range tests {
found := fetchPrimary(space, test.primary)
if !reflect.DeepEqual(test.want, found) {
t.Errorf("Case #%v: wanted: %v, found: %v", i, test.want, found)
}
}
}
func TestSpacePrimaryIteration(t *testing.T) {
space := create([]byte("a"))
found := make([]string, 0)
space.Iterate(func(grouping string) bool {
found = append(found, grouping)
return true
})
if !reflect.DeepEqual([]string{"a", "b"}, found) {
t.Errorf("Incorrect space groupings found. wanted: %v, found: %v", []string{"a", "b"}, found)
}
}