-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathkeys_test.go
124 lines (108 loc) · 3.07 KB
/
keys_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
package tormenta
import (
"bytes"
"encoding/binary"
"testing"
"github.com/jpincas/gouuidv6"
)
func Test_typeToKeyRoot(t *testing.T) {
testCases := []struct {
source string
expectedResult string
}{
{"*", ""},
{"*test", "test"},
{"*test.test", "test"},
{"*test.test.test", "test"},
{"test", "test"},
{"test.test", "test"},
{"test.test.test", "test"},
{"*", ""},
{"*Test", "test"},
{"*test.Test", "test"},
{"*Test.Test.test", "test"},
{"Test", "test"},
{"Test.test", "test"},
{"[]test.test.Test", "test"},
{"[]*Test.Test.test", "test"},
{"[]Test", "test"},
{"[]Test.test", "test"},
{"[]test.test.Test", "test"},
}
for _, test := range testCases {
result := typeToKeyRoot(test.source)
if string(result) != test.expectedResult {
t.Errorf("Converting type sig '%s' to key root produced '%s' instead of '%s'", test.source, result, test.expectedResult)
}
}
}
func Test_makeContentKey(t *testing.T) {
id := newID()
testCases := []struct {
testName string
root []byte
includeID bool
id gouuidv6.UUID
expected []byte
}{
{"No ID", []byte("myentity"), false, id, []byte("c" + keySeparator + "myentity")},
}
for _, testCase := range testCases {
var result []byte
if testCase.includeID {
result = newContentKey(testCase.root, testCase.id).bytes()
} else {
result = newContentKey(testCase.root).bytes()
}
if string(result) != string(testCase.expected) {
t.Errorf("Testing content key construction (%s). Expecting %s, got %s", testCase.testName, testCase.expected, result)
}
}
}
func Test_makeIndexKey(t *testing.T) {
id := newID()
ikey := []byte(indexKeyPrefix)
floatBuf := new(bytes.Buffer)
var float = 3.14
binary.Write(floatBuf, binary.LittleEndian, float)
intBuf := new(bytes.Buffer)
var int = 3
binary.Write(intBuf, binary.LittleEndian, uint32(int))
testCases := []struct {
testName string
root []byte
id gouuidv6.UUID
indexName string
indexContent interface{}
expected []byte
}{
{
"no index content",
[]byte("root"), id, "myindex", nil,
bytes.Join([][]byte{ikey, []byte("root"), []byte("myindex"), []byte{}, id.Bytes()}, []byte(keySeparator)),
},
{
"string index content",
[]byte("root"), id, "myindex", "indexContent",
bytes.Join([][]byte{ikey, []byte("root"), []byte("myindex"), []byte("indexcontent"), id.Bytes()}, []byte(keySeparator)),
},
{
"float index content",
[]byte("root"), id, "myindex", 3.14,
bytes.Join([][]byte{ikey, []byte("root"), []byte("myindex"), interfaceToBytes(3.14), id.Bytes()}, []byte(keySeparator)),
},
{
"int index content",
[]byte("root"), id, "myindex", 3,
bytes.Join([][]byte{ikey, []byte("root"), []byte("myindex"), interfaceToBytes(3), id.Bytes()}, []byte(keySeparator)),
},
}
for _, testCase := range testCases {
result := makeIndexKey(testCase.root, id, []byte(testCase.indexName), testCase.indexContent)
a := string(result)
b := string(testCase.expected)
if a != b {
t.Errorf("Testing make index key with %s - expected %s, got %s", testCase.testName, b, a)
}
}
}