-
Notifications
You must be signed in to change notification settings - Fork 2
/
writer_test.go
95 lines (92 loc) · 1.92 KB
/
writer_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
package xlsxsql
import (
"reflect"
"testing"
"github.com/xuri/excelize/v2"
)
func TestXLSXWriter_WriteRow(t *testing.T) {
type opts struct {
fileName string
f *excelize.File
sheet string
cellName string
}
type args struct {
row []any
columns []string
types []string
}
tests := []struct {
name string
fields opts
args args
wantErr bool
wantRows [][]string
}{
{
name: "test1",
fields: opts{
fileName: "dummy.xlsx",
f: excelize.NewFile(),
sheet: "Sheet1",
cellName: "A1",
},
args: args{
row: []any{"a", "b", "c"},
columns: []string{"A", "B", "C"},
types: []string{"string", "string", "string"},
},
wantErr: false,
wantRows: [][]string{
{"A", "B", "C"},
{"a", "b", "c"},
},
},
{
name: "test2",
fields: opts{
fileName: "dummy.xlsx",
f: excelize.NewFile(),
sheet: "Sheet1",
cellName: "A2",
},
args: args{
row: []any{"a", "b", "c"},
columns: []string{"A", "B", "C"},
types: []string{"string", "string", "string"},
},
wantErr: false,
wantRows: [][]string{
nil,
{"A", "B", "C"},
{"a", "b", "c"},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
w, err := NewXLSXWriter(
FileName(tt.fields.fileName),
Sheet(tt.fields.sheet),
Cell(tt.fields.cellName),
ClearSheet(true),
)
if err != nil {
t.Fatal(err)
}
if err := w.PreWrite(tt.args.columns, tt.args.types); err != nil {
t.Fatal(err)
}
if err := w.WriteRow(tt.args.row, tt.args.columns); (err != nil) != tt.wantErr {
t.Errorf("XLSXWriter.WriteRow() error = %v, wantErr %v", err, tt.wantErr)
}
rows, err := w.f.GetRows(tt.fields.sheet)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(rows, tt.wantRows) {
t.Errorf("XLSXWriter.WriteRow() rows = %#v, wantRows %#v", rows, tt.wantRows)
}
})
}
}