test(mongo): do not rely on internals to clean database

This commit is contained in:
Andrew Mayorov 2023-04-12 12:37:36 +03:00
parent 21e19a33ce
commit a08c000222
No known key found for this signature in database
GPG Key ID: 2837C62ACFBFED5D
1 changed files with 18 additions and 11 deletions

View File

@ -147,6 +147,16 @@ ensure_loaded() ->
_ = emqx_ee_bridge:module_info(), _ = emqx_ee_bridge:module_info(),
ok. ok.
mongo_type(Config) ->
case ?config(mongo_type, Config) of
rs ->
{rs, maps:get(<<"replica_set_name">>, ?config(mongo_config, Config))};
sharded ->
sharded;
single ->
single
end.
mongo_type_bin(rs) -> mongo_type_bin(rs) ->
<<"mongodb_rs">>; <<"mongodb_rs">>;
mongo_type_bin(sharded) -> mongo_type_bin(sharded) ->
@ -263,17 +273,14 @@ create_bridge_http(Params) ->
end. end.
clear_db(Config) -> clear_db(Config) ->
Type = mongo_type_bin(?config(mongo_type, Config)), Type = mongo_type(Config),
Name = ?config(mongo_name, Config), Host = ?config(mongo_host, Config),
#{<<"collection">> := Collection} = ?config(mongo_config, Config), Port = ?config(mongo_port, Config),
ResourceID = emqx_bridge_resource:resource_id(Type, Name), Server = Host ++ ":" ++ integer_to_list(Port),
{ok, _, #{state := #{connector_state := #{poolname := PoolName}}}} = #{<<"database">> := Db, <<"collection">> := Collection} = ?config(mongo_config, Config),
emqx_resource:get_instance(ResourceID), {ok, Client} = mongo_api:connect(Type, [Server], [], [{database, Db}, {w_mode, unsafe}]),
Selector = #{}, {true, _} = mongo_api:delete(Client, Collection, _Selector = #{}),
{true, _} = ecpool:pick_and_do( mongo_api:disconnect(Client).
PoolName, {mongo_api, delete, [Collection, Selector]}, no_handover
),
ok.
find_all(Config) -> find_all(Config) ->
Type = mongo_type_bin(?config(mongo_type, Config)), Type = mongo_type_bin(?config(mongo_type, Config)),