-
Notifications
You must be signed in to change notification settings - Fork 4
/
pure_migrations_SUITE.erl
86 lines (75 loc) · 3.12 KB
/
pure_migrations_SUITE.erl
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
-module(pure_migrations_SUITE).
-include_lib("common_test/include/ct.hrl").
-include_lib("eunit/include/eunit.hrl").
-compile(export_all).
all() -> [
wrong_filename_test,
regular_upgrade_test,
rewrite_version_test,
faulty_script_test,
start_not_from_zero_test,
versions_gap_test,
negative_version_test
].
wrong_filename_test(Opts) ->
PreparedCall = engine:migrate(
filename:join([?config(data_dir, Opts), "01-invalid-script-name"]),
fun(F) -> F() end,
query_fun([], ok)
),
?assertError(badarg, PreparedCall()).
regular_upgrade_test(Opts) ->
PreparedCall = engine:migrate(
filename:join([?config(data_dir, Opts), "02-regular-upgrade"]),
fun(F) -> F() end,
query_fun([], ok)
),
?assertEqual([ok], PreparedCall()).
rewrite_version_test(Opts) ->
PreparedCall = engine:migrate(
filename:join([?config(data_dir, Opts), "03-rewrite-version"]),
fun(F) -> F() end,
query_fun([{0, "00_very_first_script.sql"}], ok)
),
?assertError({badmatch, 1}, PreparedCall()).
faulty_script_test(Opts) ->
PreparedCall = engine:migrate(
filename:join([?config(data_dir, Opts), "00-default"]),
fun(F) -> F() end,
query_fun([], {error, system_fault, "Please check database connectivity"})
),
?assertError({badmatch, {
error, system_fault, "Please check database connectivity"
}}, PreparedCall()).
start_not_from_zero_test(Opts) ->
PreparedCall = engine:migrate(
filename:join([?config(data_dir, Opts), "04-invalid-start-version"]),
fun(F) -> F() end,
query_fun([], ok)
),
?assertError({badmatch, 0}, PreparedCall()).
versions_gap_test(Opts) ->
PreparedCall = engine:migrate(
filename:join([?config(data_dir, Opts), "04-invalid-start-version"]),
fun(F) -> F() end,
query_fun([{0, "00_very_first_script.sql"}], ok)
),
?assertError({badmatch, 1}, PreparedCall()).
negative_version_test(Opts) ->
PreparedCall = engine:migrate(
filename:join([?config(data_dir, Opts), "05-negative-version"]),
fun(F) -> F() end,
query_fun([], ok)
),
?assertError({badmatch, 0}, PreparedCall()).
query_fun(ExistingVersions, MigrationResponse) ->
fun(Q) ->
case Q of
"CREATE" ++ _Tail -> ok;
"SELECT version" ++ _Tail -> ExistingVersions;
"SELECT max" ++ _Tail -> length(ExistingVersions) - 1;
"INSERT" ++ _Tail -> ok;
<<"CREATE TABLE fruit (\n name TEXT\n);">> -> MigrationResponse;
true -> throw("Unknown calls not expected")
end
end.