Browse Source

Creater a test server for streaming randomly generated excel files

DamianSzkuat 6 years ago
parent
commit
58eaecde28
2 changed files with 112 additions and 6 deletions
  1. 6 6
      stream_style_test.go
  2. 106 0
      stream_test/stream_server.go

+ 6 - 6
stream_style_test.go

@@ -19,13 +19,13 @@ type StreamStyleSuite struct{}
 
 var _ = Suite(&StreamStyleSuite{})
 
-func (s *StreamSuite) TestStreamTestsShouldMakeRealFilesShouldBeFalse(t *C) {
+func (s *StreamStyleSuite) TestStreamTestsShouldMakeRealFilesShouldBeFalse(t *C) {
 	if StyleStreamTestsShouldMakeRealFiles {
 		t.Fatal("TestsShouldMakeRealFiles should only be true for local debugging. Don't forget to switch back before commiting.")
 	}
 }
 
-func (s *StreamSuite) TestXlsxStreamWriteWithStyle(t *C) {
+func (s *StreamStyleSuite) TestXlsxStreamWriteWithStyle(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:
@@ -418,7 +418,7 @@ func readXLSXFileS(t *C, filePath string, fileBuffer io.ReaderAt, size int64, sh
 	return sheetNames, actualWorkbookData, actualWorkBookCells
 }
 
-func (s *StreamSuite) TestDates(t *C) {
+func (s *StreamStyleSuite) TestDates(t *C) {
 	var filePath string
 	var buffer bytes.Buffer
 	if StyleStreamTestsShouldMakeRealFiles {
@@ -542,7 +542,7 @@ func (s *StreamSuite) TestMakeNewStylesAndUseIt(t *C) {
 	}
 }
 
-func (s *StreamSuite) TestCloseWithNothingWrittenToSheetsWithStyle(t *C) {
+func (s *StreamStyleSuite) TestCloseWithNothingWrittenToSheetsWithStyle(t *C) {
 	buffer := bytes.NewBuffer(nil)
 	file := NewStreamFileBuilder(buffer)
 
@@ -604,7 +604,7 @@ func (s *StreamSuite) TestCloseWithNothingWrittenToSheetsWithStyle(t *C) {
 	}
 }
 
-func (s *StreamSuite) TestBuildErrorsAfterBuildWithStyle(t *C) {
+func (s *StreamStyleSuite) TestBuildErrorsAfterBuildWithStyle(t *C) {
 	file := NewStreamFileBuilder(bytes.NewBuffer(nil))
 
 	defaultStyles := []StreamStyle{StreamStyleDefaultString, StreamStyleBoldString, StreamStyleItalicInteger, StreamStyleUnderlinedString,
@@ -634,7 +634,7 @@ func (s *StreamSuite) TestBuildErrorsAfterBuildWithStyle(t *C) {
 	}
 }
 
-func (s *StreamSuite) TestAddSheetWithStyleErrorsAfterBuild(t *C) {
+func (s *StreamStyleSuite) TestAddSheetWithStyleErrorsAfterBuild(t *C) {
 	file := NewStreamFileBuilder(bytes.NewBuffer(nil))
 
 	defaultStyles := []StreamStyle{StreamStyleDefaultString, StreamStyleBoldString, StreamStyleItalicInteger, StreamStyleUnderlinedString,

+ 106 - 0
stream_test/stream_server.go

@@ -0,0 +1,106 @@
+package main
+
+import (
+	. "github.com/damianszkuat/xlsx"
+	"io"
+	"net/http"
+	"strconv"
+	"math/rand"
+)
+
+func StreamFileWithDate(w http.ResponseWriter, r *http.Request) {
+	w.Header().Add("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
+	sheetNames, workbookData := generateExcelFile(1, 1000000, 10)
+	writeStreamFileWithStyle(w, sheetNames, workbookData, []StreamStyle{})
+}
+
+// writeStreamFile will write the file using this stream package
+func writeStreamFileWithStyle(fileBuffer io.Writer, sheetNames []string,
+	workbookData [][][]StreamCell, customStyles []StreamStyle) error {
+
+	var file *StreamFileBuilder
+	var err error
+
+	file = NewStreamFileBuilder(fileBuffer)
+
+	defaultStyles := []StreamStyle{StreamStyleDefaultString, StreamStyleBoldString, StreamStyleItalicString, StreamStyleUnderlinedString,
+		StreamStyleDefaultInteger, StreamStyleBoldInteger, StreamStyleItalicInteger, StreamStyleUnderlinedInteger,
+		StreamStyleDefaultDate}
+	allStylesToBeAdded := append(defaultStyles, customStyles...)
+	err = file.AddStreamStyleList(allStylesToBeAdded)
+	if err != nil {
+		return err
+	}
+
+	for i, sheetName := range sheetNames {
+		var colStyles []StreamStyle
+		for range workbookData[i][0] {
+			colStyles = append(colStyles, StreamStyleDefaultString)
+		}
+
+		err := file.AddSheetS(sheetName, colStyles)
+		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 _, row := range sheetData {
+			err = streamFile.WriteS(row)
+			if err != nil {
+				return err
+			}
+		}
+	}
+	err = streamFile.Close()
+	if err != nil {
+		return err
+	}
+	return nil
+}
+
+func generateExcelFile(numOfSheets int, numOfRows int, numOfCols int) ([]string, [][][]StreamCell) {
+	var sheetNames []string
+	var workbookData [][][]StreamCell
+	for i := 0; i<numOfSheets; i++{
+		sheetNames = append(sheetNames, strconv.Itoa(i))
+		workbookData = append(workbookData, [][]StreamCell{})
+		for j := 0; j<numOfRows; j++ {
+			workbookData[i] = append(workbookData[i], []StreamCell{})
+			for k := 0; k<numOfCols; k++ {
+				var style StreamStyle
+
+				if k%2==0 {
+					style = StreamStyleDefaultInteger
+				} else if k%3 == 0 {
+					style = StreamStyleBoldInteger
+				} else if k%5 == 0 {
+					style = StreamStyleItalicInteger
+				} else {
+					style = StreamStyleUnderlinedInteger
+				}
+
+				workbookData[i][j] = append(workbookData[i][j], NewStyledIntegerStreamCell(rand.Intn(100),style))
+			}
+		}
+	}
+
+	return sheetNames, workbookData
+}
+
+func main() {
+	http.HandleFunc("/", StreamFileWithDate)
+	if err := http.ListenAndServe(":8080", nil); err != nil {
+		panic(err)
+	}
+}