couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kxe...@apache.org
Subject [15/36] couchdb commit: updated refs/heads/1963-eunit to 85f2750
Date Tue, 03 Jun 2014 15:54:53 GMT
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/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb/commit/d36c7980
Tree: http://git-wip-us.apache.org/repos/asf/couchdb/tree/d36c7980
Diff: http://git-wip-us.apache.org/repos/asf/couchdb/diff/d36c7980

Branch: refs/heads/1963-eunit
Commit: d36c7980f14566b3af914dc0d7e99826df3af30b
Parents: e722195
Author: Alexander Shorin <kxepal@apache.org>
Authored: Tue May 20 07:16:41 2014 +0400
Committer: Alexander Shorin <kxepal@apache.org>
Committed: Tue Jun 3 15:14:25 2014 +0400

----------------------------------------------------------------------
 test/couchdb/Makefile.am             |   3 +
 test/couchdb/couchdb_views_tests.erl | 125 +++++++++++++++++++++++++++++
 test/couchdb/test_request.erl        |  49 ++++++++++++
 test/etap/072-cleanup.t              | 126 ------------------------------
 test/etap/Makefile.am                |   1 -
 5 files changed, 177 insertions(+), 127 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb/blob/d36c7980/test/couchdb/Makefile.am
----------------------------------------------------------------------
diff --git a/test/couchdb/Makefile.am b/test/couchdb/Makefile.am
index b8ad5ed..8d45866 100644
--- a/test/couchdb/Makefile.am
+++ b/test/couchdb/Makefile.am
@@ -14,6 +14,7 @@ noinst_SCRIPTS = run
 
 all:
 	mkdir -p {ebin,temp}
+	${ERLC} -oebin test_request.erl
 	chmod +x run
 
 eunit_files = \
@@ -27,6 +28,8 @@ eunit_files = \
     couch_stream_tests.erl \
     couch_key_tree_tests.erl \
     couch_db_tests.erl \
+    couchdb_views_tests.erl \
+    test_request.erl \
     couchdb_tests.hrl
 
 EXTRA_DIST = \

http://git-wip-us.apache.org/repos/asf/couchdb/blob/d36c7980/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..be361c5
--- /dev/null
+++ b/test/couchdb/couchdb_views_tests.erl
@@ -0,0 +1,125 @@
+% 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_lib("../../src/couchdb/couch_db.hrl").
+-include_lib("couchdb_tests.hrl").
+
+-define(ADMIN_USER, #user_ctx{roles=[<<"_admin">>]}).
+
+
+start() ->
+    couch_server_sup:start_link(?CONFIG_CHAIN),
+    % disable logging to reduce noise in stdout
+    couch_config:set("log", "level", "none", false),
+    ok.
+
+stop(_) ->
+    couch_server_sup:stop(),
+    ok.
+
+setup() ->
+    DbName = ?tempdb(),
+    {ok, _} = couch_db:create(DbName, [{user_ctx, ?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, [{user_ctx, ?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, [{user_ctx, ?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, [{user_ctx, ?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/blob/d36c7980/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..cd6e310
--- /dev/null
+++ b/test/couchdb/test_request.erl
@@ -0,0 +1,49 @@
+% 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.

http://git-wip-us.apache.org/repos/asf/couchdb/blob/d36c7980/test/etap/072-cleanup.t
----------------------------------------------------------------------
diff --git a/test/etap/072-cleanup.t b/test/etap/072-cleanup.t
deleted file mode 100755
index 9cbcdfa..0000000
--- a/test/etap/072-cleanup.t
+++ /dev/null
@@ -1,126 +0,0 @@
-#!/usr/bin/env escript
-%% -*- erlang -*-
-
-% 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.
-
--define(TEST_DB, <<"etap-test-db">>).
-
--record(user_ctx, {
-    name = null,
-    roles = [],
-    handler
-}).
-
--define(ADMIN_USER, #user_ctx{roles=[<<"_admin">>]}).
-
-main(_) ->
-    test_util:init_code_path(),
-
-    etap:plan(7),
-    try test() of
-        ok ->
-            etap:end_tests()
-    catch
-        Other ->
-            etap:diag(io_lib:format("Test died abnormally: ~p", [Other])),
-            timer:sleep(1000),
-            etap:bail(Other)
-    end,
-    ok.
-
-test() ->
-
-    {ok, _} = couch_server_sup:start_link(test_util:config_files()),
-    couch_server:delete(?TEST_DB, []),
-    timer:sleep(1000),
-
-    couch_db:create(?TEST_DB, []),
-
-    {ok, AllDbs} = couch_server:all_databases(),
-    etap:ok(lists:member(?TEST_DB, AllDbs), "Database was created."),
-
-    FooRev = create_design_doc(<<"_design/foo">>, <<"bar">>),
-    query_view("foo", "bar"),
-
-    BoozRev = create_design_doc(<<"_design/booz">>, <<"baz">>),
-    query_view("booz", "baz"),
-
-    {ok, _Db} = couch_db:open(?TEST_DB, [{user_ctx, ?ADMIN_USER}]),
-    view_cleanup(),
-    etap:is(count_index_files(), 2,
-        "Two index files before any deletions."),
-
-    delete_design_doc(<<"_design/foo">>, FooRev),
-    view_cleanup(),
-    etap:is(count_index_files(), 1,
-        "One index file after first deletion and cleanup."),
-
-    delete_design_doc(<<"_design/booz">>, BoozRev),
-    view_cleanup(),
-    etap:is(count_index_files(), 0,
-        "No index files after second deletion and cleanup."),
-
-    couch_server:delete(?TEST_DB, []),
-    {ok, AllDbs2} = couch_server:all_databases(),
-    etap:ok(not lists:member(?TEST_DB, AllDbs2),
-        "Database was deleted."),
-    ok.
-
-create_design_doc(DDName, ViewName) ->
-    {ok, Db} = couch_db:open(?TEST_DB, [{user_ctx, ?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(DDName, Rev) ->
-    {ok, Db} = couch_db:open(?TEST_DB, [{user_ctx, ?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() ->
-    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(?TEST_DB).
-
-query_view(DDoc, View) ->
-    {ok, Code, _Headers, _Body} = test_util:request(
-        db_url() ++ "/_design/" ++ DDoc ++ "/_view/" ++ View, [], get),
-    etap:is(Code, 200, "Built view index for " ++ DDoc ++ "."),
-    ok.
-
-view_cleanup() ->
-    {ok, Db} = couch_db:open(?TEST_DB, [{user_ctx, ?ADMIN_USER}]),
-    couch_mrview:cleanup(Db),
-    couch_db:close(Db).
-
-count_index_files() ->
-    % call server to fetch the index files
-    RootDir = couch_config:get("couchdb", "view_index_dir"),
-    length(filelib:wildcard(RootDir ++ "/." ++
-        binary_to_list(?TEST_DB) ++ "_design"++"/mrview/*")).

http://git-wip-us.apache.org/repos/asf/couchdb/blob/d36c7980/test/etap/Makefile.am
----------------------------------------------------------------------
diff --git a/test/etap/Makefile.am b/test/etap/Makefile.am
index 408ca40..4e4997e 100644
--- a/test/etap/Makefile.am
+++ b/test/etap/Makefile.am
@@ -36,7 +36,6 @@ fixture_files = \
     fixtures/test.couch
 
 tap_files = \
-    072-cleanup.t \
     073-changes.t \
     074-doc-update-conflicts.t \
     075-auth-cache.t \


Mime
View raw message