summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNick Vatamaniuc <vatamane@gmail.com>2022-08-18 21:20:15 -0400
committerNick Vatamaniuc <nickva@users.noreply.github.com>2022-08-19 13:18:59 -0400
commitcad96318fc41f62c67b7fd4cc75b01ebdd2c4ba3 (patch)
treef11b2f7e9c883bd2959e8b948d59aee071552605
parent381fe30d88a83becc2ac92627c73345bc647ecfc (diff)
downloadcouchdb-cad96318fc41f62c67b7fd4cc75b01ebdd2c4ba3.tar.gz
Update couch_replicator_filtered_tests
Take advantage of the helper setup and teardown functions. Switching to a simpler TDEF_FE macro instead of foreachx and inorder setup cruft also saves some lines of code.
-rw-r--r--src/couch_replicator/test/eunit/couch_replicator_filtered_tests.erl257
1 files changed, 100 insertions, 157 deletions
diff --git a/src/couch_replicator/test/eunit/couch_replicator_filtered_tests.erl b/src/couch_replicator/test/eunit/couch_replicator_filtered_tests.erl
index b77b83daa..267c4fab6 100644
--- a/src/couch_replicator/test/eunit/couch_replicator_filtered_tests.erl
+++ b/src/couch_replicator/test/eunit/couch_replicator_filtered_tests.erl
@@ -14,7 +14,7 @@
-include_lib("couch/include/couch_eunit.hrl").
-include_lib("couch/include/couch_db.hrl").
--include_lib("couch_replicator/src/couch_replicator.hrl").
+-include("couch_replicator_test.hrl").
-define(DDOC,
{[
@@ -57,206 +57,149 @@
]}
).
-setup(_) ->
- Ctx = test_util:start_couch([couch_replicator]),
- Source = create_db(),
- create_docs(Source),
- Target = create_db(),
- {Ctx, {Source, Target}}.
-
-teardown(_, {Ctx, {Source, Target}}) ->
- delete_db(Source),
- delete_db(Target),
- ok = application:stop(couch_replicator),
- ok = test_util:stop_couch(Ctx).
-
filtered_replication_test_() ->
- Pairs = [{remote, remote}],
{
"Filtered replication tests",
{
- foreachx,
- fun setup/1,
- fun teardown/2,
- [{Pair, fun should_succeed/2} || Pair <- Pairs]
+ foreach,
+ fun couch_replicator_test_helper:test_setup/0,
+ fun couch_replicator_test_helper:test_teardown/1,
+ [
+ ?TDEF_FE(should_succeed),
+ ?TDEF_FE(should_succeed_with_query),
+ ?TDEF_FE(should_succeed_with_view)
+ ]
}
}.
-query_filtered_replication_test_() ->
- Pairs = [{remote, remote}],
- {
- "Filtered with query replication tests",
- {
- foreachx,
- fun setup/1,
- fun teardown/2,
- [{Pair, fun should_succeed_with_query/2} || Pair <- Pairs]
- }
- }.
-
-view_filtered_replication_test_() ->
- Pairs = [{remote, remote}],
- {
- "Filtered with a view replication tests",
- {
- foreachx,
- fun setup/1,
- fun teardown/2,
- [{Pair, fun should_succeed_with_view/2} || Pair <- Pairs]
- }
- }.
-
-should_succeed({From, To}, {_Ctx, {Source, Target}}) ->
+should_succeed({_Ctx, {Source, Target}}) ->
+ create_docs(Source),
RepObject =
{[
- {<<"source">>, db_url(From, Source)},
- {<<"target">>, db_url(To, Target)},
+ {<<"source">>, db_url(Source)},
+ {<<"target">>, db_url(Target)},
{<<"filter">>, <<"filter_ddoc/testfilter">>}
]},
- {ok, _} = couch_replicator:replicate(RepObject, ?ADMIN_USER),
+ replicate(RepObject),
%% FilteredFun is an Erlang version of following JS function
%% function(doc, req){if (doc.class == 'mammal') return true;}
- FilterFun = fun(_DocId, {Props}) ->
+ FilterFun = fun(_DocId, #doc{body = {Props}}) ->
couch_util:get_value(<<"class">>, Props) == <<"mammal">>
end,
- {ok, TargetDbInfo, AllReplies} = compare_dbs(Source, Target, FilterFun),
- {lists:flatten(io_lib:format("~p -> ~p", [From, To])), [
- {"Target DB has proper number of docs",
- ?_assertEqual(1, proplists:get_value(doc_count, TargetDbInfo))},
- {"Target DB doesn't have deleted docs",
- ?_assertEqual(0, proplists:get_value(doc_del_count, TargetDbInfo))},
- {"All the docs filtered as expected",
- ?_assert(lists:all(fun(Valid) -> Valid end, AllReplies))}
- ]}.
+ {TargetDocCount, AllReplies} = compare_dbs(Source, Target, FilterFun),
+ % Target DB has proper number of docs,
+ ?assertEqual(1, TargetDocCount),
+ % All the docs filtered as expected
+ ?assert(lists:all(fun(Valid) -> Valid end, AllReplies)).
-should_succeed_with_query({From, To}, {_Ctx, {Source, Target}}) ->
+should_succeed_with_query({_Ctx, {Source, Target}}) ->
+ create_docs(Source),
RepObject =
{[
- {<<"source">>, db_url(From, Source)},
- {<<"target">>, db_url(To, Target)},
+ {<<"source">>, db_url(Source)},
+ {<<"target">>, db_url(Target)},
{<<"filter">>, <<"filter_ddoc/queryfilter">>},
{<<"query_params">>,
{[
{<<"starts">>, <<"a">>}
]}}
]},
- {ok, _} = couch_replicator:replicate(RepObject, ?ADMIN_USER),
- FilterFun = fun(_DocId, {Props}) ->
+ replicate(RepObject),
+ FilterFun = fun(_DocId, #doc{body = {Props}}) ->
case couch_util:get_value(<<"class">>, Props) of
<<"a", _/binary>> -> true;
_ -> false
end
end,
- {ok, TargetDbInfo, AllReplies} = compare_dbs(Source, Target, FilterFun),
- {lists:flatten(io_lib:format("~p -> ~p", [From, To])), [
- {"Target DB has proper number of docs",
- ?_assertEqual(2, proplists:get_value(doc_count, TargetDbInfo))},
- {"Target DB doesn't have deleted docs",
- ?_assertEqual(0, proplists:get_value(doc_del_count, TargetDbInfo))},
- {"All the docs filtered as expected",
- ?_assert(lists:all(fun(Valid) -> Valid end, AllReplies))}
- ]}.
+ {TargetDocCount, AllReplies} = compare_dbs(Source, Target, FilterFun),
+ % Target DB has proper number of docs
+ ?assertEqual(2, TargetDocCount),
+ % All the docs filtered as expected,
+ ?assert(lists:all(fun(Valid) -> Valid end, AllReplies)).
-should_succeed_with_view({From, To}, {_Ctx, {Source, Target}}) ->
+should_succeed_with_view({_Ctx, {Source, Target}}) ->
+ create_docs(Source),
RepObject =
{[
- {<<"source">>, db_url(From, Source)},
- {<<"target">>, db_url(To, Target)},
+ {<<"source">>, db_url(Source)},
+ {<<"target">>, db_url(Target)},
{<<"filter">>, <<"_view">>},
{<<"query_params">>,
{[
{<<"view">>, <<"filter_ddoc/mammals">>}
]}}
]},
- {ok, _} = couch_replicator:replicate(RepObject, ?ADMIN_USER),
- FilterFun = fun(_DocId, {Props}) ->
+ replicate(RepObject),
+ FilterFun = fun(_DocId, #doc{body = {Props}}) ->
couch_util:get_value(<<"class">>, Props) == <<"mammal">>
end,
- {ok, TargetDbInfo, AllReplies} = compare_dbs(Source, Target, FilterFun),
- {lists:flatten(io_lib:format("~p -> ~p", [From, To])), [
- {"Target DB has proper number of docs",
- ?_assertEqual(1, proplists:get_value(doc_count, TargetDbInfo))},
- {"Target DB doesn't have deleted docs",
- ?_assertEqual(0, proplists:get_value(doc_del_count, TargetDbInfo))},
- {"All the docs filtered as expected",
- ?_assert(lists:all(fun(Valid) -> Valid end, AllReplies))}
- ]}.
+ {TargetDocCount, AllReplies} = compare_dbs(Source, Target, FilterFun),
+ % Target DB has proper number of docs
+ ?assertEqual(1, TargetDocCount),
+ % All the docs filtered as expected
+ ?assert(lists:all(fun(Valid) -> Valid end, AllReplies)).
compare_dbs(Source, Target, FilterFun) ->
- {ok, SourceDb} = couch_db:open_int(Source, []),
- {ok, TargetDb} = couch_db:open_int(Target, []),
- {ok, TargetDbInfo} = couch_db:get_db_info(TargetDb),
- Fun = fun(FullDocInfo, Acc) ->
- {ok, DocId, SourceDoc} = read_doc(SourceDb, FullDocInfo),
- TargetReply = read_doc(TargetDb, DocId),
- case FilterFun(DocId, SourceDoc) of
- true ->
- ValidReply = {ok, DocId, SourceDoc} == TargetReply,
- {ok, [ValidReply | Acc]};
- false ->
- ValidReply = {not_found, missing} == TargetReply,
- {ok, [ValidReply | Acc]}
- end
- end,
- {ok, AllReplies} = couch_db:fold_docs(SourceDb, Fun, [], []),
- ok = couch_db:close(SourceDb),
- ok = couch_db:close(TargetDb),
- {ok, TargetDbInfo, AllReplies}.
-
-read_doc(Db, DocIdOrInfo) ->
- case couch_db:open_doc(Db, DocIdOrInfo) of
- {ok, Doc} ->
- {Props} = couch_doc:to_json_obj(Doc, [attachments]),
- DocId = couch_util:get_value(<<"_id">>, Props),
- {ok, DocId, {Props}};
- Error ->
- Error
- end.
+ {ok, TargetDocCount} = fabric:get_doc_count(Target),
+ Replies = lists:foldl(
+ fun({Id, Rev}, Acc) ->
+ SrcDoc = read_doc(Source, Id, Rev),
+ TgtDoc = read_doc(Target, Id, Rev),
+ case FilterFun(Id, SrcDoc) of
+ true ->
+ [is_record(TgtDoc, doc) | Acc];
+ false ->
+ [TgtDoc =:= not_found | Acc]
+ end
+ end,
+ [],
+ couch_replicator_test_helper:cluster_doc_revs(Source)
+ ),
+ {TargetDocCount, Replies}.
-create_db() ->
- DbName = ?tempdb(),
- {ok, Db} = couch_db:create(DbName, [?ADMIN_CTX]),
- ok = couch_db:close(Db),
- DbName.
+read_doc(Db, DocId, Rev) ->
+ couch_replicator_test_helper:cluster_open_rev(Db, DocId, Rev).
create_docs(DbName) ->
- {ok, Db} = couch_db:open(DbName, [?ADMIN_CTX]),
- DDoc = couch_doc:from_json_obj(?DDOC),
- Doc1 = couch_doc:from_json_obj(
- {[
- {<<"_id">>, <<"doc1">>},
- {<<"class">>, <<"mammal">>},
- {<<"value">>, 1}
- ]}
- ),
- Doc2 = couch_doc:from_json_obj(
- {[
- {<<"_id">>, <<"doc2">>},
- {<<"class">>, <<"amphibians">>},
- {<<"value">>, 2}
- ]}
- ),
- Doc3 = couch_doc:from_json_obj(
- {[
- {<<"_id">>, <<"doc3">>},
- {<<"class">>, <<"reptiles">>},
- {<<"value">>, 3}
- ]}
- ),
- Doc4 = couch_doc:from_json_obj(
- {[
- {<<"_id">>, <<"doc4">>},
- {<<"class">>, <<"arthropods">>},
- {<<"value">>, 2}
- ]}
- ),
- {ok, _} = couch_db:update_docs(Db, [DDoc, Doc1, Doc2, Doc3, Doc4]),
- couch_db:close(Db).
+ Docs = [
+ couch_doc:from_json_obj(?DDOC),
+ #doc{
+ id = <<"doc1">>,
+ body =
+ {[
+ {<<"class">>, <<"mammal">>},
+ {<<"value">>, 1}
+ ]}
+ },
+ #doc{
+ id = <<"doc2">>,
+ body =
+ {[
+ {<<"class">>, <<"amphibians">>},
+ {<<"value">>, 2}
+ ]}
+ },
+ #doc{
+ id = <<"doc3">>,
+ body =
+ {[
+ {<<"class">>, <<"reptiles">>},
+ {<<"value">>, 3}
+ ]}
+ },
+ #doc{
+ id = <<"doc4">>,
+ body =
+ {[
+ {<<"class">>, <<"arthropods">>},
+ {<<"value">>, 2}
+ ]}
+ }
+ ],
+ {ok, [_ | _]} = fabric:update_docs(DbName, Docs, [?ADMIN_CTX]).
-delete_db(DbName) ->
- ok = couch_server:delete(DbName, [?ADMIN_CTX]).
+db_url(DbName) ->
+ couch_replicator_test_helper:cluster_db_url(DbName).
-db_url(remote, DbName) ->
- Addr = config:get("httpd", "bind_address", "127.0.0.1"),
- Port = mochiweb_socket_server:get(couch_httpd, port),
- ?l2b(io_lib:format("http://~s:~b/~s", [Addr, Port, DbName])).
+replicate(RepObject) ->
+ couch_replicator_test_helper:replicate(RepObject).