-
Notifications
You must be signed in to change notification settings - Fork 0
/
store_test.go
112 lines (105 loc) · 2.33 KB
/
store_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
package raft
import (
"bytes"
"fmt"
"testing"
)
func TestMemoryStore(t *testing.T) {
t.Run("Get-Set methods", func(t *testing.T) {
var store memoryStore
cases := []struct {
key, value []byte
}{
{[]byte(""), []byte("")},
{[]byte("first key"), []byte("first value")},
}
for _, tc := range cases {
t.Run(fmt.Sprintf("key: %q value: %q", tc.key, tc.value), func(t *testing.T) {
// before set kv
got, err := store.Get(tc.key)
if err != nil {
t.Error(err)
}
expect := []byte{}
if bytes.Compare(expect, got) != 0 {
t.Errorf("expect %v, but got %v", expect, got)
}
// after set kv
err = store.Set(tc.key, tc.value)
if err != nil {
t.Error(err)
}
got, err = store.Get(tc.key)
if err != nil {
t.Error(err)
}
expect = tc.value
if bytes.Compare(expect, got) != 0 {
t.Errorf("expect %v, but got %v", expect, got)
}
// reset
expect = append(tc.value, []byte("-suffix")...)
err = store.Set(tc.key, expect)
if err != nil {
t.Error(err)
}
got, err = store.Get(tc.key)
if err != nil {
t.Error(err)
}
if bytes.Compare(expect, got) != 0 {
t.Errorf("expect %v, but got %v", expect, got)
}
})
}
})
t.Run("GetUint64-SetUint64 methods", func(t *testing.T) {
var store memoryStore
cases := []struct {
key []byte
value uint64
}{
{[]byte("first key"), 1},
{[]byte("second key"), 2},
}
for _, tc := range cases {
t.Run(fmt.Sprintf("key: %q value: %d", tc.key, tc.value), func(t *testing.T) {
// before set
got, err := store.GetUint64(tc.key)
if err != nil {
t.Error(err)
}
var expect uint64 = 0
if expect != got {
t.Errorf("expect: %d, but got: %d", expect, got)
}
// after set
err = store.SetUint64(tc.key, tc.value)
if err != nil {
t.Error(err)
}
got, err = store.GetUint64(tc.key)
if err != nil {
t.Error(err)
}
expect = tc.value
if expect != got {
t.Errorf("expect: %d, but got: %d", expect, got)
}
// reset
expect = tc.value + 1
err = store.SetUint64(tc.key, expect)
if err != nil {
t.Error(err)
}
got, err = store.GetUint64(tc.key)
if err != nil {
t.Error(err)
}
if expect != got {
t.Errorf("expect: %d, but got: %d", expect, got)
}
})
}
})
}