-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
183 lines (171 loc) · 2.92 KB
/
main_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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package store_test
import (
"bytes"
"io"
"os"
"testing"
. "github.com/antlinker/store"
"github.com/antlinker/store/file"
)
func TestFileUpdate(t *testing.T) {
file.InitStore("")
testUpdate(t)
testRead(t)
testMultifilePackaging(t)
}
func testUpdate(t *testing.T) {
data1 := []byte("test123132312123")
data2 := []byte("test1231323")
filename := "testUpdate12.txt"
err := SaveData(filename, data1)
if err != nil {
t.Error(err)
return
}
err = SaveData(filename, data2)
if err != nil {
t.Error(err)
return
}
err = UpdateData(filename, data2)
if err != nil {
t.Error(err)
return
}
e, err := IsExists(filename)
if err != nil {
t.Error(err)
return
}
if !e {
t.Error("文件应该存在")
return
}
size, err := Fsize(filename)
if err != nil {
t.Error(err)
return
}
if size != int64(len(data2)) {
t.Error("获取大小错误")
return
}
err = DeleteFile(filename)
if err != nil {
t.Error(err)
return
}
e, err = IsExists(filename)
if err != nil {
t.Error(err)
return
}
if e {
t.Error("文件不应该存在")
return
}
}
func testRead(t *testing.T) {
data1 := []byte("test123132312123")
filename1 := "test1.txt"
err := SaveData(filename1, data1)
if err != nil {
t.Error(err)
}
defer func() {
err1 := DeleteFile(filename1)
if err1 != nil {
t.Error(err1)
}
}()
r, err := DefaultStore.GetReader(filename1)
if err != nil {
t.Errorf("失败:%s", err)
return
}
if r == nil {
t.Errorf("读取失败!")
return
}
defer r.Close()
buff := bytes.NewBuffer(nil)
_, err = io.Copy(buff, r)
if err != nil {
t.Error(err)
return
}
data := buff.Bytes()
if bytes.Compare(data1, data) != 0 {
t.Errorf("读取的数据不是存入数据:%s", data)
return
}
}
func testMultifilePackaging(t *testing.T) {
data1 := []byte("test123132312123")
data2 := []byte("test1231323")
filename1 := "test1.txt"
filename2 := "test2.txt"
zipfname := "test.zip"
err := SaveData(filename1, data1)
if err != nil {
t.Error(err)
}
defer func() {
err1 := DeleteFile(filename1)
if err1 != nil {
t.Error(err1)
}
}()
err = SaveData(filename2, data2)
if err != nil {
t.Error(err)
}
defer func() {
err1 := DeleteFile(filename2)
if err1 != nil {
t.Error(err1)
}
}()
tf, err := os.Create(zipfname)
if err != nil {
t.Errorf("失败:%s", err)
return
}
err = DefaultStore.MultifilePackaging(tf,
FileAlias{
Alias: filename1,
Key: filename1,
},
FileAlias{
Alias: filename2,
Key: filename2,
},
)
if err != nil {
t.Errorf("失败:%s", err)
return
}
tf.Close()
r, err := IsExists(zipfname)
if err != nil {
t.Errorf("文件检查失败:%s", err)
return
}
if !r {
t.Errorf("文件不存在测试失败.")
return
}
err = DeleteFile(zipfname)
if err != nil {
t.Error(err)
}
r, err = IsExists(zipfname)
if err != nil {
t.Errorf("文件检查失败:%s", err)
return
}
if r {
t.Errorf("文件存在删除失败.")
return
}
}