-
Notifications
You must be signed in to change notification settings - Fork 42
/
sqltocsv_test.go
203 lines (148 loc) · 4.5 KB
/
sqltocsv_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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package sqltocsv_test
import (
"bytes"
"database/sql"
"io/ioutil"
"os"
"testing"
"time"
"github.com/joho/sqltocsv"
)
func init() {
os.Setenv("TZ", "UTC")
}
func TestWriteFile(t *testing.T) {
checkQueryAgainstResult(t, func(rows *sql.Rows) string {
testCsvFileName := "/tmp/test.csv"
err := sqltocsv.WriteFile(testCsvFileName, rows)
if err != nil {
t.Fatalf("error in WriteCsvToFile: %v", err)
}
bytes, err := ioutil.ReadFile(testCsvFileName)
if err != nil {
t.Fatalf("error reading %v: %v", testCsvFileName, err)
}
return string(bytes[:])
})
}
func TestWrite(t *testing.T) {
checkQueryAgainstResult(t, func(rows *sql.Rows) string {
buffer := &bytes.Buffer{}
err := sqltocsv.Write(buffer, rows)
if err != nil {
t.Fatalf("error in WriteCsvToWriter: %v", err)
}
return buffer.String()
})
}
func TestWriteString(t *testing.T) {
checkQueryAgainstResult(t, func(rows *sql.Rows) string {
csv, err := sqltocsv.WriteString(rows)
if err != nil {
t.Fatalf("error in WriteCsvToWriter: %v", err)
}
return csv
})
}
func TestWriteHeaders(t *testing.T) {
converter := getConverter(t)
converter.WriteHeaders = false
expected := "Alice,1,1973-11-29 21:33:09 +0000 UTC\n"
actual := converter.String()
assertCsvMatch(t, expected, actual)
}
func TestSetHeaders(t *testing.T) {
converter := getConverter(t)
converter.Headers = []string{"Name", "Age", "Birthday"}
expected := "Name,Age,Birthday\nAlice,1,1973-11-29 21:33:09 +0000 UTC\n"
actual := converter.String()
assertCsvMatch(t, expected, actual)
}
func TestSetRowPreProcessorModifyingRows(t *testing.T) {
converter := getConverter(t)
converter.SetRowPreProcessor(func(rows []string, columnNames []string) (bool, []string) {
return true, []string{rows[0], "X", "X"}
})
expected := "name,age,bdate\nAlice,X,X\n"
actual := converter.String()
assertCsvMatch(t, expected, actual)
}
func TestSetRowPreProcessorOmittingRows(t *testing.T) {
converter := getConverter(t)
converter.SetRowPreProcessor(func(rows []string, columnNames []string) (bool, []string) {
return false, []string{}
})
expected := "name,age,bdate\n"
actual := converter.String()
assertCsvMatch(t, expected, actual)
}
func TestSetTimeFormat(t *testing.T) {
converter := getConverter(t)
// Kitchen: 3:04PM
converter.TimeFormat = time.Kitchen
expected := "name,age,bdate\nAlice,1,9:33PM\n"
actual := converter.String()
assertCsvMatch(t, expected, actual)
}
func TestConvertingNilValueShouldReturnEmptyString(t *testing.T) {
converter := sqltocsv.New(getTestRowsByQuery(t, "SELECT|people|name,nickname,age|"))
expected := "name,nickname,age\nAlice,,1\n"
actual := converter.String()
assertCsvMatch(t, expected, actual)
}
func TestAlternateDelimiter(t *testing.T) {
converter := getConverter(t)
converter.Delimiter = '|'
expected := "name|age|bdate\nAlice|1|1973-11-29 21:33:09 +0000 UTC\n"
actual := converter.String()
assertCsvMatch(t, expected, actual)
}
func TestMissingDelimiter(t *testing.T) {
converter := getConverter(t)
var delimiter rune
converter.Delimiter = delimiter
expected := "name,age,bdate\nAlice,1,1973-11-29 21:33:09 +0000 UTC\n"
actual := converter.String()
assertCsvMatch(t, expected, actual)
}
func checkQueryAgainstResult(t *testing.T, innerTestFunc func(*sql.Rows) string) {
rows := getTestRows(t)
expected := "name,age,bdate\nAlice,1,1973-11-29 21:33:09 +0000 UTC\n"
actual := innerTestFunc(rows)
assertCsvMatch(t, expected, actual)
}
func getTestRows(t *testing.T) *sql.Rows {
return getTestRowsByQuery(t, "SELECT|people|name,age,bdate|")
}
func getTestRowsByQuery(t *testing.T, query string) *sql.Rows {
db := setupDatabase(t)
rows, err := db.Query(query)
if err != nil {
t.Fatalf("error querying: %v", err)
}
return rows
}
func getConverter(t *testing.T) *sqltocsv.Converter {
return sqltocsv.New(getTestRows(t))
}
func setupDatabase(t *testing.T) *sql.DB {
db, err := sql.Open("test", "foo")
if err != nil {
t.Fatalf("Error opening testdb %v", err)
}
exec(t, db, "WIPE")
exec(t, db, "CREATE|people|name=string,age=int32,bdate=datetime,nickname=nullstring")
exec(t, db, "INSERT|people|name=Alice,age=?,bdate=?,nickname=?", 1, time.Unix(123456789, 0), nil)
return db
}
func exec(t testing.TB, db *sql.DB, query string, args ...interface{}) {
_, err := db.Exec(query, args...)
if err != nil {
t.Fatalf("Exec of %q: %v", query, err)
}
}
func assertCsvMatch(t *testing.T, expected string, actual string) {
if actual != expected {
t.Errorf("Expected CSV:\n\n%v\n Got CSV:\n\n%v\n", expected, actual)
}
}