forked from yukithm/json2csv
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsv_writer_test.go
More file actions
168 lines (151 loc) · 3.57 KB
/
csv_writer_test.go
File metadata and controls
168 lines (151 loc) · 3.57 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
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
package json2csv
import (
"bytes"
"testing"
)
func TestKeyWithTrailingSpace(t *testing.T) {
b := &bytes.Buffer{}
wr := NewCSVWriter(b)
responses := []map[string]interface{}{
{
" A": 1,
"B ": "foo",
"C ": "FOO",
},
{
" A": 2,
"B ": "bar",
"C ": "BAR",
},
}
csvContent, err := JSON2CSV(responses) // csvContent seems to be complete!
if err != nil {
t.Fatal(err)
}
wr.WriteCSV(csvContent)
wr.Flush()
got := b.String()
want := `/ A,/B ,/C
1,foo,FOO
2,bar,BAR
`
if got != want {
t.Errorf("Expected %v, but %v", want, got)
}
}
func TestKeyWithCapitalizedWords(t *testing.T) {
b := &bytes.Buffer{}
wr := NewCSVWriter(b)
wr.HeaderStyle = InfinitusNotationStyle
responses := []map[string]interface{}{
{
"BCDTest": 1,
"abcBCD": "foo",
"ABC Test": "FOO",
},
{
"BCDTest": 2,
"abcBCD": "bar",
"ABC Test": "BAR",
},
}
csvContent, err := JSON2CSV(responses) // csvContent seems to be complete!
if err != nil {
t.Fatal(err)
}
wr.WriteCSV(csvContent)
wr.Flush()
got := b.String()
want := `ABC Test,BCD Test,Abc BCD
FOO,1,foo
BAR,2,bar
`
if got != want {
t.Errorf("Expected %v, but %v", want, got)
}
}
func TestKeysWithReadableNotationStyle(t *testing.T) {
got, err := writeCSVHelper(ReadableNotationStyle)
if err != nil {
t.Fatal(err)
}
want := `Test,This Is A Test: With A Dot,This Is A Test: With A Dot Two,This Is A Test: With Another: Dot,This Is A Test: With Array #1,This Is A Test: With Array #2,This Is A Test: With Array Object #1: Key1,This Is A Test: With Array Object #1: Key2,This Is A Test: With Array Object #2: Key1,This Is A Test: With Array Object #2: Key2
1,foo,foo2,foo3,foo4,foo5,foo6,foo7,foo8,foo9
2,bar,bar2,bar3,bar4,bar5,bar6,bar7,bar8,bar9
`
if got != want {
t.Errorf("Expected %v, but %v", want, got)
}
}
func TestKeysWithInfinitusNotationStyle(t *testing.T) {
got, err := writeCSVHelper(InfinitusNotationStyle)
if err != nil {
t.Fatal(err)
}
want := `Test,With A Dot,With A Dot Two,With Another: Dot,With Array #1,With Array #2,With Array Object #1: Key1,With Array Object #1: Key2,With Array Object #2: Key1,With Array Object #2: Key2
1,foo,foo2,foo3,foo4,foo5,foo6,foo7,foo8,foo9
2,bar,bar2,bar3,bar4,bar5,bar6,bar7,bar8,bar9
`
if got != want {
t.Errorf("Expected %v, but %v", want, got)
}
}
func writeCSVHelper(style KeyStyle) (string, error) {
responses := getTestResponse()
csvContent, err := JSON2CSV(responses) // csvContent seems to be complete!
if err != nil {
return "", err
}
b := &bytes.Buffer{}
wr := NewCSVWriter(b)
wr.HeaderStyle = style
wr.WriteCSV(csvContent)
wr.Flush()
return b.String(), nil
}
func getTestResponse() []map[string]interface{} {
return []map[string]interface{}{
{
"Test": 1,
"thisIsATest": map[string]interface{}{
"withADot": "foo",
"withADotTwo": "foo2",
"withAnother": map[string]interface{}{
"dot": "foo3",
},
"withArray": []interface{}{"foo4", "foo5"},
"withArrayObject": []interface{}{
map[string]interface{}{
"key1": "foo6",
"key2": "foo7",
},
map[string]interface{}{
"key1": "foo8",
"key2": "foo9",
},
},
},
},
{
"Test": 2,
"thisIsATest": map[string]interface{}{
"withADot": "bar",
"withADotTwo": "bar2",
"withAnother": map[string]interface{}{
"dot": "bar3",
},
"withArray": []interface{}{"bar4", "bar5"},
"withArrayObject": []interface{}{
map[string]interface{}{
"key1": "bar6",
"key2": "bar7",
},
map[string]interface{}{
"key1": "bar8",
"key2": "bar9",
},
},
},
},
}
}