forked from tealeg/xlsx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stream_test.go
487 lines (466 loc) · 17.4 KB
/
stream_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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
package xlsx
import (
"bytes"
"fmt"
"io"
"reflect"
"strings"
. "gopkg.in/check.v1"
)
const (
TestsShouldMakeRealFiles = false
)
type StreamSuite struct{}
var _ = Suite(&SheetSuite{})
func (s *StreamSuite) TestTestsShouldMakeRealFilesShouldBeFalse(t *C) {
if TestsShouldMakeRealFiles {
t.Fatal("TestsShouldMakeRealFiles should only be true for local debugging. Don't forget to switch back before commiting.")
}
}
func (s *StreamSuite) TestXlsxStreamWrite(t *C) {
// When shouldMakeRealFiles is set to true this test will make actual XLSX files in the file system.
// This is useful to ensure files open in Excel, Numbers, Google Docs, etc.
// In case of issues you can use "Open XML SDK 2.5" to diagnose issues in generated XLSX files:
// https://www.microsoft.com/en-us/download/details.aspx?id=30425
testCases := []struct {
testName string
sheetNames []string
workbookData [][][]string
headerTypes [][]*CellType
expectedError error
}{
{
testName: "One Sheet",
sheetNames: []string{
"Sheet1",
},
workbookData: [][][]string{
{
{"Token", "Name", "Price", "SKU"},
{"123", "Taco", "300", "0000000123"},
},
},
headerTypes: [][]*CellType{
{nil, CellTypeString.Ptr(), nil, CellTypeString.Ptr()},
},
},
{
testName: "One Column",
sheetNames: []string{
"Sheet1",
},
workbookData: [][][]string{
{
{"Token"},
{"123"},
},
},
},
{
testName: "Several Sheets, with different numbers of columns and rows",
sheetNames: []string{
"Sheet 1", "Sheet 2", "Sheet3",
},
workbookData: [][][]string{
{
{"Token", "Name", "Price", "SKU"},
{"123", "Taco", "300", "0000000123"},
},
{
{"Token", "Name", "Price", "SKU", "Stock"},
{"456", "Salsa", "200", "0346", "1"},
{"789", "Burritos", "400", "754", "3"},
},
{
{"Token", "Name", "Price"},
{"9853", "Guacamole", "500"},
{"2357", "Margarita", "700"},
},
},
},
{
testName: "Two Sheets with same the name",
sheetNames: []string{
"Sheet 1", "Sheet 1",
},
workbookData: [][][]string{
{
{"Token", "Name", "Price", "SKU"},
{"123", "Taco", "300", "0000000123"},
},
{
{"Token", "Name", "Price", "SKU", "Stock"},
{"456", "Salsa", "200", "0346", "1"},
{"789", "Burritos", "400", "754", "3"},
},
},
expectedError: fmt.Errorf("duplicate sheet name '%s'.", "Sheet 1"),
},
{
testName: "One Sheet Registered, tries to write to two",
sheetNames: []string{
"Sheet 1",
},
workbookData: [][][]string{
{
{"Token", "Name", "Price", "SKU"},
{"123", "Taco", "300", "0000000123"},
},
{
{"Token", "Name", "Price", "SKU"},
{"456", "Salsa", "200", "0346"},
},
},
expectedError: AlreadyOnLastSheetError,
},
{
testName: "One Sheet, too many columns in row 1",
sheetNames: []string{
"Sheet 1",
},
workbookData: [][][]string{
{
{"Token", "Name", "Price", "SKU"},
{"123", "Taco", "300", "0000000123", "asdf"},
},
},
expectedError: WrongNumberOfRowsError,
},
{
testName: "One Sheet, too few columns in row 1",
sheetNames: []string{
"Sheet 1",
},
workbookData: [][][]string{
{
{"Token", "Name", "Price", "SKU"},
{"123", "Taco", "300"},
},
},
expectedError: WrongNumberOfRowsError,
},
{
testName: "Lots of Sheets, only writes rows to one, only writes headers to one, should not error and should still create a valid file",
sheetNames: []string{
"Sheet 1", "Sheet 2", "Sheet 3", "Sheet 4", "Sheet 5", "Sheet 6",
},
workbookData: [][][]string{
{
{"Token", "Name", "Price", "SKU"},
{"123", "Taco", "300", "0000000123"},
},
{{}},
{{"Id", "Unit Cost"}},
{{}},
{{}},
{{}},
},
},
{
testName: "Two Sheets, only writes to one, should not error and should still create a valid file",
sheetNames: []string{
"Sheet 1", "Sheet 2",
},
workbookData: [][][]string{
{
{"Token", "Name", "Price", "SKU"},
{"123", "Taco", "300", "0000000123"},
},
{{}},
},
},
{
testName: "Larger Sheet",
sheetNames: []string{
"Sheet 1",
},
workbookData: [][][]string{
{
{"Token", "Name", "Price", "SKU", "Token", "Name", "Price", "SKU", "Token", "Name", "Price", "SKU", "Token", "Name", "Price", "SKU", "Token", "Name", "Price", "SKU", "Token", "Name", "Price", "SKU"},
{"123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123"},
{"456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346"},
{"789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754"},
{"123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123"},
{"456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346"},
{"789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754"},
{"123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123"},
{"456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346"},
{"789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754"},
{"123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123"},
{"456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346"},
{"789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754"},
{"123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123"},
{"456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346"},
{"789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754"},
{"123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123"},
{"456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346"},
{"789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754"},
{"123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123"},
{"456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346"},
{"789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754"},
{"123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123"},
{"456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346"},
{"789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754"},
},
},
},
{
testName: "UTF-8 Characters. This XLSX File loads correctly with Excel, Numbers, and Google Docs. It also passes Microsoft's Office File Format Validator.",
sheetNames: []string{
"Sheet1",
},
workbookData: [][][]string{
{
// String courtesy of https://github.com/minimaxir/big-list-of-naughty-strings/
// Header row contains the tags that I am filtering on
{"Token", endSheetDataTag, "Price", fmt.Sprintf(dimensionTag, "A1:D1")},
// Japanese and emojis
{"123", "パーティーへ行かないか", "300", "🍕🐵 🙈 🙉 🙊"},
// XML encoder/parser test strings
{"123", `<?xml version="1.0" encoding="ISO-8859-1"?>`, "300", `<?xml version="1.0" encoding="ISO-8859-1"?><!DOCTYPE foo [ <!ELEMENT foo ANY ><!ENTITY xxe SYSTEM "file:///etc/passwd" >]><foo>&xxe;</foo>`},
// Upside down text and Right to Left Arabic text
{"123", `˙ɐnbᴉlɐ ɐuƃɐɯ ǝɹolop ʇǝ ǝɹoqɐl ʇn ʇunpᴉpᴉɔuᴉ ɹodɯǝʇ poɯsnᴉǝ op pǝs 'ʇᴉlǝ ƃuᴉɔsᴉdᴉpɐ ɹnʇǝʇɔǝsuoɔ 'ʇǝɯɐ ʇᴉs ɹolop ɯnsdᴉ ɯǝɹo˥
00˙Ɩ$-`, "300", `ﷺ`},
{"123", "Taco", "300", "0000000123"},
},
},
},
}
for i, testCase := range testCases {
var filePath string
var buffer bytes.Buffer
if TestsShouldMakeRealFiles {
filePath = fmt.Sprintf("Workbook%d.xlsx", i)
}
err := writeStreamFile(filePath, &buffer, testCase.sheetNames, testCase.workbookData, testCase.headerTypes, TestsShouldMakeRealFiles)
if err != testCase.expectedError && err.Error() != testCase.expectedError.Error() {
t.Fatalf("Error differs from expected error. Error: %v, Expected Error: %v ", err, testCase.expectedError)
}
if testCase.expectedError != nil {
return
}
// read the file back with the xlsx package
var bufReader *bytes.Reader
var size int64
if !TestsShouldMakeRealFiles {
bufReader = bytes.NewReader(buffer.Bytes())
size = bufReader.Size()
}
actualSheetNames, actualWorkbookData := readXLSXFile(t, filePath, bufReader, size, TestsShouldMakeRealFiles)
// check if data was able to be read correctly
if !reflect.DeepEqual(actualSheetNames, testCase.sheetNames) {
t.Fatal("Expected sheet names to be equal")
}
if !reflect.DeepEqual(actualWorkbookData, testCase.workbookData) {
t.Fatal("Expected workbook data to be equal")
}
}
}
// The purpose of TestXlsxStyleBehavior is to ensure that initMaxStyleId has the correct starting value
// and that the logic in AddSheet() that predicts Style IDs is correct.
func (s *StreamSuite) TestXlsxStyleBehavior(t *C) {
file := NewFile()
sheet, err := file.AddSheet("Sheet 1")
if err != nil {
t.Fatal(err)
}
row := sheet.AddRow()
rowData := []string{"testing", "1", "2", "3"}
if count := row.WriteSlice(&rowData, -1); count != len(rowData) {
t.Fatal("not enough cells written")
}
parts, err := file.MarshallParts()
styleSheet, ok := parts["xl/styles.xml"]
if !ok {
t.Fatal("no style sheet")
}
// Created an XLSX file with only the default style.
// We expect that the number of styles is one more than our max index constant.
// This means the library adds two styles by default.
if !strings.Contains(styleSheet, fmt.Sprintf(`<cellXfs count="%d">`, initMaxStyleId+1)) {
t.Fatal("Expected sheet to have two styles")
}
file = NewFile()
sheet, err = file.AddSheet("Sheet 1")
if err != nil {
t.Fatal(err)
}
row = sheet.AddRow()
rowData = []string{"testing", "1", "2", "3", "4"}
if count := row.WriteSlice(&rowData, -1); count != len(rowData) {
t.Fatal("not enough cells written")
}
sheet.Cols[0].SetType(CellTypeString)
sheet.Cols[1].SetType(CellTypeString)
sheet.Cols[3].SetType(CellTypeNumeric)
sheet.Cols[4].SetType(CellTypeString)
parts, err = file.MarshallParts()
styleSheet, ok = parts["xl/styles.xml"]
if !ok {
t.Fatal("no style sheet")
}
// Created an XLSX file with two distinct cell types, which should create two new styles.
// The same cell type was added three times, this should be coalesced into the same style rather than
// recreating the style. This XLSX stream library depends on this behavior when predicting the next style id.
if !strings.Contains(styleSheet, fmt.Sprintf(`<cellXfs count="%d">`, initMaxStyleId+1+2)) {
t.Fatal("Expected sheet to have four styles")
}
}
// writeStreamFile will write the file using this stream package
func writeStreamFile(filePath string, fileBuffer io.Writer, sheetNames []string, workbookData [][][]string, headerTypes [][]*CellType, shouldMakeRealFiles bool) error {
var file *StreamFileBuilder
var err error
if shouldMakeRealFiles {
file, err = NewStreamFileBuilderForPath(filePath)
if err != nil {
return err
}
} else {
file = NewStreamFileBuilder(fileBuffer)
}
for i, sheetName := range sheetNames {
header := workbookData[i][0]
var sheetHeaderTypes []*CellType
if i < len(headerTypes) {
sheetHeaderTypes = headerTypes[i]
}
err := file.AddSheet(sheetName, header, sheetHeaderTypes)
if err != nil {
return err
}
}
streamFile, err := file.Build()
if err != nil {
return err
}
for i, sheetData := range workbookData {
if i != 0 {
err = streamFile.NextSheet()
if err != nil {
return err
}
}
for i, row := range sheetData {
if i == 0 {
continue
}
err = streamFile.Write(row)
if err != nil {
return err
}
}
}
err = streamFile.Close()
if err != nil {
return err
}
return nil
}
// readXLSXFile will read the file using the xlsx package.
func readXLSXFile(t *C, filePath string, fileBuffer io.ReaderAt, size int64, shouldMakeRealFiles bool) ([]string, [][][]string) {
var readFile *File
var err error
if shouldMakeRealFiles {
readFile, err = OpenFile(filePath)
if err != nil {
t.Fatal(err)
}
} else {
readFile, err = OpenReaderAt(fileBuffer, size)
if err != nil {
t.Fatal(err)
}
}
var actualWorkbookData [][][]string
var sheetNames []string
for _, sheet := range readFile.Sheets {
sheetData := [][]string{}
for _, row := range sheet.Rows {
data := []string{}
for _, cell := range row.Cells {
str, err := cell.FormattedValue()
if err != nil {
t.Fatal(err)
}
data = append(data, str)
}
sheetData = append(sheetData, data)
}
sheetNames = append(sheetNames, sheet.Name)
actualWorkbookData = append(actualWorkbookData, sheetData)
}
return sheetNames, actualWorkbookData
}
func (s *StreamSuite) TestAddSheetErrorsAfterBuild(t *C) {
file := NewStreamFileBuilder(bytes.NewBuffer(nil))
err := file.AddSheet("Sheet1", []string{"Header"}, nil)
if err != nil {
t.Fatal(err)
}
err = file.AddSheet("Sheet2", []string{"Header2"}, nil)
if err != nil {
t.Fatal(err)
}
_, err = file.Build()
if err != nil {
t.Fatal(err)
}
err = file.AddSheet("Sheet3", []string{"Header3"}, nil)
if err != BuiltStreamFileBuilderError {
t.Fatal(err)
}
}
func (s *StreamSuite) TestBuildErrorsAfterBuild(t *C) {
file := NewStreamFileBuilder(bytes.NewBuffer(nil))
err := file.AddSheet("Sheet1", []string{"Header"}, nil)
if err != nil {
t.Fatal(err)
}
err = file.AddSheet("Sheet2", []string{"Header2"}, nil)
if err != nil {
t.Fatal(err)
}
_, err = file.Build()
if err != nil {
t.Fatal(err)
}
_, err = file.Build()
if err != BuiltStreamFileBuilderError {
t.Fatal(err)
}
}
func (s *StreamSuite) TestCloseWithNothingWrittenToSheets(t *C) {
buffer := bytes.NewBuffer(nil)
file := NewStreamFileBuilder(buffer)
sheetNames := []string{"Sheet1", "Sheet2"}
workbookData := [][][]string{
{{"Header1", "Header2"}},
{{"Header3", "Header4"}},
}
err := file.AddSheet(sheetNames[0], workbookData[0][0], nil)
if err != nil {
t.Fatal(err)
}
err = file.AddSheet(sheetNames[1], workbookData[1][0], nil)
if err != nil {
t.Fatal(err)
}
stream, err := file.Build()
if err != nil {
t.Fatal(err)
}
err = stream.Close()
if err != nil {
t.Fatal(err)
}
bufReader := bytes.NewReader(buffer.Bytes())
size := bufReader.Size()
actualSheetNames, actualWorkbookData := readXLSXFile(t, "", bufReader, size, false)
// check if data was able to be read correctly
if !reflect.DeepEqual(actualSheetNames, sheetNames) {
t.Fatal("Expected sheet names to be equal")
}
if !reflect.DeepEqual(actualWorkbookData, workbookData) {
t.Fatal("Expected workbook data to be equal")
}
}