-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage_test.go
92 lines (75 loc) · 1.68 KB
/
storage_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 := "encodedpathname"
pathname := CASPathTransformFunc(key)
expectedOriginalKey := "2d09e004d0c86cffa599f704573b8050a4e9e109"
expectedPathName := "2d09e/004d0/c86cf/fa599/f7045/73b80/50a4e/9e109"
if pathname.Path != expectedPathName {
t.Errorf("expected %s, got %s", expectedPathName, pathname)
}
if pathname.Filename != expectedOriginalKey {
t.Errorf("expected %s, got %s", expectedPathName, pathname)
}
}
func TestStore(t *testing.T) {
s := newStore()
defer teardown(t, s)
for i := 0; i < 50; i++ {
key := fmt.Sprintf("Trip%d", i)
data := bytes.NewReader([]byte("dude chill!!!"))
_, err := s.writeStream(key, data)
if err != nil {
t.Error(err)
}
if ok := s.Has(key); !ok {
t.Error("expected true, got false")
}
_, r, err := s.Read(key)
if err != nil {
t.Error(err)
}
b, err := io.ReadAll(r)
if err != nil {
t.Error(err)
}
if string(b) != "dude chill!!!" {
t.Errorf("expected %s, got %s", "dude chill!!!", string(b))
}
if err := s.Delete(key); err != nil {
t.Error(err)
}
if ok := s.Has(key); ok {
t.Error("expected false, got true")
}
}
}
func TestDeleteKey(t *testing.T) {
s := newStore()
defer teardown(t, s)
key := "TripTomorrow"
data := bytes.NewReader([]byte("dude chill!!!"))
_, err := s.writeStream(key, data)
if err != nil {
t.Error(err)
}
if err := s.Delete(key); err != nil {
t.Error(err)
}
}
func newStore() *Store {
opts := StoreOpts{
PathTransformFunc: CASPathTransformFunc,
}
return NewStore(opts)
}
func teardown(t *testing.T, s *Store) {
if err := s.Close(); err != nil {
t.Error(err)
}
}