-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage_test.go
71 lines (59 loc) · 1.44 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
package main
import (
"bytes"
"fmt"
"io"
"testing"
)
func TestPathTransformFunc(t *testing.T) {
key := "onepiecepicture"
pathKey := CASPathTransformFunc(key)
expectedOriginalKey := "eac313584ec0f3e5a5da458ab909f40bc763df5c"
expectedPathname := "eac31/3584e/c0f3e/5a5da/458ab/909f4/0bc76/3df5c"
if pathKey.Pathname != expectedPathname {
t.Errorf("have %s, expected %s", pathKey.Pathname, expectedPathname)
}
if pathKey.Filename != expectedOriginalKey {
t.Errorf("have %s, expected %s", pathKey.Filename, expectedOriginalKey)
}
}
func TestStorage(t *testing.T) {
s := newStorage()
id := generateID()
defer teardown(t, s)
for i := 0; i < 50; i++ {
key := fmt.Sprintf("foo_%d", i)
data := []byte("some jpg bytes")
if _, err := s.writeStream(id, key, bytes.NewReader(data)); err != nil {
t.Error(err)
}
if ok := s.Has(id, key); !ok {
t.Errorf("expected to have have %s", key)
}
_, r, err := s.Read(id, key)
if err != nil {
t.Error(err)
}
b, _ := io.ReadAll(r)
if string(b) != string(data) {
t.Errorf("expected %s have %s", data, b)
}
if err := s.Delete(id, key); err != nil {
t.Error(err)
}
if ok := s.Has(id, key); ok {
t.Errorf("expected to NOT have key %s", key)
}
}
}
func newStorage() *Storage {
opts := StorageOptions{
PathTransformFunc: CASPathTransformFunc,
}
return NewStorage(opts)
}
func teardown(t *testing.T, s *Storage) {
if err := s.Clear(); err != nil {
t.Error(err)
}
}