-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcsvb_test.go
More file actions
143 lines (123 loc) · 3.09 KB
/
csvb_test.go
File metadata and controls
143 lines (123 loc) · 3.09 KB
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
package csvb
import (
"bytes"
"encoding/csv"
"github.com/stretchr/testify/assert"
"speter.net/go/exp/math/dec/inf"
"testing"
"time"
)
type Destination struct {
Name string
Date time.Time
Counter int64
Rating *inf.Dec
}
func TestVariableLengthFields(t *testing.T) {
header := []string{"n", "d", "c", "x"}
row := []string{"foo", "2014-04-06 10:02:21", "9834"}
input := [][]string{header, row}
s := make(map[string]string)
s["n"] = "Name"
s["d"] = "Date"
s["c"] = "Counter"
d := Destination{
Name: "foo",
Counter: 9834,
Date: time.Date(2014, 4, 6, 10, 02, 21, 0, time.UTC),
}
runScenario(t, input, s, d)
}
func TestCustomHeader(t *testing.T) {
header := map[int]string{
0: "n",
1: "d",
2: "c",
}
row := []string{"foo", "2014-04-06 10:02:21", "4459813"}
input := [][]string{row}
s := make(map[string]string)
s["n"] = "Name"
s["d"] = "Date"
s["c"] = "Counter"
opts := &Options{Header: header}
d := Destination{
Name: "foo",
Counter: 4459813,
Date: time.Date(2014, 4, 6, 10, 02, 21, 0, time.UTC),
}
runScenarioWithOptions(t, input, s, d, opts)
}
func TestTimezoneHandling(t *testing.T) {
header := []string{"n", "d", "c"}
row := []string{"foo", "2014-04-06 10:02:21", "4459813"}
input := [][]string{header, row}
s := make(map[string]string)
s["n"] = "Name"
s["d"] = "Date"
s["c"] = "Counter"
location, _ := time.LoadLocation("Europe/Stockholm")
opts := &Options{TimeZone: location}
d := Destination{
Name: "foo",
Counter: 4459813,
Date: time.Date(2014, 4, 6, 10, 02, 21, 0, location),
}
runScenarioWithOptions(t, input, s, d, opts)
}
func TestRowBinding(t *testing.T) {
header := []string{"n", "d", "c", "r"}
row := []string{"foo", "2014-04-06 10:02:21", "4459813", "1.55"}
input := [][]string{header, row}
s := make(map[string]string)
s["n"] = "Name"
s["d"] = "Date"
s["c"] = "Counter"
s["r"] = "Rating"
d := Destination{
Name: "foo",
Counter: 4459813,
Date: time.Date(2014, 4, 6, 10, 02, 21, 0, time.UTC),
Rating: inf.NewDec(155, 2),
}
runScenario(t, input, s, d)
}
func TestNullHandling(t *testing.T) {
header := []string{"n", "d", "c"}
row := []string{"foo", "2014-04-06 10:02:21", "NULL"}
input := [][]string{header, row}
s := make(map[string]string)
s["n"] = "Name"
s["d"] = "Date"
s["c"] = "Counter"
d := Destination{
Name: "foo",
Counter: 0,
Date: time.Date(2014, 4, 6, 10, 02, 21, 0, time.UTC),
}
runScenario(t, input, s, d)
}
func runScenario(t *testing.T, input [][]string, s map[string]string, expected interface{}) {
runScenarioWithOptions(t, input, s, expected, nil)
}
func runScenarioWithOptions(t *testing.T, input [][]string, s map[string]string, expected interface{}, opts *Options) {
var buf bytes.Buffer
w := csv.NewWriter(&buf)
for _, row := range input {
w.Write(row)
}
w.Flush()
var d Destination
if opts == nil {
opts = &Options{NullMarker: "NULL"}
}
b, err := NewBinder(&buf, opts)
assert.NoError(t, err)
b.ForEach(func(r Row) (bool, error) {
if err := r.Bind(&d, s); err != nil {
return false, err
}
return false, nil
})
assert.Equal(t, d, expected)
}