aboutsummaryrefslogtreecommitdiffstats
path: root/test/mysqlerl_SUITE.erl
blob: 58591f8a6bbed4d7848d3880f3e02c1bead229e9 (plain)
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
%%%-------------------------------------------------------------------
%%% @author Brian Cully <bjc@kublai.com>
%%% @copyright (C) 2012, Brian Cully
%%% @doc
%%%
%%% @end
%%% Created :  6 Feb 2012 by Brian Cully <bjc@kublai.com>
%%%-------------------------------------------------------------------
-module(mysqlerl_SUITE).

-compile(export_all).

-include_lib("common_test/include/ct.hrl").

-define(Q, "SELECT username, password FROM user ORDER BY username ASC").
-define(COLS, ["username", "password"]).

%%--------------------------------------------------------------------
%% @spec suite() -> Info
%% Info = [tuple()]
%% @end
%%--------------------------------------------------------------------
suite() ->
    [{timetrap,{seconds,30}},
     {require, db_info}].

mysql_cmd(undefined, undefined) ->
    "mysql";
mysql_cmd(User, undefined) ->
    io_lib:format("mysql -u'~s'", [User]);
mysql_cmd(undefined, Pass) ->
    io_lib:format("mysql -p'~s'", [Pass]);
mysql_cmd(User, Pass) ->
    io_lib:format("mysql -u'~s' -p'~s'", [User, Pass]).

create_db(User, Pass, Name) ->
    drop_db(User, Pass, Name),
    SQL = io_lib:format("CREATE DATABASE ~s", [Name]),
    CMD = mysql_cmd(User, Pass),
    os:cmd(io_lib:format("echo '~s' | ~s", [SQL, CMD])).

drop_db(User, Pass, Name) ->
    SQL = io_lib:format("DROP DATABASE IF EXISTS ~s", [Name]),
    CMD = mysql_cmd(User, Pass),
    os:cmd(io_lib:format("echo '~s' | ~s", [SQL, CMD])).

create_table(User, Pass, Name, DataDir) ->
    CMD = mysql_cmd(User, Pass),
    os:cmd(io_lib:format("~s ~s < ~s/table-data.sql", [CMD, Name, DataDir])).

%%--------------------------------------------------------------------
%% @spec init_per_suite(Config0) ->
%%     Config1 | {skip,Reason} | {skip_and_save,Reason,Config1}
%% Config0 = Config1 = [tuple()]
%% Reason = term()
%% @end
%%--------------------------------------------------------------------
init_per_suite(Config) ->
    DBInfo  = ct:get_config(db_info),
    DataDir = ?config(data_dir, Config),
    User    = ?config(username, DBInfo),
    Pass    = ?config(password, DBInfo),
    Name    = ?config(name, DBInfo),

    create_db(User, Pass, Name),
    create_table(User, Pass, Name, DataDir),
    ok = application:start(mysqlerl),
    Config.

%%--------------------------------------------------------------------
%% @spec end_per_suite(Config0) -> void() | {save_config,Config1}
%% Config0 = Config1 = [tuple()]
%% @end
%%--------------------------------------------------------------------
end_per_suite(_Config) ->
    DBInfo = ct:get_config(db_info),
    User   = ?config(username, DBInfo),
    Pass   = ?config(password, DBInfo),
    Name   = ?config(name, DBInfo),

    ok = application:stop(mysqlerl),
    drop_db(User, Pass, Name).

%%--------------------------------------------------------------------
%% @spec init_per_group(GroupName, Config0) ->
%%               Config1 | {skip,Reason} | {skip_and_save,Reason,Config1}
%% GroupName = atom()
%% Config0 = Config1 = [tuple()]
%% Reason = term()
%% @end
%%--------------------------------------------------------------------
init_per_group(_GroupName, Config) ->
    Config.

%%--------------------------------------------------------------------
%% @spec end_per_group(GroupName, Config0) ->
%%               void() | {save_config,Config1}
%% GroupName = atom()
%% Config0 = Config1 = [tuple()]
%% @end
%%--------------------------------------------------------------------
end_per_group(_GroupName, _Config) ->
    ok.

%%--------------------------------------------------------------------
%% @spec init_per_testcase(TestCase, Config0) ->
%%               Config1 | {skip,Reason} | {skip_and_save,Reason,Config1}
%% TestCase = atom()
%% Config0 = Config1 = [tuple()]
%% Reason = term()
%% @end
%%--------------------------------------------------------------------
init_per_testcase(_TestCase, Config) ->
    DBInfo = ct:get_config(db_info),
    {ok, DBRef} = mysqlerl:connect(?config(host, DBInfo),
                                   ?config(port, DBInfo),
                                   ?config(name, DBInfo),
                                   ?config(username, DBInfo),
                                   ?config(password, DBInfo),
                                   ?config(options, DBInfo)),
    [{db_ref, DBRef} | Config].

%%--------------------------------------------------------------------
%% @spec end_per_testcase(TestCase, Config0) ->
%%               void() | {save_config,Config1} | {fail,Reason}
%% TestCase = atom()
%% Config0 = Config1 = [tuple()]
%% Reason = term()
%% @end
%%--------------------------------------------------------------------
end_per_testcase(_TestCase, Config) ->
    ok = mysqlerl:disconnect(?config(db_ref, Config)).

%%--------------------------------------------------------------------
%% @spec groups() -> [Group]
%% Group = {GroupName,Properties,GroupsAndTestCases}
%% GroupName = atom()
%% Properties = [parallel | sequence | Shuffle | {RepeatType,N}]
%% GroupsAndTestCases = [Group | {group,GroupName} | TestCase]
%% TestCase = atom()
%% Shuffle = shuffle | {shuffle,{integer(),integer(),integer()}}
%% RepeatType = repeat | repeat_until_all_ok | repeat_until_all_fail |
%%              repeat_until_any_ok | repeat_until_any_fail
%% N = integer() | forever
%% @end
%%--------------------------------------------------------------------
groups() ->
    [{all, [sequence],
      [{group, read_queries}, {group, cursor}]},
     {read_queries, [shuffle],
      [describe_table, sql_query, param_query, select_count]},
     {cursor, [shuffle],
      [first, last, next, prev,
       next_after_first, next_after_last, prev_after_first, prev_after_last,
       next_all, prev_all, next_prev_next, prev_next_prev,
       select_next, select_relative, select_absolute]},
     {trans, [sequence],
      [commit, rollback]}].

%%--------------------------------------------------------------------
%% @spec all() -> GroupsAndTestCases | {skip,Reason}
%% GroupsAndTestCases = [{group,GroupName} | TestCase]
%% GroupName = atom()
%% TestCase = atom()
%% Reason = term()
%% @end
%%--------------------------------------------------------------------
all() ->
    [{group, all}].

%%--------------------------------------------------------------------
%% @spec TestCase(Config0) ->
%%               ok | exit() | {skip,Reason} | {comment,Comment} |
%%               {save_config,Config1} | {skip_and_save,Reason,Config1}
%% Config0 = Config1 = [tuple()]
%% Reason = term()
%% Comment = term()
%% @end
%%--------------------------------------------------------------------
app_starts(_Config) ->
    ok = application:start(mysqlerl).

app_stops(_Config) ->
    ok = application:start(mysqlerl).

describe_table(Config) ->
    io:format("describe_table ~p", [Config]),
    {ok, Description} = mysqlerl:describe_table(?config(db_ref, Config),
                                                "user"),
    [{"username", {sql_varchar, 20}}, {"password", {sql_varchar, 64}}] = Description.

sql_query(Config) ->
    {selected, ?COLS, Rows} = mysqlerl:sql_query(?config(db_ref, Config),
                                                 ?Q),
    [{"bjc", _}, {"siobain", _}] = Rows.

param_query(Config) ->
    {selected, ?COLS, Rows} = mysqlerl:param_query(?config(db_ref, Config),
                                                   "SELECT * FROM user WHERE username=?",
                                                   [{{sql_varchar, 20}, "bjc"}]),
    [{"bjc", _}] = Rows.

select_count(Config) ->
    {ok, 2} = mysqlerl:select_count(?config(db_ref, Config),
                                    "SELECT username FROM user").

select_next(Config) ->
    mysqlerl:select_count(?config(db_ref, Config), ?Q),
    {selected, ?COLS, Rows} = mysqlerl:select(?config(db_ref, Config),
                                              next, 1),
    [{"bjc", _}] = Rows.

select_absolute(Config) ->
    mysqlerl:select_count(?config(db_ref, Config), ?Q),
    mysqlerl:next(?config(db_ref, Config)),
    {selected, ?COLS, Rows} = mysqlerl:select(?config(db_ref, Config),
                                              {absolute, 1}, 1),
    [{"bjc", _}] = Rows.

select_relative(Config) ->
    mysqlerl:select_count(?config(db_ref, Config), ?Q),
    mysqlerl:next(?config(db_ref, Config)),
    {selected, ?COLS, Rows} = mysqlerl:select(?config(db_ref, Config),
                                              {relative, 1}, 1),
    [{"siobain", _}] = Rows.

first(Config) ->
    mysqlerl:select_count(?config(db_ref, Config), ?Q),
    {selected, ?COLS, Rows} = mysqlerl:first(?config(db_ref, Config)),
    [{"bjc", _}] = Rows.

last(Config) ->
    mysqlerl:select_count(?config(db_ref, Config), ?Q),
    {selected, ?COLS, Rows} = mysqlerl:last(?config(db_ref, Config)),
    [{"siobain", _}] = Rows.

next(Config) ->
    mysqlerl:select_count(?config(db_ref, Config), ?Q),
    {selected, ?COLS, Rows} = mysqlerl:next(?config(db_ref, Config)),
    [{"bjc", _}] = Rows.

next_after_first(Config) ->
    mysqlerl:select_count(?config(db_ref, Config), ?Q),
    mysqlerl:first(?config(db_ref, Config)),
    {selected, ?COLS, [{"siobain", _}]} = mysqlerl:next(?config(db_ref, Config)).

next_after_last(Config) ->
    mysqlerl:select_count(?config(db_ref, Config), ?Q),
    mysqlerl:last(?config(db_ref, Config)),
    {selected, ?COLS, []} = mysqlerl:next(?config(db_ref, Config)).

next_all(Config) ->
    mysqlerl:select_count(?config(db_ref, Config), ?Q),
    {selected, ?COLS, [{"bjc", _}]} = mysqlerl:next(?config(db_ref, Config)),
    {selected, ?COLS, [{"siobain", _}]} = mysqlerl:next(?config(db_ref, Config)),
    {selected, ?COLS, []} = mysqlerl:next(?config(db_ref, Config)).

prev(Config) ->
    mysqlerl:select_count(?config(db_ref, Config), ?Q),
    mysqlerl:last(?config(db_ref, Config)),
    {selected, ?COLS, Rows} = mysqlerl:prev(?config(db_ref, Config)),
    [{"bjc", _}] = Rows.

prev_all(Config) ->
    mysqlerl:select_count(?config(db_ref, Config), ?Q),
    mysqlerl:last(?config(db_ref, Config)),
    {selected, ?COLS, [{"bjc", _}]} = mysqlerl:prev(?config(db_ref, Config)),
    {selected, ?COLS, []} = mysqlerl:prev(?config(db_ref, Config)).

prev_after_first(Config) ->
    mysqlerl:select_count(?config(db_ref, Config), ?Q),
    mysqlerl:first(?config(db_ref, Config)),
    {selected, ?COLS, []} = mysqlerl:prev(?config(db_ref, Config)).

prev_after_last(Config) ->
    mysqlerl:select_count(?config(db_ref, Config), ?Q),
    mysqlerl:last(?config(db_ref, Config)),
    {selected, ?COLS, [{"bjc", _}]} = mysqlerl:prev(?config(db_ref, Config)).

next_prev_next(Config) ->
    mysqlerl:select_count(?config(db_ref, Config), ?Q),
    mysqlerl:first(?config(db_ref, Config)),
    {selected, ?COLS, [{"siobain", _}]} = mysqlerl:next(?config(db_ref, Config)),
    {selected, ?COLS, [{"bjc", _}]} = mysqlerl:prev(?config(db_ref, Config)),
    {selected, ?COLS, [{"siobain", _}]} = mysqlerl:next(?config(db_ref, Config)).

prev_next_prev(Config) ->
    mysqlerl:select_count(?config(db_ref, Config), ?Q),
    mysqlerl:last(?config(db_ref, Config)),
    {selected, ?COLS, [{"bjc", _}]} = mysqlerl:prev(?config(db_ref, Config)),
    {selected, ?COLS, [{"siobain", _}]} = mysqlerl:next(?config(db_ref, Config)),
    {selected, ?COLS, [{"bjc", _}]} = mysqlerl:prev(?config(db_ref, Config)).

commit(Config) ->
    ok = mysqlerl:commit(?config(db_ref, Config), commit),
    {skip, "Not implemented"}.

rollback(Config) ->
    ok = mysqlerl:rollback(?config(db_ref, Config), rollback),
    {skip, "Not implemented"}.