-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
load_test.go
194 lines (169 loc) · 3.96 KB
/
load_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
package csvtag
import (
"testing"
)
type test struct {
Name string `csv:"header1"`
ID int `csv:"header2"`
Num float64 `csv:"header3"`
}
type testTagKey struct {
Name string `json:"header1"`
ID int `json:"header2"`
Num float64 `json:"header3"`
}
type testNoID struct {
Name string `csv:"header1"`
ID int
Num float64 `csv:"header"`
}
type testBool struct {
Name string `csv:"header1"`
ABool bool `csv:"header2"`
}
type testF32 struct {
Name string `csv:"header1"`
Num float32 `csv:"header2"`
}
// Check the values are correct
func checkValues(tabT []test) bool {
return false ||
tabT[0].Name != "line1" || tabT[0].ID != 1 || tabT[0].Num != 1.2 ||
tabT[1].Name != "line2" || tabT[1].ID != 2 || tabT[1].Num != 2.3 ||
tabT[2].Name != "line3" || tabT[2].ID != 3 || tabT[2].Num != 3.4
}
func checkBoolValues(tabT []testBool) bool {
return false ||
tabT[0].Name != "line1" || tabT[0].ABool != true ||
tabT[1].Name != "line2" || tabT[1].ABool != false ||
tabT[2].Name != "line3" || tabT[2].ABool != false
}
func checkFloat32Values(tabT []testF32) bool {
return false ||
tabT[0].Name != "line1" || tabT[0].Num != 1.2 ||
tabT[1].Name != "line2" || tabT[1].Num != 2.3
}
func TestValideFile(t *testing.T) {
tabT := []test{}
err := LoadFromPath("csv_files/valid.csv", &tabT)
if err != nil || checkValues(tabT) {
t.Fail()
}
}
func TestValideFileWithBOM(t *testing.T) {
tabT := []test{}
err := LoadFromPath("csv_files/valid_bom.csv", &tabT)
if err != nil || checkValues(tabT) {
t.Fail()
}
}
func TestBool(t *testing.T) {
tabT := []testBool{}
err := LoadFromPath("csv_files/bool.csv", &tabT)
if err != nil || checkBoolValues(tabT) {
t.Fail()
}
}
func TestF32(t *testing.T) {
tabT := []testF32{}
err := LoadFromPath("csv_files/float32.csv", &tabT)
if err != nil || checkFloat32Values(tabT) {
t.Fail()
}
}
func TestBadHeader(t *testing.T) {
tabT := []test{}
err := LoadFromPath("csv_files/badHeader.csv", &tabT)
if err != nil || checkValues(tabT) {
t.Fail()
}
}
func TestMissATag(t *testing.T) {
tabT := []testNoID{}
err := LoadFromPath("csv_files/valid.csv", &tabT)
if err != nil ||
tabT[0].Name != "line1" || tabT[0].ID != 0 || tabT[0].Num != 0 ||
tabT[1].Name != "line2" || tabT[1].ID != 0 || tabT[1].Num != 0 ||
tabT[2].Name != "line3" || tabT[2].ID != 0 || tabT[2].Num != 0 {
t.Fail()
}
}
func TestEmptyFile(t *testing.T) {
tabT := []test{}
err := LoadFromPath("csv_files/empty.csv", &tabT)
if err != nil || len(tabT) != 0 {
t.Fail()
}
}
func TestNoHeader(t *testing.T) {
tabT := []test{}
err := LoadFromPath(
"csv_files/noHeader.csv",
&tabT,
CsvOptions{Header: []string{"header1", "header2", "header3"}})
if err != nil || checkValues(tabT) {
t.Fail()
}
}
func TestWithSemicolon(t *testing.T) {
tabT := []test{}
err := LoadFromPath(
"csv_files/semicolon.csv",
&tabT,
CsvOptions{Separator: ';'})
if err != nil || checkValues(tabT) {
t.Fail()
}
}
func TestWithTrailingSpaces(t *testing.T) {
tabT := []test{}
err := LoadFromPath("csv_files/trailingSpaces.csv", &tabT)
if err != nil || checkValues(tabT) {
t.Fail()
}
}
func TestToMutchOptions(t *testing.T) {
tabT := []test{}
err := LoadFromPath(
"csv_files/semicolon.csv",
&tabT,
CsvOptions{Separator: ';'},
CsvOptions{Separator: ';'})
if err == nil {
t.Fail()
}
}
func TestBadFormat(t *testing.T) {
tabT := []test{}
err := LoadFromPath("csv_files/badFormat.csv", &tabT)
if err == nil {
t.Fail()
}
}
func TestNonexistingFile(t *testing.T) {
tabT := []test{}
err := LoadFromPath("csv_files/nonexistingfile.csv", &tabT)
if err == nil {
t.Fail()
}
}
func TestBadInt(t *testing.T) {
tabT := []test{}
err := LoadFromPath("csv_files/badInt.csv", &tabT)
if err == nil {
t.Fail()
}
}
func TestBadFloat(t *testing.T) {
tabT := []test{}
err := LoadFromPath("csv_files/badFloat.csv", &tabT)
if err == nil {
t.Fail()
}
}
func TestNoDist(t *testing.T) {
err := LoadFromPath("csv_files/valid.csv", &test{})
if err == nil {
t.Fail()
}
}