github.com/artisanhe/tools@v1.0.1-0.20210607022958-19a8fef2eb04/slice_to_excel/to_excel_test.go (about)

     1  package slice_to_excel_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  
     8  	"github.com/artisanhe/tools/slice_to_excel"
     9  	"github.com/artisanhe/tools/timelib"
    10  )
    11  
    12  type GroupedColumn struct {
    13  	Int    int    `xlsx:"int1"`
    14  	String string `xlsx:"string1"`
    15  }
    16  
    17  type Column struct {
    18  	GroupedColumn
    19  	Int    int                    `xlsx:"int2"`
    20  	String string                 `xlsx:"string2"`
    21  	Time   timelib.MySQLTimestamp `xlsx:"time1"`
    22  	Time2  timelib.MySQLTimestamp `xlsx:"time2"`
    23  }
    24  
    25  func TestToExcel(t *testing.T) {
    26  	tt := assert.New(t)
    27  	time, _ := timelib.ParseMySQLTimestampFromString("2017-06-01T10:00:00Z")
    28  	time2 := timelib.MySQLTimestampZero
    29  	rows := []Column{
    30  		{
    31  			GroupedColumn: GroupedColumn{
    32  				Int:    11,
    33  				String: "s1",
    34  			},
    35  			Int:    12,
    36  			String: "s2",
    37  			Time:   time,
    38  			Time2:  time2,
    39  		},
    40  	}
    41  
    42  	file, err := slice_to_excel.GetExcel("test", rows)
    43  	tt.Nil(err)
    44  
    45  	{
    46  		labelRows := []string{"int1", "string1", "int2", "string2", "time1", "time2"}
    47  
    48  		cellValues := []string{}
    49  
    50  		for _, cell := range file.Sheet["test"].Rows[0].Cells {
    51  			cellValues = append(cellValues, cell.Value)
    52  		}
    53  
    54  		tt.Equal(labelRows, cellValues)
    55  
    56  	}
    57  	{
    58  		valueRow := []string{"11", "s1", "12", "s2", "2017-06-01T18:00:00+08:00", ""}
    59  
    60  		cellValues := []string{}
    61  
    62  		for _, cell := range file.Sheet["test"].Rows[1].Cells {
    63  			cellValues = append(cellValues, cell.Value)
    64  		}
    65  
    66  		tt.Equal(valueRow, cellValues)
    67  	}
    68  }