2019-12-10 00:16:17 +08:00
|
|
|
package excelize
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/rand"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestStreamWriter(t *testing.T) {
|
|
|
|
file := NewFile()
|
|
|
|
streamWriter, err := file.NewStreamWriter("Sheet1")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// Test max characters in a cell.
|
|
|
|
row := make([]interface{}, 1)
|
|
|
|
row[0] = strings.Repeat("c", 32769)
|
|
|
|
assert.NoError(t, streamWriter.SetRow("A1", &row))
|
|
|
|
|
|
|
|
// Test leading and ending space(s) character characters in a cell.
|
|
|
|
row = make([]interface{}, 1)
|
|
|
|
row[0] = " characters"
|
|
|
|
assert.NoError(t, streamWriter.SetRow("A2", &row))
|
|
|
|
|
|
|
|
row = make([]interface{}, 1)
|
|
|
|
row[0] = []byte("Word")
|
|
|
|
assert.NoError(t, streamWriter.SetRow("A3", &row))
|
|
|
|
|
|
|
|
for rowID := 10; rowID <= 51200; rowID++ {
|
|
|
|
row := make([]interface{}, 50)
|
|
|
|
for colID := 0; colID < 50; colID++ {
|
|
|
|
row[colID] = rand.Intn(640000)
|
|
|
|
}
|
|
|
|
cell, _ := CoordinatesToCellName(1, rowID)
|
|
|
|
assert.NoError(t, streamWriter.SetRow(cell, &row))
|
|
|
|
}
|
|
|
|
|
2019-12-22 00:02:09 +08:00
|
|
|
assert.NoError(t, streamWriter.Flush())
|
2019-12-10 00:16:17 +08:00
|
|
|
// Save xlsx file by the given path.
|
|
|
|
assert.NoError(t, file.SaveAs(filepath.Join("test", "TestStreamWriter.xlsx")))
|
|
|
|
|
|
|
|
// Test error exceptions
|
2019-12-23 00:07:40 +08:00
|
|
|
_, err = file.NewStreamWriter("SheetN")
|
2019-12-10 00:16:17 +08:00
|
|
|
assert.EqualError(t, err, "sheet SheetN is not exist")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFlush(t *testing.T) {
|
|
|
|
// Test error exceptions
|
|
|
|
file := NewFile()
|
|
|
|
streamWriter, err := file.NewStreamWriter("Sheet1")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
streamWriter.Sheet = "SheetN"
|
|
|
|
assert.EqualError(t, streamWriter.Flush(), "sheet SheetN is not exist")
|
2019-12-22 00:02:09 +08:00
|
|
|
|
|
|
|
// Test close temporary file error
|
|
|
|
file = NewFile()
|
|
|
|
streamWriter, err = file.NewStreamWriter("Sheet1")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
for rowID := 10; rowID <= 51200; rowID++ {
|
|
|
|
row := make([]interface{}, 50)
|
|
|
|
for colID := 0; colID < 50; colID++ {
|
|
|
|
row[colID] = rand.Intn(640000)
|
|
|
|
}
|
|
|
|
cell, _ := CoordinatesToCellName(1, rowID)
|
|
|
|
assert.NoError(t, streamWriter.SetRow(cell, &row))
|
|
|
|
}
|
|
|
|
assert.NoError(t, streamWriter.tmpFile.Close())
|
|
|
|
assert.Error(t, streamWriter.Flush())
|
2019-12-10 00:16:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSetRow(t *testing.T) {
|
|
|
|
// Test error exceptions
|
|
|
|
file := NewFile()
|
|
|
|
streamWriter, err := file.NewStreamWriter("Sheet1")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualError(t, streamWriter.SetRow("A", &[]interface{}{}), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
|
|
|
|
assert.EqualError(t, streamWriter.SetRow("A1", []interface{}{}), `pointer to slice expected`)
|
|
|
|
}
|