-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsource_test.go
92 lines (88 loc) · 2.19 KB
/
source_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
package xls2sheets
import (
"io/ioutil"
"os"
"strconv"
"testing"
"time"
)
func Test_generateName(t *testing.T) {
type args struct {
prefix string
extension string
}
tests := []struct {
name string
args args
want string
}{
{"with prefix", args{"prefix", ".xlsx"}, "prefix" + strconv.Itoa(int(time.Now().Unix())) + ".xlsx"},
{"no prefix", args{"", ""}, strconv.Itoa(int(time.Now().Unix())) + ""},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := generateName(tt.args.prefix, tt.args.extension); got != tt.want {
t.Errorf("generateName() = %v, want %v", got, tt.want)
}
})
}
}
func Test_filename(t *testing.T) {
type args struct {
loc string
}
tests := []struct {
name string
args args
want string
wantErr bool
}{
{"just file", args{"file://sample.xls"}, "sample.xls", false},
{"full path", args{"file:///tmp/subdir/subdir2/file.txt"}, "/tmp/subdir/subdir2/file.txt", false},
{"current dir file", args{"file://./some_old_file.xls"}, "some_old_file.xls", false},
{"url parse error", args{"://what_proto_is_that?"}, "", true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := filename(tt.args.loc)
if (err != nil) != tt.wantErr {
t.Errorf("filename() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("filename() = %v, want %v", got, tt.want)
}
})
}
}
func Test_fileType(t *testing.T) {
f, err := ioutil.TempFile("", tempFilePrefix+"*")
if err != nil {
t.Fatal(err)
}
f.Close()
defer func() {
os.Remove(f.Name())
}()
type args struct {
loc string
}
tests := []struct {
name string
args args
want srcType
}{
{"local file with schema", args{"file://tea_with_lemon.xls"}, srcFile},
{"local file without schema", args{f.Name()}, srcFile},
{"remote file", args{"https://shadywebsite.com/README.xlsx"}, srcWeb},
{"google sheets id", args{"1jw2phhb11w6vKw5nkklWtSZHOxADIsXcyRgVLyea4ak"}, srcGSheet},
{"unknown", args{""}, srcUnknown},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := fileType(tt.args.loc); got != tt.want {
t.Errorf("fileType() = %v, want %v", got, tt.want)
}
})
}
}