-
Notifications
You must be signed in to change notification settings - Fork 0
/
store_test.go
149 lines (118 loc) · 3.48 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// print the data
package main
import (
"bytes"
"fmt"
"io"
"testing"
)
// ------------------------ Utility func ------------------------ //
func newStore() *Store {
opts := StoreOpts{
PathTransformFunc: CASPathTransformFunc,
}
return NewStore(opts)
}
func cleanupTest(t *testing.T, s *Store) {
if err := s.Clear(); err != nil {
t.Error(err)
}
} // for cleanup, what files we have created in the store in the test, we need to remove them
// ------------------------ Path Transformation function test ------------------------ //
func TestPathTransformFunc(t *testing.T) {
key := "momo.gif"
pathKey := CASPathTransformFunc(key)
fmt.Println(pathKey) // {4295b/3c8f0/a940a/89eb1/0af84/15c9d/8ff32/34234 4295b3c8f0a940a89eb10af8415c9d8ff3234234}
fmt.Println(pathKey.FullPath())
expectedFilename := "4295b3c8f0a940a89eb10af8415c9d8ff3234234"
expectedPathName := "4295b/3c8f0/a940a/89eb1/0af84/15c9d/8ff32/34234"
if pathKey.Filename != expectedFilename {
t.Errorf(" Recieved %s expected %s", pathKey.Filename, expectedFilename)
}
if pathKey.PathName != expectedPathName {
t.Errorf("Recieved %s expected %s", pathKey.PathName, expectedPathName)
}
}
// -------------------------- Store Read, Write, Delete test ------------------------- //
func TestStore(t *testing.T) {
s := newStore()
id := generateID()
defer cleanupTest(t, s)
for i := 0; i < 3; 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 key %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("want %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)
}
}
}
/*
// -------------------- Write Test ------------------------ //
func TestWriteFunc(t *testing.T) {
// Write the file to the store
store := newStore()
defer cleanupTest(t, store)
id := "user102"
key := "momo.png"
data := []byte("some png bytes")
if _, err := store.Write(id, key, bytes.NewReader(data)); err != nil {
t.Error(err)
}
check := store.Has(id, key)
assert.True(t, check)
}
// --------------------- Read Test ------------------------- //
func TestReadFunction(t *testing.T) {
store := newStore()
id := "user103"
key := "momo.md"
data := []byte("markdown data in bytes")
defer cleanupTest(t, store)
// write the file with the data
if _, err := store.Write(id, key, bytes.NewReader(data)); err != nil { // we provide bytes.NewReader to convert the data to io.Reader
t.Error(err)
}
// read the file
_, reader, err := store.Read(id, key)
if err != nil {
t.Error(err)
}
b, _ := io.ReadAll(reader)
fmt.Println("Data: ", string(b))
// TEST
assert.Equal(t, data, b)
}
// -------------------- Delete Test ------------------------ //
func TestDeleteFunc(t *testing.T) {
s := newStore()
id := "user101"
key := "momo.jpeg"
// defer cleanupTest(t, store) // already deleting might hinder assert.False
data := []byte("some jpg bytes")
if _, err := s.writeStream(id, key, bytes.NewReader(data)); err != nil {
t.Error(err)
}
// TEST
if err := s.Delete(id, key); err != nil {
t.Error(err)
}
assert.False(t, s.Has(id, key)) // this should return false
}
// ----------------------------------- xxxxxxxxxxx ----------------------------------- // */