-
Notifications
You must be signed in to change notification settings - Fork 13
/
main_test.go
231 lines (219 loc) · 5.28 KB
/
main_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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package main
import (
"bytes"
"log"
"os/exec"
"reflect"
"sort"
"strings"
"testing"
"time"
)
type tests []struct {
name string
targetDBs []string
query string
expected []string
}
type testConfig map[string]database
var baseTests = tests{
{
name: "reads from one database",
targetDBs: []string{"db1"},
query: "SELECT id FROM table1",
expected: []string{
"",
"1",
"2",
"3",
},
},
{
name: "reads from two databases",
targetDBs: []string{"db1", "db2"},
query: "SELECT id FROM table1",
expected: []string{
"",
"db1\t1",
"db1\t2",
"db1\t3",
"db2\t1",
"db2\t2",
"db2\t3",
},
},
{
name: "reads from all databases with the all keyword",
targetDBs: []string{"all"},
query: "SELECT id FROM table1",
expected: []string{
"",
"db1\t1",
"db1\t2",
"db1\t3",
"db2\t1",
"db2\t2",
"db2\t3",
"db3\t1",
"db3\t2",
"db3\t3",
},
},
{
name: "reads two fields from all databases",
targetDBs: []string{"all"},
query: "SELECT id, name FROM table1",
expected: []string{
"",
"db1\t1\tJohn",
"db1\t2\tGeorge",
"db1\t3\tRichard",
"db2\t1\tRob",
"db2\t2\tKen",
"db2\t3\tRobert",
"db3\t1\tAthos",
"db3\t2\tPorthos",
"db3\t3\tAramis",
},
},
}
var mysqlTests = tests{{
name: `mysql outputs vertical with \G`,
targetDBs: []string{"db1"},
query: `SELECT id, name FROM table1 order by id asc limit 1\G`,
expected: []string{
"",
"*************************** 1. row ***************************",
"id: 1",
"name: John",
},
},
}
func Test_MySQL(t *testing.T) {
awaitDB(mySQL, t)
var (
testConfig = testConfig{
"db1": database{DbServer: "test-mysql", DbName: "db1", User: "root", Pass: "", SQLType: "mysql"},
"db2": database{DbServer: "test-mysql", DbName: "db2", User: "root", Pass: "", SQLType: ""},
"db3": database{DbServer: "test-mysql", DbName: "db3", User: "root", Pass: "", SQLType: ""},
}
)
runTests(baseTests, testConfig, t)
runTests(mysqlTests, testConfig, t)
}
func Test_PostgreSQL(t *testing.T) {
awaitDB(postgreSQL, t)
var (
testConfig = testConfig{
"db1": database{DbServer: "test-postgres", DbName: "db1", User: "root", Pass: "", SQLType: "postgres"},
"db2": database{DbServer: "test-postgres", DbName: "db2", User: "root", Pass: "", SQLType: "postgres"},
"db3": database{DbServer: "test-postgres", DbName: "db3", User: "root", Pass: "", SQLType: "postgres"},
}
)
runTests(baseTests, testConfig, t)
}
func Test_Mix_Mysql_PostgreSQL(t *testing.T) {
awaitDB(mySQL, t)
awaitDB(postgreSQL, t)
var (
testConfig = testConfig{
"db1": database{DbServer: "test-postgres", DbName: "db1", User: "root", Pass: "", SQLType: "postgres"},
"db2": database{DbServer: "test-postgres", DbName: "db2", User: "root", Pass: "", SQLType: "postgres"},
"db3": database{DbServer: "test-mysql", DbName: "db3", User: "root", Pass: "", SQLType: ""},
"db4": database{DbServer: "test-mysql", DbName: "db1", User: "root", Pass: "", SQLType: "mysql"},
}
ts = tests{
{
name: "reads from two databases",
targetDBs: []string{"db1", "db3"},
query: "SELECT id FROM table1",
expected: []string{
"",
"db1\t1",
"db1\t2",
"db1\t3",
"db3\t1",
"db3\t2",
"db3\t3",
},
},
{
name: "reads from all databases with the all keyword",
targetDBs: []string{"all"},
query: "SELECT id FROM table1",
expected: []string{
"",
"db1\t1",
"db1\t2",
"db1\t3",
"db2\t1",
"db2\t2",
"db2\t3",
"db3\t1",
"db3\t2",
"db3\t3",
"db4\t1",
"db4\t2",
"db4\t3",
},
},
{
name: "reads two fields from all databases",
targetDBs: []string{"all"},
query: "SELECT id, name FROM table1",
expected: []string{
"",
"db1\t1\tJohn",
"db1\t2\tGeorge",
"db1\t3\tRichard",
"db2\t1\tRob",
"db2\t2\tKen",
"db2\t3\tRobert",
"db3\t1\tAthos",
"db3\t2\tPorthos",
"db3\t3\tAramis",
"db4\t1\tJohn",
"db4\t2\tGeorge",
"db4\t3\tRichard",
},
},
}
)
runTests(ts, testConfig, t)
}
func runTests(ts tests, testConfig testConfig, t *testing.T) {
for _, tc := range ts {
t.Run(tc.name, func(t *testing.T) {
var buf = bytes.Buffer{}
_main(testConfig, tc.targetDBs, tc.query, newThreadSafePrintliner(&buf).println)
var actual = strings.Split(buf.String(), "\n")
sort.Strings(actual)
if !reflect.DeepEqual(tc.expected, actual) {
t.Errorf("Expected\n%#v\n but got\n%#v\n", tc.expected, actual)
}
})
}
}
func awaitDB(typ sqlType, t *testing.T) {
var pgTestCmds = []string{"-h", "test-postgres", "-U", "root", "-d", "db1", "-c", "SELECT * FROM table1"}
var msTestCmds = []string{"-h", "test-mysql", "-u", "root", "-e", "SELECT * FROM db1.table1"}
var err error
var c *exec.Cmd
for i := 1; i <= 30; i++ {
if typ == mySQL {
c = exec.Command("mysql", msTestCmds...)
} else {
c = exec.Command("psql", pgTestCmds...)
}
if err = c.Run(); err == nil {
log.Printf("%s ready after %v/30 tries", typ, i)
break
}
log.Printf("Retrying (%v/30) in 1 sec because %s is not yet ready", i, typ)
time.Sleep(1 * time.Second)
}
for err != nil {
t.Errorf("bailing because couldn't connect to %s after 30 tries: %v", typ, err)
t.FailNow()
}
}