forked from tommyblue/smugmug-backup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsv_test.go
106 lines (90 loc) · 1.87 KB
/
csv_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
package smugmug
import (
"bytes"
"io"
"os"
"path/filepath"
"testing"
)
func Test_createMetadataCSV(t *testing.T) {
fpath := filepath.Join(t.TempDir(), "file.csv")
if err := createMetadataCSV(fpath); err != nil {
t.Fatalf("cannot create csv file: %v", err)
}
f, err := os.Open(fpath)
if err != nil {
t.Fatalf("cannot open csv file: %v", err)
}
n, err := lineCounter(t, f)
if err != nil {
t.Fatalf("cannot count lines in csv file: %v", err)
}
if n != 1 {
t.Fatalf("want 1 line, got %d", n)
}
}
func Test_writeToCSV(t *testing.T) {
fpath := filepath.Join(t.TempDir(), "file.csv")
if err := createMetadataCSV(fpath); err != nil {
t.Fatalf("cannot create csv file: %v", err)
}
w := &Worker{
cfg: &Conf{
metadataFile: fpath,
},
}
images := []albumImage{
{
builtFilename: "fname1",
ArchivedUri: "url",
Caption: "asdsad",
Keywords: "a,b,c",
Latitude: "40.123",
Longitude: "11.11",
},
{
builtFilename: "fname2",
ArchivedUri: "url",
Caption: "asdsad",
Keywords: "a,b,c",
Latitude: "40.123",
Longitude: "11.11",
},
{
builtFilename: "fname3",
ArchivedUri: "url",
Caption: "asdsad",
Keywords: "a,b,c",
Latitude: "40.123",
Longitude: "11.11",
},
}
w.writeToCSV(images, "test")
f, err := os.Open(fpath)
if err != nil {
t.Fatalf("cannot open csv file: %v", err)
}
n, err := lineCounter(t, f)
if err != nil {
t.Fatalf("cannot count lines in csv file: %v", err)
}
if n != 4 {
t.Fatalf("want 4 lines, got %d", n)
}
}
func lineCounter(t *testing.T, r io.Reader) (int, error) {
t.Helper()
buf := make([]byte, 32*1024)
count := 0
lineSep := []byte{'\n'}
for {
c, err := r.Read(buf)
count += bytes.Count(buf[:c], lineSep)
switch {
case err == io.EOF:
return count, nil
case err != nil:
return count, err
}
}
}