-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore_test.go
92 lines (75 loc) · 1.77 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
package main
import (
"bytes"
"fmt"
"io"
"testing"
)
func TestPathTransformFunc(t *testing.T) {
key := "funnypicture"
pathkey := CASPathTransformFunc(key)
expectedOriginalKey := "21d7a8342d65b75149512f65125e3519e2e63fd8"
expectedPathname := "21d7a/8342d/65b75/14951/2f651/25e35/19e2e/63fd8"
pathname := pathkey.PathName
original := pathkey.FileName
if pathname != expectedPathname {
t.Errorf("have %s want %s", pathname, expectedPathname)
}
if original != expectedOriginalKey {
t.Errorf("have %s want %s", original, expectedOriginalKey)
}
}
func TestStoreDeleteKey(t *testing.T) {
opts := StoreOpts{
PathTransformFunc: CASPathTransformFunc,
}
s := NewStore(opts)
key := "funnydata"
data := []byte("lol lol funny data lmaooooooo")
if _, err := s.writeStream(key, bytes.NewReader(data)); err != nil {
t.Error(err)
}
if err := s.Delete(key); err != nil {
t.Error(err)
}
}
func TestStore(t *testing.T) {
s := newStore()
defer teardown(t, s)
count := 50
for i := 0; i < count; i++ {
key := fmt.Sprintf("funnydata_%d", i)
data := []byte("lol lol funny data lmaooooooo")
if _, err := s.writeStream(key, bytes.NewReader(data)); err != nil {
t.Error(err)
}
if ok := s.Has(key); !ok {
t.Errorf("expected to have key: %s", key)
}
_, r, err := s.Read(key)
if err != nil {
t.Error(err)
}
b, _ := io.ReadAll(r)
if string(b) != string(data) {
t.Errorf("want %s have %s", data, b)
}
if err := s.Delete(key); err != nil {
t.Error(err)
}
if ok := s.Has(key); ok {
t.Errorf("expected to NOT have key: %s", key)
}
}
}
func newStore() *Store {
opts := StoreOpts{
PathTransformFunc: CASPathTransformFunc,
}
return NewStore(opts)
}
func teardown(t *testing.T, s *Store) {
if err := s.Clear(); err != nil {
t.Error(err)
}
}