jiahuili430 commented on code in PR #4703:
URL: https://github.com/apache/couchdb/pull/4703#discussion_r1313535277


##########
src/chttpd/test/eunit/chttpd_purge_tests.erl:
##########
@@ -205,287 +110,228 @@ test_accepted_purge_request(Url) ->
                 {accepted, [
                     {accepted, [
                         {1,
-                            <<57, 27, 64, 134, 152, 18, 73, 243, 40, 1, 141, 
214, 135, 104, 79,
-                                188>>}
+                            <<187, 82, 160, 135, 14, 97, 52, 47, 28, 172, 13, 
249, 96, 182, 127,
+                                97>>}
                     ]}
                 ]}
             end
         ),
-        {ok, Status, _, ResultBody} = test_request:post(
-            Url ++ "/_purge/",
-            [?CONTENT_JSON, ?AUTH],
-            IdsRevs
-        ),
-        ResultJson = ?JSON_DECODE(ResultBody),
-        meck:unload(fabric),
+        {_, IdsRevs} = get_id_rev_map(Url, "doc1"),
+        {Status, Response} = req(post, url(Url, "_purge"), IdsRevs),
         ?assert(Status =:= 202),
-        ?assertEqual(
-            {[
-                {<<"purge_seq">>, null},
-                {<<"purged">>,
-                    {[
-                        {<<"doc1">>, [Rev1]}
-                    ]}}
-            ]},
-            ResultJson
-        )
-    end).
-
-test_partial_purge_request(Url) ->
-    ?_test(begin
-        {ok, _, _, Body} = create_doc(Url, "doc1"),
-        {Json} = ?JSON_DECODE(Body),
-        Rev1 = couch_util:get_value(<<"rev">>, Json, undefined),
-
-        NewDoc =
-            "{\"new_edits\": false, \"docs\": [{\"_id\": \"doc1\",\n"
-            "            \"_revisions\": {\"start\": 1, \"ids\": [\"12345\", 
\"67890\"]},\n"
-            "            \"content\": \"updated\", \"_rev\": \"" ++ ?b2l(Rev1) 
++ "\"}]}",
-        {ok, _, _, _} = test_request:post(
-            Url ++ "/_bulk_docs/",
-            [?CONTENT_JSON, ?AUTH],
-            NewDoc
-        ),
+        ?assertMatch(#{<<"purge_seq">> := null, <<"purged">> := IdsRevs}, 
Response)
+    after
+        meck:unload(fabric)
+    end.
+
+t_partial_purge_request(Url) ->
+    IdsRevs = create_and_update_doc(Url, "doc1"),
+    {Status1, Response1} = req(post, url(Url, "_purge"), IdsRevs),
+    ?assert(Status1 =:= 201 orelse Status1 =:= 202),
+    ?assertMatch(#{<<"purge_seq">> := null, <<"purged">> := IdsRevs}, 
Response1),
+    {Status2, #{<<"content">> := Content}} = req(get, url(Url, "doc1")),
+    ?assertEqual(<<"updated">>, Content),
+    ?assert(Status2 =:= 200).
+
+t_mixed_purge_request(Url) ->
+    Doc1IdRevs = create_and_update_doc(Url, "doc1"),
+    [Rev1] = maps:get(?l2b("doc1"), Doc1IdRevs),
+    get_id_rev_map(Url, "doc2"),
+    {Rev3, _} = get_id_rev_map(Url, "doc3"),
+
+    IdsRevs = #{
+        % partial purge
+        <<"doc1">> => [Rev1],
+        % correct format, but invalid rev
+        <<"doc2">> => [Rev1, Rev3],
+        % correct format and rev
+        <<"doc3">> => [Rev3]
+    },
+
+    {Status1, Response} = req(post, url(Url, "_purge"), IdsRevs),
+    ?assert(Status1 =:= 201 orelse Status1 =:= 202),
+    ?assertMatch(
+        #{
+            <<"purge_seq">> := null,
+            <<"purged">> := #{<<"doc1">> := [Rev1], <<"doc2">> := [], 
<<"doc3">> := [Rev3]}
+        },
+        Response
+    ),
+
+    {Status2, #{<<"content">> := Content}} = req(get, url(Url, "doc1")),
+    ?assertEqual(<<"updated">>, Content),
+    ?assert(Status2 =:= 200).
+
+t_over_many_ids_or_revs_purge_request(Url) ->
+    Doc1IdRevs = create_and_update_doc(Url, "doc1"),
+    [Rev1] = maps:get(?l2b("doc1"), Doc1IdRevs),
+    get_id_rev_map(Url, "doc2"),
+    {Rev3, _} = get_id_rev_map(Url, "doc3"),
+
+    IdsRevs = #{
+        % partial purge
+        <<"doc1">> => [Rev1],
+        % correct format, but invalid rev
+        <<"doc2">> => [Rev1, Rev3],
+        % correct format and rev
+        <<"doc3">> => [Rev3]
+    },
+
+    % Ids larger than expected
+    config:set("purge", "max_document_id_number", "1", _Persist = false),
+    try
+        {Status1, #{<<"reason">> := Error1}} = req(post, url(Url, "_purge"), 
IdsRevs),
+        ?assertEqual(400, Status1),
+        ?assertEqual(<<"Exceeded maximum number of documents.">>, Error1)
+    after
+        config:delete("purge", "max_document_id_number", _Persist)
+    end,
+
+    % Revs larger than expected
+    config:set("purge", "max_revisions_number", "1", _Persist),
+    try
+        {Status2, #{<<"reason">> := Error2}} = req(post, url(Url, "_purge"), 
IdsRevs),
+        ?assertEqual(400, Status2),
+        ?assertEqual(<<"Exceeded maximum number of revisions.">>, Error2)
+    after
+        config:delete("purge", "max_revisions_number", _Persist)
+    end.
 
-        IdsRevsEJson = {[{<<"doc1">>, [Rev1]}]},
-        IdsRevs = binary_to_list(?JSON_ENCODE(IdsRevsEJson)),
-        {ok, Status, _, ResultBody} = test_request:post(
-            Url ++ "/_purge/",
-            [?CONTENT_JSON, ?AUTH],
-            IdsRevs
-        ),
-        ResultJson = ?JSON_DECODE(ResultBody),
-        ?assert(Status =:= 201 orelse Status =:= 202),
-        ?assertEqual(
-            {[
-                {<<"purge_seq">>, null},
-                {<<"purged">>,
-                    {[
-                        {<<"doc1">>, [Rev1]}
-                    ]}}
-            ]},
-            ResultJson
-        ),
-        {ok, Status2, _, ResultBody2} = test_request:get(
-            Url ++
-                "/doc1/",
-            [?AUTH]
-        ),
-        {Json2} = ?JSON_DECODE(ResultBody2),
-        Content = couch_util:get_value(<<"content">>, Json2, undefined),
-        ?assertEqual(<<"updated">>, Content),
-        ?assert(Status2 =:= 200)
-    end).
-
-test_mixed_purge_request(Url) ->
-    ?_test(begin
-        {ok, _, _, Body} = create_doc(Url, "doc1"),
-        {Json} = ?JSON_DECODE(Body),
-        Rev1 = couch_util:get_value(<<"rev">>, Json, undefined),
-
-        NewDoc =
-            "{\"new_edits\": false, \"docs\": [{\"_id\": \"doc1\",\n"
-            "            \"_revisions\": {\"start\": 1, \"ids\": [\"12345\", 
\"67890\"]},\n"
-            "            \"content\": \"updated\", \"_rev\": \"" ++ ?b2l(Rev1) 
++ "\"}]}",
-        {ok, _, _, _} = test_request:post(
-            Url ++ "/_bulk_docs/",
-            [?CONTENT_JSON, ?AUTH],
-            NewDoc
-        ),
+t_purged_infos_limit_only_get_put_allowed(Url) ->
+    {Status, Response} = req(post, url(Url, "_purged_infos_limit"), "2"),
+    ?assert(Status =:= 405),
+    ?assertMatch(#{<<"reason">> := <<"Only GET,PUT allowed">>}, Response).
 
-        {ok, _, _, _Body2} = create_doc(Url, "doc2", "content2"),
-        {ok, _, _, Body3} = create_doc(Url, "doc3", "content3"),
-        {Json3} = ?JSON_DECODE(Body3),
-        Rev3 = couch_util:get_value(<<"rev">>, Json3, undefined),
-
-        IdsRevsEJson =
-            {[
-                % partial purge
-                {<<"doc1">>, [Rev1]},
-                % correct format, but invalid rev
-                {<<"doc2">>, [Rev3, Rev1]},
-                % correct format and rev
-                {<<"doc3">>, [Rev3]}
-            ]},
-        IdsRevs = binary_to_list(?JSON_ENCODE(IdsRevsEJson)),
-        {ok, Status, _, Body4} = test_request:post(
-            Url ++ "/_purge/",
-            [?CONTENT_JSON, ?AUTH],
-            IdsRevs
-        ),
-        ResultJson = ?JSON_DECODE(Body4),
-        ?assert(Status =:= 201 orelse Status =:= 202),
-        ?assertEqual(
-            {[
-                {<<"purge_seq">>, null},
-                {<<"purged">>,
-                    {[
-                        {<<"doc1">>, [Rev1]},
-                        {<<"doc2">>, []},
-                        {<<"doc3">>, [Rev3]}
-                    ]}}
-            ]},
-            ResultJson
-        ),
-        {ok, Status2, _, Body5} = test_request:get(
-            Url ++
-                "/doc1/",
-            [?AUTH]
-        ),
-        {Json5} = ?JSON_DECODE(Body5),
-        Content = couch_util:get_value(<<"content">>, Json5, undefined),
-        ?assertEqual(<<"updated">>, Content),
-        ?assert(Status2 =:= 200)
-    end).
-
-test_overmany_ids_or_revs_purge_request(Url) ->
-    ?_test(begin
-        {ok, _, _, Body} = create_doc(Url, "doc1"),
-        {Json} = ?JSON_DECODE(Body),
-        Rev1 = couch_util:get_value(<<"rev">>, Json, undefined),
-
-        NewDoc =
-            "{\"new_edits\": false, \"docs\": [{\"_id\": \"doc1\",\n"
-            "            \"_revisions\": {\"start\": 1, \"ids\": [\"12345\", 
\"67890\"]},\n"
-            "            \"content\": \"updated\", \"_rev\": \"" ++ ?b2l(Rev1) 
++ "\"}]}",
-        {ok, _, _, _} = test_request:post(
-            Url ++ "/_bulk_docs/",
-            [?CONTENT_JSON, ?AUTH],
-            NewDoc
-        ),
+t_exceed_limits_on_purge_infos(Url) ->
+    {Status1, _} = req(put, url(Url, "_purged_infos_limit"), "2"),
+    ?assert(Status1 =:= 200),
 
-        {ok, _, _, _Body2} = create_doc(Url, "doc2", "content2"),
-        {ok, _, _, Body3} = create_doc(Url, "doc3", "content3"),
-        {Json3} = ?JSON_DECODE(Body3),
-        Rev3 = couch_util:get_value(<<"rev">>, Json3, undefined),
-
-        IdsRevsEJson =
-            {[
-                % partial purge
-                {<<"doc1">>, [Rev1]},
-                % correct format, but invalid rev
-                {<<"doc2">>, [Rev3, Rev1]},
-                % correct format and rev
-                {<<"doc3">>, [Rev3]}
-            ]},
-        IdsRevs = binary_to_list(?JSON_ENCODE(IdsRevsEJson)),
-
-        % Ids larger than expected
-        config:set("purge", "max_document_id_number", "1"),
-        {ok, Status, _, Body4} = test_request:post(
-            Url ++ "/_purge/",
-            [?CONTENT_JSON, ?AUTH],
-            IdsRevs
-        ),
-        config:delete("purge", "max_document_id_number"),
-        ResultJson = ?JSON_DECODE(Body4),
-        ?assertEqual(400, Status),
-        ?assertMatch(
-            {[
-                {<<"error">>, <<"bad_request">>},
-                {<<"reason">>, <<"Exceeded maximum number of documents.">>}
-            ]},
-            ResultJson
-        ),
+    {201, Response1} = create_docs(Url, docs(3)),
+    IdsRevs = ids_revs(Response1),
 
-        % Revs larger than expected
-        config:set("purge", "max_revisions_number", "1"),
-        {ok, Status2, _, Body5} = test_request:post(
-            Url ++ "/_purge/",
-            [?CONTENT_JSON, ?AUTH],
-            IdsRevs
-        ),
-        config:delete("purge", "max_revisions_number"),
-        ResultJson2 = ?JSON_DECODE(Body5),
-        ?assertEqual(400, Status2),
-        ?assertMatch(
-            {[
-                {<<"error">>, <<"bad_request">>},
-                {<<"reason">>, <<"Exceeded maximum number of revisions.">>}
-            ]},
-            ResultJson2
-        )
-    end).
-
-test_exceed_limits_on_purge_infos(Url) ->
-    ?_test(begin
-        {ok, Status1, _, _} = test_request:put(
-            Url ++ "/_purged_infos_limit/",
-            [?CONTENT_JSON, ?AUTH],
-            "2"
-        ),
-        ?assert(Status1 =:= 200),
-
-        {ok, _, _, Body} = create_doc(Url, "doc1"),
-        {Json} = ?JSON_DECODE(Body),
-        Rev1 = couch_util:get_value(<<"rev">>, Json, undefined),
-        {ok, _, _, Body2} = create_doc(Url, "doc2"),
-        {Json2} = ?JSON_DECODE(Body2),
-        Rev2 = couch_util:get_value(<<"rev">>, Json2, undefined),
-        {ok, _, _, Body3} = create_doc(Url, "doc3"),
-        {Json3} = ?JSON_DECODE(Body3),
-        Rev3 = couch_util:get_value(<<"rev">>, Json3, undefined),
-
-        IdsRevsEJson =
-            {[
-                {<<"doc1">>, [Rev1]},
-                {<<"doc2">>, [Rev2]},
-                {<<"doc3">>, [Rev3]}
-            ]},
-        IdsRevs = binary_to_list(?JSON_ENCODE(IdsRevsEJson)),
-
-        {ok, Status2, _, ResultBody} = test_request:post(
-            Url ++ "/_purge/",
-            [?CONTENT_JSON, ?AUTH],
-            IdsRevs
-        ),
+    {Status2, Response2} = req(post, url(Url, "_purge"), IdsRevs),
+    ?assert(Status2 =:= 201 orelse Status2 =:= 202),
+    ?assertMatch(#{<<"purge_seq">> := null, <<"purged">> := IdsRevs}, 
Response2).
 
-        ResultJson = ?JSON_DECODE(ResultBody),
-        ?assert(Status2 =:= 201 orelse Status2 =:= 202),
-        ?assertEqual(
-            {[
-                {<<"purge_seq">>, null},
-                {<<"purged">>,
-                    {[
-                        {<<"doc1">>, [Rev1]},
-                        {<<"doc2">>, [Rev2]},
-                        {<<"doc3">>, [Rev3]}
-                    ]}}
-            ]},
-            ResultJson
-        )
-    end).
-
-should_error_set_purged_docs_limit_to0(Url) ->
-    ?_test(begin
-        {ok, Status, _, _} = test_request:put(
-            Url ++ "/_purged_infos_limit/",
-            [?CONTENT_JSON, ?AUTH],
-            "0"
-        ),
-        ?assert(Status =:= 400)
-    end).
+t_should_error_set_purged_docs_limit_to_0(Url) ->
+    {Status, _} = req(put, url(Url, "_purged_infos_limit"), "0"),
+    ?assert(Status =:= 400).
 
-test_timeout_set_purged_infos_limit(Url) ->
-    ?_test(begin
+t_timeout_set_purged_infos_limit(Url) ->
+    try
         meck:new(fabric, [passthrough]),
-        meck:expect(fabric, set_purge_infos_limit, fun(_, _, _) ->
-            {error, timeout}
-        end),
-        {ok, Status, _, ResultBody} = test_request:put(
-            Url ++
-                "/_purged_infos_limit/",
-            [?CONTENT_JSON, ?AUTH],
-            "2"
-        ),
-        meck:unload(fabric),
-        ResultJson = ?JSON_DECODE(ResultBody),
+        meck:expect(fabric, set_purge_infos_limit, fun(_, _, _) -> {error, 
timeout} end),
+        {Status, #{<<"reason">> := Error}} = req(put, url(Url, 
"_purged_infos_limit"), "2"),
         ?assert(Status =:= 500),
-        ?assertMatch(
-            {[
-                {<<"error">>, <<"error">>},
-                {<<"reason">>, <<"timeout">>}
-            ]},
-            ResultJson
-        )
-    end).
+        ?assertEqual(<<"timeout">>, Error)
+    after
+        meck:unload(fabric)
+    end.
+
+t_purged_info_only_get_allowed(Url) ->
+    {Status, Response} = req(post, url(Url, "_purged_infos")),
+    ?assert(Status =:= 405),
+    ?assertMatch(#{<<"reason">> := <<"Only GET allowed">>}, Response).
+
+t_empty_purged_info(Url) ->
+    {Status, Response} = req(get, url(Url, "_purged_infos")),
+    ?assert(Status =:= 200),
+    ?assertMatch(#{<<"purged_infos">> := []}, Response).
+
+t_purged_info(Url) ->
+    PurgedDocsNum = 3,
+    {Status1, Response1} = create_docs(Url, docs(PurgedDocsNum)),
+    ?assert(Status1 =:= 201 orelse Status1 =:= 202),
+    IdsRevs = ids_revs(Response1),
+
+    {Status2, Response2} = req(post, url(Url, "_purge"), IdsRevs),
+    ?assert(Status2 =:= 201 orelse Status2 =:= 202),
+    ?assertMatch(#{<<"purge_seq">> := null, <<"purged">> := IdsRevs}, 
Response2),
+
+    {Status3, Response3} = req(get, url(Url, "_purged_infos")),
+    ?assertEqual(Status3, 200),
+    ?assertMatch(#{<<"purged_infos">> := [_ | _]}, Response3),
+    Info = maps:get(<<"purged_infos">>, Response3),
+    ?assertEqual(3, length(Info)).

Review Comment:
   Add a new test `t_purged_infos_after_multiple_purge_requests`.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to