couchdb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From chewbranca <...@git.apache.org>
Subject [GitHub] couchdb pull request: Replace etap tests with eunit
Date Wed, 02 Jul 2014 23:33:47 GMT
Github user chewbranca commented on a diff in the pull request:

    https://github.com/apache/couchdb/pull/253#discussion_r14490451
  
    --- Diff: src/couch_mrview/test/couch_mrview_collation_tests.erl ---
    @@ -0,0 +1,202 @@
    +% 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_mrview_collation_tests).
    +
    +-include("couch_eunit.hrl").
    +-include_lib("couchdb/couch_db.hrl").
    +
    +-define(ADMIN_USER, {user_ctx, #user_ctx{roles=[<<"_admin">>]}}).
    +-define(TIMEOUT, 1000).
    +-define(VALUES, [
    +    null,
    +    false,
    +    true,
    +
    +    1,
    +    2,
    +    3.0,
    +    4,
    +
    +    <<"a">>,
    +    <<"A">>,
    +    <<"aa">>,
    +    <<"b">>,
    +    <<"B">>,
    +    <<"ba">>,
    +    <<"bb">>,
    +
    +    [<<"a">>],
    +    [<<"b">>],
    +    [<<"b">>, <<"c">>],
    +    [<<"b">>, <<"c">>, <<"a">>],
    +    [<<"b">>, <<"d">>],
    +    [<<"b">>, <<"d">>, <<"e">>],
    +
    +    {[{<<"a">>, 1}]},
    +    {[{<<"a">>, 2}]},
    +    {[{<<"b">>, 1}]},
    +    {[{<<"b">>, 2}]},
    +    {[{<<"b">>, 2}, {<<"a">>, 1}]},
    +    {[{<<"b">>, 2}, {<<"c">>, 2}]}
    +]).
    +
    +
    +start() ->
    +    {ok, Pid} = couch_server_sup:start_link(?CONFIG_CHAIN),
    +    Pid.
    +
    +stop(Pid) ->
    +    erlang:monitor(process, Pid),
    +    couch_server_sup:stop(),
    +    receive
    +        {'DOWN', _, _, Pid, _} ->
    +            ok
    +    after ?TIMEOUT ->
    +        throw({timeout, server_stop})
    +    end.
    +
    +setup() ->
    +    {ok, Db1} = couch_mrview_test_util:new_db(?tempdb(), map),
    +    {ok, Db2} = couch_mrview_test_util:save_docs(Db1, make_docs()),
    +    Db2.
    +
    +teardown(Db) ->
    +    couch_db:close(Db),
    +    couch_server:delete(Db#db.name, [?ADMIN_USER]),
    +    ok.
    +
    +
    +collation_test_() ->
    +    {
    +        "Collation tests",
    +        {
    +            setup,
    +            fun start/0, fun stop/1,
    +            {
    +                foreach,
    +                fun setup/0, fun teardown/1,
    +                [
    +                    fun should_collate_fwd/1,
    +                    fun should_collate_rev/1,
    +                    fun should_collate_range/1,
    +                    fun should_collate_with_inclusive_end_fwd/1,
    +                    fun should_collate_with_inclusive_end_rev/1,
    +                    fun should_collate_without_inclusive_end_fwd/1,
    +                    fun should_collate_without_inclusive_end_rev/1,
    +                    fun should_collate_with_endkey_docid/1
    +                ]
    +            }
    +        }
    +    }.
    +
    +
    +should_collate_fwd(Db) ->
    +    {ok, Results} = run_query(Db, []),
    +    Expect = [{meta, [{total, 26}, {offset, 0}]}] ++ rows(),
    +    %% cannot use _assertEqual since mrview converts
    +    %% value 3.0 to 3 making assertion fail
    +    ?_assert(Expect == Results).
    +
    +should_collate_rev(Db) ->
    +    {ok, Results} = run_query(Db, [{direction, rev}]),
    +    Expect = [{meta, [{total, 26}, {offset, 0}]}] ++ lists:reverse(rows()),
    +    %% cannot use _assertEqual since mrview converts
    +    %% value 3.0 to 3 making assertion fail
    +    ?_assert(Expect == Results).
    +
    +should_collate_range(Db) ->
    +    ?_assertNot(
    +        begin
    +            {_, Error} = lists:foldl(fun(V, {Count, Error}) ->
    +                {ok, Results} = run_query(Db, [{start_key, V}, {end_key, V}]),
    +                Id = list_to_binary(integer_to_list(Count)),
    +                Expect = [
    +                    {meta, [{total, 26}, {offset, Count}]},
    +                    {row, [{id, Id}, {key, V}, {value, 0}]}
    +                ],
    +                case Results == Expect of
    +                    true -> {Count+1, Error};
    +                    _ -> {Count+1, true}
    +                end
    +            end, {0, false}, ?VALUES),
    +            Error
    +        end).
    --- End diff --
    
    Ideally assertions should be one line, and the use of begin is a general sign of non optimal
code structures. I would separate the construction of the results from the assertion, ie something
like `{_, Error} = lists:foldl(...), ?_assertNot(Error)`.
    
    Also, -1 to magic numbers, the number `26` is meaningless to anyone looking at this. I
would add a macro like `-define(ROW_COUNT, length(?VALUES)).`, especially given that `26`
is used in multiple places.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

Mime
View raw message