summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Avdey <eiri@eiri.ca>2017-03-21 19:37:08 -0300
committerJay Doane <jay.s.doane@gmail.com>2017-04-24 11:53:57 -0700
commit64573fc2a3ddeded169a74039fee3b2bd168f694 (patch)
tree310c8022d11762e5b974c2ae995a9ee8d67fd181
parent9c5dc071084c9fc89b3e242f0b58aef413f9920f (diff)
downloadcouchdb-64573fc2a3ddeded169a74039fee3b2bd168f694.tar.gz
Add utests for _local_docs
-rw-r--r--src/couch_mrview/src/couch_mrview_test_util.erl16
-rw-r--r--src/couch_mrview/test/couch_mrview_local_docs_tests.erl132
2 files changed, 146 insertions, 2 deletions
diff --git a/src/couch_mrview/src/couch_mrview_test_util.erl b/src/couch_mrview/src/couch_mrview_test_util.erl
index 8cebd6dfa..2e0cb794e 100644
--- a/src/couch_mrview/src/couch_mrview_test_util.erl
+++ b/src/couch_mrview/src/couch_mrview_test_util.erl
@@ -24,10 +24,13 @@ init_db(Name, Type) ->
init_db(Name, Type, Count) ->
{ok, Db} = new_db(Name, Type),
- Docs = make_docs(Count),
+ Docs = make_docs(Type, Count),
save_docs(Db, Docs).
+new_db(Name, local) ->
+ couch_server:delete(Name, [?ADMIN_CTX]),
+ couch_db:create(Name, [?ADMIN_CTX]);
new_db(Name, Type) ->
couch_server:delete(Name, [?ADMIN_CTX]),
{ok, Db} = couch_db:create(Name, [?ADMIN_CTX]),
@@ -41,7 +44,9 @@ save_docs(Db, Docs) ->
couch_db:reopen(Db).
-make_docs(Count) ->
+make_docs(local, Count) ->
+ [local_doc(I) || I <- lists:seq(1, Count)];
+make_docs(_, Count) ->
[doc(I) || I <- lists:seq(1, Count)].
ddoc(changes) ->
@@ -117,3 +122,10 @@ doc(Id) ->
{<<"_id">>, list_to_binary(integer_to_list(Id))},
{<<"val">>, Id}
]}).
+
+
+local_doc(Id) ->
+ couch_doc:from_json_obj({[
+ {<<"_id">>, list_to_binary(io_lib:format("_local/~b", [Id]))},
+ {<<"val">>, Id}
+ ]}).
diff --git a/src/couch_mrview/test/couch_mrview_local_docs_tests.erl b/src/couch_mrview/test/couch_mrview_local_docs_tests.erl
new file mode 100644
index 000000000..c16f53c62
--- /dev/null
+++ b/src/couch_mrview/test/couch_mrview_local_docs_tests.erl
@@ -0,0 +1,132 @@
+% 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_local_docs_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(), local),
+ Db.
+
+teardown(Db) ->
+ couch_db:close(Db),
+ couch_server:delete(Db#db.name, [?ADMIN_CTX]),
+ ok.
+
+
+all_docs_test_() ->
+ {
+ "_local_docs view tests",
+ {
+ setup,
+ fun test_util:start_couch/0, fun test_util:stop_couch/1,
+ {
+ foreach,
+ fun setup/0, fun teardown/1,
+ [
+ fun should_query/1,
+ fun should_query_with_range/1,
+ fun should_query_with_range_rev/1,
+ fun should_query_with_limit_and_skip/1,
+ fun should_query_with_include_docs/1
+ ]
+ }
+ }
+ }.
+
+
+should_query(Db) ->
+ Result = run_query(Db, []),
+ Expect = {ok, [
+ {meta, [{total, 10}, {offset, 0}]},
+ mk_row(1),
+ mk_row(10),
+ mk_row(2),
+ mk_row(3),
+ mk_row(4),
+ mk_row(5),
+ mk_row(6),
+ mk_row(7),
+ mk_row(8),
+ mk_row(9)
+ ]},
+ ?_assertEqual(Expect, Result).
+
+should_query_with_range(Db) ->
+ Result = run_query(Db, [
+ {start_key, <<"_local/3">>},
+ {end_key, <<"_local/5">>}
+ ]),
+ Expect = {ok, [
+ {meta, [{total, 10}, {offset, 3}]},
+ mk_row(3),
+ mk_row(4),
+ mk_row(5)
+ ]},
+ ?_assertEqual(Expect, Result).
+
+should_query_with_range_rev(Db) ->
+ Result = run_query(Db, [
+ {direction, rev},
+ {start_key, <<"_local/5">>}, {end_key, <<"_local/3">>},
+ {inclusive_end, true}
+ ]),
+ Expect = {ok, [
+ {meta, [{total, 10}, {offset, 4}]},
+ mk_row(5),
+ mk_row(4),
+ mk_row(3)
+ ]},
+ ?_assertEqual(Expect, Result).
+
+should_query_with_limit_and_skip(Db) ->
+ Result = run_query(Db, [
+ {start_key, <<"_local/2">>},
+ {limit, 3},
+ {skip, 3}
+ ]),
+ Expect = {ok, [
+ {meta, [{total, 10}, {offset, 5}]},
+ mk_row(5),
+ mk_row(6),
+ mk_row(7)
+ ]},
+ ?_assertEqual(Expect, Result).
+
+should_query_with_include_docs(Db) ->
+ Result = run_query(Db, [
+ {start_key, <<"_local/8">>},
+ {end_key, <<"_local/8">>},
+ {include_docs, true}
+ ]),
+ {row, Doc0} = mk_row(8),
+ Doc = Doc0 ++ [{doc, {[{<<"val">>, 8}]}}],
+ Expect = {ok, [
+ {meta, [{total, 10}, {offset, 8}]},
+ {row, Doc}
+ ]},
+ ?_assertEqual(Expect, Result).
+
+
+mk_row(IntId) ->
+ Id = list_to_binary(io_lib:format("_local/~b", [IntId])),
+ {row, [{id, Id}, {key, Id}, {value, {[{rev, <<"0-1">>}]}}]}.
+
+run_query(Db, Opts0) ->
+ Opts = [{extra, [{namespace, <<"_local">>}]} | Opts0],
+ couch_mrview:query_all_docs(Db, Opts).