forked from miaomiao3/xlsxutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsv_test.go
67 lines (58 loc) · 1.42 KB
/
csv_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
package xlsxutil
import (
"fmt"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
type Person struct {
Name string `xls:"name""`
Money float64 `xls:"money,precision:10"`
Age int `xls:"age"`
Edu `xls:",inline"`
}
type Edu struct {
School string `xls:"school"`
Address string `xls:"address"`
}
const (
personCsvStr = `name,money,age,school,address
n-0,1.2345678900,20,school-0,hali-0
n-1,1.2345678900,21,school-1,hali-1
n-2,1.2345678900,22,school-2,hali-2
n-3,1.2345678900,23,school-3,hali-3
n-4,1.2345678900,24,school-4,hali-4
`
)
func prepareTestData() []*Person {
persons := make([]*Person, 0, 5)
for i := 0; i < 5; i++ {
newPerson := &Person{
Name: fmt.Sprintf("n-%d", i),
Age: i + 20,
Money: 1.23456789,
}
newPerson.School = fmt.Sprintf("school-%d", i)
newPerson.Address = fmt.Sprintf("hali-%d", i)
persons = append(persons, newPerson)
}
return persons
}
func TestCsvDump(t *testing.T) {
Convey("CsvDump", t, func() {
persons := prepareTestData()
buf, err := CsvDump(",", persons)
fmt.Println(buf.String())
fmt.Println(err)
So(err, ShouldEqual, nil)
So(buf.String(), ShouldEqual, personCsvStr)
})
}
func TestCsvBind(t *testing.T) {
Convey("TestCsvBindByYamlTag", t, func() {
persons := make([]*Person, 0)
err := CsvLoad("./example/csv/people.csv", ",", &persons)
So(err, ShouldEqual, nil)
So(len(persons), ShouldEqual, 5)
So(persons[4].Name, ShouldEqual, "n-4")
})
}