-
Notifications
You must be signed in to change notification settings - Fork 0
/
csvlib_test.go
99 lines (88 loc) · 2.34 KB
/
csvlib_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
package csvlib
import (
"reflect"
"testing"
"github.com/stretchr/testify/assert"
"github.com/tiendc/gofn"
)
func Test_Unmarshal(t *testing.T) {
type Item struct {
ColX bool `csv:",optional"`
ColY bool `csv:"-"`
Col1 int `csv:"col1"`
Col2 float32 `csv:"col2"`
}
t.Run("#1: success", func(t *testing.T) {
data := gofn.MultilineString(
`col1,col2
1,2.123
100,200`)
var v []Item
ret, err := Unmarshal([]byte(data), &v)
assert.Nil(t, err)
assert.Equal(t, 3, ret.TotalRow())
assert.Equal(t, []string{"ColX"}, ret.MissingOptionalColumns())
assert.Equal(t, []Item{{Col1: 1, Col2: 2.123}, {Col1: 100, Col2: 200}}, v)
})
}
func Test_Marshal(t *testing.T) {
type Item struct {
ColX bool `csv:",optional"`
ColY bool
Col1 int `csv:"col1"`
Col2 float32 `csv:"col2"`
}
t.Run("#1: success", func(t *testing.T) {
v := []Item{
{Col1: 1, Col2: 2.123},
{Col1: 100, Col2: 200},
}
data, err := Marshal(v)
assert.Nil(t, err)
assert.Equal(t, gofn.MultilineString(
`ColX,col1,col2
false,1,2.123
false,100,200
`), string(data))
})
}
func Test_GetHeaderDetails(t *testing.T) {
t.Run("#1: success", func(t *testing.T) {
type Item struct {
Col1 int `csv:"col1,omitempty"`
Col2 *string `csv:"col2,optional"`
Col3 bool `csv:"-"`
Col4 float32
Col5 InlineColumn[int] `csv:"col5,inline"`
}
details, err := GetHeaderDetails(Item{}, "csv")
assert.Nil(t, err)
assert.Equal(t, []ColumnDetail{
{Name: "col1", DataType: reflect.TypeOf(int(0)), OmitEmpty: true},
{Name: "col2", DataType: reflect.TypeOf(gofn.New("")), Optional: true},
{Name: "col5", DataType: reflect.TypeOf(InlineColumn[int]{}), Inline: true},
}, details)
})
t.Run("#2: invalid type", func(t *testing.T) {
_, err := GetHeaderDetails("abc", "csv")
assert.ErrorIs(t, err, ErrTypeInvalid)
})
}
func Test_GetHeader(t *testing.T) {
t.Run("#1: success", func(t *testing.T) {
type Item struct {
Col1 int `csv:"col1,omitempty"`
Col2 *string `csv:"col2,optional"`
Col3 bool `csv:"-"`
Col4 float32
Col5 InlineColumn[int] `csv:"col5,inline"`
}
header, err := GetHeader(Item{}, "csv")
assert.Nil(t, err)
assert.Equal(t, []string{"col1", "col2", "col5"}, header)
})
t.Run("#2: invalid type", func(t *testing.T) {
_, err := GetHeader(0, "csv")
assert.ErrorIs(t, err, ErrTypeInvalid)
})
}