summaryrefslogtreecommitdiff
path: root/src/chttpd/test/chttpd_view_test.erl
blob: 3457c6f303c1b38e39cad67c3c84dccb612922ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
% 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(chttpd_view_test).

-include_lib("couch/include/couch_eunit.hrl").
-include_lib("couch/include/couch_db.hrl").

-define(USER, "chttpd_view_test_admin").
-define(PASS, "pass").
-define(AUTH, {basic_auth, {?USER, ?PASS}}).
-define(CONTENT_JSON, {"Content-Type", "application/json"}).
-define(DDOC, "{\"_id\": \"_design/bar\", \"views\": {\"baz\":
               {\"map\": \"function(doc) {emit(doc._id, doc._id);}\"}}}").

-define(FIXTURE_TXT, ?ABS_PATH(?FILE)).
-define(i2l(I), integer_to_list(I)).

setup() ->
    Hashed = couch_passwords:hash_admin_password(?PASS),
    ok = config:set("admins", ?USER, ?b2l(Hashed), _Persist=false),
    TmpDb = ?tempdb(),
    Addr = config:get("chttpd", "bind_address", "127.0.0.1"),
    Port = mochiweb_socket_server:get(chttpd, port),
    Url = lists:concat(["http://", Addr, ":", Port, "/", ?b2l(TmpDb)]),
    create_db(Url),
    Url.

teardown(Url) ->
    delete_db(Url),
    ok = config:delete("admins", ?USER, _Persist=false).

create_db(Url) ->
    {ok, Status, _, _} = test_request:put(Url, [?CONTENT_JSON, ?AUTH], "{}"),
    ?assert(Status =:= 201 orelse Status =:= 202).


create_doc(Url, Id) ->
    test_request:put(Url ++ "/" ++ Id,
      [?CONTENT_JSON, ?AUTH], "{\"mr\": \"rockoartischocko\"}").

delete_db(Url) ->
    {ok, 200, _, _} = test_request:delete(Url, [?AUTH]).

all_view_test_() ->
    {
        "chttpd view tests",
        {
            setup,
            fun chttpd_test_util:start_couch/0, fun chttpd_test_util:stop_couch/1,
            {
                foreach,
                fun setup/0, fun teardown/1,
                [
                    fun should_succeed_on_view_with_queries_keys/1,
                    fun should_succeed_on_view_with_queries_limit_skip/1,
                    fun should_succeed_on_view_with_multiple_queries/1
                ]
            }
        }
    }.


should_succeed_on_view_with_queries_keys(Url) ->
    ?_test(begin
        [create_doc(Url, "testdoc" ++ ?i2l(I)) || I <- lists:seq(1, 10)],
        {ok, _, _, _} = test_request:put(Url ++ "/_design/bar",
            [?CONTENT_JSON, ?AUTH], ?DDOC),
        QueryDoc = "{\"queries\": [{\"keys\": [ \"testdoc3\",
            \"testdoc8\"]}]}",
        {ok, _, _, RespBody} = test_request:post(Url ++ "/_design/bar/"
            ++ "_view/baz/queries/", [?CONTENT_JSON, ?AUTH], QueryDoc),
        {ResultJson} = ?JSON_DECODE(RespBody),
        ResultJsonBody = couch_util:get_value(<<"results">>, ResultJson),
        {InnerJson} = lists:nth(1, ResultJsonBody),
        ?assertEqual(2, length(couch_util:get_value(<<"rows">>, InnerJson)))
    end).


should_succeed_on_view_with_queries_limit_skip(Url) ->
    ?_test(begin
        [create_doc(Url, "testdoc" ++ ?i2l(I)) || I <- lists:seq(1, 10)],
        {ok, _, _, _} = test_request:put(Url ++ "/_design/bar",
            [?CONTENT_JSON, ?AUTH], ?DDOC),
        QueryDoc = "{\"queries\": [{\"limit\": 5, \"skip\": 2}]}",
        {ok, RC, _, RespBody} = test_request:post(Url ++ "/_design/bar/"
            ++ "_view/baz/queries/", [?CONTENT_JSON, ?AUTH], QueryDoc),
        ?assertEqual(200, RC),
        {ResultJson} = ?JSON_DECODE(RespBody),
        ResultJsonBody = couch_util:get_value(<<"results">>, ResultJson),
        {InnerJson} = lists:nth(1, ResultJsonBody),
        ?assertEqual(2, couch_util:get_value(<<"offset">>, InnerJson)),
        ?assertEqual(5, length(couch_util:get_value(<<"rows">>, InnerJson)))
    end).


should_succeed_on_view_with_multiple_queries(Url) ->
    ?_test(begin
        [create_doc(Url, "testdoc" ++ ?i2l(I)) || I <- lists:seq(1, 10)],
        {ok, _, _, _} = test_request:put(Url ++ "/_design/bar",
            [?CONTENT_JSON, ?AUTH], ?DDOC),
        QueryDoc = "{\"queries\": [{\"keys\": [ \"testdoc3\",
            \"testdoc8\"]}, {\"limit\": 5, \"skip\": 2}]}",
        {ok, RC, _, RespBody} = test_request:post(Url ++ "/_design/bar/"
            ++ "_view/baz/queries/", [?CONTENT_JSON, ?AUTH], QueryDoc),
        ?assertEqual(200, RC),
        {ResultJson} = ?JSON_DECODE(RespBody),
        ResultJsonBody = couch_util:get_value(<<"results">>, ResultJson),
        {InnerJson1} = lists:nth(1, ResultJsonBody),
        ?assertEqual(2, length(couch_util:get_value(<<"rows">>, InnerJson1))),
        {InnerJson2} = lists:nth(2, ResultJsonBody),
        ?assertEqual(2, couch_util:get_value(<<"offset">>, InnerJson2)),
        ?assertEqual(5, length(couch_util:get_value(<<"rows">>, InnerJson2)))
    end).