Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 17 additions & 16 deletions src/couch_mrview/src/couch_mrview_test_util.erl
Original file line number Diff line number Diff line change
Expand Up @@ -49,26 +49,27 @@ make_docs(local, Count) ->
make_docs(_, Count) ->
[doc(I) || I <- lists:seq(1, Count)].

ddoc(changes) ->
ddoc({changes, Opts}) ->
ViewOpts = case Opts of
seq_indexed ->
[{<<"seq_indexed">>, true}];
keyseq_indexed ->
[{<<"keyseq_indexed">>, true}];
seq_indexed_keyseq_indexed ->
[
{<<"seq_indexed">>, true},
{<<"keyseq_indexed">>, true}
]
end,
couch_doc:from_json_obj({[
{<<"_id">>, <<"_design/bar">>},
{<<"options">>, {[
{<<"seq_indexed">>, true}
]}},
{<<"options">>, {ViewOpts}},
{<<"views">>, {[
{<<"baz">>, {[
{<<"map">>, <<"function(doc) {emit(doc.val, doc.val);}">>}
]}},
{<<"bing">>, {[
{<<"map">>, <<"function(doc) {}">>}
]}},
{<<"zing">>, {[
{<<"map">>, <<
"function(doc) {\n"
" if(doc.foo !== undefined)\n"
" emit(doc.foo, 0);\n"
"}"
>>}
{
<<"map">>,
<<"function(doc) {emit(doc.val.toString(), doc.val);}">>
}
]}}
]}}
]});
Expand Down
6 changes: 3 additions & 3 deletions src/couch_mrview/src/couch_mrview_util.erl
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ get_view_changes_count(View) ->
{ok, 0};
{#btree{}, nil} ->
couch_btree:fold_reduce(SBtree, CountFun, 0, []);
{nil, #btree{}} ->
{_, #btree{}} ->
couch_btree:fold_reduce(KSBtree, CountFun, 0, [])
end,
case {SBtree, KSBtree} of
Expand Down Expand Up @@ -813,9 +813,9 @@ changes_expand_dups([{{[Key, Seq], DocId}, {dups, Vals}} | Rest], Acc) ->
changes_expand_dups([{{Seq, Key}, {DocId, {dups, Vals}}} | Rest], Acc) ->
Expanded = [{{Seq, Key, DocId}, Val} || Val <- Vals],
changes_expand_dups(Rest, Expanded ++ Acc);
changes_expand_dups([{{[Key, Seq], DocId}, Val} | Rest], Acc) ->
changes_expand_dups([{{[Key, Seq], DocId}, {Val, _}} | Rest], Acc) ->
changes_expand_dups(Rest, [{{Seq, Key, DocId}, Val} | Acc]);
changes_expand_dups([{{Seq, Key}, {DocId, Val}} | Rest], Acc) ->
changes_expand_dups([{{Seq, Key}, {DocId, Val, _}} | Rest], Acc) ->
changes_expand_dups(Rest, [{{Seq, Key, DocId}, Val} | Acc]).

maybe_load_doc(_Db, _DI, #mrargs{include_docs=false}) ->
Expand Down
140 changes: 92 additions & 48 deletions src/couch_mrview/test/couch_mrview_changes_since_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,75 @@

-define(TIMEOUT, 1000).



setup() ->
{ok, Db} = couch_mrview_test_util:init_db(?tempdb(), changes),
Db.

teardown(Db) ->
couch_db:close(Db),
couch_server:delete(Db#db.name, [?ADMIN_CTX]),
ok.


changes_since_test() ->
changes_since_basic_test_() ->
{
"changes_since tests",
{
setup,
fun test_util:start_couch/0, fun test_util:stop_couch/1,
{
foreach,
fun setup/0, fun teardown/1,
fun() ->
Type = {changes, seq_indexed},
{ok, Db} = couch_mrview_test_util:init_db(?tempdb(), Type),
Db
end,
fun teardown/1,
[
fun test_basic/1,
fun test_range/1,
fun test_basic_since/1,
fun test_range_since/1,
fun test_basic_count/1,
fun test_range_count/1,
fun test_basic_count_since/1,
fun test_compact/1
]
}
}
}.

changes_since_range_test_() ->
{
"changes_since_range tests",
{
setup,
fun test_util:start_couch/0, fun test_util:stop_couch/1,
{
foreach,
fun() ->
Type = {changes, keyseq_indexed},
{ok, Db} = couch_mrview_test_util:init_db(?tempdb(), Type),
Db
end,
fun teardown/1,
[
fun test_range/1,
fun test_range_since/1
]
}
}
}.

changes_since_range_count_test_() ->
{
"changes_since_range_count tests",
{
setup,
fun test_util:start_couch/0, fun test_util:stop_couch/1,
{
foreach,
fun() ->
Type = {changes, seq_indexed_keyseq_indexed},
{ok, Db} = couch_mrview_test_util:init_db(?tempdb(), Type),
Db
end,
fun teardown/1,
[
fun test_range_count/1,
fun test_range_count_since/1,
fun test_compact/1,
fun test_remove_key/1
]
}
Expand All @@ -57,46 +95,48 @@ changes_since_test() ->
test_basic(Db) ->
Result = run_query(Db, 0, []),
Expect = {ok, [
{{2, 1, <<"1">>}, 1},
{{3, 10, <<"10">>}, 10},
{{4, 2, <<"2">>}, 2},
{{5, 3, <<"3">>}, 3},
{{6, 4, <<"4">>}, 4},
{{7, 5, <<"5">>}, 5},
{{8, 6, <<"6">>}, 6},
{{9, 7, <<"7">>}, 7},
{{10, 8, <<"8">>}, 8},
{{11, 9, <<"9">>}, 9}
{{2, <<"1">>, <<"1">>}, 1},
{{3, <<"10">>, <<"10">>}, 10},
{{4, <<"2">>, <<"2">>}, 2},
{{5, <<"3">>, <<"3">>}, 3},
{{6, <<"4">>, <<"4">>}, 4},
{{7, <<"5">>, <<"5">>}, 5},
{{8, <<"6">>, <<"6">>}, 6},
{{9, <<"7">>, <<"7">>}, 7},
{{10, <<"8">>, <<"8">>}, 8},
{{11, <<"9">>, <<"9">>}, 9}
]},
?_assertEqual(Result, Expect).


test_range(Db) ->
Result = run_query(Db, 0, [{start_key, 3}, {end_key, 5}]),
Range = [{start_key, <<"3">>}, {end_key, <<"5">>}],
Result = run_query(Db, 0, Range),
Expect = {ok, [
{{5, 3, <<"3">>}, 3},
{{6, 4, <<"4">>}, 4},
{{7, 5, <<"5">>}, 5}
{{5, <<"3">>, <<"3">>}, 3},
{{6, <<"4">>, <<"4">>}, 4},
{{7, <<"5">>, <<"5">>}, 5}
]},
?_assertEqual(Result, Expect).

test_basic_since(Db) ->
Result = run_query(Db, 5, []),
Expect = {ok, [
{{6, 4, <<"4">>}, 4},
{{7, 5, <<"5">>}, 5},
{{8, 6, <<"6">>}, 6},
{{9, 7, <<"7">>}, 7},
{{10, 8, <<"8">>}, 8},
{{11, 9, <<"9">>}, 9}
{{6, <<"4">>, <<"4">>}, 4},
{{7, <<"5">>, <<"5">>}, 5},
{{8, <<"6">>, <<"6">>}, 6},
{{9, <<"7">>, <<"7">>}, 7},
{{10, <<"8">>, <<"8">>}, 8},
{{11, <<"9">>, <<"9">>}, 9}
]},
?_assertEqual(Result, Expect).

test_range_since(Db) ->
Result = run_query(Db, 5, [{start_key, 3}, {end_key, 5}]),
Range = [{start_key, <<"3">>}, {end_key, <<"5">>}],
Result = run_query(Db, 5, Range),
Expect = {ok, [
{{6, 4, <<"4">>}, 4},
{{7, 5, <<"5">>}, 5}
{{6, <<"4">>, <<"4">>}, 4},
{{7, <<"5">>, <<"5">>}, 5}
]},
?_assertEqual(Result, Expect).

Expand All @@ -105,22 +145,23 @@ test_basic_count(Db) ->
?_assertEqual(Result, 10).

test_range_count(Db) ->
Result = run_count_query(Db, 0, [{start_key, 3}, {end_key, 5}]),
Range = [{start_key, <<"3">>}, {end_key, <<"5">>}],
Result = run_count_query(Db, 0, Range),
?_assertEqual(Result, 3).

test_basic_count_since(Db) ->
Result = run_count_query(Db, 5, []),
?_assertEqual(Result, 6).

test_range_count_since(Db) ->
Result = run_count_query(Db, 5, [{start_key, 3}, {end_key, 5}]),
Range = [{start_key, <<"3">>}, {end_key, <<"5">>}],
Result = run_count_query(Db, 5, Range),
?_assertEqual(Result, 2).

test_compact(Db) ->
Result = couch_mrview:compact(Db, <<"_design/bar">>),
?_assertEqual(Result, ok),
Count = run_count_query(Db, 0, []),
?_assertEqual(Count, 10).
[?_assertEqual(Result, ok), ?_assertEqual(Count, 10)].

test_remove_key(Db) ->
%% add new doc
Expand All @@ -130,13 +171,12 @@ test_remove_key(Db) ->
{ok, _} = couch_db:ensure_full_commit(Db),
{ok, Db1} = couch_db:reopen(Db),
Result = run_count_query(Db1, 0, []),
?_assertEqual(Result, 11),
%% check new view key
Result1 = run_query(Db1, 0, [{start_key, 11}, {end_key, 11}]),
Range = [{start_key, <<"11">>}, {end_key, <<"11">>}],
Result1 = run_query(Db1, 0, Range),
Expect = {ok, [
{{12, 11, <<"11">>}, 11}
{{12, <<"11">>, <<"11">>}, 11}
]},
?_assertEqual(Result1, Expect),

%% delete doc
Doc2 = couch_doc:from_json_obj({[
Expand All @@ -147,13 +187,17 @@ test_remove_key(Db) ->
{ok, _} = couch_db:update_doc(Db1, Doc2, []),
{ok, Db2} = couch_db:reopen(Db1),
Result2 = run_count_query(Db2, 0, []),
?_assertEqual(Result2, 11),
%% check new view key
Result3 = run_query(Db2, 0, [{start_key, 11}, {end_key, 11}]),
Result3 = run_query(Db2, 0, Range),
Expect2 = {ok, [
{{13, 11, <<"11">>}, {[{<<"_removed">>, true}]}}
{{13, <<"11">>, <<"11">>}, removed}
]},
?_assertEqual(Result3, Expect2).
[
?_assertEqual(Result, 11),
?_assertEqual(Result1, Expect),
?_assertEqual(Result2, 11),
?_assertEqual(Result3, Expect2)
].

run_query(Db, Since, Opts) ->
Fun = fun(KV, Acc) -> {ok, [KV | Acc]} end,
Expand Down