-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathadvance_test.go
221 lines (195 loc) · 4.72 KB
/
advance_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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package excel_test
import (
"archive/zip"
"os"
"reflect"
"testing"
convert "github.com/szyhf/go-convert"
excel "github.com/szyhf/go-excel"
)
var expectAdvanceList = []Advance{
{
ID: 1,
Name: "Andy",
NamePtr: strPtr("Andy"),
Age: 1,
Slice: []int{1, 2},
Temp: &Temp{
Foo: "Andy",
},
},
{
ID: 2,
Name: "Leo",
NamePtr: strPtr("Leo"),
Age: 2,
Slice: []int{2, 3, 4},
Temp: &Temp{
Foo: "Leo",
},
},
{
ID: 3,
Name: "",
NamePtr: strPtr("Ben"),
Age: 180, // using default
Slice: []int{3, 4, 5, 6},
Temp: &Temp{
Foo: "Ben",
},
},
{
ID: 4,
Name: "Ming",
NamePtr: strPtr("Ming"),
Age: 4,
Slice: []int{1},
Temp: &Temp{
Foo: "Default",
},
},
}
type Advance struct {
// use field name as default column name
ID int
// column means to map the column name, and skip cell that value equal to "Ben"
Name string `xlsx:"column(NameOf);nil(Ben);req();"`
// you can map a column into more than one field
NamePtr *string `xlsx:"column(NameOf);req();"`
// omit `column` if only want to map to column name, it's equal to `column(AgeOf)`
// use 180 as default if cell is empty.
Age int `xlsx:"column(AgeOf);default(180);req();"`
// split means to split the string into slice by the `|`
Slice []int `xlsx:"split(|);req();"`
// use default also can marshal to struct
Temp *Temp `xlsx:"column(UnmarshalString);default({\"Foo\":\"Default\"});req();"`
// use '-' to ignore.
WantIgnored string `xlsx:"-"`
// By default, required tag req is not set
NotRequired string
}
func TestRead(t *testing.T) {
// file
conn := excel.NewConnecter()
err := conn.Open(filePath)
if err != nil {
t.Error(err)
}
defer conn.Close()
rd, err := conn.NewReaderByConfig(&excel.Config{
// Sheet name as string or sheet model as object or as slice of objecg.
Sheet: advSheetName,
// Use the index row as title, every row before title-row will be ignore, default is 0.
TitleRowIndex: 1,
// Skip n row after title, default is 0 (not skip), empty row is not counted.
Skip: 1,
// Auto prefix to sheet name.
Prefix: "",
// Auto suffix to sheet name.
Suffix: advSheetSuffix,
})
if err != nil {
t.Error(err)
}
defer rd.Close()
expectedSheetSize := uint64(2036)
if rd.GetSheetSize() != expectedSheetSize {
t.Errorf("unexpect sheet size: %d", rd.GetSheetSize())
}
idx := 0
for rd.Next() {
var a Advance
err := rd.Read(&a)
if err != nil {
t.Error(err)
return
}
expect := expectAdvanceList[idx]
if !reflect.DeepEqual(expect, a) {
t.Errorf("unexpect advance at %d = \n%s", idx, convert.MustJsonPrettyString(a))
}
idx++
}
if rd.InputOffset() != int64(expectedSheetSize) {
t.Errorf("unexpect input offset: %d", rd.InputOffset())
}
}
func testReadAllWithOpenedConnector(t *testing.T, conn excel.Connecter) {
rd, err := conn.NewReaderByConfig(&excel.Config{
// Sheet name as string or sheet model as object or a slice of object.
Sheet: advSheetName,
// Use the index row as title, every row before title-row will be ignore, default is 0.
TitleRowIndex: 1,
// Skip n row after title, default is 0 (not skip), empty row is not counted.
Skip: 1,
// Auto prefix to sheet name.
Prefix: "",
// Auto suffix to sheet name.
Suffix: advSheetSuffix,
})
if err != nil {
t.Error(err)
}
defer conn.Close()
var slc []Advance
err = rd.ReadAll(&slc)
if err != nil {
t.Error(err)
return
}
if !reflect.DeepEqual(slc, expectAdvanceList) {
t.Errorf("unexpect advance list: \n%s", convert.MustJsonPrettyString(slc))
}
}
func TestEmptyWorkbook(t *testing.T) {
conn := excel.NewConnecter()
// see the Advancd.suffix sheet in simple.xlsx
err := conn.Open(emptyFilePath)
if err != nil {
t.Error(err)
}
rd, err := conn.NewReader(conn.GetSheetNames()[0])
if err != nil {
t.Error(err)
}
if len(rd.GetTitles()) != 0 {
t.Error("empty workbook should have no titles")
}
if rd.Next() {
t.Error("empty workbook should have no rows")
}
}
func TestReadAll(t *testing.T) {
conn := excel.NewConnecter()
t.Run("ReadAll", func(t *testing.T) {
// see the Advancd.suffix sheet in simple.xlsx
err := conn.Open(filePath)
if err != nil {
t.Error(err)
}
testReadAllWithOpenedConnector(t, conn)
})
t.Run("ReadAllByReader", func(t *testing.T) {
zipReader, err := zip.OpenReader(filePath)
if err != nil {
t.Error(err)
}
err = conn.OpenReader(&zipReader.Reader)
if err != nil {
t.Error(err)
}
testReadAllWithOpenedConnector(t, conn)
})
t.Run("ReadAllByBinary", func(t *testing.T) {
// Read data from file
binaryData, err := os.ReadFile(filePath)
if err != nil {
t.Error(err)
}
err = conn.OpenBinary(binaryData)
if err != nil {
t.Error(err)
}
testReadAllWithOpenedConnector(t, conn)
})
}