Browse Source

test: update eunit tests

JianBo He 2 years atrás
parent
commit
2e35024df1

+ 3 - 3
apps/emqx_auth_mnesia/src/emqx_authn_mnesia.erl

@@ -78,8 +78,6 @@
     {<<"is_superuser">>, atom}
 ]).
 
--elvis([{elvis_style, nesting_level, disable}]).
-
 %%------------------------------------------------------------------------------
 %% Mnesia bootstrap
 %%------------------------------------------------------------------------------
@@ -177,7 +175,9 @@ import_users({PasswordType, Filename, FileData}, State) ->
     Convertor = convertor(PasswordType, State),
     try
         {_NewUsersCnt, Users} = parse_import_users(Filename, FileData, Convertor),
-        case do_import_users(Users) of
+        case length(Users) > 0 andalso do_import_users(Users) of
+            false ->
+                error(empty_users);
             ok ->
                 ok;
             {error, Reason} ->

+ 24 - 0
apps/emqx_auth_mnesia/test/emqx_authn_mnesia_SUITE.erl

@@ -251,6 +251,30 @@ t_import_users(_) ->
             sample_filename_and_data(<<"user-credentials-malformed.csv">>),
             State
         )
+    ),
+
+    ?assertEqual(
+        {error, empty_users},
+        emqx_authn_mnesia:import_users(
+            {hash, <<"empty_users.json">>, <<"[]">>},
+            State
+        )
+    ),
+
+    ?assertEqual(
+        {error, empty_users},
+        emqx_authn_mnesia:import_users(
+            {hash, <<"empty_users.csv">>, <<>>},
+            State
+        )
+    ),
+
+    ?assertEqual(
+        {error, empty_users},
+        emqx_authn_mnesia:import_users(
+            {hash, prepared_user_list, []},
+            State
+        )
     ).
 
 t_import_users_plain(_) ->

+ 5 - 3
apps/emqx_utils/src/emqx_utils_stream.erl

@@ -50,6 +50,8 @@
 
 -dialyzer(no_improper_lists).
 
+-elvis([{elvis_style, nesting_level, disable}]).
+
 %%
 
 %% @doc Make a stream that produces no values.
@@ -189,8 +191,8 @@ csv(Bin) when is_binary(Bin) ->
     case get_csv_header(CSVData) of
         {ok, CSVHeaders, CSVLines} ->
             fun() -> Reader(CSVHeaders, CSVLines) end;
-        {error, Reason} ->
-            error(Reason)
+        error ->
+            empty()
     end.
 
 csv_data(Data) ->
@@ -203,7 +205,7 @@ get_csv_header(CSV) ->
             Seq = binary:split(Line, [<<",">>, <<" ">>, <<"\n">>], [global, trim_all]),
             {ok, Seq, NewCSV};
         eof ->
-            {error, empty_file}
+            error
     end.
 
 csv_read_line({csv_data, [Line | Lines]}) ->

+ 22 - 0
apps/emqx_utils/test/emqx_utils_stream_tests.erl

@@ -82,3 +82,25 @@ mqueue_test() ->
         [1, 42, 2],
         emqx_utils_stream:consume(emqx_utils_stream:mqueue(400))
     ).
+
+csv_test() ->
+    Data = <<"h1,h2,h3\r\nv1,v2,v3\r\nv4,v5,v6">>,
+    ?assertEqual(
+        [
+            #{<<"h1">> => <<"v1">>, <<"h2">> => <<"v2">>, <<"h3">> => <<"v3">>},
+            #{<<"h1">> => <<"v4">>, <<"h2">> => <<"v5">>, <<"h3">> => <<"v6">>}
+        ],
+        emqx_utils_stream:consume(emqx_utils_stream:csv(Data))
+    ),
+
+    ?assertEqual(
+        [],
+        emqx_utils_stream:consume(emqx_utils_stream:csv(<<"">>))
+    ),
+
+    BadData = <<"h1,h2,h3\r\nv1,v2,v3\r\nv4,v5">>,
+    ?assertException(
+        error,
+        bad_format,
+        emqx_utils_stream:consume(emqx_utils_stream:csv(BadData))
+    ).