summaryrefslogtreecommitdiff
path: root/src/couch/test/couch_bt_engine_upgrade_tests.erl
blob: 1d2a86d7135ace193b34a27920091b96b511597a (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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
% 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_bt_engine_upgrade_tests).

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


setup() ->
    Ctx = test_util:start_couch(),
    DbDir = config:get("couchdb", "database_dir"),
    DbFileNames = [
        "db_without_purge_req.couch",
        "db_with_1_purge_req.couch",
        "db_with_2_purge_req.couch",
        "db_with_1_purge_req_for_2_docs.couch"
    ],
    NewPaths = lists:map(fun(DbFileName) ->
        OldDbFilePath = filename:join([?FIXTURESDIR, DbFileName]),
        NewDbFilePath = filename:join([DbDir, DbFileName]),
        ok = filelib:ensure_dir(NewDbFilePath),
        file:delete(NewDbFilePath),
        {ok, _} = file:copy(OldDbFilePath, NewDbFilePath),
        NewDbFilePath
    end, DbFileNames),
    {Ctx, NewPaths}.


teardown({Ctx, Paths}) ->
    test_util:stop_couch(Ctx),
    lists:foreach(fun(Path) ->
        file:delete(Path)
    end, Paths).


upgrade_test_() ->
    {
        "Couch Bt Engine Upgrade tests",
        {
            setup,
            fun setup/0,
            fun teardown/1,
            [
                t_upgrade_without_purge_req(),
                t_upgrade_with_1_purge_req(),
                t_upgrade_with_N_purge_req(),
                t_upgrade_with_1_purge_req_for_2_docs()
            ]
        }
    }.


t_upgrade_without_purge_req() ->
    ?_test(begin
        % There are three documents in the fixture
        % db with zero purge entries
        DbName = <<"db_without_purge_req">>,

        {ok, UpgradedPurged} = couch_util:with_db(DbName, fun(Db) ->
            ?assertEqual(0, couch_db:get_purge_seq(Db)),
            couch_db:fold_purge_infos(Db, 0, fun fold_fun/2, [])
        end),
        ?assertEqual([], UpgradedPurged),

        {ok, Rev} = save_doc(
            DbName, {[{<<"_id">>, <<"doc4">>}, {<<"v">>, 1}]}
        ),
        {ok, _} = save_doc(DbName, {[{<<"_id">>, <<"doc5">>}, {<<"v">>, 2}]}),

        couch_util:with_db(DbName, fun(Db) ->
            ?assertEqual({ok, 5}, couch_db:get_doc_count(Db)),
            ?assertEqual(0, couch_db:get_purge_seq(Db))
        end),

        PurgeReqs = [
            {couch_uuids:random(), <<"doc4">>, [Rev]}
        ],

        {ok, [{ok, PRevs}]} = couch_util:with_db(DbName, fun(Db) ->
            couch_db:purge_docs(Db, PurgeReqs)
        end),
        ?assertEqual(PRevs, [Rev]),

        couch_util:with_db(DbName, fun(Db) ->
            ?assertEqual({ok, 4}, couch_db:get_doc_count(Db)),
            ?assertEqual(1, couch_db:get_purge_seq(Db))
        end)
    end).


t_upgrade_with_1_purge_req() ->
    ?_test(begin
        % There are two documents in the fixture database
        % with a single purge entry
        DbName = <<"db_with_1_purge_req">>,

        {ok, UpgradedPurged} = couch_util:with_db(DbName, fun(Db) ->
            ?assertEqual(1, couch_db:get_purge_seq(Db)),
            couch_db:fold_purge_infos(Db, 0, fun fold_fun/2, [])
        end),
        ?assertEqual([{1, <<"doc1">>}], UpgradedPurged),

        {ok, Rev} = save_doc(
            DbName, {[{<<"_id">>, <<"doc4">>}, {<<"v">>, 1}]}
        ),
        {ok, _} = save_doc(DbName, {[{<<"_id">>, <<"doc5">>}, {<<"v">>, 2}]}),

        couch_util:with_db(DbName, fun(Db) ->
            ?assertEqual({ok, 4}, couch_db:get_doc_count(Db)),
            ?assertEqual(1, couch_db:get_purge_seq(Db))
        end),

        PurgeReqs = [
            {couch_uuids:random(), <<"doc4">>, [Rev]}
        ],

        {ok, [{ok, PRevs}]} = couch_util:with_db(DbName, fun(Db) ->
            couch_db:purge_docs(Db, PurgeReqs)
        end),
        ?assertEqual(PRevs, [Rev]),

        couch_util:with_db(DbName, fun(Db) ->
            ?assertEqual({ok, 3}, couch_db:get_doc_count(Db)),
            ?assertEqual(2, couch_db:get_purge_seq(Db))
        end)
    end).


t_upgrade_with_N_purge_req() ->
    ?_test(begin
        % There is one document in the fixture database
        % with two docs that have been purged
        DbName = <<"db_with_2_purge_req">>,

        {ok, UpgradedPurged} = couch_util:with_db(DbName, fun(Db) ->
            ?assertEqual(2, couch_db:get_purge_seq(Db)),
            couch_db:fold_purge_infos(Db, 1, fun fold_fun/2, [])
        end),
        ?assertEqual([{2, <<"doc2">>}], UpgradedPurged),

        {ok, Rev} = save_doc(DbName, {[{<<"_id">>, <<"doc4">>}, {<<"v">>, 1}]}),
        {ok, _} = save_doc(DbName, {[{<<"_id">>, <<"doc5">>}, {<<"v">>, 2}]}),

        couch_util:with_db(DbName, fun(Db) ->
            ?assertEqual({ok, 3}, couch_db:get_doc_count(Db)),
            ?assertEqual(2, couch_db:get_purge_seq(Db))
        end),

        PurgeReqs = [
            {couch_uuids:random(), <<"doc4">>, [Rev]}
        ],

        {ok, [{ok, PRevs}]} = couch_util:with_db(DbName, fun(Db) ->
            couch_db:purge_docs(Db, PurgeReqs)
        end),
        ?assertEqual(PRevs, [Rev]),

        couch_util:with_db(DbName, fun(Db) ->
            ?assertEqual({ok, 2}, couch_db:get_doc_count(Db)),
            ?assertEqual(3, couch_db:get_purge_seq(Db))
        end)
    end).


t_upgrade_with_1_purge_req_for_2_docs() ->
    ?_test(begin
        % There are two documents (Doc4 and Doc5) in the fixture database
        % with three docs (Doc1, Doc2 and Doc3) that have been purged, and
        % with one purge req for Doc1 and another purge req for Doc 2 and Doc3
        DbName = <<"db_with_1_purge_req_for_2_docs">>,

        {ok, UpgradedPurged} = couch_util:with_db(DbName, fun(Db) ->
            ?assertEqual(3, couch_db:get_purge_seq(Db)),
            couch_db:fold_purge_infos(Db, 1, fun fold_fun/2, [])
        end),
        ?assertEqual([{3,<<"doc2">>},{2,<<"doc3">>}], UpgradedPurged),

        {ok, Rev} = save_doc(DbName, {[{<<"_id">>, <<"doc6">>}, {<<"v">>, 1}]}),
        {ok, _} = save_doc(DbName, {[{<<"_id">>, <<"doc7">>}, {<<"v">>, 2}]}),

        couch_util:with_db(DbName, fun(Db) ->
            ?assertEqual({ok, 4}, couch_db:get_doc_count(Db)),
            ?assertEqual(3, couch_db:get_purge_seq(Db))
        end),

        PurgeReqs = [
            {couch_uuids:random(), <<"doc6">>, [Rev]}
        ],

        {ok, [{ok, PRevs}]} = couch_util:with_db(DbName, fun(Db) ->
            couch_db:purge_docs(Db, PurgeReqs)
        end),
        ?assertEqual(PRevs, [Rev]),

        couch_util:with_db(DbName, fun(Db) ->
            ?assertEqual({ok, 3}, couch_db:get_doc_count(Db)),
            ?assertEqual(4, couch_db:get_purge_seq(Db))
        end)
    end).


save_doc(DbName, Json) ->
    Doc = couch_doc:from_json_obj(Json),
    couch_util:with_db(DbName, fun(Db) ->
        couch_db:update_doc(Db, Doc, [])
    end).


fold_fun({PSeq, _UUID, Id, _Revs}, Acc) ->
    {ok, [{PSeq, Id} | Acc]}.