stream_test.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. package xlsx
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io"
  6. "reflect"
  7. "strings"
  8. "testing"
  9. "time"
  10. )
  11. const (
  12. TestsShouldMakeRealFiles = false
  13. )
  14. func TestTestsShouldMakeRealFilesShouldBeFalse(t *testing.T) {
  15. if TestsShouldMakeRealFiles {
  16. t.Fatal("TestsShouldMakeRealFiles should only be true for local debugging. Don't forget to switch back before commiting.")
  17. }
  18. }
  19. func TestPartialReadsNoSharedStrings(t *testing.T) {
  20. rowLimit := 10
  21. start := time.Now()
  22. file, err := OpenFileWithRowLimit("testdocs/large_sheet_no_shared_strings_no_dimension_tag.xlsx", rowLimit)
  23. if err != nil {
  24. t.Fatal(err)
  25. }
  26. timeSpent := time.Now().Sub(start)
  27. timeLimit := 100 * time.Millisecond
  28. if timeSpent > timeLimit {
  29. t.Errorf("Reading %v rows from a sheet with ~31,000 rows took %v, must take less than %v", rowLimit, timeSpent, timeLimit)
  30. }
  31. if len(file.Sheets[0].Rows) != rowLimit {
  32. t.Errorf("Expected sheet to have %v rows, but found %v rows", rowLimit, len(file.Sheets[0].Rows))
  33. }
  34. }
  35. func TestPartialReadsWithSharedStrings(t *testing.T) {
  36. rowLimit := 10
  37. start := time.Now()
  38. file, err := OpenFileWithRowLimit("testdocs/large_sheet_large_sharedstrings_dimension_tag.xlsx", rowLimit)
  39. if err != nil {
  40. t.Fatal(err)
  41. }
  42. timeSpent := time.Now().Sub(start)
  43. timeLimit := time.Second
  44. if timeSpent > timeLimit {
  45. t.Errorf("Reading %v rows from a sheet with ~31,000 rows took %v, must take less than %v", rowLimit, timeSpent, timeLimit)
  46. }
  47. // This is testing that the sheet was truncated, but it is also testing that the dimension tag was ignored.
  48. // If the dimension tag is not correctly ignored, there will be 10 rows of the data, plus ~31k empty rows tacked on.
  49. if len(file.Sheets[0].Rows) != rowLimit {
  50. t.Errorf("Expected sheet to have %v rows, but found %v rows", rowLimit, len(file.Sheets[0].Rows))
  51. }
  52. }
  53. func TestXlsxStreamWrite(t *testing.T) {
  54. // When shouldMakeRealFiles is set to true this test will make actual XLSX files in the file system.
  55. // This is useful to ensure files open in Excel, Numbers, Google Docs, etc.
  56. // In case of issues you can use "Open XML SDK 2.5" to diagnose issues in generated XLSX files:
  57. // https://www.microsoft.com/en-us/download/details.aspx?id=30425
  58. testCases := []struct {
  59. testName string
  60. sheetNames []string
  61. workbookData [][][]string
  62. headerTypes [][]*CellType
  63. expectedError error
  64. }{
  65. {
  66. testName: "One Sheet",
  67. sheetNames: []string{
  68. "Sheet1",
  69. },
  70. workbookData: [][][]string{
  71. {
  72. {"Token", "Name", "Price", "SKU"},
  73. {"123", "Taco", "300", "0000000123"},
  74. },
  75. },
  76. headerTypes: [][]*CellType{
  77. {nil, CellTypeString.Ptr(), nil, CellTypeString.Ptr()},
  78. },
  79. },
  80. {
  81. testName: "One Column",
  82. sheetNames: []string{
  83. "Sheet1",
  84. },
  85. workbookData: [][][]string{
  86. {
  87. {"Token"},
  88. {"123"},
  89. },
  90. },
  91. },
  92. {
  93. testName: "Several Sheets, with different numbers of columns and rows",
  94. sheetNames: []string{
  95. "Sheet 1", "Sheet 2", "Sheet3",
  96. },
  97. workbookData: [][][]string{
  98. {
  99. {"Token", "Name", "Price", "SKU"},
  100. {"123", "Taco", "300", "0000000123"},
  101. },
  102. {
  103. {"Token", "Name", "Price", "SKU", "Stock"},
  104. {"456", "Salsa", "200", "0346", "1"},
  105. {"789", "Burritos", "400", "754", "3"},
  106. },
  107. {
  108. {"Token", "Name", "Price"},
  109. {"9853", "Guacamole", "500"},
  110. {"2357", "Margarita", "700"},
  111. },
  112. },
  113. },
  114. {
  115. testName: "Two Sheets with same the name",
  116. sheetNames: []string{
  117. "Sheet 1", "Sheet 1",
  118. },
  119. workbookData: [][][]string{
  120. {
  121. {"Token", "Name", "Price", "SKU"},
  122. {"123", "Taco", "300", "0000000123"},
  123. },
  124. {
  125. {"Token", "Name", "Price", "SKU", "Stock"},
  126. {"456", "Salsa", "200", "0346", "1"},
  127. {"789", "Burritos", "400", "754", "3"},
  128. },
  129. },
  130. expectedError: fmt.Errorf("duplicate sheet name '%s'.", "Sheet 1"),
  131. },
  132. {
  133. testName: "One Sheet Registered, tries to write to two",
  134. sheetNames: []string{
  135. "Sheet 1",
  136. },
  137. workbookData: [][][]string{
  138. {
  139. {"Token", "Name", "Price", "SKU"},
  140. {"123", "Taco", "300", "0000000123"},
  141. },
  142. {
  143. {"Token", "Name", "Price", "SKU"},
  144. {"456", "Salsa", "200", "0346"},
  145. },
  146. },
  147. expectedError: AlreadyOnLastSheetError,
  148. },
  149. {
  150. testName: "One Sheet, too many columns in row 1",
  151. sheetNames: []string{
  152. "Sheet 1",
  153. },
  154. workbookData: [][][]string{
  155. {
  156. {"Token", "Name", "Price", "SKU"},
  157. {"123", "Taco", "300", "0000000123", "asdf"},
  158. },
  159. },
  160. expectedError: WrongNumberOfRowsError,
  161. },
  162. {
  163. testName: "One Sheet, too few columns in row 1",
  164. sheetNames: []string{
  165. "Sheet 1",
  166. },
  167. workbookData: [][][]string{
  168. {
  169. {"Token", "Name", "Price", "SKU"},
  170. {"123", "Taco", "300"},
  171. },
  172. },
  173. expectedError: WrongNumberOfRowsError,
  174. },
  175. {
  176. testName: "Lots of Sheets, only writes rows to one, only writes headers to one, should not error and should still create a valid file",
  177. sheetNames: []string{
  178. "Sheet 1", "Sheet 2", "Sheet 3", "Sheet 4", "Sheet 5", "Sheet 6",
  179. },
  180. workbookData: [][][]string{
  181. {
  182. {"Token", "Name", "Price", "SKU"},
  183. {"123", "Taco", "300", "0000000123"},
  184. },
  185. {{}},
  186. {{"Id", "Unit Cost"}},
  187. {{}},
  188. {{}},
  189. {{}},
  190. },
  191. },
  192. {
  193. testName: "Two Sheets, only writes to one, should not error and should still create a valid file",
  194. sheetNames: []string{
  195. "Sheet 1", "Sheet 2",
  196. },
  197. workbookData: [][][]string{
  198. {
  199. {"Token", "Name", "Price", "SKU"},
  200. {"123", "Taco", "300", "0000000123"},
  201. },
  202. {{}},
  203. },
  204. },
  205. {
  206. testName: "Larger Sheet",
  207. sheetNames: []string{
  208. "Sheet 1",
  209. },
  210. workbookData: [][][]string{
  211. {
  212. {"Token", "Name", "Price", "SKU", "Token", "Name", "Price", "SKU", "Token", "Name", "Price", "SKU", "Token", "Name", "Price", "SKU", "Token", "Name", "Price", "SKU", "Token", "Name", "Price", "SKU"},
  213. {"123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123"},
  214. {"456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346"},
  215. {"789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754"},
  216. {"123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123"},
  217. {"456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346"},
  218. {"789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754"},
  219. {"123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123"},
  220. {"456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346"},
  221. {"789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754"},
  222. {"123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123"},
  223. {"456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346"},
  224. {"789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754"},
  225. {"123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123"},
  226. {"456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346"},
  227. {"789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754"},
  228. {"123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123"},
  229. {"456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346"},
  230. {"789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754"},
  231. {"123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123"},
  232. {"456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346"},
  233. {"789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754"},
  234. {"123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123"},
  235. {"456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346"},
  236. {"789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754"},
  237. },
  238. },
  239. },
  240. {
  241. testName: "UTF-8 Characters. This XLSX File loads correctly with Excel, Numbers, and Google Docs. It also passes Microsoft's Office File Format Validator.",
  242. sheetNames: []string{
  243. "Sheet1",
  244. },
  245. workbookData: [][][]string{
  246. {
  247. // String courtesy of https://github.com/minimaxir/big-list-of-naughty-strings/
  248. // Header row contains the tags that I am filtering on
  249. {"Token", endSheetDataTag, "Price", fmt.Sprintf(dimensionTag, "A1:D1")},
  250. // Japanese and emojis
  251. {"123", "パーティーへ行かないか", "300", "🍕🐵 🙈 🙉 🙊"},
  252. // XML encoder/parser test strings
  253. {"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>`},
  254. // Upside down text and Right to Left Arabic text
  255. {"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˥
  256. 00˙Ɩ$-`, "300", `ﷺ`},
  257. {"123", "Taco", "300", "0000000123"},
  258. },
  259. },
  260. },
  261. }
  262. for i, testCase := range testCases {
  263. t.Run(testCase.testName, func(t *testing.T) {
  264. var filePath string
  265. var buffer bytes.Buffer
  266. if TestsShouldMakeRealFiles {
  267. filePath = fmt.Sprintf("Workbook%d.xlsx", i)
  268. }
  269. err := writeStreamFile(filePath, &buffer, testCase.sheetNames, testCase.workbookData, testCase.headerTypes, TestsShouldMakeRealFiles)
  270. if err != testCase.expectedError && err.Error() != testCase.expectedError.Error() {
  271. t.Fatalf("Error differs from expected error. Error: %v, Expected Error: %v ", err, testCase.expectedError)
  272. }
  273. if testCase.expectedError != nil {
  274. return
  275. }
  276. // read the file back with the xlsx package
  277. var bufReader *bytes.Reader
  278. var size int64
  279. if !TestsShouldMakeRealFiles {
  280. bufReader = bytes.NewReader(buffer.Bytes())
  281. size = bufReader.Size()
  282. }
  283. actualSheetNames, actualWorkbookData := readXLSXFile(t, filePath, bufReader, size, TestsShouldMakeRealFiles)
  284. // check if data was able to be read correctly
  285. if !reflect.DeepEqual(actualSheetNames, testCase.sheetNames) {
  286. t.Fatal("Expected sheet names to be equal")
  287. }
  288. if !reflect.DeepEqual(actualWorkbookData, testCase.workbookData) {
  289. t.Fatal("Expected workbook data to be equal")
  290. }
  291. })
  292. }
  293. }
  294. // The purpose of TestXlsxStyleBehavior is to ensure that initMaxStyleId has the correct starting value
  295. // and that the logic in AddSheet() that predicts Style IDs is correct.
  296. func TestXlsxStyleBehavior(t *testing.T) {
  297. file := NewFile()
  298. sheet, err := file.AddSheet("Sheet 1")
  299. if err != nil {
  300. t.Fatal(err)
  301. }
  302. row := sheet.AddRow()
  303. rowData := []string{"testing", "1", "2", "3"}
  304. if count := row.WriteSlice(&rowData, -1); count != len(rowData) {
  305. t.Fatal("not enough cells written")
  306. }
  307. parts, err := file.MarshallParts()
  308. styleSheet, ok := parts["xl/styles.xml"]
  309. if !ok {
  310. t.Fatal("no style sheet")
  311. }
  312. // Created an XLSX file with only the default style.
  313. // We expect that the number of styles is one more than our max index constant.
  314. // This means the library adds two styles by default.
  315. if !strings.Contains(styleSheet, fmt.Sprintf(`<cellXfs count="%d">`, initMaxStyleId+1)) {
  316. t.Fatal("Expected sheet to have two styles")
  317. }
  318. file = NewFile()
  319. sheet, err = file.AddSheet("Sheet 1")
  320. if err != nil {
  321. t.Fatal(err)
  322. }
  323. row = sheet.AddRow()
  324. rowData = []string{"testing", "1", "2", "3", "4"}
  325. if count := row.WriteSlice(&rowData, -1); count != len(rowData) {
  326. t.Fatal("not enough cells written")
  327. }
  328. sheet.Cols[0].SetType(CellTypeString)
  329. sheet.Cols[1].SetType(CellTypeString)
  330. sheet.Cols[3].SetType(CellTypeNumeric)
  331. sheet.Cols[4].SetType(CellTypeString)
  332. parts, err = file.MarshallParts()
  333. styleSheet, ok = parts["xl/styles.xml"]
  334. if !ok {
  335. t.Fatal("no style sheet")
  336. }
  337. // Created an XLSX file with two distinct cell types, which should create two new styles.
  338. // The same cell type was added three times, this should be coalesced into the same style rather than
  339. // recreating the style. This XLSX stream library depends on this behavior when predicting the next style id.
  340. if !strings.Contains(styleSheet, fmt.Sprintf(`<cellXfs count="%d">`, initMaxStyleId+1+2)) {
  341. t.Fatal("Expected sheet to have four styles")
  342. }
  343. }
  344. // writeStreamFile will write the file using this stream package
  345. func writeStreamFile(filePath string, fileBuffer io.Writer, sheetNames []string, workbookData [][][]string, headerTypes [][]*CellType, shouldMakeRealFiles bool) error {
  346. var file *StreamFileBuilder
  347. var err error
  348. if shouldMakeRealFiles {
  349. file, err = NewStreamFileBuilderForPath(filePath)
  350. if err != nil {
  351. return err
  352. }
  353. } else {
  354. file = NewStreamFileBuilder(fileBuffer)
  355. }
  356. for i, sheetName := range sheetNames {
  357. header := workbookData[i][0]
  358. var sheetHeaderTypes []*CellType
  359. if i < len(headerTypes) {
  360. sheetHeaderTypes = headerTypes[i]
  361. }
  362. err := file.AddSheet(sheetName, header, sheetHeaderTypes)
  363. if err != nil {
  364. return err
  365. }
  366. }
  367. streamFile, err := file.Build()
  368. if err != nil {
  369. return err
  370. }
  371. for i, sheetData := range workbookData {
  372. if i != 0 {
  373. err = streamFile.NextSheet()
  374. if err != nil {
  375. return err
  376. }
  377. }
  378. for i, row := range sheetData {
  379. if i == 0 {
  380. continue
  381. }
  382. err = streamFile.Write(row)
  383. if err != nil {
  384. return err
  385. }
  386. }
  387. }
  388. err = streamFile.Close()
  389. if err != nil {
  390. return err
  391. }
  392. return nil
  393. }
  394. // readXLSXFile will read the file using the xlsx package.
  395. func readXLSXFile(t *testing.T, filePath string, fileBuffer io.ReaderAt, size int64, shouldMakeRealFiles bool) ([]string, [][][]string) {
  396. var readFile *File
  397. var err error
  398. if shouldMakeRealFiles {
  399. readFile, err = OpenFile(filePath)
  400. if err != nil {
  401. t.Fatal(err)
  402. }
  403. } else {
  404. readFile, err = OpenReaderAt(fileBuffer, size)
  405. if err != nil {
  406. t.Fatal(err)
  407. }
  408. }
  409. var actualWorkbookData [][][]string
  410. var sheetNames []string
  411. for _, sheet := range readFile.Sheets {
  412. sheetData := [][]string{}
  413. for _, row := range sheet.Rows {
  414. data := []string{}
  415. for _, cell := range row.Cells {
  416. str, err := cell.FormattedValue()
  417. if err != nil {
  418. t.Fatal(err)
  419. }
  420. data = append(data, str)
  421. }
  422. sheetData = append(sheetData, data)
  423. }
  424. sheetNames = append(sheetNames, sheet.Name)
  425. actualWorkbookData = append(actualWorkbookData, sheetData)
  426. }
  427. return sheetNames, actualWorkbookData
  428. }
  429. func TestAddSheetErrorsAfterBuild(t *testing.T) {
  430. file := NewStreamFileBuilder(bytes.NewBuffer(nil))
  431. err := file.AddSheet("Sheet1", []string{"Header"}, nil)
  432. if err != nil {
  433. t.Fatal(err)
  434. }
  435. err = file.AddSheet("Sheet2", []string{"Header2"}, nil)
  436. if err != nil {
  437. t.Fatal(err)
  438. }
  439. _, err = file.Build()
  440. if err != nil {
  441. t.Fatal(err)
  442. }
  443. err = file.AddSheet("Sheet3", []string{"Header3"}, nil)
  444. if err != BuiltStreamFileBuilderError {
  445. t.Fatal(err)
  446. }
  447. }
  448. func TestBuildErrorsAfterBuild(t *testing.T) {
  449. file := NewStreamFileBuilder(bytes.NewBuffer(nil))
  450. err := file.AddSheet("Sheet1", []string{"Header"}, nil)
  451. if err != nil {
  452. t.Fatal(err)
  453. }
  454. err = file.AddSheet("Sheet2", []string{"Header2"}, nil)
  455. if err != nil {
  456. t.Fatal(err)
  457. }
  458. _, err = file.Build()
  459. if err != nil {
  460. t.Fatal(err)
  461. }
  462. _, err = file.Build()
  463. if err != BuiltStreamFileBuilderError {
  464. t.Fatal(err)
  465. }
  466. }
  467. func TestCloseWithNothingWrittenToSheets(t *testing.T) {
  468. buffer := bytes.NewBuffer(nil)
  469. file := NewStreamFileBuilder(buffer)
  470. sheetNames := []string{"Sheet1", "Sheet2"}
  471. workbookData := [][][]string{
  472. {{"Header1", "Header2"}},
  473. {{"Header3", "Header4"}},
  474. }
  475. err := file.AddSheet(sheetNames[0], workbookData[0][0], nil)
  476. if err != nil {
  477. t.Fatal(err)
  478. }
  479. err = file.AddSheet(sheetNames[1], workbookData[1][0], nil)
  480. if err != nil {
  481. t.Fatal(err)
  482. }
  483. stream, err := file.Build()
  484. if err != nil {
  485. t.Fatal(err)
  486. }
  487. err = stream.Close()
  488. if err != nil {
  489. t.Fatal(err)
  490. }
  491. bufReader := bytes.NewReader(buffer.Bytes())
  492. size := bufReader.Size()
  493. actualSheetNames, actualWorkbookData := readXLSXFile(t, "", bufReader, size, false)
  494. // check if data was able to be read correctly
  495. if !reflect.DeepEqual(actualSheetNames, sheetNames) {
  496. t.Fatal("Expected sheet names to be equal")
  497. }
  498. if !reflect.DeepEqual(actualWorkbookData, workbookData) {
  499. t.Fatal("Expected workbook data to be equal")
  500. }
  501. }