-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsv2json_test.go
121 lines (102 loc) · 2.99 KB
/
csv2json_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
package main
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
)
type Person struct {
Name interface{} `json:"name"`
Age interface{} `json:"age"`
Location interface{} `json:"location"`
Active interface{} `json:"active"`
}
func TestConvertShouldUseFirstRowAsHeadersIfHeadersAreEmpty(t *testing.T) {
assert := assert.New(t)
csv := `name,age,location
John Doe,21,Singapore
James Smith,25,England
Joe Biden,75,USA`
result := Convert(csv, []Header{})
actual := []Person{}
expected := []Person{}
json.Unmarshal([]byte(`[
{"name": "John Doe", "age": "21", "location": "Singapore"},
{"name": "James Smith", "age": "25", "location": "England"},
{"name": "Joe Biden", "age": "75", "location": "USA"}
]`), &expected)
json.Unmarshal([]byte(result), &actual)
assert.Equal(expected, actual)
}
func TestConvertShouldIgnoreEmptyLines(t *testing.T) {
assert := assert.New(t)
csv := `name,age,location
John Doe,21,Singapore
Joe Biden,75,USA
`
result := Convert(csv, []Header{})
actual := []Person{}
expected := []Person{}
json.Unmarshal([]byte(`[
{"name": "John Doe", "age": "21", "location": "Singapore"},
{"name": "Joe Biden", "age": "75", "location": "USA"}
]`), &expected)
json.Unmarshal([]byte(result), &actual)
assert.Equal(expected, actual)
}
func TestConvertShouldUseHeaderMetaDataIfProvided(t *testing.T) {
assert := assert.New(t)
csv := `John Doe,21,Singapore
Joe Biden,75,USA`
headers := []Header{
Header{Name: "name"},
Header{Name: "age"},
Header{Name: "location"},
}
result := Convert(csv, headers)
actual := []Person{}
expected := []Person{}
json.Unmarshal([]byte(`[
{"name": "John Doe", "age": "21", "location": "Singapore"},
{"name": "Joe Biden", "age": "75", "location": "USA"}
]`), &expected)
json.Unmarshal([]byte(result), &actual)
assert.Equal(expected, actual)
}
func TestConvertShouldParseWithDataTypeSpecifiedInHeader(t *testing.T) {
assert := assert.New(t)
csv := `John Doe,21,true
Joe Biden,75,false`
headers := []Header{
Header{Name: "name"},
Header{Name: "age", Type: "number"},
Header{Name: "active", Type: "boolean"},
}
result := Convert(csv, headers)
actual := []Person{}
expected := []Person{}
json.Unmarshal([]byte(`[
{"name": "John Doe", "age": 21, "active": true},
{"name": "Joe Biden", "age": 75, "active": false}
]`), &expected)
json.Unmarshal([]byte(result), &actual)
assert.Equal(expected, actual)
}
func TestConvertShouldFallBackToDefaultValuesSpecifiedInHeaderIfProvidedValueIsInvalid(t *testing.T) {
assert := assert.New(t)
csv := `John Doe,,true
Joe Biden,75,`
headers := []Header{
Header{Name: "name"},
Header{Name: "age", Type: "number"},
Header{Name: "active", Type: "boolean", Default: true},
}
result := Convert(csv, headers)
actual := []Person{}
expected := []Person{}
json.Unmarshal([]byte(`[
{"name": "John Doe", "age": null, "active": true},
{"name": "Joe Biden", "age": 75, "active": true}
]`), &expected)
json.Unmarshal([]byte(result), &actual)
assert.Equal(expected, actual)
}