You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by rn...@apache.org on 2015/09/28 15:25:29 UTC
[1/3] couch-log commit: updated refs/heads/master to fb41573
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 <ei...@eiri.ca>
Authored: Fri Sep 25 10:40:16 2015 -0300
Committer: Eric Avdey <ei...@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
[3/3] couch-log commit: updated refs/heads/master to fb41573
Posted by rn...@apache.org.
Assert the whole calls history
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/fb415737
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-couch-log/tree/fb415737
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-couch-log/diff/fb415737
Branch: refs/heads/master
Commit: fb4157370403c4c97f19d958a51c889950a66a94
Parents: 19e2716
Author: Eric Avdey <ei...@eiri.ca>
Authored: Fri Sep 25 12:57:17 2015 -0300
Committer: Eric Avdey <ei...@eiri.ca>
Committed: Fri Sep 25 12:57:17 2015 -0300
----------------------------------------------------------------------
src/couch_log.erl | 33 ++++++++++++++++++---------------
1 file changed, 18 insertions(+), 15 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/couchdb-couch-log/blob/fb415737/src/couch_log.erl
----------------------------------------------------------------------
diff --git a/src/couch_log.erl b/src/couch_log.erl
index 7bdb476..384f85a 100644
--- a/src/couch_log.erl
+++ b/src/couch_log.erl
@@ -92,30 +92,23 @@ callbacks_test_() ->
fun cleanup/1,
[
?_assertEqual({ok, couch_log_eunit}, get_backend()),
+ ?_assertEqual(ok, couch_log:set_level(info)),
?_assertEqual(ok, couch_log:debug("debug", [])),
- ?_assertEqual({debug, ["debug", []]}, last_meck_call()),
?_assertEqual(ok, couch_log:info("info", [])),
- ?_assertEqual({info, ["info", []]}, last_meck_call()),
?_assertEqual(ok, couch_log:notice("notice", [])),
- ?_assertEqual({notice, ["notice", []]}, last_meck_call()),
?_assertEqual(ok, couch_log:warning("warning", [])),
- ?_assertEqual({warning, ["warning", []]}, last_meck_call()),
?_assertEqual(ok, couch_log:error("error", [])),
- ?_assertEqual({error, ["error", []]}, last_meck_call()),
?_assertEqual(ok, couch_log:critical("critical", [])),
- ?_assertEqual({critical, ["critical", []]}, last_meck_call()),
?_assertEqual(ok, couch_log:alert("alert", [])),
- ?_assertEqual({alert, ["alert", []]}, last_meck_call()),
?_assertEqual(ok, couch_log:emergency("emergency", [])),
- ?_assertEqual({emergency, ["emergency", []]}, last_meck_call()),
- ?_assertEqual(ok, couch_log:set_level(info)),
- ?_assertEqual({set_level, [info]}, last_meck_call())
+ ?_assertEqual(stats_calls(), meck:history(couch_stats, self())),
+ ?_assertEqual(log_calls(), meck:history(couch_log_eunit, self()))
]
}.
setup() ->
meck:new([couch_stats, couch_log_eunit], [non_strict]),
- meck:expect(couch_stats, increment_counter, fun(_) -> ok end),
+ meck:expect(couch_stats, increment_counter, 1, ok),
setup_couch_log_eunit(),
application:load(?MODULE),
application:set_env(?MODULE, backend, couch_log_eunit).
@@ -130,9 +123,19 @@ setup_couch_log_eunit() ->
meck:expect(couch_log_eunit, Fun, 2, ok)
end, Levels).
-last_meck_call() ->
- History = meck:history(couch_log_eunit, self()),
- {_, {couch_log_eunit, M, A}, _} = hd(lists:reverse(History)),
- {M, A}.
+stats_calls() ->
+ Levels = [debug, info, notice, warning, error, critical, alert, emergency],
+ lists:map(fun(Level) ->
+ MFA = {couch_stats, increment_counter, [[couch_log, level, Level]]},
+ {self(), MFA, ok}
+ end, Levels).
+
+log_calls() ->
+ Levels = [debug, info, notice, warning, error, critical, alert, emergency],
+ Calls = lists:map(fun(Level) ->
+ MFA = {couch_log_eunit, Level, [atom_to_list(Level),[]]},
+ {self(), MFA, ok}
+ end, Levels),
+ [{self(), {couch_log_eunit, set_level, [info]}, ok}|Calls].
-endif.
[2/3] couch-log commit: updated refs/heads/master to fb41573
Posted by rn...@apache.org.
Remove the custom backend, mock it instead
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/19e27160
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-couch-log/tree/19e27160
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-couch-log/diff/19e27160
Branch: refs/heads/master
Commit: 19e27160562b653cfdea00430edca2cb7b4d2e09
Parents: 345c206
Author: Eric Avdey <ei...@eiri.ca>
Authored: Fri Sep 25 12:19:54 2015 -0300
Committer: Eric Avdey <ei...@eiri.ca>
Committed: Fri Sep 25 12:19:54 2015 -0300
----------------------------------------------------------------------
src/couch_log.erl | 37 +++++++++-----
test/couch_log_eunit.erl | 114 ------------------------------------------
2 files changed, 24 insertions(+), 127 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/couchdb-couch-log/blob/19e27160/src/couch_log.erl
----------------------------------------------------------------------
diff --git a/src/couch_log.erl b/src/couch_log.erl
index eaa887f..7bdb476 100644
--- a/src/couch_log.erl
+++ b/src/couch_log.erl
@@ -93,35 +93,46 @@ callbacks_test_() ->
[
?_assertEqual({ok, couch_log_eunit}, get_backend()),
?_assertEqual(ok, couch_log:debug("debug", [])),
- ?_assertEqual("debug", couch_log_eunit:debug()),
+ ?_assertEqual({debug, ["debug", []]}, last_meck_call()),
?_assertEqual(ok, couch_log:info("info", [])),
- ?_assertEqual("info", couch_log_eunit:info()),
+ ?_assertEqual({info, ["info", []]}, last_meck_call()),
?_assertEqual(ok, couch_log:notice("notice", [])),
- ?_assertEqual("notice", couch_log_eunit:notice()),
+ ?_assertEqual({notice, ["notice", []]}, last_meck_call()),
?_assertEqual(ok, couch_log:warning("warning", [])),
- ?_assertEqual("warning", couch_log_eunit:warning()),
+ ?_assertEqual({warning, ["warning", []]}, last_meck_call()),
?_assertEqual(ok, couch_log:error("error", [])),
- ?_assertEqual("error", couch_log_eunit:error()),
+ ?_assertEqual({error, ["error", []]}, last_meck_call()),
?_assertEqual(ok, couch_log:critical("critical", [])),
- ?_assertEqual("critical", couch_log_eunit:critical()),
+ ?_assertEqual({critical, ["critical", []]}, last_meck_call()),
?_assertEqual(ok, couch_log:alert("alert", [])),
- ?_assertEqual("alert", couch_log_eunit:alert()),
+ ?_assertEqual({alert, ["alert", []]}, last_meck_call()),
?_assertEqual(ok, couch_log:emergency("emergency", [])),
- ?_assertEqual("emergency", couch_log_eunit:emergency()),
+ ?_assertEqual({emergency, ["emergency", []]}, last_meck_call()),
?_assertEqual(ok, couch_log:set_level(info)),
- ?_assertEqual(info, couch_log_eunit:get_level())
+ ?_assertEqual({set_level, [info]}, last_meck_call())
]
}.
setup() ->
- meck:new([couch_stats]),
+ meck:new([couch_stats, couch_log_eunit], [non_strict]),
meck:expect(couch_stats, increment_counter, fun(_) -> ok end),
- couch_log_eunit:setup(),
+ setup_couch_log_eunit(),
application:load(?MODULE),
application:set_env(?MODULE, backend, couch_log_eunit).
cleanup(_) ->
- meck:unload([couch_stats]),
- couch_log_eunit:cleanup().
+ meck:unload([couch_stats, couch_log_eunit]).
+
+setup_couch_log_eunit() ->
+ meck:expect(couch_log_eunit, set_level, 1, ok),
+ Levels = [debug, info, notice, warning, error, critical, alert, emergency],
+ lists:foreach(fun(Fun) ->
+ meck:expect(couch_log_eunit, Fun, 2, ok)
+ end, Levels).
+
+last_meck_call() ->
+ History = meck:history(couch_log_eunit, self()),
+ {_, {couch_log_eunit, M, A}, _} = hd(lists:reverse(History)),
+ {M, A}.
-endif.
http://git-wip-us.apache.org/repos/asf/couchdb-couch-log/blob/19e27160/test/couch_log_eunit.erl
----------------------------------------------------------------------
diff --git a/test/couch_log_eunit.erl b/test/couch_log_eunit.erl
deleted file mode 100644
index f5af18c..0000000
--- a/test/couch_log_eunit.erl
+++ /dev/null
@@ -1,114 +0,0 @@
-% 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