couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bbast...@apache.org
Subject [31/41] couch-mrview commit: updated refs/heads/master to 28e51f3
Date Fri, 31 Oct 2014 19:53:46 GMT
Port view changes tests to eunit


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

Branch: refs/heads/master
Commit: d6771df75c68d0796d4758e02b653a2d211707e3
Parents: 487df67
Author: Benjamin Bastian <benjamin.bastian@gmail.com>
Authored: Mon Sep 8 15:10:17 2014 -0700
Committer: Benjamin Bastian <benjamin.bastian@gmail.com>
Committed: Thu Oct 30 13:40:31 2014 -0700

----------------------------------------------------------------------
 src/couch_mrview_test_util.erl            |   2 +-
 test/08-changes_since.t                   | 156 ------------------
 test/09-index-events.t                    |  73 ---------
 test/10-index-changes.t                   | 216 ------------------------
 test/couch_mrview_changes_since_tests.erl | 166 +++++++++++++++++++
 test/couch_mrview_index_changes_tests.erl | 217 +++++++++++++++++++++++++
 6 files changed, 384 insertions(+), 446 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-couch-mrview/blob/d6771df7/src/couch_mrview_test_util.erl
----------------------------------------------------------------------
diff --git a/src/couch_mrview_test_util.erl b/src/couch_mrview_test_util.erl
index 1d3d788..c8da1ee 100644
--- a/src/couch_mrview_test_util.erl
+++ b/src/couch_mrview_test_util.erl
@@ -34,7 +34,7 @@ new_db(Name, Type) ->
     save_docs(Db, [ddoc(Type)]).
 
 delete_db(Name) ->
-    couch_server:delete(Name, [{user_ctx, ?ADMIN}]).
+    couch_server:delete(Name, [?ADMIN_USER]).
 
 save_docs(Db, Docs) ->
     {ok, _} = couch_db:update_docs(Db, Docs, []),

http://git-wip-us.apache.org/repos/asf/couchdb-couch-mrview/blob/d6771df7/test/08-changes_since.t
----------------------------------------------------------------------
diff --git a/test/08-changes_since.t b/test/08-changes_since.t
deleted file mode 100644
index 3107c1a..0000000
--- a/test/08-changes_since.t
+++ /dev/null
@@ -1,156 +0,0 @@
-#!/usr/bin/env escript
-%% -*- erlang -*-
-
-% 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.
-
-main(_) ->
-    etap:plan(14),
-    case (catch test()) of
-        ok ->
-            etap:end_tests();
-        Other ->
-            etap:diag(io_lib:format("Test died abnormally: ~p", [Other])),
-            etap:bail(Other)
-    end,
-    timer:sleep(300),
-    ok.
-
-test() ->
-    test_util:start_couch(),
-
-    {ok, Db} = couch_mrview_test_util:init_db(<<"foo">>, changes),
-
-    test_basic(Db),
-    test_range(Db),
-    test_basic_since(Db),
-    test_range_since(Db),
-    test_basic_count(Db),
-    test_range_count(Db),
-    test_basic_count_since(Db),
-    test_range_count_since(Db),
-    test_compact(Db),
-    test_remove_key(Db),
-    catch test_util:stop_couch(),
-    ok.
-
-
-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}
-    ]},
-    etap:is(Result, Expect, "Simple view query worked.").
-
-
-test_range(Db) ->
-    Result = run_query(Db, 0, [{start_key, 3}, {end_key, 5}]),
-    Expect = {ok, [
-                {{5, 3, <<"3">>}, 3},
-                {{6, 4, <<"4">>}, 4},
-                {{7, 5, <<"5">>}, 5}
-    ]},
-    etap:is(Result, Expect, "Query with range works.").
-
-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}
-    ]},
-    etap:is(Result, Expect, "Simple view query since 5 worked.").
-
-test_range_since(Db) ->
-    Result = run_query(Db, 5, [{start_key, 3}, {end_key, 5}]),
-    Expect = {ok, [
-                {{6, 4, <<"4">>}, 4},
-                {{7, 5, <<"5">>}, 5}
-    ]},
-    etap:is(Result, Expect, "Query with range since 5 works.").
-
-test_basic_count(Db) ->
-    Result = run_count_query(Db, 0, []),
-    etap:is(Result, 10, "Simple view count worked.").
-
-test_range_count(Db) ->
-    Result = run_count_query(Db, 0, [{start_key, 3}, {end_key, 5}]),
-    etap:is(Result, 3, "Count with range works.").
-
-test_basic_count_since(Db) ->
-    Result = run_count_query(Db, 5, []),
-    etap:is(Result, 6, "Simple view count since 5 worked.").
-
-test_range_count_since(Db) ->
-    Result = run_count_query(Db, 5, [{start_key, 3}, {end_key, 5}]),
-    etap:is(Result, 2, "Count with range since 5 works.").
-
-test_compact(Db) ->
-    Result = couch_mrview:compact(Db, <<"_design/bar">>),
-    etap:is(Result, ok, "compact view is OK"),
-    Count = run_count_query(Db, 0, []),
-    etap:is(Count, 10, "compact view worked.").
-
-test_remove_key(Db) ->
-    %% add new doc
-    Doc = couch_mrview_test_util:doc(11),
-    {ok, Rev} = couch_db:update_doc(Db, Doc, []),
-    RevStr = couch_doc:rev_to_str(Rev),
-    {ok, _} =  couch_db:ensure_full_commit(Db),
-    {ok, Db1} = couch_db:reopen(Db),
-    Result = run_count_query(Db1, 0, []),
-    etap:is(Result, 11, "Add new doc worked."),
-    %% check new view key
-    Result1 = run_query(Db1, 0, [{start_key, 11}, {end_key, 11}]),
-    Expect = {ok, [
-                {{12, 11, <<"11">>}, 11}
-    ]},
-    etap:is(Result1, Expect, "added key OK."),
-
-    %% delete doc
-    Doc2 = couch_doc:from_json_obj({[
-                {<<"_id">>, <<"11">>},
-                {<<"_rev">>, RevStr},
-                {<<"_deleted">>, true}
-    ]}),
-    {ok, _} = couch_db:update_doc(Db1, Doc2, []),
-    {ok, Db2} = couch_db:reopen(Db1),
-    Result2 = run_count_query(Db2, 0, []),
-    etap:is(Result2, 11, "removed key saved."),
-    %% check new view key
-    Result3 = run_query(Db2, 0, [{start_key, 11}, {end_key, 11}]),
-    Expect2 = {ok, [
-                {{13, 11, <<"11">>}, {[{<<"_removed">>, true}]}}
-    ]},
-    etap:is(Result3, Expect2, "removed key OK.").
-
-run_query(Db, Since, Opts) ->
-    Fun = fun(KV, Acc) -> {ok, [KV | Acc]} end,
-    {ok, R} = couch_mrview:view_changes_since(Db, <<"_design/bar">>, <<"baz">>,
-                                              Since, Fun, Opts, []),
-    {ok, lists:reverse(R)}.
-
-run_count_query(Db, Since, Opts) ->
-    couch_mrview:count_view_changes_since(Db, <<"_design/bar">>, <<"baz">>,
-                                          Since, Opts).

http://git-wip-us.apache.org/repos/asf/couchdb-couch-mrview/blob/d6771df7/test/09-index-events.t
----------------------------------------------------------------------
diff --git a/test/09-index-events.t b/test/09-index-events.t
deleted file mode 100644
index 26d904d..0000000
--- a/test/09-index-events.t
+++ /dev/null
@@ -1,73 +0,0 @@
-#!/usr/bin/env escript
-%% -*- erlang -*-
-
-% 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.
-
-main(_) ->
-    etap:plan(5),
-    case (catch test()) of
-        ok ->
-            etap:end_tests();
-        Other ->
-            etap:diag(io_lib:format("Test died abnormally: ~p", [Other])),
-            etap:bail(Other)
-    end,
-    timer:sleep(300),
-    ok.
-
-test() ->
-    test_util:start_couch(),
-    {ok, Db} = couch_mrview_test_util:init_db(<<"foo">>, changes),
-    test_info(Db),
-    test_update_event(Db),
-    test_delete_event(Db),
-    test_util:stop_couch(),
-    ok.
-
-test_info(Db) ->
-    {ok, Info} = couch_mrview:get_info(Db, <<"_design/bar">>),
-    etap:is(getval(update_options, Info), [<<"seq_indexed">>],
-            "update options OK"),
-    ok.
-
-test_update_event(Db) ->
-    {ok, Pid} = couch_index_event:start_link(self()),
-    etap:ok(is_pid(Pid), "event handler added"),
-    ok = couch_mrview:refresh(Db, <<"_design/bar">>),
-    Expect = {index_update, {<<"foo">>, <<"_design/bar">>,
-                             couch_mrview_index}},
-    receive
-        Event ->
-            etap:is(Event, Expect, "index update events OK")
-    end,
-    couch_index_event:stop(Pid).
-
-test_delete_event(Db) ->
-    ok = couch_mrview:refresh(Db, <<"_design/bar">>),
-    timer:sleep(300),
-    {ok, Pid} = couch_index_event:start_link(self()),
-    etap:ok(is_pid(Pid), "delete event handler added"),
-
-
-    catch couch_mrview_test_util:delete_db(<<"foo">>),
-    Expect = {index_delete, {<<"foo">>, <<"_design/bar">>,
-                             couch_mrview_index}},
-    receive
-        Event ->
-            etap:is(Event, Expect, "index delete events OK")
-    end,
-    couch_index_event:stop(Pid).
-
-getval(Key, PL) ->
-    {value, {Key, Val}} = lists:keysearch(Key, 1, PL),
-    Val.

http://git-wip-us.apache.org/repos/asf/couchdb-couch-mrview/blob/d6771df7/test/10-index-changes.t
----------------------------------------------------------------------
diff --git a/test/10-index-changes.t b/test/10-index-changes.t
deleted file mode 100644
index 0c5e924..0000000
--- a/test/10-index-changes.t
+++ /dev/null
@@ -1,216 +0,0 @@
-#!/usr/bin/env escript
-%% -*- erlang -*-
-
-% 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.
-
-main(_) ->
-    etap:plan(8),
-    case (catch test()) of
-        ok ->
-            etap:end_tests();
-        Other ->
-            etap:diag(io_lib:format("Test died abnormally: ~p", [Other])),
-            etap:bail(Other)
-    end,
-    timer:sleep(300),
-    ok.
-
-test() ->
-    test_util:start_couch(),
-    {ok, Db} = couch_mrview_test_util:init_db(<<"foo">>, changes),
-    test_normal_changes(Db),
-    test_stream_once(Db),
-    test_stream_once_since(Db),
-    test_stream_once_timeout(Db),
-    test_stream_once_heartbeat(Db),
-    test_stream(Db),
-    test_indexer(Db),
-    test_util:stop_couch(),
-    ok.
-
-test_normal_changes(Db) ->
-    Result = run_query(Db, []),
-    Expect = {ok, 11, [
-                {{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}
-    ]},
-    etap:is(Result, Expect, "normal changes worked.").
-
-test_stream_once(Db) ->
-    Result = run_query(Db, [{stream, once}]),
-    Expect = {ok, 11, [
-                {{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}
-    ]},
-    etap:is(Result, Expect, "stream once since 0 worked.").
-
-
-test_stream_once_since(Db) ->
-    Self = self(),
-    spawn(fun() ->
-                Result = run_query(Db, [{since, 11},
-                                        {stream, once}]),
-                Self ! {result, Result}
-        end),
-
-    spawn(fun() ->
-                timer:sleep(1000),
-                {ok, Db1} = save_doc(Db, 11),
-                couch_mrview:refresh(Db1, <<"_design/bar">>)
-        end),
-
-    Expect = {ok,12,[{{12,11,<<"11">>},11}]},
-
-    receive
-        {result, Result} ->
-            etap:is(Result, Expect, "normal changes worked.")
-    after 5000 ->
-            io:format("never got the change", [])
-    end.
-
-
-test_stream_once_timeout(Db) ->
-    Self = self(),
-    spawn(fun() ->
-                Result = run_query(Db, [{since, 12},
-                                        {stream, once},
-                                        {timeout, 3000}]),
-                Self ! {result, Result}
-        end),
-
-
-
-    Expect = {ok, 12, []},
-
-    receive
-        {result, Result} ->
-            etap:is(Result, Expect, "got timeout.")
-    after 5000 ->
-            io:format("never got the change", [])
-    end.
-
-test_stream_once_heartbeat(Db) ->
-    Self = self(),
-    spawn(fun() ->
-                Result = run_query(Db, [{since, 12},
-                                        {stream, once},
-                                        {heartbeat, 1000}]),
-                Self ! {result, Result}
-        end),
-
-    spawn(fun() ->
-                timer:sleep(3000),
-                {ok, Db1} = save_doc(Db, 12),
-                couch_mrview:refresh(Db1, <<"_design/bar">>)
-        end),
-
-    Expect = {ok,13,[heartbeat,
-                     heartbeat,
-                     heartbeat,
-                     {{13,12,<<"12">>},12}]},
-
-
-
-    receive
-        {result, Result} ->
-            etap:is(Result, Expect, "heartbeat OK.")
-    after 5000 ->
-            io:format("never got the change", [])
-    end.
-
-
-test_stream(Db) ->
-    Self = self(),
-    spawn(fun() ->
-                Result = run_query(Db, [{since, 13},
-                                        stream,
-                                        {timeout, 3000}]),
-                Self ! {result, Result}
-        end),
-
-    spawn(fun() ->
-                timer:sleep(1000),
-                {ok, Db1} = save_doc(Db, 13),
-                couch_mrview:refresh(Db1, <<"_design/bar">>),
-                {ok, Db2} = save_doc(Db1, 14),
-                couch_mrview:refresh(Db2, <<"_design/bar">>)
-        end),
-
-    Expect = {ok, 15,[{{14,13,<<"13">>},13},
-                     {{15,14,<<"14">>},14}]},
-
-    receive
-        {result, Result} ->
-            etap:is(Result, Expect, "stream OK.")
-    after 5000 ->
-            io:format("never got the change", [])
-    end.
-
-
-test_indexer(Db) ->
-    Result = run_query(Db, [{since, 14}, refresh]),
-    Expect = {ok, 15, [{{15,14,<<"14">>},14}]},
-    etap:is(Result, Expect, "refresh index by hand OK."),
-
-    {ok, Db1} = save_doc(Db, 15),
-    timer:sleep(1500),
-    Result1 = run_query(Db1, [{since, 14}], false),
-    Expect1 = {ok, 16, [{{15,14,<<"14">>},14},
-                       {{16,15,<<"15">>},15}]},
-    etap:is(Result1, Expect1, "changes indexed in background OK."),
-    ok.
-
-
-save_doc(Db, Id) ->
-    Doc = couch_mrview_test_util:doc(Id),
-    {ok, _Rev} = couch_db:update_doc(Db, Doc, []),
-    {ok, _} =  couch_db:ensure_full_commit(Db),
-    couch_db:reopen(Db).
-
-run_query(Db, Opts) ->
-    run_query(Db, Opts, true).
-
-run_query(Db, Opts, Refresh) ->
-    Fun = fun
-        (stop, {LastSeq, Acc}) ->
-            {ok, LastSeq, Acc};
-        (heartbeat, Acc) ->
-            {ok, [heartbeat | Acc]};
-        (Event, Acc) ->
-            {ok, [Event | Acc]}
-    end,
-    case Refresh of
-        true ->
-            couch_mrview:refresh(Db, <<"_design/bar">>);
-        false ->
-            ok
-    end,
-    {ok, LastSeq, R} = couch_mrview_changes:handle_changes(Db, <<"_design/bar">>,
-                                                  <<"baz">>, Fun, [], Opts),
-    {ok, LastSeq, lists:reverse(R)}.

http://git-wip-us.apache.org/repos/asf/couchdb-couch-mrview/blob/d6771df7/test/couch_mrview_changes_since_tests.erl
----------------------------------------------------------------------
diff --git a/test/couch_mrview_changes_since_tests.erl b/test/couch_mrview_changes_since_tests.erl
new file mode 100644
index 0000000..65f5a0b
--- /dev/null
+++ b/test/couch_mrview_changes_since_tests.erl
@@ -0,0 +1,166 @@
+% 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_changes_since_tests).
+
+-include_lib("couch/include/couch_eunit.hrl").
+-include_lib("couch/include/couch_db.hrl").
+
+-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_USER]),
+    ok.
+
+
+changes_since_test() ->
+    {
+        "changes_since tests",
+        {
+            setup,
+            fun test_util:start_couch/0, fun test_util:stop_couch/1,
+            {
+                foreach,
+                fun setup/0, fun teardown/1,
+                [
+                    test_basic/1,
+                    test_range/1,
+                    test_basic_since/1,
+                    test_range_since/1,
+                    test_basic_count/1,
+                    test_range_count/1,
+                    test_basic_count_since/1,
+                    test_range_count_since/1,
+                    test_compact/1,
+                    test_remove_key/1
+                ]
+            }
+        }
+    }.
+
+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}
+    ]},
+    ?_assertEqual(Result, Expect).
+
+
+test_range(Db) ->
+    Result = run_query(Db, 0, [{start_key, 3}, {end_key, 5}]),
+    Expect = {ok, [
+                {{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}
+    ]},
+    ?_assertEqual(Result, Expect).
+
+test_range_since(Db) ->
+    Result = run_query(Db, 5, [{start_key, 3}, {end_key, 5}]),
+    Expect = {ok, [
+                {{6, 4, <<"4">>}, 4},
+                {{7, 5, <<"5">>}, 5}
+    ]},
+    ?_assertEqual(Result, Expect).
+
+test_basic_count(Db) ->
+    Result = run_count_query(Db, 0, []),
+    ?_assertEqual(Result, 10).
+
+test_range_count(Db) ->
+    Result = run_count_query(Db, 0, [{start_key, 3}, {end_key, 5}]),
+    ?_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}]),
+    ?_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).
+
+test_remove_key(Db) ->
+    %% add new doc
+    Doc = couch_mrview_test_util:doc(11),
+    {ok, Rev} = couch_db:update_doc(Db, Doc, []),
+    RevStr = couch_doc:rev_to_str(Rev),
+    {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}]),
+    Expect = {ok, [
+                {{12, 11, <<"11">>}, 11}
+    ]},
+    ?_assertEqual(Result1, Expect),
+
+    %% delete doc
+    Doc2 = couch_doc:from_json_obj({[
+                {<<"_id">>, <<"11">>},
+                {<<"_rev">>, RevStr},
+                {<<"_deleted">>, true}
+    ]}),
+    {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}]),
+    Expect2 = {ok, [
+                {{13, 11, <<"11">>}, {[{<<"_removed">>, true}]}}
+    ]},
+    ?_assertEqual(Result3, Expect2).
+
+run_query(Db, Since, Opts) ->
+    Fun = fun(KV, Acc) -> {ok, [KV | Acc]} end,
+    {ok, R} = couch_mrview:view_changes_since(Db, <<"_design/bar">>, <<"baz">>,
+                                              Since, Fun, Opts, []),
+    {ok, lists:reverse(R)}.
+
+run_count_query(Db, Since, Opts) ->
+    couch_mrview:count_view_changes_since(Db, <<"_design/bar">>, <<"baz">>,
+                                          Since, Opts).

http://git-wip-us.apache.org/repos/asf/couchdb-couch-mrview/blob/d6771df7/test/couch_mrview_index_changes_tests.erl
----------------------------------------------------------------------
diff --git a/test/couch_mrview_index_changes_tests.erl b/test/couch_mrview_index_changes_tests.erl
new file mode 100644
index 0000000..4a3a35c
--- /dev/null
+++ b/test/couch_mrview_index_changes_tests.erl
@@ -0,0 +1,217 @@
+% 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_all_docs_tests).
+
+-include_lib("couch/include/couch_eunit.hrl").
+-include_lib("couch/include/couch_db.hrl").
+
+
+changes_index_test() ->
+    {
+        "changes index tests",
+        {
+            setup,
+            fun test_util:start_couch/0, fun test_util:stop_couch/1,
+            {
+                foreach,
+                fun setup/0, fun teardown/1,
+                [
+                    fun test_normal_changes/1,
+                    fun test_stream_once/1,
+                    fun test_stream_once_since/1,
+                    fun test_stream_once_timeout/1,
+                    fun test_stream_once_heartbeat/1,
+                    fun test_stream/1,
+                    fun test_indexer/1
+                ]
+            }
+        }
+    }.
+
+
+test_normal_changes(Db) ->
+    Result = run_query(Db, []),
+    Expect = {ok, 11, [
+                {{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_stream_once(Db) ->
+    Result = run_query(Db, [{stream, once}]),
+    Expect = {ok, 11, [
+                {{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_stream_once_since(Db) ->
+    Self = self(),
+    spawn(fun() ->
+                Result = run_query(Db, [{since, 11},
+                                        {stream, once}]),
+                Self ! {result, Result}
+        end),
+
+    spawn(fun() ->
+                timer:sleep(1000),
+                {ok, Db1} = save_doc(Db, 11),
+                couch_mrview:refresh(Db1, <<"_design/bar">>)
+        end),
+
+    Expect = {ok,12,[{{12,11,<<"11">>},11}]},
+
+    receive
+        {result, Result} ->
+            ?_assertEqual(Result, Expect)
+    after 5000 ->
+            io:format("never got the change", [])
+    end.
+
+
+test_stream_once_timeout(Db) ->
+    Self = self(),
+    spawn(fun() ->
+                Result = run_query(Db, [{since, 12},
+                                        {stream, once},
+                                        {timeout, 3000}]),
+                Self ! {result, Result}
+        end),
+
+
+
+    Expect = {ok, 12, []},
+
+    receive
+        {result, Result} ->
+            ?_assertEqual(Result, Expect)
+    after 5000 ->
+            io:format("never got the change", [])
+    end.
+
+test_stream_once_heartbeat(Db) ->
+    Self = self(),
+    spawn(fun() ->
+                Result = run_query(Db, [{since, 12},
+                                        {stream, once},
+                                        {heartbeat, 1000}]),
+                Self ! {result, Result}
+        end),
+
+    spawn(fun() ->
+                timer:sleep(3000),
+                {ok, Db1} = save_doc(Db, 12),
+                couch_mrview:refresh(Db1, <<"_design/bar">>)
+        end),
+
+    Expect = {ok,13,[heartbeat,
+                     heartbeat,
+                     heartbeat,
+                     {{13,12,<<"12">>},12}]},
+
+
+
+    receive
+        {result, Result} ->
+            ?_assertEqual(Result, Expect)
+    after 5000 ->
+            io:format("never got the change", [])
+    end.
+
+
+test_stream(Db) ->
+    Self = self(),
+    spawn(fun() ->
+                Result = run_query(Db, [{since, 13},
+                                        stream,
+                                        {timeout, 3000}]),
+                Self ! {result, Result}
+        end),
+
+    spawn(fun() ->
+                timer:sleep(1000),
+                {ok, Db1} = save_doc(Db, 13),
+                couch_mrview:refresh(Db1, <<"_design/bar">>),
+                {ok, Db2} = save_doc(Db1, 14),
+                couch_mrview:refresh(Db2, <<"_design/bar">>)
+        end),
+
+    Expect = {ok, 15,[{{14,13,<<"13">>},13},
+                     {{15,14,<<"14">>},14}]},
+
+    receive
+        {result, Result} ->
+            ?_assertEqual(Result, Expect)
+    after 5000 ->
+            io:format("never got the change", [])
+    end.
+
+
+test_indexer(Db) ->
+    Result = run_query(Db, [{since, 14}, refresh]),
+    Expect = {ok, 15, [{{15,14,<<"14">>},14}]},
+    ?_assertEqual(Result, Expect),
+
+    {ok, Db1} = save_doc(Db, 15),
+    timer:sleep(1500),
+    Result1 = run_query(Db1, [{since, 14}], false),
+    Expect1 = {ok, 16, [{{15,14,<<"14">>},14},
+                       {{16,15,<<"15">>},15}]},
+    ?_assertEqual(Result1, Expect1),
+    ok.
+
+
+save_doc(Db, Id) ->
+    Doc = couch_mrview_test_util:doc(Id),
+    {ok, _Rev} = couch_db:update_doc(Db, Doc, []),
+    {ok, _} =  couch_db:ensure_full_commit(Db),
+    couch_db:reopen(Db).
+
+run_query(Db, Opts) ->
+    run_query(Db, Opts, true).
+
+run_query(Db, Opts, Refresh) ->
+    Fun = fun
+        (stop, {LastSeq, Acc}) ->
+            {ok, LastSeq, Acc};
+        (heartbeat, Acc) ->
+            {ok, [heartbeat | Acc]};
+        (Event, Acc) ->
+            {ok, [Event | Acc]}
+    end,
+    case Refresh of
+        true ->
+            couch_mrview:refresh(Db, <<"_design/bar">>);
+        false ->
+            ok
+    end,
+    {ok, LastSeq, R} = couch_mrview_changes:handle_changes(Db, <<"_design/bar">>,
+                                                  <<"baz">>, Fun, [], Opts),
+    {ok, LastSeq, lists:reverse(R)}.


Mime
View raw message