You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@couchdb.apache.org by GitBox <gi...@apache.org> on 2022/10/17 17:26:31 UTC

[GitHub] [couchdb] nickva commented on a diff in pull request #4209: Add bulk_get tests

nickva commented on code in PR #4209:
URL: https://github.com/apache/couchdb/pull/4209#discussion_r997330197


##########
src/chttpd/test/eunit/chttpd_bulk_get_test.erl:
##########
@@ -0,0 +1,785 @@
+% 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(chttpd_bulk_get_test).
+
+-include_lib("couch/include/couch_db.hrl").
+-include_lib("couch/include/couch_eunit.hrl").
+
+-define(USER, "chttpd_bulk_get_test_admin").
+-define(PASS, "pass").
+-define(AUTH, {basic_auth, {?USER, ?PASS}}).
+-define(JSON, {"Content-Type", "application/json"}).
+
+-define(DOC, <<"doc">>).
+-define(REVA, <<"reva">>).
+-define(REVB, <<"revb">>).
+-define(REVC, <<"revc">>).
+-define(ATT, <<"att">>).
+-define(ATT_DATA, <<"dGhlZGF0YQ==">>).
+
+-define(DOC_COUNT, 2000).
+
+test_docs_revs() ->
+    [
+        {?DOC, [?REVA]},
+        {?DOC, [?REVB, ?REVA]},
+        {?DOC, [?REVC, ?REVA]}
+    ].
+
+bulk_get_test_() ->
+    {
+        setup,
+        fun setup_basic/0,
+        fun teardown/1,
+        with([
+            ?TDEF(t_empty_request),
+            ?TDEF(t_no_docs),
+            ?TDEF(t_invalid_doc),
+            ?TDEF(t_doc_no_id),
+            ?TDEF(t_missing_doc),
+            ?TDEF(t_invalid_rev),
+            ?TDEF(t_missing_rev),
+            ?TDEF(t_doc_all_revs),
+            ?TDEF(t_specific_rev),
+            ?TDEF(t_specific_rev_latest),
+            ?TDEF(t_ancestor_rev_latest),
+            ?TDEF(t_revs_true),
+            ?TDEF(t_attachments_true),
+            ?TDEF(t_atts_since),
+            ?TDEF(t_atts_since_returns_attachment),
+            ?TDEF(t_atts_since_attachments_true),
+            ?TDEF(t_atts_since_multiple),
+            ?TDEF(t_atts_since_multiple_attachments_true),
+            ?TDEF(t_missing_rev_latest)
+        ])
+    }.
+
+bulk_get_multiple_docs_test_() ->
+    {
+        foreach,
+        fun setup_multiple/0,
+        fun teardown/1,
+        [
+            ?TDEF_FE(t_multiple_docs, 10)
+        ]
+    }.
+
+t_empty_request({_, DbUrl}) ->
+    {Code, Res} = bulk_get(DbUrl, []),
+    ?assertEqual(200, Code),
+    ?assertEqual([], Res).
+
+t_no_docs({_, DbUrl}) ->
+    {Code, #{}} = req(post, DbUrl ++ "/_bulk_get", #{}),
+    ?assertEqual(400, Code).
+
+t_invalid_doc({_, DbUrl}) ->
+    {Code, Res} = bulk_get(DbUrl, [<<"foo">>]),
+    ?assertEqual(200, Code),
+    ?assertMatch(
+        [
+            #{
+                <<"docs">> := [
+                    #{
+                        <<"error">> := #{
+                            <<"id">> := null,
+                            <<"rev">> := null,
+                            <<"error">> := <<"bad_request">>
+                        }
+                    }
+                ],
+                <<"id">> := null
+            }
+        ],
+        Res
+    ).
+
+t_doc_no_id({_, DbUrl}) ->
+    {Code, Res} = bulk_get(DbUrl, [#{<<"rev">> => <<"1-foo">>}]),
+    ?assertEqual(200, Code),
+    ?assertMatch(
+        [
+            #{
+                <<"docs">> := [
+                    #{
+                        <<"error">> := #{
+                            <<"id">> := null,
+                            <<"rev">> := null,
+                            <<"error">> := <<"bad_request">>
+                        }
+                    }
+                ],
+                <<"id">> := null
+            }
+        ],
+        Res
+    ).
+
+t_missing_doc({_, DbUrl}) ->
+    {Code, Res} = bulk_get(DbUrl, [#{<<"id">> => <<"missing">>}]),
+    ?assertEqual(200, Code),
+    ?assertMatch(
+        [
+            #{
+                <<"docs">> := [
+                    #{
+                        <<"error">> := #{
+                            <<"error">> := <<"not_found">>,
+                            <<"id">> := <<"missing">>,
+                            <<"rev">> := <<"undefined">>
+                        }
+                    }
+                ],
+                <<"id">> := <<"missing">>
+            }
+        ],
+        Res
+    ).
+
+t_invalid_rev({_, DbUrl}) ->
+    Doc = #{<<"id">> => ?DOC, <<"rev">> => 42},
+    {Code, Res} = bulk_get(DbUrl, [Doc]),
+    ?assertEqual(200, Code),
+    ?assertMatch(
+        [
+            #{
+                <<"docs">> := [
+                    #{
+                        <<"error">> := #{
+                            <<"error">> := <<"bad_request">>,
+                            <<"id">> := ?DOC,
+                            <<"rev">> := 42
+                        }
+                    }
+                ],
+                <<"id">> := ?DOC
+            }
+        ],
+        Res
+    ).
+
+t_missing_rev({_, DbUrl}) ->
+    Doc = #{<<"id">> => ?DOC, <<"rev">> => <<"1-x">>},
+    {Code, Res} = bulk_get(DbUrl, [Doc]),
+    ?assertEqual(200, Code),
+    ?assertMatch(
+        [
+            #{
+                <<"docs">> := [
+                    #{
+                        <<"error">> := #{
+                            <<"error">> := <<"not_found">>,
+                            <<"id">> := ?DOC,
+                            <<"rev">> := <<"1-x">>
+                        }
+                    }
+                ],
+                <<"id">> := ?DOC
+            }
+        ],
+        Res
+    ).
+
+t_doc_all_revs({_, DbUrl}) ->
+    {Code, Res} = bulk_get(DbUrl, [#{<<"id">> => ?DOC}]),
+    ?assertEqual(200, Code),
+    ?assertMatch(
+        [
+            #{
+                <<"docs">> := [
+                    #{
+                        <<"ok">> := #{
+                            <<"_id">> := ?DOC,
+                            <<"_rev">> := <<"2-revb">>,
+                            <<"_attachments">> := #{
+                                ?ATT := #{<<"stub">> := true}
+                            }
+                        }
+                    },
+                    #{
+                        <<"ok">> := #{
+                            <<"_id">> := ?DOC,
+                            <<"_rev">> := <<"2-revc">>,
+                            <<"_attachments">> := #{
+                                ?ATT := #{<<"stub">> := true}
+                            }
+                        }
+                    }
+                ],
+                <<"id">> := ?DOC
+            }
+        ],
+        Res
+    ).
+
+t_specific_rev({_, DbUrl}) ->
+    Doc = #{<<"id">> => ?DOC, <<"rev">> => <<"2-revb">>},
+    {Code, Res} = bulk_get(DbUrl, [Doc]),
+    ?assertEqual(200, Code),
+    ?assertMatch(
+        [
+            #{
+                <<"docs">> := [
+                    #{
+                        <<"ok">> := #{
+                            <<"_id">> := ?DOC,
+                            <<"_rev">> := <<"2-revb">>,
+                            <<"_attachments">> := #{
+                                ?ATT := #{<<"stub">> := true}
+                            }
+                        }
+                    }
+                ],
+                <<"id">> := ?DOC
+            }
+        ],
+        Res
+    ).
+
+t_specific_rev_latest({_, DbUrl}) ->
+    Doc = #{<<"id">> => ?DOC, <<"rev">> => <<"2-revb">>},
+    {Code, Res} = bulk_get(DbUrl, [Doc], "?latest=true"),
+    ?assertEqual(200, Code),
+    ?assertMatch(
+        [
+            #{
+                <<"docs">> := [
+                    #{
+                        <<"ok">> := #{
+                            <<"_id">> := ?DOC,
+                            <<"_rev">> := <<"2-revb">>,
+                            <<"_attachments">> := #{
+                                ?ATT := #{<<"stub">> := true}
+                            }
+                        }
+                    }
+                ],
+                <<"id">> := ?DOC
+            }
+        ],
+        Res
+    ).
+
+t_ancestor_rev_latest({_, DbUrl}) ->
+    Doc = #{<<"id">> => ?DOC, <<"rev">> => <<"1-reva">>},
+    {Code, Res} = bulk_get(DbUrl, [Doc], "?latest=true"),

Review Comment:
   Good idea to document it. We document it for doc GETs https://docs.couchdb.org/en/3.2.2-docs/api/document/common.html#get--db-docid and we cheat for the _bulk_get but allowing passing most of the same option.
   
   `latest` as implemented today means, given a particular revision, we don't want to return that revision only but all the latest descendants (leafs) of that revision.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@couchdb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org