summaryrefslogtreecommitdiff
path: root/src/mango/test/01-index-crud-test.py
blob: e85e1d8ea8f41cc72a6dd13742a2ba7e2efd2490 (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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
# 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.

import random

import mango
import copy
import unittest

DOCS = [
    {"_id": "1", "name": "Jimi", "age": 10, "cars": 1},
    {"_id": "2", "name": "kate", "age": 8, "cars": 0},
]


class IndexCrudTests(mango.DbPerClass):
    def setUp(self):
        self.db.recreate()

    def test_bad_fields(self):
        bad_fields = [
            None,
            True,
            False,
            "bing",
            2.0,
            {"foo": "bar"},
            [{"foo": 2}],
            [{"foo": "asc", "bar": "desc"}],
            [{"foo": "asc"}, {"bar": "desc"}],
            [""],
        ]
        for fields in bad_fields:
            try:
                self.db.create_index(fields)
            except Exception as e:
                self.assertEqual(e.response.status_code, 400)
            else:
                raise AssertionError("bad create index")

    def test_bad_types(self):
        bad_types = [
            None,
            True,
            False,
            1.5,
            "foo",  # Future support
            "geo",  # Future support
            {"foo": "bar"},
            ["baz", 3.0],
        ]
        for bt in bad_types:
            try:
                self.db.create_index(["foo"], idx_type=bt)
            except Exception as e:
                self.assertEqual(
                    e.response.status_code, 400, (bt, e.response.status_code)
                )
            else:
                raise AssertionError("bad create index")

    def test_bad_names(self):
        bad_names = ["", True, False, 1.5, {"foo": "bar"}, [None, False]]
        for bn in bad_names:
            try:
                self.db.create_index(["foo"], name=bn)
            except Exception as e:
                self.assertEqual(e.response.status_code, 400)
            else:
                raise AssertionError("bad create index")

    def test_bad_ddocs(self):
        bad_ddocs = ["", True, False, 1.5, {"foo": "bar"}, [None, False]]
        for bd in bad_ddocs:
            try:
                self.db.create_index(["foo"], ddoc=bd)
            except Exception as e:
                self.assertEqual(e.response.status_code, 400)
            else:
                raise AssertionError("bad create index")

    def test_bad_url(self):
        r = self.db.sess.get(self.db.path("_index/foo"))
        self.assertEqual(r.status_code, 405)

    def test_create_idx_01(self):
        fields = ["foo", "bar"]
        ret = self.db.create_index(fields, name="idx_01")
        assert ret is True
        for idx in self.db.list_indexes():
            if idx["name"] != "idx_01":
                continue
            self.assertEqual(idx["def"]["fields"], [{"foo": "asc"}, {"bar": "asc"}])
            return
        raise AssertionError("index not created")

    def test_create_idx_01_exists(self):
        fields = ["foo", "bar"]
        ret = self.db.create_index(fields, name="idx_01")
        assert ret is True
        ret = self.db.create_index(fields, name="idx_01")
        assert ret is False

    def test_create_idx_02(self):
        fields = ["baz", "foo"]
        ret = self.db.create_index(fields, name="idx_02")
        assert ret is True
        for idx in self.db.list_indexes():
            if idx["name"] != "idx_02":
                continue
            self.assertEqual(idx["def"]["fields"], [{"baz": "asc"}, {"foo": "asc"}])
            return
        raise AssertionError("index not created")

    def test_read_idx_doc(self):
        self.db.create_index(["foo", "bar"], name="idx_01")
        self.db.create_index(["hello", "bar"])
        for idx in self.db.list_indexes():
            if idx["type"] == "special":
                continue
            ddocid = idx["ddoc"]
            doc = self.db.open_doc(ddocid)
            self.assertEqual(doc["_id"], ddocid)
            info = self.db.ddoc_info(ddocid)
            self.assertEqual(info["name"], ddocid.split("_design/")[-1])

    def test_delete_idx_escaped(self):
        self.db.create_index(["foo", "bar"], name="idx_01")
        pre_indexes = self.db.list_indexes()
        ret = self.db.create_index(["bing"], name="idx_del_1")
        assert ret is True
        for idx in self.db.list_indexes():
            if idx["name"] != "idx_del_1":
                continue
            self.assertEqual(idx["def"]["fields"], [{"bing": "asc"}])
            self.db.delete_index(idx["ddoc"].replace("/", "%2F"), idx["name"])
        post_indexes = self.db.list_indexes()
        self.assertEqual(pre_indexes, post_indexes)

    def test_delete_idx_unescaped(self):
        pre_indexes = self.db.list_indexes()
        ret = self.db.create_index(["bing"], name="idx_del_2")
        assert ret is True
        for idx in self.db.list_indexes():
            if idx["name"] != "idx_del_2":
                continue
            self.assertEqual(idx["def"]["fields"], [{"bing": "asc"}])
            self.db.delete_index(idx["ddoc"], idx["name"])
        post_indexes = self.db.list_indexes()
        self.assertEqual(pre_indexes, post_indexes)

    def test_delete_idx_no_design(self):
        pre_indexes = self.db.list_indexes()
        ret = self.db.create_index(["bing"], name="idx_del_3")
        assert ret is True
        for idx in self.db.list_indexes():
            if idx["name"] != "idx_del_3":
                continue
            self.assertEqual(idx["def"]["fields"], [{"bing": "asc"}])
            self.db.delete_index(idx["ddoc"].split("/")[-1], idx["name"])
        post_indexes = self.db.list_indexes()
        self.assertEqual(pre_indexes, post_indexes)

    def test_bulk_delete(self):
        fields = ["field1"]
        ret = self.db.create_index(fields, name="idx_01")
        assert ret is True

        fields = ["field2"]
        ret = self.db.create_index(fields, name="idx_02")
        assert ret is True

        fields = ["field3"]
        ret = self.db.create_index(fields, name="idx_03")
        assert ret is True

        docids = []

        for idx in self.db.list_indexes():
            if idx["ddoc"] is not None:
                docids.append(idx["ddoc"])

        docids.append("_design/this_is_not_an_index_name")

        ret = self.db.bulk_delete(docids)

        self.assertEqual(ret["fail"][0]["id"], "_design/this_is_not_an_index_name")
        self.assertEqual(len(ret["success"]), 3)

        for idx in self.db.list_indexes():
            assert idx["type"] != "json"
            assert idx["type"] != "text"

    def test_recreate_index(self):
        pre_indexes = self.db.list_indexes()
        for i in range(5):
            ret = self.db.create_index(["bing"], name="idx_recreate")
            assert ret is True
            for idx in self.db.list_indexes():
                if idx["name"] != "idx_recreate":
                    continue
                self.assertEqual(idx["def"]["fields"], [{"bing": "asc"}])
                self.db.delete_index(idx["ddoc"], idx["name"])
                break
            post_indexes = self.db.list_indexes()
            self.assertEqual(pre_indexes, post_indexes)

    def test_delete_missing(self):
        # Missing design doc
        try:
            self.db.delete_index("this_is_not_a_design_doc_id", "foo")
        except Exception as e:
            self.assertEqual(e.response.status_code, 404)
        else:
            raise AssertionError("bad index delete")

        # Missing view name
        ret = self.db.create_index(["fields"], name="idx_01")
        indexes = self.db.list_indexes()
        not_special = [idx for idx in indexes if idx["type"] != "special"]
        idx = random.choice(not_special)
        ddocid = idx["ddoc"].split("/")[-1]
        try:
            self.db.delete_index(ddocid, "this_is_not_an_index_name")
        except Exception as e:
            self.assertEqual(e.response.status_code, 404)
        else:
            raise AssertionError("bad index delete")

        # Bad view type
        try:
            self.db.delete_index(ddocid, idx["name"], idx_type="not_a_real_type")
        except Exception as e:
            self.assertEqual(e.response.status_code, 404)
        else:
            raise AssertionError("bad index delete")

    def test_limit_skip_index(self):
        fields = ["field1"]
        ret = self.db.create_index(fields, name="idx_01")
        assert ret is True

        fields = ["field2"]
        ret = self.db.create_index(fields, name="idx_02")
        assert ret is True

        fields = ["field3"]
        ret = self.db.create_index(fields, name="idx_03")
        assert ret is True

        fields = ["field4"]
        ret = self.db.create_index(fields, name="idx_04")
        assert ret is True

        fields = ["field5"]
        ret = self.db.create_index(fields, name="idx_05")
        assert ret is True

        self.assertEqual(len(self.db.list_indexes(limit=2)), 2)
        self.assertEqual(len(self.db.list_indexes(limit=5, skip=4)), 2)
        self.assertEqual(len(self.db.list_indexes(skip=5)), 1)
        self.assertEqual(len(self.db.list_indexes(skip=6)), 0)
        self.assertEqual(len(self.db.list_indexes(skip=100)), 0)
        self.assertEqual(len(self.db.list_indexes(limit=10000000)), 6)

        try:
            self.db.list_indexes(skip=-1)
        except Exception as e:
            self.assertEqual(e.response.status_code, 500)

        try:
            self.db.list_indexes(limit=0)
        except Exception as e:
            self.assertEqual(e.response.status_code, 500)

    def test_out_of_sync(self):
        self.db.save_docs(copy.deepcopy(DOCS))
        self.db.create_index(["age"], name="age")

        selector = {"age": {"$gt": 0}}
        docs = self.db.find(
            selector, use_index="_design/a017b603a47036005de93034ff689bbbb6a873c4"
        )
        self.assertEqual(len(docs), 2)

        self.db.delete_doc("1")

        docs1 = self.db.find(
            selector,
            update="False",
            use_index="_design/a017b603a47036005de93034ff689bbbb6a873c4",
        )
        self.assertEqual(len(docs1), 1)


@unittest.skipUnless(mango.has_text_service(), "requires text service")
class IndexCrudTextTests(mango.DbPerClass):
    def setUp(self):
        self.db.recreate()

    def test_create_text_idx(self):
        fields = [
            {"name": "stringidx", "type": "string"},
            {"name": "booleanidx", "type": "boolean"},
        ]
        ret = self.db.create_text_index(fields=fields, name="text_idx_01")
        assert ret is True
        for idx in self.db.list_indexes():
            if idx["name"] != "text_idx_01":
                continue
            self.assertEqual(
                idx["def"]["fields"],
                [{"stringidx": "string"}, {"booleanidx": "boolean"}],
            )
            return
        raise AssertionError("index not created")

    def test_create_bad_text_idx(self):
        bad_fields = [
            True,
            False,
            "bing",
            2.0,
            ["foo", "bar"],
            [{"name": "foo2"}],
            [{"name": "foo3", "type": "garbage"}],
            [{"type": "number"}],
            [{"name": "age", "type": "number"}, {"name": "bad"}],
            [{"name": "age", "type": "number"}, "bla"],
            [{"name": "", "type": "number"}, "bla"],
        ]
        for fields in bad_fields:
            try:
                self.db.create_text_index(fields=fields)
            except Exception as e:
                self.assertEqual(e.response.status_code, 400)
            else:
                raise AssertionError("bad create text index")

    def test_limit_skip_index(self):
        fields = ["field1"]
        ret = self.db.create_index(fields, name="idx_01")
        assert ret is True

        fields = ["field2"]
        ret = self.db.create_index(fields, name="idx_02")
        assert ret is True

        fields = ["field3"]
        ret = self.db.create_index(fields, name="idx_03")
        assert ret is True

        fields = ["field4"]
        ret = self.db.create_index(fields, name="idx_04")
        assert ret is True

        fields = [
            {"name": "stringidx", "type": "string"},
            {"name": "booleanidx", "type": "boolean"},
        ]
        ret = self.db.create_text_index(fields=fields, name="idx_05")
        assert ret is True

        self.assertEqual(len(self.db.list_indexes(limit=2)), 2)
        self.assertEqual(len(self.db.list_indexes(limit=5, skip=4)), 2)
        self.assertEqual(len(self.db.list_indexes(skip=5)), 1)
        self.assertEqual(len(self.db.list_indexes(skip=6)), 0)
        self.assertEqual(len(self.db.list_indexes(skip=100)), 0)
        self.assertEqual(len(self.db.list_indexes(limit=10000000)), 6)

        try:
            self.db.list_indexes(skip=-1)
        except Exception as e:
            self.assertEqual(e.response.status_code, 500)

        try:
            self.db.list_indexes(limit=0)
        except Exception as e:
            self.assertEqual(e.response.status_code, 500)