-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexcel_writer_test.go
144 lines (109 loc) · 3.96 KB
/
excel_writer_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
package peanut_test
import (
"os"
"github.com/360EntSecGroup-Skylar/excelize/v2"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/jimsmart/peanut"
)
var _ = Describe("ExcelWriter", func() {
newFn := func(suffix string) peanut.Writer {
w := peanut.NewExcelWriter("./test/output-", suffix)
return w
}
expectedOutput1 := [][]string{
{"foo_string", "foo_int"},
{"test 1", "1"},
{"test 2", "2"},
{"test 3", "3"},
}
expectedOutput2 := [][]string{
{"bar_int", "bar_string"},
{"1", "test 1"},
{"2", "test 2"},
{"3", "test 3"},
}
expectedOutput3 := [][]string{
{"baz_string", "baz_bool", "baz_float32", "baz_float64", "baz_int", "baz_int8", "baz_int16", "baz_int32", "baz_int64", "baz_uint", "baz_uint8", "baz_uint16", "baz_uint32", "baz_uint64"},
{"test 1", "1", "1.234", "9.876", "-12345", "-8", "-16", "-32", "-64", "12345", "8", "16", "32", "64"},
// {"test 1", "TRUE", "1.234", "9.876", "-12345", "-8", "-16", "-32", "-64", "12345", "8", "16", "32", "64"},
}
AfterEach(func() {
os.Remove("./test/output-Foo-sequential.xlsx")
os.Remove("./test/output-Bar-sequential.xlsx")
os.Remove("./test/output-Baz-sequential.xlsx")
os.Remove("./test/output-Qux-sequential.xlsx")
os.Remove("./test/output-Foo-interleave.xlsx")
os.Remove("./test/output-Bar-interleave.xlsx")
os.Remove("./test/output-Baz-interleave.xlsx")
os.Remove("./test/output-Qux-interleave.xlsx")
})
It("should write the correct data when sequential structs are written", func() {
w := newFn("-sequential")
testWritesAndCloseSequential(w)
output1, err := readExcel("./test/output-Foo-sequential.xlsx")
Expect(err).To(BeNil())
Expect(output1).To(Equal(expectedOutput1))
output2, err := readExcel("./test/output-Bar-sequential.xlsx")
Expect(err).To(BeNil())
Expect(output2).To(Equal(expectedOutput2))
output3, err := readExcel("./test/output-Baz-sequential.xlsx")
Expect(err).To(BeNil())
Expect(output3).To(Equal(expectedOutput3))
Expect("./test/output-Qux-sequential.xlsx").ToNot(BeAnExistingFile())
})
It("should write the correct data when interleaved structs are written", func() {
w := newFn("-interleave")
testWritesAndCloseSequential(w)
output1, err := readExcel("./test/output-Foo-interleave.xlsx")
Expect(err).To(BeNil())
Expect(output1).To(Equal(expectedOutput1))
output2, err := readExcel("./test/output-Bar-interleave.xlsx")
Expect(err).To(BeNil())
Expect(output2).To(Equal(expectedOutput2))
output3, err := readExcel("./test/output-Baz-interleave.xlsx")
Expect(err).To(BeNil())
Expect(output3).To(Equal(expectedOutput3))
Expect("./test/output-Qux-interleave.xlsx").ToNot(BeAnExistingFile())
})
It("should not write anything when structs are written and cancel is called", func() {
w := newFn("-cancel")
testWritesAndCancel(w)
Expect("./test/output-Foo-cancel.xlsx").ToNot(BeAnExistingFile())
Expect("./test/output-Bar-cancel.xlsx").ToNot(BeAnExistingFile())
})
It("should return an error when Write is called after Close", func() {
w := newFn("-close-write")
testWriteAfterClose(w)
Expect("./test/output-Foo-close-write.xlsx").ToNot(BeAnExistingFile())
})
It("should return an error when the path is bad", func() {
w := peanut.NewExcelWriter("./no-such-location/output-bogus-", "")
err := w.Write(testOutputFoo[0])
Expect(err).To(BeNil())
err = w.Close()
Expect(err).ToNot(BeNil())
})
Context("when given a struct with an unsupported field type", func() {
It("should return an error with an informative message", func() {
w := peanut.NewExcelWriter("./no-such-location/output-bogus-", "")
testWriteBadType(w)
// TODO(js) Do we need further checks, e.g. file not exists ...?
})
})
})
func readExcel(filename string) ([][]string, error) {
f, err := excelize.OpenFile(filename)
if err != nil {
return nil, err
}
var out [][]string
rows, err := f.GetRows("Sheet1")
if err != nil {
return nil, err
}
for _, row := range rows {
out = append(out, row)
}
return out, nil
}