-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathmain_test.go
136 lines (117 loc) · 3.1 KB
/
main_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
package main
import (
"encoding/hex"
"testing"
"github.com/stretchr/testify/require"
)
func TestBuildRawKVKey(t *testing.T) {
assert := require.New(t)
keyMode, prefix, err := getKeyPrefix("rawkv", 0)
assert.NoError(err)
assert.Equal(KeyModeRaw, *keyMode)
tests := []struct {
rawKey string
format string
expected string
err bool
}{
{
rawKey: "",
format: "str",
expected: "7200000000000000fb",
err: false,
},
{
rawKey: "test",
format: "str",
expected: "7200000074657374ff0000000000000000f7",
err: false,
},
{
rawKey: "testtest",
format: "str",
expected: "7200000074657374ff7465737400000000fb",
err: false,
},
{
rawKey: "74657374",
format: "hex",
expected: "7200000074657374ff0000000000000000f7",
err: false,
},
{
rawKey: "test",
format: "invalid",
expected: "",
err: true,
},
}
for _, tt := range tests {
actualKey, err := buildRawKVKey(prefix, tt.rawKey, tt.format)
if tt.err && err == nil {
t.Errorf("expected error, but got nil")
}
if !tt.err && err != nil {
t.Errorf("expected nil error, but got %v", err)
}
if hex.EncodeToString(actualKey) != tt.expected {
t.Errorf("expected key %s, but got %s", tt.expected, hex.EncodeToString(actualKey))
}
}
}
func TestParseRawKVKey(t *testing.T) {
// ❯ ./mok 7200000074657374ff0000000000000000f7
// "7200000074657374ff0000000000000000f7"
// └─## decode hex key
// └─"r\000\000\000test\377\000\000\000\000\000\000\000\000\367"
// ├─## decode mvcc key
// │ └─"r\000\000\000test"
// │ └─## decode keyspace
// │ ├─key mode: rawkv
// │ ├─keyspace: 0
// │ └─"test"
// └─## decode keyspace
// ├─key mode: rawkv
// ├─keyspace: 0
// └─"test\377\000\000\000\000\000\000\000\000\367"
assert := require.New(t)
tests := []struct {
encoded string
expected string
}{
{
encoded: "7200000000000000fb",
expected: "",
},
{
encoded: "7200000074657374ff0000000000000000f7",
expected: "test",
},
{
encoded: "7200000074657374ff7465737400000000fb",
expected: "testtest",
},
}
for _, tt := range tests {
n := N("key", []byte(tt.encoded)).Expand()
assert.Len(n.variants, 1) // decode hex key
assert.Equal("decode hex key", n.variants[0].method)
assert.Len(n.variants[0].children, 1)
n = n.variants[0].children[0]
assert.Len(n.variants, 2) // with and without mvcc
variant := n.variants[0]
assert.Equal("decode mvcc key", variant.method)
assert.Len(variant.children, 1)
n = variant.children[0]
assert.Len(n.variants, 1)
variant = n.variants[0]
assert.Equal("decode keyspace", variant.method)
assert.Len(variant.children, 3)
assert.Equal("key_mode", variant.children[0].typ)
assert.Equal("r", string(variant.children[0].val))
assert.Equal("keyspace_id", variant.children[1].typ)
assert.Equal([]byte{0, 0, 0}, variant.children[1].val)
assert.Equal("raw_key", variant.children[2].typ)
assert.Equal(tt.expected, string(variant.children[2].val))
}
}