-
Notifications
You must be signed in to change notification settings - Fork 1
/
csv2json_test.go
173 lines (146 loc) · 4.25 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
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
169
170
171
172
173
package main
import (
"flag"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"testing"
)
func Test_getFileData(t *testing.T) {
tests := []struct {
name string
want inputFile
wantErr bool
osArgs []string
}{
{"Default parameters", inputFile{"test.csv", "comma", false}, false, []string{"cmd", "test.csv"}},
{"No parameters", inputFile{}, true, []string{"cmd"}},
{"Semicolon enabled", inputFile{"test.csv", "semicolon", false}, false, []string{"cmd", "--separator=semicolon", "test.csv"}},
{"Pretty enabled", inputFile{"test.csv", "comma", true}, false, []string{"cmd", "--pretty", "test.csv"}},
{"Pretty and semicolon enabled", inputFile{"test.csv", "semicolon", true}, false, []string{"cmd", "--pretty", "--separator=semicolon", "test.csv"}},
{"Separator not identified", inputFile{}, true, []string{"cmd", "--separator=pipe", "test.csv"}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actualOsArgs := os.Args
defer func() {
os.Args = actualOsArgs
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError) //flags are now reset
}()
os.Args = tt.osArgs
got, err := getFileData()
if (err != nil) != tt.wantErr {
t.Errorf("getFileData() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("getFileData() = %v, want %v", got, tt.want)
}
})
}
}
func Test_checkIfValidFile(t *testing.T) {
tmpfile, err := ioutil.TempFile("", "test*.csv")
if err != nil {
panic(err)
}
defer os.Remove(tmpfile.Name())
tests := []struct {
name string
filename string
want bool
wantErr bool
}{
{"File does exist", tmpfile.Name(), true, false},
{"File does not exist", "nowhere/test.csv", false, true},
{"File is not csv", "test.txt", false, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := checkIfValidFile(tt.filename)
if (err != nil) != tt.wantErr {
t.Errorf("checkIfValidFile() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("checkIfValidFile() = %v, want %v", got, tt.want)
}
})
}
}
func Test_processCsvFile(t *testing.T) {
wantMapSlice := []map[string]string{
{"COL1": "1", "COL2": "2", "COL3": "3"},
{"COL1": "4", "COL2": "5", "COL3": "6"},
}
tests := []struct {
name string
csvString string
separator string
}{
{"Comma separator", "COL1,COL2,COL3\n1,2,3\n4,5,6\n", "comma"},
{"Semicolon separator", "COL1;COL2;COL3\n1;2;3\n4;5;6\n", "semicolon"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tmpfile, err := ioutil.TempFile("", "test*.csv")
check(err)
defer os.Remove(tmpfile.Name())
_, err = tmpfile.WriteString(tt.csvString)
tmpfile.Sync()
testFileData := inputFile{
filepath: tmpfile.Name(),
pretty: false,
separator: tt.separator,
}
writerChannel := make(chan map[string]string)
go processCsvFile(testFileData, writerChannel)
for _, wantMap := range wantMapSlice {
record := <-writerChannel
if !reflect.DeepEqual(record, wantMap) {
t.Errorf("processCsvFile() = %v, want %v", record, wantMap)
}
}
})
}
}
func Test_writeJSONFile(t *testing.T) {
dataMap := []map[string]string{
{"COL1": "1", "COL2": "2", "COL3": "3"},
{"COL1": "4", "COL2": "5", "COL3": "6"},
}
tests := []struct {
csvPath string
jsonPath string
pretty bool
name string
}{
{"compact.csv", "compact.json", false, "Compact JSON"},
{"pretty.csv", "pretty.json", true, "Pretty JSON"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
writerChannel := make(chan map[string]string)
done := make(chan bool)
go func() {
for _, record := range dataMap {
writerChannel <- record
}
close(writerChannel)
}()
go writeJSONFile(tt.csvPath, writerChannel, done, tt.pretty)
<-done
testOutput, err := ioutil.ReadFile(tt.jsonPath)
if err != nil {
t.Errorf("writeJSONFile(), Output file got error: %v", err)
}
defer os.Remove(tt.jsonPath)
wantOutput, err := ioutil.ReadFile(filepath.Join("testJsonFiles", tt.jsonPath))
check(err)
if (string(testOutput)) != (string(wantOutput)) {
t.Errorf("writeJSONFile() = %v, want %v", string(testOutput), string(wantOutput))
}
})
}
}