forked from iotexproject/w3bstream
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache_test.go
91 lines (73 loc) · 1.68 KB
/
cache_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
package project
import (
"crypto/sha256"
"fmt"
"os"
"testing"
"github.com/agiledragon/gomonkey/v2"
"github.com/pkg/errors"
"github.com/stretchr/testify/require"
)
func Test_newCache(t *testing.T) {
r := require.New(t)
t.Run("FailedToCreateDir", func(t *testing.T) {
p := gomonkey.NewPatches()
defer p.Reset()
p.ApplyFuncReturn(os.MkdirAll, errors.New(t.Name()))
_, err := newCache("")
r.ErrorContains(err, t.Name())
})
t.Run("Success", func(t *testing.T) {
p := gomonkey.NewPatches()
defer p.Reset()
p.ApplyFuncReturn(os.MkdirAll, nil)
_, err := newCache("")
r.NoError(err)
})
}
func TestCache_get(t *testing.T) {
r := require.New(t)
c := &cache{}
t.Run("FailedToRead", func(t *testing.T) {
p := gomonkey.NewPatches()
defer p.Reset()
p.ApplyFuncReturn(os.ReadFile, nil, errors.New(t.Name()))
d := c.get(uint64(0), nil)
r.Empty(d)
})
t.Run("FailedToValidate", func(t *testing.T) {
p := gomonkey.NewPatches()
defer p.Reset()
p.ApplyFuncReturn(os.ReadFile, []byte("data"), nil)
d := c.get(uint64(0), []byte("data"))
r.Empty(d)
})
t.Run("Success", func(t *testing.T) {
p := gomonkey.NewPatches()
defer p.Reset()
p.ApplyFuncReturn(os.ReadFile, []byte("data"), nil)
data := []byte("data")
h := sha256.New()
h.Write(data)
hash := h.Sum(nil)
d := c.get(uint64(0), hash)
r.Equal(data, d)
})
}
func TestCache_getPath(t *testing.T) {
r := require.New(t)
c := &cache{
dir: "test",
}
path := c.getPath(uint64(0))
r.Equal(fmt.Sprintf("%s/%d", c.dir, 0), path)
}
func TestCache_set(t *testing.T) {
p := gomonkey.NewPatches()
defer p.Reset()
p.ApplyFuncReturn(os.WriteFile, nil)
c := &cache{
dir: "test",
}
c.set(1, []byte{})
}