Skip to content

Commit

Permalink
fix delete_db & more.
Browse files Browse the repository at this point in the history
--HG--
branch : refactor
rename : t/007-attachment.t => t/006-attachment.t
  • Loading branch information
benoitc committed Sep 29, 2009
1 parent f494c37 commit 9e47f93
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 96 deletions.
1 change: 1 addition & 0 deletions include/couchbeam.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@


-record(db, {
name,
server,
couchdb = #couchdb_params{},
base
Expand Down
10 changes: 7 additions & 3 deletions src/couchbeam_db.erl
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ delete_attachment(Db, Doc, AName) ->
%% @private

init({DbName, #server_state{couchdb=C, prefix=Prefix} = ServerState}) ->
State = #db{server = ServerState,
State = #db{name = DbName,
server = ServerState,
couchdb = C,
base = Prefix ++ DbName},
{ok, State}.
Expand Down Expand Up @@ -279,8 +280,11 @@ handle_call({delete_attachment, Doc, AName}, _From, #db{couchdb=C, base=Base}=St
handle_cast(_Msg, State) ->
{no_reply, State}.

handle_info(_Info, State) ->
{noreply, State}.
handle_info({'EXIT', _Pid, _Reason}, State) ->
{stop, State};
handle_info(Msg, State) ->
io:format("Bad message received for db ~s: ~p", [State#db.name, Msg]),
exit({error, Msg}).

terminate(_Reason, _State) ->
ok.
Expand Down
20 changes: 18 additions & 2 deletions src/couchbeam_server.erl
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ init(#server_state{couchdb=C, prefix=P} = InitialState) ->
State = InitialState#server_state{dbs_by_name = DbsByNameTid,
dbs_by_pid = DbsByPidTid,
uuids_pid = UuidsPid},
process_flag(trap_exit, true),
{ok, State}.

handle_call(info, _From, #server_state{prefix=Base, couchdb=C}=State) ->
Expand All @@ -143,7 +144,10 @@ handle_call({open_db, DbName}, _From, #server_state{prefix=Base,
{error, Reason} -> Reason
end;
[{_, DbPid1}] ->
DbPid1
case couchbeam_resource:get(C, Base ++ DbName, [], [], []) of
{ok, _} -> DbPid1;
{error, Reason} -> Reason
end
end,
{reply, Pid, State};

Expand All @@ -161,7 +165,11 @@ handle_call({create_db, DbName}, _From, #server_state{prefix=Base,
DbPid;
{error, Reason} -> Reason
end;
[{_, _}] -> already_exist
[{_, DbPid1}] ->
case couchbeam_resource:put(C, Base ++ DbName, [], [], [], []) of
ok -> DbPid1;
{error, Reason} -> Reason
end
end,
{reply, Pid, State};

Expand All @@ -185,6 +193,14 @@ handle_cast(_Msg, State) ->
{no_reply, State}.


handle_info({'EXIT', Pid, _Reason}, #server_state{dbs_by_name=DbsNameTid,
dbs_by_pid=DbsPidTid}=State) ->
[{Pid, DbName}] = ets:lookup(DbsPidTid, Pid),
[{DbName, Pid}] = ets:lookup(DbsNameTid, DbName),
true = ets:delete(DbsPidTid, Pid),
true = ets:delete(DbsNameTid, DbName),
{noreply, State};

handle_info(_Info, State) ->
{noreply, State}.

Expand Down
10 changes: 6 additions & 4 deletions t/003-database.t
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ main(_) ->

start_app() ->
couchbeam:start(),
Pid = couchbeam_server:start_connection_link(),
couchbeam_server:start_connection_link(),
catch couchbeam_server:delete_db(default, "couchbeam_testdb"),
catch couchbeam_server:delete_db(default, "couchbeam_testdb2"),
ok.

stop_test() ->
Expand All @@ -29,14 +31,14 @@ stop_test() ->
test() ->
Db = couchbeam_server:create_db(default, "couchbeam_testdb"),
etap:is(is_pid(Db), true, "db created ok"),
F = fun() -> couchbeam_server:create_db(default, "couchbeam_testdb") end,
etap:fun_is(F, precondition_failed, "conclict database ok"),
Db0 = couchbeam_server:create_db(default, "couchbeam_testdb"),
etap:is(Db0, precondition_failed, "conclict database ok"),
Db2 = couchbeam_server:create_db(default, "couchbeam_testdb2"),
etap:is(is_pid(Db2), true, "db2 created ok"),
AllDbs = couchbeam_server:all_dbs(default),
etap:ok(is_list(AllDbs), "all_dbs return a list"),
etap:ok(lists:member(<<"couchbeam_testdb">>, AllDbs), "couchbeam_testdb exists ok "),
etap:ok(couchbeam_db:is_db(default, "couchbeam_testdb"), "is_db exists ok "),
etap:ok(couchbeam_server:is_db(default, "couchbeam_testdb"), "is_db exists ok "),
etap:ok(lists:member(<<"couchbeam_testdb2">>, AllDbs), "couchbeam_testdb2 exists ok"),
etap:is(couchbeam_server:delete_db(default, "couchbeam_testdb2"), ok, "delete couchbeam_testdb2 ok"),
AllDbs1 = couchbeam_server:all_dbs(default),
Expand Down
28 changes: 14 additions & 14 deletions t/004-document.t
Original file line number Diff line number Diff line change
Expand Up @@ -17,43 +17,43 @@ main(_) ->
ok.

start_app() ->
application:start(crypto),
application:start(couchbeam),
catch couchbeam:delete_db(default, "couchbeam_testdb"),
catch couchbeam:delete_db(default, "couchbeam_testdb2"),
couchbeam:start(),
couchbeam_server:start_connection_link(),
catch couchbeam_server:delete_db(default, "couchbeam_testdb"),
catch couchbeam_server:delete_db(default, "couchbeam_testdb2"),
ok.

stop_test() ->
catch couchbeam:delete_db(default, "couchbeam_testdb"),
catch couchbeam:delete_db(default, "couchbeam_testdb2"),
catch couchbeam_server:delete_db(default, "couchbeam_testdb"),
catch couchbeam_server:delete_db(default, "couchbeam_testdb2"),
ok.

test() ->
etap:is(couchbeam:create_db(default, "couchbeam_testdb"), ok, "db created ok"),
etap:ok(case couchbeam:save_doc(default, "couchbeam_testdb", {[{"test", blah}]}) of
etap:is(couchbeam_server:create_db(default, "couchbeam_testdb"), ok, "db created ok"),
etap:ok(case couchbeam_db:save_doc(default, "couchbeam_testdb", {[{"test", blah}]}) of
{ok, _} -> true;
_E -> false
end, "save doc ok"),
etap:ok(case couchbeam:save_doc(default, "couchbeam_testdb",
etap:ok(case couchbeam_db:save_doc(default, "couchbeam_testdb",
{[{<<"_id">>,<<"test">>}, {<<"test">>,<<"blah">>}]}) of
{ok, {[{<<"id">>,<<"test">>}|_]}} -> true;
_ -> false
end, "save do with id ok"),
F = fun() ->
couchbeam:save_doc(default, "couchbeam_testdb",
couchbeam_db:save_doc(default, "couchbeam_testdb",
{[{<<"_id">>,<<"test">>}, {<<"test">>,<<"blah">>}]})
end,
etap_exception:throws_ok(F, conflict, "conflict raised"),
{Doc} = couchbeam:open_doc(default, "couchbeam_testdb", "test"),
{Doc} = couchbeam_db:open_doc(default, "couchbeam_testdb", "test"),
etap:is(proplists:get_value(<<"test">>, Doc), <<"blah">>, "fetch doc ok"),
couchbeam:save_doc(default, "couchbeam_testdb",
couchbeam_db:save_doc(default, "couchbeam_testdb",
{[{<<"_id">>,<<"test2">>}, {<<"test">>,<<"blah">>}]}),
F1 = fun() ->
couchbeam:open_doc(default, "couchbeam_testdb", "test2")
couchbeam_db:open_doc(default, "couchbeam_testdb", "test2")
end,
Doc1 = F1(),
etap_exception:lives_ok(F1,"test2 has been created"),
etap:ok(case couchbeam:delete_doc(default,"couchbeam_testdb", Doc1) of
etap:ok(case couchbeam_db:delete_doc(default,"couchbeam_testdb", Doc1) of
{ok, _} -> true;
_E2 -> false
end, "doc2 has been deleted"),
Expand Down
File renamed without changes.
73 changes: 0 additions & 73 deletions t/006-standalone.t

This file was deleted.

0 comments on commit 9e47f93

Please sign in to comment.