-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexcelx_test.go
157 lines (138 loc) · 3.13 KB
/
excelx_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
package excelx_test
import (
"bytes"
"encoding/json"
"fmt"
"os"
"testing"
"github.com/prongbang/excelx"
)
// Define a sample struct
type Person struct {
Name string `header:"Name" no:"3"`
Age int `header:"Age" no:"1"`
City string `header:"City" no:"2"`
Other string `header:"other" no:""`
}
type Person2 struct {
Age int `header:"Age" no:"1"`
Other string `header:"other" no:"2"`
}
type Simple struct {
A *string `header:"A"`
B *float64 `header:"B"`
C *float64 `header:"C"`
D *float64 `header:"D"`
E *float64 `header:"E"`
F *float64 `header:"F"`
G *float64 `header:"G"`
H *float64 `header:"H"`
I *float64 `header:"I"`
J *float64 `header:"J"`
K *float64 `header:"K"`
L *float64 `header:"L"`
M *float64 `header:"M"`
N *float64 `header:"N"`
O *float64 `header:"O"`
P *float64 `header:"P"`
Q *float64 `header:"Q"`
R *float64 `header:"R"`
S *float64 `header:"S"`
T *float64 `header:"T"`
U *float64 `header:"U"`
V *float64 `header:"V"`
W *float64 `header:"W"`
X *float64 `header:"X"`
Y *float64 `header:"Y"`
Z *float64 `header:"Z"`
}
func TestNumberToColName(t *testing.T) {
// Given
column := 10
// When
actual := excelx.NumberToColName(column)
// Then
if actual != "J" {
t.Error("Error", actual)
}
}
func TestConvertIsNotEmpty(t *testing.T) {
// Given
persons := []Person{
{"John Doe", 25, "Exec York", "555"},
{"Jane Doe", 30, "San Francisco", "555"},
{"Bob Smith", 22, "Chicago", "555"},
}
// When
_, err := excelx.Convert[Person](persons)
// Then
if err != nil {
t.Error("Error", err)
}
}
func TestConvertsIsNotEmpty(t *testing.T) {
// Given
persons1 := []Person{
{"John Doe", 25, "Exec York", "555"},
{"Jane Doe", 30, "San Francisco", "555"},
{"Bob Smith", 22, "Chicago", "555"},
}
persons2 := []Person2{
{25, "Exec York1"},
{26, "Exec York2"},
{27, "Exec York3"},
}
persons3 := []Person2{}
// When
file, err := excelx.Converts(func(file excelx.Xlsx) []excelx.Sheet {
return []excelx.Sheet{
{
Name: "Person1",
Exec: func(name string) { excelx.NewSheet(file, name, persons1) },
},
{
Name: "Person2",
Exec: func(name string) { excelx.NewSheet(file, name, persons2) },
},
{
Name: "Person3",
Exec: func(name string) { excelx.NewSheet(file, name, persons3) },
},
}
})
// Then
if err != nil {
t.Error("Error", err)
} else {
_ = file.File.SaveAs("output.xlsx")
}
}
func TestConvertIsEmpty(t *testing.T) {
// Given
persons := []Person{}
// When
_, err := excelx.Convert[Person](persons)
// Then
if err == nil {
t.Error("Not error", err)
}
}
func TestParser(t *testing.T) {
// Given
data, err := os.ReadFile("simple.xlsx")
reader := bytes.NewReader(data)
expect := `{"A":"A1","B":2,"C":3,"D":4,"E":5,"F":5,"G":null,"H":7,"I":8,"J":9,"K":10,"L":11.5,"M":12,"N":5,"O":14.5,"P":null,"Q":null,"R":2.3,"S":1.4,"T":1.4,"U":1.4,"V":1.4,"W":1.4,"X":4,"Y":null,"Z":null}`
// When
simple, err := excelx.Parser[Simple](reader)
// Then
if err != nil {
t.Error("Not error", err)
}
fmt.Println(len(simple))
for _, s := range simple {
b, _ := json.Marshal(s)
if string(b) != expect {
t.Error("Parse error", string(b))
}
}
}