Skip to content

Commit

Permalink
[#63] Use common test. Replaced lists:map/2 for LC.
Browse files Browse the repository at this point in the history
  • Loading branch information
jfacorro committed Sep 18, 2014
1 parent 5c9c1ef commit 38beaee
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 46 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ DIALYZER_OUT?=${NAME}.plt

ERL_ARGS?=-pa ebin -pa deps/*/ebin -name ${NODE}

CT_SUITES=migration

all: getdeps compile
${REBAR} compile

Expand All @@ -33,7 +35,8 @@ compile:
${REBAR} compile

test: compile
ERL_AFLAGS="-config test/test.config" ${REBAR} eunit skip_deps=true
${REBAR} skip_deps=true ct
open log/ct/index.html

clean:
${REBAR} clean
Expand Down
6 changes: 5 additions & 1 deletion rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{lib_dirs,["deps", "examples"]}.
{erl_opts, [
{parse_transform, lager_transform},
{src_dirs, ["src", "examples/blog/src"]},
{src_dirs, ["src", "examples/blog/src", "test"]},
warn_unused_vars,
warn_export_all,
warn_shadow_vars,
Expand All @@ -27,3 +27,7 @@
]}.
{xref_warnings, true}.
{xref_checks, [undefined_function_calls, undefined_functions, locals_not_used, deprecated_function_calls, deprecated_functions]}.

%% Common test
{ct_log_dir,"log/ct"}.
{ct_extra_params,"-no_auto_compile -dir ebin -pa deps/*/ebin -smp enable -s emysql -s sumo_db -erl_args -config test/test.config"}.
48 changes: 5 additions & 43 deletions src/sumo_migration.erl
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,11 @@

-export([migrate/0, rollback/0]).

-export([
init_test/0,
migrate_update_list_test/0,
migrate_test/0,
rollback_test/0
]).

%%% sumo_db callbacks
-export([sumo_schema/0, sumo_wakeup/1, sumo_sleep/1]).

-callback up() -> string().
-callback down() -> string().
-callback up() -> ok.
-callback down() -> ok.

-type version() :: atom().
-record(migration, {id :: integer(),
Expand Down Expand Up @@ -79,8 +72,7 @@ last_migration_version() ->
case sumo:find_all(?MODULE) of
[] -> undefined;
Migrations ->
FunVersion = fun (M) -> M#migration.version end,
Versions = lists:map(FunVersion, Migrations),
Versions = [Version || #migration{version = Version} <- Migrations],
lists:max(Versions)
end.

Expand All @@ -90,10 +82,10 @@ migration_update_list(LastVersion) ->
Files = filelib:wildcard("*.erl", MigrationsDir),

F = compose([fun filename:rootname/1, fun list_to_atom/1]),
AvailableVersion = lists:map(F, lists:sort(Files)),
AvailableVersions = lists:map(F, lists:sort(Files)),

FunFilter = fun(X) -> (LastVersion == undefined) or (X > LastVersion) end,
lists:filter(FunFilter, AvailableVersion).
lists:filter(FunFilter, AvailableVersions).

-spec migrations_dir() -> string().
migrations_dir() ->
Expand All @@ -119,33 +111,3 @@ compose(Funs) ->
fun(X) ->
lists:foldl(Compose, X, Funs)
end.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Tests
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

init_test() ->
application:ensure_all_started(emysql),
application:ensure_all_started(sumo_db),
sumo:delete_all(sumo_migration).

migrate_update_list_test() ->
[_, _, _] = migration_update_list('20140901'),
[_, _] = migration_update_list('20140902'),
[_] = migration_update_list('20140903'),
[] = migration_update_list('20140904').

migrate_test() ->
migrate(),
[_, _, _] = sumo:find_all(sumo_migration).

rollback_test() ->
[_, _, _] = sumo:find_all(sumo_migration),
rollback(),
[_, _] = sumo:find_all(sumo_migration),
rollback(),
[_] = sumo:find_all(sumo_migration),
rollback(),
[] = sumo:find_all(sumo_migration),
rollback(),
rollback().
62 changes: 62 additions & 0 deletions test/migration_SUITE.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
-module(migration_SUITE).

-export([
all/0,
init_per_suite/1,
end_per_suite/1
]).

-export([
migrate/1,
rollback/1
]).

-define(EXCLUDED_FUNS,
[
module_info,
all,
test,
init_per_suite,
end_per_suite
]).

-type config() :: [{atom(), term()}].

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Common test
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

-spec all() -> [atom()].
all() ->
Exports = ?MODULE:module_info(exports),
[F || {F, _} <- Exports, not lists:member(F, ?EXCLUDED_FUNS)].

-spec init_per_suite(config()) -> config().
init_per_suite(Config) ->
application:ensure_all_started(emysql),
application:ensure_all_started(sumo_db),
sumo:delete_all(sumo_migration),
Config.

-spec end_per_suite(config()) -> config().
end_per_suite(Config) ->
Config.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Tests cases
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

migrate(_Config) ->
sumo:migrate(),
[_, _, _] = sumo:find_all(sumo_migration).

rollback(_Config) ->
[_, _, _] = sumo:find_all(sumo_migration),
sumo:rollback(),
[_, _] = sumo:find_all(sumo_migration),
sumo:rollback(),
[_] = sumo:find_all(sumo_migration),
sumo:rollback(),
[] = sumo:find_all(sumo_migration),
sumo:rollback(),
sumo:rollback().
2 changes: 1 addition & 1 deletion test/test.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{
sumo_db,
[
{migrations_dir, "../test/migrations"},
{migrations_dir, "../../../test/migrations"},
{log_queries, true},
{query_timeout, 30000},
{storage_backends,
Expand Down
4 changes: 4 additions & 0 deletions variables-ct@jfacorro
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{config,[]}.
{event_handler,[]}.
{ct_hooks,[]}.
{enable_builtin_hooks,undefined}.

0 comments on commit 38beaee

Please sign in to comment.