-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcsv_test.go
118 lines (111 loc) · 3.18 KB
/
csv_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
package gimlet
import (
"net/http/httptest"
"reflect"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type SampleSubStruct struct {
Sub1 string `csv:"this"`
NoCSV string
}
type SampleStruct struct {
FieldA string `csv:"fieldA"`
FieldInt int `csv:"fieldB"`
FieldBool bool `csv:"boolean"`
Sub1 SampleSubStruct `csv:"sub1"`
Sub2 SampleSubStruct
NoCSV string
}
func sampleGenerator() []SampleStruct {
return []SampleStruct{
{
FieldA: "hello",
FieldInt: 1,
FieldBool: true,
Sub1: SampleSubStruct{"this", "print"},
Sub2: SampleSubStruct{"another", "no"},
NoCSV: "hi",
},
{
FieldA: "world",
FieldInt: 1,
FieldBool: true,
Sub1: SampleSubStruct{"mongo", "db"},
Sub2: SampleSubStruct{"ever", "green"},
NoCSV: "hi",
},
}
}
func TestCSV(t *testing.T) {
t.Run("ResolveFieldsAndValues", func(t *testing.T) {
t.Run("Fields", func(t *testing.T) {
assert.Equal(t, []string{"fieldA", "fieldB", "boolean", "this", "this"}, getCSVFields(reflect.TypeOf(SampleStruct{})))
})
t.Run("Values", func(t *testing.T) {
s := SampleStruct{
FieldA: "hello",
FieldInt: 1,
FieldBool: true,
Sub1: SampleSubStruct{"this", "print"},
Sub2: SampleSubStruct{"another", "no"},
NoCSV: "hi",
}
values := getCSVValues(s)
assert.Equal(t, []string{"hello", "1", "true", "this", "another"}, values)
})
})
t.Run("ConvertValues", func(t *testing.T) {
t.Run("ErrorSingle", func(t *testing.T) {
single := sampleGenerator()[0]
_, err := convertDataToCSVRecord(single)
assert.Error(t, err)
})
t.Run("ErrorEmpty", func(t *testing.T) {
_, err := convertDataToCSVRecord([]SampleStruct{})
assert.Error(t, err)
})
t.Run("Validation", func(t *testing.T) {
samples := sampleGenerator()
strings, err := convertDataToCSVRecord(samples)
require.NoError(t, err)
require.Len(t, strings, 3)
assert.Equal(t, []string{"fieldA", "fieldB", "boolean", "this", "this"}, strings[0])
assert.Equal(t, []string{"hello", "1", "true", "this", "another"}, strings[1])
assert.Equal(t, []string{"world", "1", "true", "mongo", "ever"}, strings[2])
})
})
t.Run("ResponseWrappers", func(t *testing.T) {
t.Run("StatusOK", func(t *testing.T) {
rsp := httptest.NewRecorder()
WriteCSV(rsp, sampleGenerator())
assert.Equal(t, 200, rsp.Code)
assert.NotNil(t, rsp.Body)
})
t.Run("StatusBadRequest", func(t *testing.T) {
rsp := httptest.NewRecorder()
WriteCSVError(rsp, sampleGenerator())
assert.Equal(t, 400, rsp.Code)
assert.NotNil(t, rsp.Body)
})
t.Run("InternalError", func(t *testing.T) {
rsp := httptest.NewRecorder()
WriteCSVInternalError(rsp, sampleGenerator())
assert.Equal(t, 500, rsp.Code)
assert.NotNil(t, rsp.Body)
})
t.Run("BadInputEmpty", func(t *testing.T) {
rsp := httptest.NewRecorder()
WriteCSV(rsp, []SampleStruct{})
assert.Equal(t, 500, rsp.Code)
assert.NotNil(t, rsp.Body)
})
t.Run("BadInputNil", func(t *testing.T) {
rsp := httptest.NewRecorder()
WriteCSV(rsp, "hi")
assert.Equal(t, 500, rsp.Code)
assert.NotNil(t, rsp.Body)
})
})
}