couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kocol...@apache.org
Subject [24/50] couch commit: updated refs/heads/2971-count-distinct to ee32cd5
Date Wed, 01 Mar 2017 16:39:11 GMT
Add tests for filter by filter function


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

Branch: refs/heads/2971-count-distinct
Commit: 87b01e84e7e9d884a0f020a11fe530035be17614
Parents: 3eeda46
Author: Eric Avdey <eiri@eiri.ca>
Authored: Fri Nov 11 20:50:37 2016 -0400
Committer: Eric Avdey <eiri@eiri.ca>
Committed: Tue Dec 13 10:05:59 2016 -0400

----------------------------------------------------------------------
 test/couch_changes_tests.erl | 68 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 68 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-couch/blob/87b01e84/test/couch_changes_tests.erl
----------------------------------------------------------------------
diff --git a/test/couch_changes_tests.erl b/test/couch_changes_tests.erl
index fa64e9e..3c0e5f6 100644
--- a/test/couch_changes_tests.erl
+++ b/test/couch_changes_tests.erl
@@ -66,6 +66,7 @@ changes_test_() ->
                 filter_by_design(),
                 continuous_feed(),
                 %%filter_by_custom_function()
+                filter_by_filter_function(),
                 filter_by_view()
             ]
         }
@@ -131,6 +132,19 @@ filter_by_custom_function() ->
         }
     }.
 
+filter_by_filter_function() ->
+    {
+        "Filter by filters",
+        {
+            foreach,
+            fun setup/0, fun teardown/1,
+            [
+                fun should_filter_by_doc_attribute/1,
+                fun should_filter_by_user_ctx/1
+            ]
+        }
+    }.
+
 filter_by_view() ->
     {
         "Filter _view",
@@ -590,6 +604,60 @@ should_receive_heartbeats(_) ->
              ?assert(Heartbeats3 > Heartbeats2)
         end)}.
 
+should_filter_by_doc_attribute({DbName, _}) ->
+    ?_test(
+        begin
+            DDocId = <<"_design/app">>,
+            DDoc = couch_doc:from_json_obj({[
+                {<<"_id">>, DDocId},
+                {<<"language">>, <<"javascript">>},
+                {<<"filters">>, {[
+                    {<<"valid">>, <<"function(doc, req) {"
+                    " if (doc._id == 'doc3') {"
+                        " return true; "
+                    "} }">>}
+                ]}}
+            ]}),
+            ChArgs = #changes_args{filter = "app/valid"},
+            Req = {json_req, null},
+            ok = update_ddoc(DbName, DDoc),
+            {Rows, LastSeq, UpSeq} = run_changes_query(DbName, ChArgs, Req),
+            ?assertEqual(1, length(Rows)),
+            [#row{seq = Seq, id = Id}] = Rows,
+            ?assertEqual(<<"doc3">>, Id),
+            ?assertEqual(6, Seq),
+            ?assertEqual(UpSeq, LastSeq)
+        end).
+
+should_filter_by_user_ctx({DbName, _}) ->
+    ?_test(
+        begin
+            DDocId = <<"_design/app">>,
+            DDoc = couch_doc:from_json_obj({[
+                {<<"_id">>, DDocId},
+                {<<"language">>, <<"javascript">>},
+                {<<"filters">>, {[
+                    {<<"valid">>, <<"function(doc, req) {"
+                    " if (req.userCtx.name == doc._id) {"
+                        " return true; "
+                    "} }">>}
+                ]}}
+            ]}),
+            ChArgs = #changes_args{filter = "app/valid"},
+            UserCtx = #user_ctx{name = <<"doc3">>, roles = []},
+            DbRec = #db{name = DbName, user_ctx = UserCtx},
+            Req = {json_req, {[{
+                <<"userCtx">>, couch_util:json_user_ctx(DbRec)
+            }]}},
+            ok = update_ddoc(DbName, DDoc),
+            {Rows, LastSeq, UpSeq} = run_changes_query(DbName, ChArgs, Req),
+            ?assertEqual(1, length(Rows)),
+            [#row{seq = Seq, id = Id}] = Rows,
+            ?assertEqual(<<"doc3">>, Id),
+            ?assertEqual(6, Seq),
+            ?assertEqual(UpSeq, LastSeq)
+        end).
+
 should_filter_by_view({DbName, _}) ->
     ?_test(
         begin


Mime
View raw message