couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rnew...@apache.org
Subject [1/3] couch-log commit: updated refs/heads/master to fb41573
Date Mon, 28 Sep 2015 13:25:29 GMT
Repository: couchdb-couch-log
Updated Branches:
  refs/heads/master 7e615ac1b -> fb4157370


Isolate couch_log from backend for unit testing

This adds a custom backend for unit testing to decouple
couch_log from project specific backend and allow to assert
that callback calls carried through.


Project: http://git-wip-us.apache.org/repos/asf/couchdb-couch-log/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-couch-log/commit/345c2066
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-couch-log/tree/345c2066
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-couch-log/diff/345c2066

Branch: refs/heads/master
Commit: 345c20662564aa50eb2be60d423bf2f0f7e20636
Parents: 7e615ac
Author: Eric Avdey <eiri@eiri.ca>
Authored: Fri Sep 25 10:40:16 2015 -0300
Committer: Eric Avdey <eiri@eiri.ca>
Committed: Fri Sep 25 10:40:16 2015 -0300

----------------------------------------------------------------------
 src/couch_log.erl        |  53 ++++++++++----------
 test/couch_log_eunit.erl | 114 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 140 insertions(+), 27 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-couch-log/blob/345c2066/src/couch_log.erl
----------------------------------------------------------------------
diff --git a/src/couch_log.erl b/src/couch_log.erl
index 669223f..eaa887f 100644
--- a/src/couch_log.erl
+++ b/src/couch_log.erl
@@ -91,38 +91,37 @@ callbacks_test_() ->
         fun setup/0,
         fun cleanup/1,
         [
-            ?_assertMatch({ok, _}, get_backend()),
-            ?_assertEqual(ok, couch_log:debug("message", [])),
-            ?_assertEqual(ok, couch_log:info("message", [])),
-            ?_assertEqual(ok, couch_log:notice("message", [])),
-            ?_assertEqual(ok, couch_log:warning("message", [])),
-            ?_assertEqual(ok, couch_log:error("message", [])),
-            ?_assertEqual(ok, couch_log:critical("message", [])),
-            ?_assertEqual(ok, couch_log:alert("message", [])),
-            ?_assertEqual(ok, couch_log:emergency("message", [])),
-            ?_assertEqual(ok, couch_log:set_level(info))
+            ?_assertEqual({ok, couch_log_eunit}, get_backend()),
+            ?_assertEqual(ok, couch_log:debug("debug", [])),
+            ?_assertEqual("debug", couch_log_eunit:debug()),
+            ?_assertEqual(ok, couch_log:info("info", [])),
+            ?_assertEqual("info", couch_log_eunit:info()),
+            ?_assertEqual(ok, couch_log:notice("notice", [])),
+            ?_assertEqual("notice", couch_log_eunit:notice()),
+            ?_assertEqual(ok, couch_log:warning("warning", [])),
+            ?_assertEqual("warning", couch_log_eunit:warning()),
+            ?_assertEqual(ok, couch_log:error("error", [])),
+            ?_assertEqual("error", couch_log_eunit:error()),
+            ?_assertEqual(ok, couch_log:critical("critical", [])),
+            ?_assertEqual("critical", couch_log_eunit:critical()),
+            ?_assertEqual(ok, couch_log:alert("alert", [])),
+            ?_assertEqual("alert", couch_log_eunit:alert()),
+            ?_assertEqual(ok, couch_log:emergency("emergency", [])),
+            ?_assertEqual("emergency", couch_log_eunit:emergency()),
+            ?_assertEqual(ok, couch_log:set_level(info)),
+            ?_assertEqual(info, couch_log_eunit:get_level())
         ]
     }.
 
 setup() ->
     meck:new([couch_stats]),
     meck:expect(couch_stats, increment_counter, fun(_) -> ok end),
-    start().
-
-start() ->
-    start([], couch_log).
-
-start(Acc, App) ->
-    case application:start(App) of
-        ok ->
-            [App | Acc];
-        {error, {not_started, Dep}} ->
-            Acc1 = start(Acc, Dep),
-            start(Acc1, App)
-    end.
-
-cleanup(Deps) ->
-    [application:stop(Dep) || Dep <- Deps],
-    meck:unload([couch_stats]).
+    couch_log_eunit:setup(),
+    application:load(?MODULE),
+    application:set_env(?MODULE, backend, couch_log_eunit).
+
+cleanup(_) ->
+    meck:unload([couch_stats]),
+    couch_log_eunit:cleanup().
 
 -endif.

http://git-wip-us.apache.org/repos/asf/couchdb-couch-log/blob/345c2066/test/couch_log_eunit.erl
----------------------------------------------------------------------
diff --git a/test/couch_log_eunit.erl b/test/couch_log_eunit.erl
new file mode 100644
index 0000000..f5af18c
--- /dev/null
+++ b/test/couch_log_eunit.erl
@@ -0,0 +1,114 @@
+% Licensed under the Apache License, Version 2.0 (the "License"); you may not
+% use this file except in compliance with the License. You may obtain a copy of
+% the License at
+%
+%   http://www.apache.org/licenses/LICENSE-2.0
+%
+% Unless required by applicable law or agreed to in writing, software
+% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+% License for the specific language governing permissions and limitations under
+% the License.
+
+-module(couch_log_eunit).
+
+-behaviour(couch_log).
+
+-export([
+    debug/2,
+    info/2,
+    notice/2,
+    warning/2,
+    error/2,
+    critical/2,
+    alert/2,
+    emergency/2,
+    set_level/1
+]).
+
+-export([
+    setup/0,
+    cleanup/0,
+    debug/0,
+    info/0,
+    notice/0,
+    warning/0,
+    error/0,
+    critical/0,
+    alert/0,
+    emergency/0,
+    get_level/0
+]).
+
+debug() ->
+    read_log(debug).
+
+debug(Fmt, Args) ->
+    write_log(debug, Fmt, Args).
+
+info() ->
+    read_log(info).
+
+info(Fmt, Args) ->
+    write_log(info, Fmt, Args).
+
+notice() ->
+    read_log(notice).
+
+notice(Fmt, Args) ->
+    write_log(notice, Fmt, Args).
+
+warning() ->
+    read_log(warning).
+
+warning(Fmt, Args) ->
+    write_log(warning, Fmt, Args).
+
+error() ->
+    read_log(error).
+
+error(Fmt, Args) ->
+    write_log(error, Fmt, Args).
+
+critical() ->
+    read_log(critical).
+
+critical(Fmt, Args) ->
+    write_log(critical, Fmt, Args).
+
+alert() ->
+    read_log(alert).
+
+alert(Fmt, Args) ->
+    write_log(alert, Fmt, Args).
+
+emergency() ->
+    read_log(emergency).
+
+emergency(Fmt, Args) ->
+    write_log(emergency, Fmt, Args).
+
+get_level() ->
+    read_log(level).
+
+set_level(Level) ->
+    true = ets:insert(?MODULE, {level, Level}),
+    ok.
+
+
+setup() ->
+    ets:new(?MODULE, [public, named_table]).
+
+cleanup() ->
+    ets:delete(?MODULE).
+
+write_log(Key, Fmt, Args) ->
+    Msg = io_lib:format(Fmt, Args),
+    true = ets:insert(?MODULE, {Key, Msg}),
+    ok.
+
+read_log(Key) ->
+    case ets:lookup(?MODULE, Key) of
+        [] -> undefined;
+        [{Key, Value}] -> Value
+    end.
\ No newline at end of file


Mime
View raw message