-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxlsx_test.go
97 lines (88 loc) · 1.5 KB
/
xlsx_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
package xlsx_tables
import (
"archive/zip"
"io"
"strings"
"testing"
)
func TestReader(t *testing.T) {
expected := []string{
"name,country",
"tellus s1,uk",
"tellus s2,us",
"tellus s3,us",
}
f, err := zip.OpenReader("testdata/xlsx_sample.xlsx")
if err != nil {
t.Fatal(err)
}
defer f.Close()
r := NewReader(f)
res := []string{}
for {
row, err := r.Read()
if err != nil {
if err == io.EOF {
break
}
t.Fatal(err)
}
res = append(res, strings.Join(row, ","))
}
for k, v := range expected {
if res[k] != v {
t.Fatalf("unexpected results, got: %q, want: %q", res[k], v)
}
}
}
func TestReaderWorksheet2(t *testing.T) {
expected := []string{
"code,product",
"1,oil",
"2,grease",
"3.1,tellus",
}
f, err := zip.OpenReader("testdata/xlsx_sample.xlsx")
if err != nil {
t.Fatal(err)
}
defer f.Close()
r := NewReader(f)
r.Worksheet = "sheet2"
res := []string{}
for {
row, err := r.Read()
if err != nil {
if err == io.EOF {
break
}
t.Fatal(err)
}
res = append(res, strings.Join(row, ","))
}
for k, v := range expected {
if res[k] != v {
t.Fatalf("unexpected results, got: %q, want: %q", res[k], v)
}
}
}
func BenchmarkRead(b *testing.B) {
b.ReportAllocs()
for n := 0; n < b.N; n++ {
f, err := zip.OpenReader("testdata/xlsx_sample.xlsx")
if err != nil {
b.Fatal(err)
}
defer f.Close()
r := NewReader(f)
for {
_, err := r.Read()
if err != nil {
if err == io.EOF {
break
}
b.Fatal(err)
}
}
}
}