forked from gocarina/gocsv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unmarshaller_test.go
155 lines (134 loc) · 3.71 KB
/
unmarshaller_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
package gocsv
import (
"bytes"
"encoding/csv"
"io"
"strings"
"testing"
)
func TestUnmarshallerLongRow(t *testing.T) {
type sample struct {
FieldA string `csv:"field_a"`
FieldB string `csv:"field_b"`
}
const csvContents = `field_a,field_b
a,b
c,d,e
`
reader := csv.NewReader(strings.NewReader(csvContents))
reader.FieldsPerRecord = -1
um, err := NewUnmarshaller(reader, sample{})
if err != nil {
t.Fatalf("Error calling NewUnmarshaller: %#v", err)
}
obj, err := um.Read()
if err != nil {
t.Fatalf("Error calling Read(): %#v", err)
}
if obj.(sample).FieldA != "a" || obj.(sample).FieldB != "b" {
t.Fatalf("Unepxected result from Read(): %#v", obj)
}
obj, err = um.Read()
if err != nil {
t.Fatalf("Error calling Read(): %#v", err)
}
if obj.(sample).FieldA != "c" || obj.(sample).FieldB != "d" {
t.Fatalf("Unepxected result from Read(): %#v", obj)
}
obj, err = um.Read()
if err != io.EOF {
t.Fatalf("Unepxected result from Read(): (%#v, %#v)", obj, err)
}
}
func TestUnmarshalListOfStructsAfterMarshal(t *testing.T) {
type Additional struct {
Value string
}
type Option struct {
Additional []*Additional
Key string
}
inData := []*Option{
{
Key: "test",
},
}
// First, marshal our test data to a CSV format
buffer := new(bytes.Buffer)
innerWriter := csv.NewWriter(buffer)
innerWriter.Comma = '|'
csvWriter := NewSafeCSVWriter(innerWriter)
if err := MarshalCSV(inData, csvWriter); err != nil {
t.Fatalf("Error marshalling data to CSV: %#v", err)
}
if string(buffer.Bytes()) != "Additional|Key\nnull|test\n" {
t.Fatalf("Marshalled data had an unexpected form of %s", buffer.Bytes())
}
// Next, attempt to unmarshal our test data from a CSV format
var outData []*Option
innerReader := csv.NewReader(buffer)
innerReader.Comma = '|'
if err := UnmarshalCSV(innerReader, &outData); err != nil {
t.Fatalf("Error unmarshalling data from CSV: %#v", err)
}
// Finally, verify the data
if len(outData) != 1 {
t.Fatalf("Data expected to have one entry, had %d entries", len(outData))
} else if len(outData[0].Additional) != 0 {
t.Fatalf("Data Additional field expected to be empty, had length of %d", len(outData[0].Additional))
} else if outData[0].Key != "test" {
t.Fatalf("Data Key field did not contain expected value, had %q", outData[0].Key)
}
}
func TestUnmarshallerRenormalizeHeaders(t *testing.T) {
type sample struct {
FieldA string `csv:"field_a_map"`
FieldB string `csv:"field_b_map"`
}
const csvContents = `field_a,field_b
a,b
c,d,e
`
headerNormalizer := func(headers []string) []string {
normalizedHeaders := make([]string, len(headers))
for i, header := range headers {
normalizedHeader := header
switch header {
case "field_a":
normalizedHeader = "field_a_map"
case "field_b":
normalizedHeader = "field_b_map"
}
normalizedHeaders[i] = normalizedHeader
}
return normalizedHeaders
}
reader := csv.NewReader(strings.NewReader(csvContents))
reader.FieldsPerRecord = -1
um, err := NewUnmarshaller(reader, sample{})
if err != nil {
t.Fatalf("Error calling NewUnmarshaller: %#v", err)
}
err = um.RenormalizeHeaders(headerNormalizer)
if err != nil {
t.Fatalf("Error calling RenormalizeHeaders: %#v", err)
}
obj, err := um.Read()
if err != nil {
t.Fatalf("Error calling Read(): %#v", err)
}
if obj.(sample).FieldA != "a" || obj.(sample).FieldB != "b" {
t.Fatalf("Unepxected result from Read(): %#v", obj)
}
obj, err = um.Read()
if err != nil {
t.Fatalf("Error calling Read(): %#v", err)
}
if obj.(sample).FieldA != "c" || obj.(sample).FieldB != "d" {
t.Fatalf("Unepxected result from Read(): %#v", obj)
}
obj, err = um.Read()
if err != io.EOF {
t.Fatalf("Unepxected result from Read(): (%#v, %#v)", obj, err)
}
}