-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore_test.go
75 lines (63 loc) · 1.52 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
package main
import (
"bytes"
"fmt"
"io"
"testing"
)
func TestPathTransformFunc(t *testing.T) {
key := "someimportantdata"
pathKey := CASPathTransformFunc(key)
expectedFileName := "349d8ede57e318d842787b6c9ac811a6dfb0dc7a"
expectedPathName := "349d8/ede57/e318d/84278/7b6c9/ac811/a6dfb/0dc7a"
if expectedPathName != pathKey.PathName {
t.Fatalf("expected %s, got %s", expectedPathName, pathKey.PathName)
}
if expectedFileName != pathKey.FileName {
t.Fatalf("expected %s, got %s", expectedFileName, pathKey.FileName)
}
}
func TestStore(t *testing.T) {
s := newStore()
id := generateID()
defer teardown(t, s)
for i := 0; i < 100; i++ {
key := fmt.Sprintf("myspecialdata_%d", i)
data := []byte("some data")
_, err := s.writeStream(id, key, bytes.NewReader(data))
if err != nil {
t.Fatalf("writeStream failed: %v", err)
}
ok := s.Has(id, key)
if !ok {
t.Fatalf("Key not found")
}
_, r, err := s.Read(id, key)
if err != nil {
t.Fatalf("Read failed: %v", err)
}
b, _ := io.ReadAll(r)
fmt.Println(string(b))
if string(b) != string(data) {
t.Fatalf("expected %s, got %s", string(data), string(b))
}
if err := s.Delete(id, key); err != nil {
t.Fatalf("Delete failed: %v", err)
}
if ok := s.Has(id, key); ok {
t.Fatalf("Key should have been deleted")
}
}
}
func newStore() *Store {
opts := StoreOptions{
PathTransformFunc: CASPathTransformFunc,
}
return NewStore(opts)
}
func teardown(t *testing.T, s *Store) {
t.Helper()
if err := s.Clear(); err != nil {
t.Error(err)
}
}