-
Notifications
You must be signed in to change notification settings - Fork 5
/
dogfish_test
executable file
·464 lines (398 loc) · 11.3 KB
/
dogfish_test
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
#!/usr/bin/env bash
# vim: ts=2:sw=2:expandtab:foldmethod=marker
# dogfish test suite
# by Dan Brown <[email protected]>
# https://github.com/dwb/dogfish
#
# Copyright (c) 2013, Dan Brown
# This is licenced under the 3-clause BSD license. See the LICENSE file
# supplied in this repository for the full text.
# Setup {{{
set -e
set -o nounset
cd "$(dirname "$0")"
root_dir=$(pwd)
test_template_dir=test_template
test_dir=test_run
db_name=dogfish_test
db_type=
migration_name=test_migration_name
if [[ ! -f .dogfish-test-warning-given ]]; then
echo "This test suite will destroy databases called ${db_name} in your"
echo "MySQL and PostgreSQL servers pointed to by your default mysql"
echo "and psql client settings. If you don't want this to happen,"
echo "^C now. To continue, hit Enter."
read
touch .dogfish-test-warning-given
fi
# }}}
# General helpers {{{
DOGFISH_MYSQL_OPTS=( ${DOGFISH_MYSQL_OPTS:-} )
DOGFISH_PG_OPTS=( ${DOGFISH_PG_OPTS:-} )
function dogfish_mysql() {
mysql -BN "${DOGFISH_MYSQL_OPTS[@]:+${DOGFISH_MYSQL_OPTS[@]}}" "$@"
}
function dogfish_psql() {
psql --no-psqlrc --quiet --tuples-only --no-align "${DOGFISH_PG_OPTS[@]:+${DOGFISH_PG_OPTS[@]}}" "$@"
}
function dogfish() {
./dogfish "$@"
return $?
}
function inform() {
echo "$@"
}
function die() {
echo -e "$@" >&2
exit 1
}
function pass() {
echo -e "\033[1;32mPASS\033[0m"
}
function fail() {
if [[ -n ${1:-} ]]; then
local msg=": $1"
else
local msg=""
fi
die "\033[1;31mFAIL\033[0m${msg}"
return $?
}
function setup_test() {
cd "$root_dir"
rm -rf "$test_dir"
cp -R "$test_template_dir" "$test_dir"
cd "$test_dir"
db_type=
}
function use_mysql() {
mv disabled_mysql_migrations mysql_migrations
dogfish_mysql -e "DROP DATABASE IF EXISTS $db_name; CREATE DATABASE $db_name;" 2>&1 | while read line; do
if [[ $line == ERROR* ]]; then
fail "$line"
fi
done
assert_success "MySQL setup failed"
db_type=mysql
}
function mysql_db_exec_to_stderr() {
# shellcheck disable=SC2119
dogfish_mysql $db_name 3>&1 1>&2- 2>&3- | while read line; do
if [[ $line == ERROR\ * ]]; then
fail "$line"
return $?
fi
done
return $?
}
function mysql_db_exec() {
# shellcheck disable=SC2119
mysql_db_exec_to_stderr 2>&1
return $?
}
function use_pg() {
mv disabled_pg_migrations pg_migrations
dogfish_psql -q <<END
DROP DATABASE IF EXISTS $db_name;
CREATE DATABASE $db_name;
END
assert_success "PostgreSQL setup failed"
db_type=pg
}
function pg_db_exec() {
dogfish_psql -qtA "$db_name"
return $?
}
function lines_to_space_sep_words() {
tr "\n" ' ' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//'
}
# }}}
# Assertions {{{
function assert_equal() {
if [[ $1 != "$2" ]]; then
if [[ -n ${3:-} ]]; then
local msg=$3
else
local msg="Expected '$2' to equal '$1'"
fi
fail "$msg"
fi
}
function assert_success() {
if [[ $? -ne 0 ]]; then
fail "${1:-Success expected}"
fi
}
function assert_failure() {
if [[ $? -eq 0 ]]; then
fail "$1"
fi
}
function assert_tables_exist() {
assert_${db_type}_tables_exist "$1"
return $?
}
function assert_mysql_tables_exist() {
# shellcheck disable=SC2119
assert_equal "$1" \
"$(echo "SHOW TABLES;" | mysql_db_exec | lines_to_space_sep_words)"
}
function assert_pg_tables_exist() {
# shellcheck disable=SC2119
assert_equal "$1" \
"$(echo "\dt" | pg_db_exec | cut -d \| -f 2 | lines_to_space_sep_words)"
}
function assert_migrations_recorded() {
assert_${db_type}_migrations_recorded "$1"
}
function assert_mysql_migrations_recorded() {
# shellcheck disable=SC2119
assert_equal "$1" \
"$(echo "SELECT * FROM schema_migrations ORDER BY migration_id ASC" | \
mysql_db_exec | lines_to_space_sep_words)"
}
function assert_pg_migrations_recorded() {
# shellcheck disable=SC2119
assert_equal "$1" \
"$(echo "SELECT * FROM schema_migrations ORDER BY migration_id ASC" | \
pg_db_exec | lines_to_space_sep_words)"
}
function assert_migration_files_exist() {
local migration_nm=$1
assert_equal "2" \
"$(find test_migrations \( -name 'migrate-*' -or -name 'rollback-*' \) \
-and -name "*-${migration_nm}.sql" | wc -l | sed -e 's/^ *//')"
}
function run_test() {
echo -n "Testing $1... "
if "test_$1"; then
pass
else
exit 1
fi
}
# }}}
### Tests
function test_version() {
for flag in -v --version; do
assert_equal 1 "$(dogfish $flag | wc -l | sed -e 's/^ *//')" \
"version not returned"
done
}
function test_help() {
for flag in -h --help; do
if [[ $(dogfish $flag | wc -l) -lt 5 ]]; then
fail "help not long enough"
fi
done
}
function test_action_required() {
dogfish 2>/dev/null
assert_failure "dogfish should error on no action given"
}
function test_db_name_required() {
dogfish migrate 2>/dev/null
assert_failure "dogfish should error on no database name given"
}
function test_create_unnamed_migration() {
db_type=test
mkdir "${db_type}_migrations"
DOGFISH_DB_TYPE=${db_type} dogfish create-migration >/dev/null
assert_success
assert_migration_files_exist ""
}
function test_create_named_migration() {
db_type=test
mkdir "${db_type}_migrations"
DOGFISH_DB_TYPE=${db_type} dogfish create-migration "${migration_name}" >/dev/null
assert_success
assert_migration_files_exist "${migration_name}"
}
function test_full_mysql_migration() {
use_mysql
assert_tables_exist ""
dogfish migrate $db_name >/dev/null
assert_success
assert_tables_exist "schema_migrations stuff things"
assert_migrations_recorded "1234 5678"
}
function test_full_pg_migration() {
use_pg
assert_tables_exist ""
dogfish migrate $db_name >/dev/null
assert_success
assert_tables_exist "schema_migrations stuff things"
assert_migrations_recorded "1234 5678"
}
function test_full_mysql_migration_read_only() {
local schema_file=mysql_migrations/schema.sql
use_mysql
rm "${schema_file}"
assert_tables_exist ""
dogfish -r migrate $db_name >/dev/null
assert_success
assert_tables_exist "schema_migrations stuff things"
assert_migrations_recorded "1234 5678"
[[ -f $schema_file ]]
assert_failure "schema.sql file written to"
}
function test_full_pg_migration_read_only() {
local schema_file=pg_migrations/schema.sql
use_pg
rm "${schema_file}"
assert_tables_exist ""
dogfish -r migrate $db_name >/dev/null
assert_success
assert_tables_exist "schema_migrations stuff things"
assert_migrations_recorded "1234 5678"
[[ -f $schema_file ]]
assert_failure "schema.sql file written to"
}
function test_empty_mysql_migration_creates_schema_migrations_table() {
use_mysql
assert_tables_exist ""
rm "${db_type}_migrations/"*.sql
dogfish migrate $db_name >/dev/null
assert_tables_exist "schema_migrations"
assert_migrations_recorded ""
}
function test_empty_pg_migration_creates_schema_migrations_table() {
use_pg
assert_tables_exist ""
rm ${db_type}_migrations/*.sql
dogfish migrate $db_name >/dev/null
assert_tables_exist "schema_migrations"
assert_migrations_recorded ""
}
function test_mysql_rollback() {
use_mysql
dogfish migrate $db_name >/dev/null
assert_tables_exist "schema_migrations stuff things"
dogfish rollback $db_name >/dev/null
assert_tables_exist "schema_migrations stuff"
assert_migrations_recorded "1234"
}
function test_pg_rollback() {
use_pg
dogfish migrate $db_name >/dev/null
assert_tables_exist "schema_migrations stuff things"
dogfish rollback $db_name >/dev/null
assert_tables_exist "schema_migrations stuff"
assert_migrations_recorded "1234"
}
function test_mysql_migrate_to_version() {
use_mysql
dogfish migrate $db_name 1234 >/dev/null
assert_success
assert_tables_exist "schema_migrations stuff"
assert_migrations_recorded "1234"
dogfish migrate $db_name >/dev/null
assert_tables_exist "schema_migrations stuff things"
assert_migrations_recorded "1234 5678"
}
function test_pg_migrate_to_version() {
use_pg
dogfish migrate $db_name 1234 >/dev/null
assert_success
assert_tables_exist "schema_migrations stuff"
assert_migrations_recorded "1234"
dogfish migrate $db_name >/dev/null
assert_tables_exist "schema_migrations stuff things"
assert_migrations_recorded "1234 5678"
}
function test_mysql_rollback_to_version() {
use_mysql
dogfish migrate $db_name >/dev/null
assert_success
assert_tables_exist "schema_migrations stuff things"
assert_migrations_recorded "1234 5678"
dogfish rollback $db_name 5678 >/dev/null
assert_tables_exist "schema_migrations stuff"
assert_migrations_recorded "1234"
}
function test_pg_rollback_to_version() {
use_pg
dogfish migrate $db_name >/dev/null
assert_success
assert_tables_exist "schema_migrations stuff things"
assert_migrations_recorded "1234 5678"
dogfish rollback $db_name 5678 >/dev/null
assert_tables_exist "schema_migrations stuff"
assert_migrations_recorded "1234"
}
function test_mysql_schema_load() {
use_mysql
dogfish load-schema $db_name >/dev/null
assert_success
assert_tables_exist "schema_migrations stuff things"
assert_migrations_recorded "1234 5678"
}
function test_pg_schema_load() {
use_pg
dogfish load-schema $db_name >/dev/null
assert_success
assert_tables_exist "schema_migrations stuff things"
assert_migrations_recorded "1234 5678"
}
function test_mysql_schema_dump() {
use_mysql
local schema_file=mysql_migrations/schema.sql
dogfish load-schema $db_name >/dev/null
assert_success
rm "${schema_file}"
dogfish dump-schema $db_name >/dev/null
assert_success
[[ -r $schema_file ]]
assert_success "Expected schema.sql to be created"
[[ $(wc -c "${schema_file}" | awk '{print $1}') -gt 0 ]]
assert_success "Expected schema.sql to be non-empty"
}
function test_schema_dump_fails_read_only_mode() {
use_mysql
local schema_file=mysql_migrations/schema.sql
dogfish load-schema $db_name >/dev/null
assert_success
rm "${schema_file}"
dogfish -r dump-schema $db_name >/dev/null 2>&1
assert_failure "could dump-schema in read-only mode with option -r"
dogfish --read-only dump-schema $db_name >/dev/null 2>&1
assert_failure "could dump-schema in read-only mode with option --read-only"
}
function test_pg_schema_load() {
use_pg
local schema_file=pg_migrations/schema.sql
dogfish load-schema $db_name >/dev/null
assert_success
rm "$schema_file"
dogfish dump-schema $db_name >/dev/null
assert_success
[[ -r $schema_file ]]
assert_success "Expected schema.sql to be created"
[[ $(wc -c "${schema_file}" | awk '{print $1}') -gt 0 ]]
assert_success "Expected schema.sql to be non-empty"
}
# Run shellcheck
shellcheck=shellcheck
if ! command -v "$shellcheck" >/dev/null; then
if ! ([[ -x ./shellcheck ]] && [[ -f ./shellcheck ]]); then
shellcheck="shellcheck-zbin/shellcheck-$(uname -s | tr '[:upper:]' '[:lower:]')-$(uname -m).gz"
if ! [[ -f $shellcheck ]]; then
echo "Shellcheck not found and there isn't a binary for $(uname -sm)"
echo "in this repository. Please install it:"
die "\`brew install shellcheck\` on OS X or visit http://shellcheck.net"
fi
gunzip -c "$shellcheck" > ./shellcheck
chmod a+x ./shellcheck
fi
shellcheck="./shellcheck"
fi
"$shellcheck" dogfish "$0"
# Run all tests {{{
function all_test_function_names() {
compgen -A function | sed -ne "s/^test_\(.\{1,\}\)$/\1/p"
}
for test_name in $(all_test_function_names); do
setup_test
run_test "$test_name"
done
# }}}