summaryrefslogtreecommitdiff
path: root/src/couch/test/couch_db_plugin_tests.erl
blob: 94dd3dfa5ec3813635a7422f43ef78a0e68f4d21 (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
% 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(couch_db_plugin_tests).

-export([
    validate_dbname/2,
    before_doc_update/2,
    after_doc_read/2,
    validate_docid/1,
    check_is_admin/1,
    on_delete/2
]).

-export([ %% couch_epi_plugin behaviour
    app/0,
    providers/0,
    services/0,
    data_providers/0,
    data_subscriptions/0,
    processes/0,
    notify/3
]).

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

%% couch_epi_plugin behaviour

app() -> test_app.
providers() -> [{couch_db, ?MODULE}].
services() -> [].
data_providers() -> [].
data_subscriptions() -> [].
processes() -> [].
notify(_, _, _) -> ok.
fake_db() -> element(2, couch_db:clustered_db(fake, totes_fake)).

setup() ->
    couch_tests:setup([
        couch_epi_dispatch:dispatch(chttpd, ?MODULE)
    ]).

teardown(Ctx) ->
    couch_tests:teardown(Ctx).

validate_dbname({true, _Db}, _) -> {decided, true};
validate_dbname({false, _Db}, _) -> {decided, false};
validate_dbname({fail, _Db}, _) -> throw(validate_dbname);
validate_dbname({pass, _Db}, _) -> no_decision.

before_doc_update({fail, _Doc}, _Db) -> throw(before_doc_update);
before_doc_update({true, Doc}, Db) -> [{true, [before_doc_update|Doc]}, Db];
before_doc_update({false, Doc}, Db) -> [{false, Doc}, Db].

after_doc_read({fail, _Doc}, _Db) -> throw(after_doc_read);
after_doc_read({true, Doc}, Db) -> [{true, [after_doc_read|Doc]}, Db];
after_doc_read({false, Doc}, Db) -> [{false, Doc}, Db].

validate_docid({true, _Id}) -> true;
validate_docid({false, _Id}) -> false;
validate_docid({fail, _Id}) -> throw(validate_docid).

check_is_admin({true, _Db}) -> true;
check_is_admin({false, _Db}) -> false;
check_is_admin({fail, _Db}) -> throw(check_is_admin).

on_delete(true, _Opts) -> true;
on_delete(false, _Opts) -> false;
on_delete(fail, _Opts) -> throw(on_delete).

callback_test_() ->
    {
        "callback tests",
        {
            setup, fun setup/0, fun teardown/1,
            [
                {"validate_dbname_match", fun validate_dbname_match/0},
                {"validate_dbname_no_match", fun validate_dbname_no_match/0},
                {"validate_dbname_throw", fun validate_dbname_throw/0},
                {"validate_dbname_pass", fun validate_dbname_pass/0},

                {"before_doc_update_match", fun before_doc_update_match/0},
                {"before_doc_update_no_match", fun before_doc_update_no_match/0},
                {"before_doc_update_throw", fun before_doc_update_throw/0},

                {"after_doc_read_match", fun after_doc_read_match/0},
                {"after_doc_read_no_match", fun after_doc_read_no_match/0},
                {"after_doc_read_throw", fun after_doc_read_throw/0},

                {"validate_docid_match", fun validate_docid_match/0},
                {"validate_docid_no_match", fun validate_docid_no_match/0},
                {"validate_docid_throw", fun validate_docid_throw/0},

                {"check_is_admin_match", fun check_is_admin_match/0},
                {"check_is_admin_no_match", fun check_is_admin_no_match/0},
                {"check_is_admin_throw", fun check_is_admin_throw/0},

                {"on_delete_match", fun on_delete_match/0},
                {"on_delete_no_match", fun on_delete_no_match/0},
                {"on_delete_throw", fun on_delete_throw/0}
            ]
        }
    }.


validate_dbname_match() ->
    ?assert(couch_db_plugin:validate_dbname(
        {true, [db]}, db, fun(_, _) -> pass end)).

validate_dbname_no_match() ->
    ?assertNot(couch_db_plugin:validate_dbname(
        {false, [db]}, db, fun(_, _) -> pass end)).

validate_dbname_throw() ->
    ?assertThrow(
        validate_dbname,
        couch_db_plugin:validate_dbname(
            {fail, [db]}, db, fun(_, _) -> pass end)).

validate_dbname_pass() ->
    ?assertEqual(pass, couch_db_plugin:validate_dbname(
        {pass, [db]}, db, fun(_, _) -> pass end)).

before_doc_update_match() ->
    ?assertMatch(
        {true, [before_doc_update, doc]},
        couch_db_plugin:before_doc_update(fake_db(), {true, [doc]})).

before_doc_update_no_match() ->
    ?assertMatch(
        {false, [doc]},
        couch_db_plugin:before_doc_update(fake_db(), {false, [doc]})).

before_doc_update_throw() ->
    ?assertThrow(
        before_doc_update,
        couch_db_plugin:before_doc_update(fake_db(), {fail, [doc]})).


after_doc_read_match() ->
    ?assertMatch(
        {true, [after_doc_read, doc]},
        couch_db_plugin:after_doc_read(fake_db(), {true, [doc]})).

after_doc_read_no_match() ->
    ?assertMatch(
        {false, [doc]},
        couch_db_plugin:after_doc_read(fake_db(), {false, [doc]})).

after_doc_read_throw() ->
    ?assertThrow(
        after_doc_read,
        couch_db_plugin:after_doc_read(fake_db(), {fail, [doc]})).


validate_docid_match() ->
    ?assert(couch_db_plugin:validate_docid({true, [doc]})).

validate_docid_no_match() ->
    ?assertNot(couch_db_plugin:validate_docid({false, [doc]})).

validate_docid_throw() ->
    ?assertThrow(
        validate_docid,
        couch_db_plugin:validate_docid({fail, [doc]})).


check_is_admin_match() ->
    ?assert(couch_db_plugin:check_is_admin({true, [db]})).

check_is_admin_no_match() ->
    ?assertNot(couch_db_plugin:check_is_admin({false, [db]})).

check_is_admin_throw() ->
    ?assertThrow(
        check_is_admin,
        couch_db_plugin:check_is_admin({fail, [db]})).

on_delete_match() ->
    ?assertMatch(
       [true],
       couch_db_plugin:on_delete(true, [])).

on_delete_no_match() ->
    ?assertMatch(
       [false],
       couch_db_plugin:on_delete(false, [])).

on_delete_throw() ->
    ?assertThrow(
        on_delete,
        couch_db_plugin:on_delete(fail, [])).