test(erlang): add shared test helper and DRY app setup
This commit is contained in:
parent
daf6749c34
commit
d06690f651
|
|
@ -3,22 +3,6 @@
|
||||||
-include("ip2region.hrl").
|
-include("ip2region.hrl").
|
||||||
|
|
||||||
pools_started_test() ->
|
pools_started_test() ->
|
||||||
try application:stop(ip2region) catch _:_ -> ok end,
|
{ok, _} = ip2region_test_util:start_app(),
|
||||||
try application:unload(ip2region) catch _:_ -> ok end,
|
|
||||||
ok = application:load(ip2region),
|
|
||||||
%% NOTE: rebar3 compiles tests under _build/test/lib/ip2region via a
|
|
||||||
%% symlink back to the source tree, so ?FILE resolves to the original
|
|
||||||
%% binding/erlang/test/... path. If rebar3 is configured to copy
|
|
||||||
%% instead of symlink, this path traversal must be adjusted.
|
|
||||||
TestDir = filename:dirname(?FILE),
|
|
||||||
ErlangDir = filename:dirname(TestDir),
|
|
||||||
BindingDir = filename:dirname(ErlangDir),
|
|
||||||
RepoRoot = filename:dirname(BindingDir),
|
|
||||||
V6File = filename:join([RepoRoot, "data", "ip2region_v6.xdb"]),
|
|
||||||
ok = application:set_env(ip2region, db, [
|
|
||||||
{ipv4, "ip2region.xdb"},
|
|
||||||
{ipv6, V6File}
|
|
||||||
]),
|
|
||||||
{ok, _} = application:ensure_all_started(ip2region),
|
|
||||||
?assert(is_pid(whereis(?IP2REGION_POOL_V4))),
|
?assert(is_pid(whereis(?IP2REGION_POOL_V4))),
|
||||||
?assert(is_pid(whereis(?IP2REGION_POOL_V6))).
|
?assert(is_pid(whereis(?IP2REGION_POOL_V6))).
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,51 @@
|
||||||
|
-module(ip2region_test_util).
|
||||||
|
|
||||||
|
-export([
|
||||||
|
repo_root/0,
|
||||||
|
v6_xdb_path/0,
|
||||||
|
default_db_config/0,
|
||||||
|
reset_app/0,
|
||||||
|
load_app/0,
|
||||||
|
set_db_config/1,
|
||||||
|
start_app/0,
|
||||||
|
start_app/1,
|
||||||
|
stop_app/0
|
||||||
|
]).
|
||||||
|
|
||||||
|
repo_root() ->
|
||||||
|
TestDir = filename:dirname(?FILE),
|
||||||
|
ErlangDir = filename:dirname(TestDir),
|
||||||
|
BindingDir = filename:dirname(ErlangDir),
|
||||||
|
filename:dirname(BindingDir).
|
||||||
|
|
||||||
|
v6_xdb_path() ->
|
||||||
|
filename:join([repo_root(), "data", "ip2region_v6.xdb"]).
|
||||||
|
|
||||||
|
default_db_config() ->
|
||||||
|
[
|
||||||
|
{ipv4, "ip2region.xdb"},
|
||||||
|
{ipv6, v6_xdb_path()}
|
||||||
|
].
|
||||||
|
|
||||||
|
reset_app() ->
|
||||||
|
try application:stop(ip2region) catch _:_ -> ok end,
|
||||||
|
try application:unload(ip2region) catch _:_ -> ok end,
|
||||||
|
ok.
|
||||||
|
|
||||||
|
load_app() ->
|
||||||
|
ok = application:load(ip2region).
|
||||||
|
|
||||||
|
set_db_config(Config) ->
|
||||||
|
ok = application:set_env(ip2region, db, Config).
|
||||||
|
|
||||||
|
start_app() ->
|
||||||
|
start_app(default_db_config()).
|
||||||
|
|
||||||
|
start_app(Config) ->
|
||||||
|
reset_app(),
|
||||||
|
load_app(),
|
||||||
|
set_db_config(Config),
|
||||||
|
application:ensure_all_started(ip2region).
|
||||||
|
|
||||||
|
stop_app() ->
|
||||||
|
application:stop(ip2region).
|
||||||
Loading…
Reference in New Issue