Port 072-cleanup.t etap test suite to eunit requests functions from test_util were moved to test_request module with nicer and simpler API.
Project: http://git-wip-us.apache.org/repos/asf/couchdb-couch/repo Commit: http://git-wip-us.apache.org/repos/asf/couchdb-couch/commit/30cbd1a1 Tree: http://git-wip-us.apache.org/repos/asf/couchdb-couch/tree/30cbd1a1 Diff: http://git-wip-us.apache.org/repos/asf/couchdb-couch/diff/30cbd1a1 Branch: refs/heads/1963-eunit-bigcouch Commit: 30cbd1a1ead45cf49897d7f8c61ee51e2c9c34c2 Parents: cd3f57f Author: Alexander Shorin <[email protected]> Authored: Tue May 20 07:16:41 2014 +0400 Committer: Russell Branca <[email protected]> Committed: Mon Aug 11 13:08:07 2014 -0700 ---------------------------------------------------------------------- test/couchdb/couchdb_views_tests.erl | 129 ++++++++++++++++++++++++++++++ test/couchdb/test_request.erl | 51 ++++++++++++ 2 files changed, 180 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/couchdb-couch/blob/30cbd1a1/test/couchdb/couchdb_views_tests.erl ---------------------------------------------------------------------- diff --git a/test/couchdb/couchdb_views_tests.erl b/test/couchdb/couchdb_views_tests.erl new file mode 100644 index 0000000..e44cef6 --- /dev/null +++ b/test/couchdb/couchdb_views_tests.erl @@ -0,0 +1,129 @@ +% 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(couchdb_views_tests). + +-include("couch_eunit.hrl"). +-include_lib("couchdb/couch_db.hrl"). + +-define(ADMIN_USER, {user_ctx, #user_ctx{roles=[<<"_admin">>]}}). +-define(TIMEOUT, 1000). + +start() -> + {ok, Pid} = couch_server_sup:start_link(?CONFIG_CHAIN), + Pid. + +stop(Pid) -> + erlang:monitor(process, Pid), + couch_server_sup:stop(), + receive + {'DOWN', _, _, Pid, _} -> + ok + after ?TIMEOUT -> + throw({timeout, server_stop}) + end. + +setup() -> + DbName = ?tempdb(), + {ok, _} = couch_db:create(DbName, [?ADMIN_USER]), + FooRev = create_design_doc(DbName, <<"_design/foo">>, <<"bar">>), + ok = query_view(DbName, "foo", "bar"), + BooRev = create_design_doc(DbName, <<"_design/boo">>, <<"baz">>), + ok = query_view(DbName, "boo", "baz"), + {DbName, {FooRev, BooRev}}. + +teardown({DbName, _}) -> + ok = couch_server:delete(DbName, []), + ok. + + +view_indexes_cleanup_test_() -> + { + "View indexes cleanup", + { + setup, + fun start/0, fun stop/1, + { + foreach, + fun setup/0, fun teardown/1, + [ + fun should_have_two_indexes_alive_before_deletion/1, + fun should_cleanup_index_file_after_ddoc_deletion/1, + fun should_cleanup_all_index_files/1 + ] + } + } + }. + +should_have_two_indexes_alive_before_deletion({DbName, _}) -> + view_cleanup(DbName), + ?_assertEqual(2, count_index_files(DbName)). + +should_cleanup_index_file_after_ddoc_deletion({DbName, {FooRev, _}}) -> + delete_design_doc(DbName, <<"_design/foo">>, FooRev), + view_cleanup(DbName), + ?_assertEqual(1, count_index_files(DbName)). + +should_cleanup_all_index_files({DbName, {FooRev, BooRev}})-> + delete_design_doc(DbName, <<"_design/foo">>, FooRev), + delete_design_doc(DbName, <<"_design/boo">>, BooRev), + view_cleanup(DbName), + ?_assertEqual(0, count_index_files(DbName)). + + +create_design_doc(DbName, DDName, ViewName) -> + {ok, Db} = couch_db:open(DbName, [?ADMIN_USER]), + DDoc = couch_doc:from_json_obj({[ + {<<"_id">>, DDName}, + {<<"language">>, <<"javascript">>}, + {<<"views">>, {[ + {ViewName, {[ + {<<"map">>, <<"function(doc) { emit(doc.value, 1); }">>} + ]}} + ]}} + ]}), + {ok, Rev} = couch_db:update_doc(Db, DDoc, []), + couch_db:ensure_full_commit(Db), + couch_db:close(Db), + Rev. + +delete_design_doc(DbName, DDName, Rev) -> + {ok, Db} = couch_db:open(DbName, [?ADMIN_USER]), + DDoc = couch_doc:from_json_obj({[ + {<<"_id">>, DDName}, + {<<"_rev">>, couch_doc:rev_to_str(Rev)}, + {<<"_deleted">>, true} + ]}), + {ok, _} = couch_db:update_doc(Db, DDoc, [Rev]), + couch_db:close(Db). + +db_url(DbName) -> + Addr = couch_config:get("httpd", "bind_address", "127.0.0.1"), + Port = integer_to_list(mochiweb_socket_server:get(couch_httpd, port)), + "http://" ++ Addr ++ ":" ++ Port ++ "/" ++ binary_to_list(DbName). + +query_view(DbName, DDoc, View) -> + {ok, Code, _Headers, _Body} = test_request:get( + db_url(DbName) ++ "/_design/" ++ DDoc ++ "/_view/" ++ View), + ?assertEqual(200, Code), + ok. + +view_cleanup(DbName) -> + {ok, Db} = couch_db:open(DbName, [?ADMIN_USER]), + couch_mrview:cleanup(Db), + couch_db:close(Db). + +count_index_files(DbName) -> + % call server to fetch the index files + RootDir = couch_config:get("couchdb", "view_index_dir"), + length(filelib:wildcard(RootDir ++ "/." ++ + binary_to_list(DbName) ++ "_design"++"/mrview/*")). http://git-wip-us.apache.org/repos/asf/couchdb-couch/blob/30cbd1a1/test/couchdb/test_request.erl ---------------------------------------------------------------------- diff --git a/test/couchdb/test_request.erl b/test/couchdb/test_request.erl new file mode 100644 index 0000000..7abb92f --- /dev/null +++ b/test/couchdb/test_request.erl @@ -0,0 +1,51 @@ +% 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(test_request). + +-export([get/1, get/2]). +-export([request/3, request/4]). + +get(Url) -> + request(get, Url, []). + +get(Url, Headers) -> + request(get, Url, Headers). + + +request(Method, Url, Headers) -> + request(Method, Url, Headers, []). + +request(Method, Url, Headers, Body) -> + request(Method, Url, Headers, Body, 3). + +request(_Method, _Url, _Headers, _Body, 0) -> + {error, request_failed}; +request(Method, Url, Headers, Body, N) -> + case code:is_loaded(ibrowse) of + false -> + {ok, _} = ibrowse:start(); + _ -> + ok + end, + case ibrowse:send_req(Url, Headers, Method, Body) of + {ok, Code0, RespHeaders, RespBody0} -> + Code = list_to_integer(Code0), + RespBody = iolist_to_binary(RespBody0), + {ok, Code, RespHeaders, RespBody}; + {error, {'EXIT', {normal, _}}} -> + % Connection closed right after a successful request that + % used the same connection. + request(Method, Url, Headers, Body, N - 1); + Error -> + Error + end.
