-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
118 lines (107 loc) · 2.64 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
package main
import (
"errors"
"fmt"
"os"
"testing"
)
func TestDownloader(t *testing.T) {
var downloaderTests = []struct {
description string
url string
expected error
}{
{
description: "Download work",
url: "https://agritrop.cirad.fr/584726/1/Rapport.pdf",
expected: nil,
},
{
description: "invalid url",
url: "",
expected: fmt.Errorf("invalid url"),
},
{
description: "unable to download file with multithreading",
url: "https://youtu.be/w0NQlEMjntI",
expected: errors.New("unable to download file with multithreads"),
},
{
description: "unable to parse variable",
url: "https://github.com/disco07/file-downloader",
expected: errors.New("unable to parse variable"),
},
}
for _, tt := range downloaderTests {
t.Run(tt.description, func(t *testing.T) {
err := downloader(tt.url)
if tt.expected == nil && err != nil {
t.Errorf("Unexpected error for input %v: %v (expected %v)", tt.url, err, tt.expected)
}
if tt.expected != nil && err.Error() != tt.expected.Error() {
t.Errorf("Unexpected error for input %v: %v (expected %v)", tt.url, err, tt.expected)
}
})
}
}
func TestAppend(t *testing.T) {
f, err := os.Create("filename")
if err != nil {
t.Errorf("failed to create file: %v", err.Error())
}
defer f.Close()
part, err := os.Create("part0")
if err != nil {
t.Errorf("failed to create file: %v", err.Error())
}
_, err = os.Create("part1")
if err != nil {
t.Errorf("failed to create file: %v", err.Error())
}
tests := []struct {
description string
part int
offset int
file *os.File
expected error
}{
{
description: "append data in file",
part: 0,
offset: 1000,
file: f,
expected: nil,
},
//{
// description: "unable to delete file",
// part: 1,
// offset: 1000,
// file: f,
// expected: errors.New("cannot delete the file because it is still open"),
//},
{
description: "file not found",
part: 2,
offset: 1000,
file: f,
expected: errors.New("file not found"),
},
}
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
part.Close()
err := Append(tt.file, tt.part, tt.offset)
if tt.expected == nil && err != nil {
t.Errorf("got %v want %v", err.Error(), tt.expected)
}
if tt.expected != nil && err.Error() != tt.expected.Error() {
t.Errorf("got %v want %v", err.Error(), tt.expected)
}
})
}
}
func BenchmarkDownloader(b *testing.B) {
for i := 0; i < b.N; i++ {
downloader("https://agritrop.cirad.fr/584726/1/Rapport.pdf")
}
}