-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatastore_test.go
89 lines (77 loc) · 1.66 KB
/
datastore_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
package main
import (
"bytes"
"io"
"testing"
)
func TestDataStores(t *testing.T) {
tests := []struct {
name string
d DataStore
}{
{
name: "mem",
d: NewMemoryDataStore(),
},
{
name: "fs",
d: NewFileSystemDataStore(t.TempDir()),
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
testDataStore(t, test.d)
})
}
}
func testDataStore(t *testing.T, d DataStore) {
// set a key
err := d.Set("a", readCloserString("val-a"))
if err != nil {
t.Errorf("error setting key: %v", err)
}
// make sure we can read it back
var aval string
aval, err = stringErrReadCloserErr(d.Get("a"))
if err != nil {
t.Errorf("error getting key: %v", err)
}
if aval != "val-a" {
t.Errorf("expected %q, got %q", "val-a", aval)
}
// set a different key
err = d.Set("b", readCloserString("val-b"))
if err != nil {
t.Errorf("error setting key: %v", err)
}
// make sure we can read it back too
var bval string
bval, err = stringErrReadCloserErr(d.Get("b"))
if err != nil {
t.Errorf("error getting key: %v", err)
}
if bval != "val-b" {
t.Errorf("expected %q, got %q", "val-b", aval)
}
// make sure we can still read the first key
aval, err = stringErrReadCloserErr(d.Get("a"))
if err != nil {
t.Errorf("error getting key: %v", err)
}
if aval != "val-a" {
t.Errorf("expected %q, got %q", "val-a", aval)
}
}
func readCloserString(s string) io.ReadCloser {
return io.NopCloser(bytes.NewBuffer([]byte(s)))
}
func stringErrReadCloserErr(r io.ReadCloser, err error) (string, error) {
if err != nil {
return "", err
}
rv, err := io.ReadAll(r)
if err != nil {
return "", err
}
return string(rv), r.Close()
}