-
Notifications
You must be signed in to change notification settings - Fork 3
/
sqltocsv_test.go
133 lines (116 loc) · 2.52 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
package main
import (
"fmt"
"log"
"testing"
"github.com/jmoiron/sqlx"
)
func BenchmarkAppend(b *testing.B) {
str := "Florgle"
for i := 0; i < b.N; i++ {
_ = "\"" + str + "\""
}
fmt.Printf("\"" + str + "\"")
}
func BenchmarkSprintf(b *testing.B) {
str := "Florgle"
for i := 0; i < b.N; i++ {
_ = fmt.Sprintf("\"%s\"", str)
}
fmt.Printf(fmt.Sprintf("\"%s\"", str))
}
func getMySQLConfig() *config {
return &config{
dbAdapter: "mysql",
connString: "root@/sqltocsvtest",
sqlQuery: "SELECT * FROM sample",
obfuscateFields: "",
delimiter: "comma",
quoteFields: "",
quoteType: "",
}
}
func getPostgreSQLConfig() *config {
return &config{
dbAdapter: "postgres",
connString: "dbname=sqltocsvtest sslmode=disable",
sqlQuery: "SELECT * FROM sample",
obfuscateFields: "",
delimiter: "comma",
quoteFields: "",
quoteType: "",
}
}
func setupMySQL(db *sqlx.DB) {
// Create the table
_, err := db.Exec(`CREATE TABLE sample(
VARCHAR50 varchar(50),
CHAR2 char(2),
NUM int,
DECIMAL82 decimal(8,2),
TEXT text,
BOOL bit
)`)
// Insert the data
if err != nil {
log.Fatal(err)
}
// Insert the data
_, err = db.Exec(`INSERT INTO sample(VARCHAR50,CHAR2,NUM,DECIMAL82,TEXT,BOOL) VALUES ('Words','ZZ',100,12.89,'HI, EVERYBODY!',1)`)
if err != nil {
log.Fatal(err)
}
}
func tearDownMySQL(db *sqlx.DB) {
// Drop the table
db.Exec("DROP TABLE sample")
}
func setupPostgreSQL(db *sqlx.DB) {
// Create the table
_, err := db.Exec(`CREATE TABLE sample(
VARCHAR50 varchar(50),
CHAR2 char(2),
NUM int,
DECIMAL82 decimal(8,2),
TEXT text,
BOOL bit
)`)
if err != nil {
log.Fatal(err)
}
// Insert the data
_, err = db.Exec(`INSERT INTO sample(VARCHAR50,CHAR2,NUM,DECIMAL82,TEXT,BOOL) VALUES ('Words','ZZ',100,12.89,'HI, EVERYBODY!',B'1')`)
if err != nil {
log.Fatal(err)
}
}
func tearDownPostgreSQL(db *sqlx.DB) {
// Drop the table
db.Exec("DROP TABLE sample")
}
func TestMySQL(t *testing.T) {
cfg := getMySQLConfig()
db, err := sqlx.Open(cfg.dbAdapter, cfg.connString)
if err != nil {
t.Fatal(err)
}
tearDownMySQL(db)
defer tearDownMySQL(db)
setupMySQL(db)
if err := run(cfg); err != nil {
t.Fatal(err)
}
}
func TestPostgreSQL(t *testing.T) {
cfg := getPostgreSQLConfig()
db, err := sqlx.Open(cfg.dbAdapter, cfg.connString)
if err != nil {
t.Fatal(err)
}
tearDownPostgreSQL(db)
defer tearDownPostgreSQL(db)
setupPostgreSQL(db)
if err := run(cfg); err != nil {
t.Fatal(err)
}
}