xref: /5.5.2/couchdb/test/etap/070-couch-db.t (revision 8be914dd)
1#!/usr/bin/env escript
2%% -*- erlang -*-
3%%! -smp enable
4
5% Licensed under the Apache License, Version 2.0 (the "License"); you may not
6% use this file except in compliance with the License. You may obtain a copy of
7% the License at
8%
9%   http://www.apache.org/licenses/LICENSE-2.0
10%
11% Unless required by applicable law or agreed to in writing, software
12% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14% License for the specific language governing permissions and limitations under
15% the License.
16
17main(_) ->
18    test_util:init_code_path(),
19
20    etap:plan(4),
21    case (catch test()) of
22        ok ->
23            etap:end_tests();
24        Other ->
25            etap:diag(io_lib:format("Test died abnormally: ~p", [Other])),
26            etap:bail(Other)
27    end,
28    ok.
29
30test() ->
31
32    couch_server_sup:start_link(test_util:config_files()),
33
34    couch_db:create(<<"etap-test-db">>, []),
35    {ok, AllDbs} = couch_server:all_databases(),
36    etap:ok(lists:member(<<"etap-test-db">>, AllDbs), "Database was created."),
37
38    couch_server:delete(<<"etap-test-db">>, []),
39    {ok, AllDbs2} = couch_server:all_databases(),
40    etap:ok(not lists:member(<<"etap-test-db">>, AllDbs2),
41        "Database was deleted."),
42
43    MkDbName = fun(Int) -> list_to_binary("lru-" ++ integer_to_list(Int)) end,
44
45    lists:foreach(fun(Int) ->
46        {ok, TestDbs} = couch_server:all_databases(),
47        ok = case lists:member(MkDbName(Int), TestDbs) of
48            true -> couch_server:delete(MkDbName(Int), []);
49            _ -> ok
50        end,
51        {ok, Db} = couch_db:create(MkDbName(Int), []),
52        ok = couch_db:close(Db)
53    end, lists:seq(1, 6)),
54
55    {ok, AllDbs3} = couch_server:all_databases(),
56    NumCreated = lists:foldl(fun(Int, Acc) ->
57        true = lists:member(MkDbName(Int), AllDbs3),
58        Acc+1
59    end, 0, lists:seq(1, 6)),
60    etap:is(6, NumCreated, "Created all databases."),
61
62    lists:foreach(fun(Int) ->
63        ok = couch_server:delete(MkDbName(Int), [])
64    end, lists:seq(1, 6)),
65
66    {ok, AllDbs4} = couch_server:all_databases(),
67    NumDeleted = lists:foldl(fun(Int, Acc) ->
68        false = lists:member(MkDbName(Int), AllDbs4),
69        Acc+1
70    end, 0, lists:seq(1, 6)),
71    etap:is(6, NumDeleted, "Deleted all databases."),
72
73    ok.
74