Source

whoosh / tests / test_backends.py

Full commit
  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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
import random
import tempfile

import pytest

from whoosh.kv import db as kvdb
from whoosh.compat import b, xrange
from whoosh.util import random_bytes, random_name
from whoosh.util.loading import find_object
from whoosh.util.testing import TempDir


tempfile.tempdir = "/Users/matt/dev/temp"


dblist = [
    "whoosh.kv.zodb.Zodb",
    "whoosh.kv.memory.MemoryDB",
    "whoosh.kv.blueline.Blueline",
    # "whoosh.kv.bsd.BSD",
    "whoosh.kv.pylmdb.LMDB",
    "whoosh.kv.plyveldb.Plyvel",
    "whoosh.kv.sqlite.Sqlite",
    # "whoosh.kv.tcdb.TC",
    # "whoosh.kv.dbm.DBM",
]


class DBEnv(object):
    def __init__(self, dbname):
        self.cls = find_object(dbname)
        self.obj = None

    def __enter__(self):
        self.obj = self.cls.temp()
        self.obj.create()
        return self.obj

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.obj.destroy()


@pytest.mark.parametrize("dbname", dblist)
def test_writable_method(dbname):
    with DBEnv(dbname) as db:
        with db.open(write=True) as w:
            assert w.writable()
            w[b"a"] = b"b"

        with db.open() as r:
            assert not r.writable()
            with pytest.raises(kvdb.ReadOnlyError):
                r[b"b"] = b"c"
            with pytest.raises(kvdb.ReadOnlyError):
                del r[b"a"]
            with pytest.raises(kvdb.ReadOnlyError):
                r.update({b"c": b"d"})


@pytest.mark.parametrize("dbname", dblist)
def test_write_read(dbname):
    with DBEnv(dbname) as db:
        items = [
            (b"alfa", b"bravo"),
            (b"charlie", b"delta"),
            (b"echo", b"foxtrot"),
            (b"golf", b"hotel"),
            (b"india", b"juliet"),
            (b"kilo", b"lima"),
            (b"mike", b"november"),
            (b"oskar", b"papa"),
            (b"quebec", b"romeo"),
        ]
        ritems = list(items)
        random.shuffle(ritems)

        with db.open(write=True) as w:
            assert isinstance(w, kvdb.DBWriter)
            for k, v in ritems:
                w[k] = v
            assert list(w.keys()) == [item[0] for item in items]
            assert list(w.items()) == items

        with db.open() as r:
            # assert not isinstance(r, kvdb.DBWriter)
            for k, v in items:
                assert r[k] == v
            assert list(r.keys()) == [item[0] for item in items]
            assert list(r.items()) == items


@pytest.mark.parametrize("dbname", dblist)
def test_len(dbname):
    with DBEnv(dbname) as db:
        with db.open(write=True) as w:
            w[b"alfa"] = b"bravo"
            w[b"charlie"] = b"delta"
            assert len(w) == 2

            w[b"echo"] = b"foxtrot"
            assert len(w) == 3

        with db.open() as r:
            assert len(r) == 3


@pytest.mark.parametrize("dbname", dblist)
def test_rewrite(dbname):
    with DBEnv(dbname) as db:
        with db.open(write=True) as w:
            w[b"alfa"] = b"bravo"
            w[b"charlie"] = b"delta"

        with db.open(write=True) as w:
            w[b"alfa"] = b"echo"
            w[b"foxtrot"] = b"golf"

            assert w[b"alfa"] == b"echo"
            assert w[b"foxtrot"] == b"golf"

        with db.open() as r:
            assert r[b"alfa"] == b"echo"
            assert r[b"charlie"] == b"delta"
            assert r[b"foxtrot"] == b"golf"


@pytest.mark.parametrize("dbname", dblist)
def test_roundtrip(dbname):
    with DBEnv(dbname) as db:
        data = [(random_bytes(10), random_bytes(128)) for _ in xrange(1000)]
        with db.open(write=True) as w:
            for k, v in data:
                w[k] = v

        with db.open() as r:
            for k, v in data:
                assert r[k] == v


@pytest.mark.parametrize("dbname", dblist)
def test_empty_value(dbname):
    with DBEnv(dbname) as db:
        with db.open(write=True) as w:
            w[b"key"] = b""

        with db.open() as r:
            assert r[b"key"] == b""


@pytest.mark.parametrize("dbname", dblist)
def test_db_find(dbname):
    with DBEnv(dbname) as db:
        keys = b"alfa bravo charlie delta echo foxtrot".split()
        with db.open(write=True) as w:
            for key in keys:
                w[key] = b"x"

            assert w.find(b"") == b"alfa"
            assert w.find(b"b") == b"bravo"
            assert w.find(b"delta") == b"delta"
            assert w.find(b"f") == b"foxtrot"
            assert w.find(b"g") is None

        with db.open() as r:
            assert r.find(b"") == b"alfa"
            assert r.find(b"b") == b"bravo"
            assert r.find(b"delta") == b"delta"
            assert r.find(b"g") is None

        with db.open(write=True) as w:
            w[b"foo"] = b"y"

            assert w.find(b"f") == b"foo"


@pytest.mark.parametrize("dbname", dblist)
def test_db_key_range(dbname):
    with DBEnv(dbname) as db:
        keys = b"a c e g".split()
        with db.open(write=True) as w:
            for key in keys:
                w[key] = b"x"

        with db.open() as r:
            assert list(r.key_range(b"a", b"g")) == b"a c e".split()
            assert list(r.key_range(b"b", b"f")) == b"c e".split()
            assert list(r.key_range(b"", b"\xff")) == b"a c e g".split()


@pytest.mark.parametrize("dbname", dblist)
def test_writer_range(dbname):
    with DBEnv(dbname) as db:
        def make_key_range(start, end):
            ks = [b("%04d" % i) for i in xrange(start, end)]
            random.shuffle(ks)
            return ks

        k1 = make_key_range(10, 20)
        k2 = make_key_range(30, 50)
        k3 = make_key_range(40, 60)

        leftkey = b"0015"
        rightkey = b"0045"

        with db.open(write=True) as w:
            for k in k1 + k3:
                w[k] = b"x"

        with db.open(write=True) as w:
            for k in k1 + k3:
                assert k in w

            allkeys = sorted(k1 + k3)
            lefti = allkeys.index(leftkey)
            righti = allkeys.index(rightkey)
            assert list(w.key_range(leftkey, rightkey)) == allkeys[lefti:righti]

            for k in k2:
                w[k] = b"y"

            allkeys = sorted(set(k1 + k2 + k3))
            lefti = allkeys.index(leftkey)
            righti = allkeys.index(rightkey)
            keys = list(w.key_range(leftkey, rightkey))
            print("keys=", keys)
            print("    =", allkeys[lefti:righti])
            assert keys == allkeys[lefti:righti]


@pytest.mark.parametrize("dbname", dblist)
def test_missing(dbname):
    with DBEnv(dbname) as db:
        with db.open(write=True) as w:
            w[b"alfa"] = b"bravo"

        with db.open() as r:
            assert r[b"alfa"] == b"bravo"
            assert r.get(b"foo") is None
            assert r.get(b"foo", b"x") == b"x"
            with pytest.raises(KeyError):
                _ = r[b"foo"]


@pytest.mark.parametrize("dbname", dblist)
def test_db_expand_prefix(dbname):
    with DBEnv(dbname) as db:
        keys = b"good alfa apple able apply ace ape aping are base".split()
        with db.open(write=True) as w:
            for key in keys:
                w[key] = b"x"

        with db.open() as r:
            assert list(r.expand_prefix(b"ap")) == b"ape aping apple apply".split()


@pytest.mark.parametrize("dbname", dblist)
def test_contains(dbname):
    with DBEnv(dbname) as db:
        with db.open(write=True) as w:
            w[b"alfa"] = b"bravo"
            w[b"charlie"] = b"delta"
            w[b"echo"] = b"foxtrot"

        with db.open() as r:
            assert b"alfa" in r
            assert b"charlie" in r
            assert b"echo" in r
            assert b"bravo" not in r
            assert b"foo" not in r


@pytest.mark.parametrize("dbname", dblist)
def test_delete(dbname):
    with DBEnv(dbname) as db:
        with db.open(write=True) as w:
            w[b"alfa"] = b"bravo"
            w[b"charlie"] = b"delta"
            w[b"echo"] = b"foxtrot"

        with db.open(write=True) as w:
            del w[b"alfa"]
            assert len(w) == 2
            del w[b"echo"]
            assert len(w) == 1

            # Implementations need to silently ignore trying to delete a
            # non-existant key (unfortunately)
            del w[b"alfa"]
            del w[b"foo"]

            assert b"alfa" not in w
            assert b"echo" not in w
            assert list(w.keys()) == [b"charlie"]

        with db.open() as r:
            assert b"alfa" not in r
            assert b"echo" not in r
            assert len(r) == 1
            assert list(r.keys()) == [b"charlie"]

        with db.open(write=True) as w:
            del w[b"alfa"]
            del w[b"foo"]

            del w[b"charlie"]
            assert len(w) == 0
            assert not w


@pytest.mark.parametrize("dbname", dblist)
def test_optimize(dbname):
    with DBEnv(dbname) as db:
        with db.open(write=True) as w:
            for i in xrange(100):
                w[b("%08d" % i)] = b(hex(i))
            assert len(w) == 100

        with db.open(write=True) as w:
            for i in xrange(10, 90):
                del w[b("%08d" % i)]
            assert len(w) == 20

            w.optimize()
            assert len(w) == 20


@pytest.mark.parametrize("dbname", dblist)
def test_fuzzing(dbname):
    with DBEnv(dbname) as db:
        data = {}
        commands = []
        with db.open(write=True) as w:
            for _ in xrange(1000):
                op = random.randint(0, 2)
                key = val = None
                if op == 0:  # Set
                    key = random_bytes(32)
                    val = random_bytes(32)
                elif op == 1 and data:  # Compare
                    key = random.choice(list(data))
                elif op == 2 and data:  # Delete
                    key = random.choice(list(data))
                commands.append((op, key, val))

                if op == 0:
                    data[key] = val
                    w[key] = val
                elif op == 1 and data:
                    assert w[key] == data[key]
                elif op == 2 and data:
                    del data[key]
                    del w[key]

        with db.open() as r:
            assert list(r.items()) == sorted(data.items())


@pytest.mark.parametrize("dbname", dblist)
def test_cursor(dbname):
    with DBEnv(dbname) as db:
        vs = []
        with db.open(write=True) as w:
            for i in xrange(0, 1000, 3):
                k = ("%08d" % i).encode("ascii")
                v = random_bytes(64)
                vs.append((k, v))
                w[k] = v
        vs.sort()

        with db.open() as r:
            assert list(r.keys())

            cur = r.cursor()

            assert cur.is_active()
            assert cur.key() == vs[0][0]
            assert cur.value() == vs[0][1]
            cur.next()
            assert cur.key() == vs[1][0]
            assert cur.value() == vs[1][1]

            pos = 54
            cur.find(vs[pos][0])
            assert cur.is_active()
            assert cur.key() == vs[pos][0]
            assert cur.value() == vs[pos][1]

            cur.find(b"00000298")
            assert cur.is_active()
            assert cur.key() == vs[100][0]
            assert cur.value() == vs[100][1]

        with db.open(write=True) as w:
            assert len(vs) == 334
            todel = (
                list(xrange(0, 10))
                + list(xrange(11, 300))
                + list(xrange(301, 334))
            )
            for i in todel:
                del w[vs[i][0]]

            cur = w.cursor()
            assert cur.is_active()
            assert cur.key() == vs[10][0]
            assert cur.value() == vs[10][1]

            cur.next()
            assert cur.is_active()
            assert cur.key() == vs[300][0]
            assert cur.value() == vs[300][1]

            cur.next()
            assert not cur.is_active()

            cur = w.cursor()
            cur.find(vs[200][0])
            assert cur.key() == vs[300][0]

            cur = w.cursor()
            cur.find(vs[310][0])
            assert not cur.is_active()


@pytest.mark.parametrize("dbname", dblist)
def test_backwards_find(dbname):
    with DBEnv(dbname) as db:
        with db.open(write=True) as w:
            w[b"alfa"] = b"bravo"
            w[b"charlie"] = b"delta"
            w[b"echo"] = b"foxtrot"
            w[b"golf"] = b"hotel"

        with db.open() as r:
            cur = r.cursor()
            cur.find(b"d")
            assert cur.key() == b"echo"
            cur.find(b"b")
            assert cur.key() == b"charlie"


@pytest.mark.parametrize("dbname", dblist)
def test_invalid_cursor(dbname):
    with DBEnv(dbname) as db:
        with db.open(write=True) as w:
            w[b"alfa"] = b"bravo"
            w[b"charlie"] = b"delta"
            w[b"echo"] = b"foxtrot"
            w[b"golf"] = b"hotel"

        with db.open() as r:
            cur = r.cursor()
            cur.find(b"india")
            assert not cur.is_active()
            assert cur.key() is None
            assert cur.value() is None

            cur = r.cursor()
            cur.find(b"go")
            cur.next()
            assert not cur.is_active()
            assert cur.key() is None
            assert cur.value() is None
            with pytest.raises(kvdb.OverrunError):
                cur.next()


@pytest.mark.parametrize("dbname", dblist)
def test_idempotent(dbname):
    with DBEnv(dbname) as db:
        db.create()
        db.create()
        db.destroy()
        db.destroy()


@pytest.mark.parametrize("dbname", dblist)
def test_deletion(dbname):
    keys = [random_bytes(16) for _ in xrange(100)]
    with DBEnv(dbname) as db:
        with db.open(write=True) as w:
            for k in keys:
                w[k] = k

        random.shuffle(keys)
        with db.open(write=True) as w:
            for k in keys:
                del w[k]

            # assert len(w) == 0

        with db.open() as r:
            assert len(r) == 0


@pytest.mark.parametrize("dbname", dblist)
def test_empty_value(dbname):
    empty = b""
    with DBEnv(dbname) as db:
        with db.open(write=True) as w:
            for i in xrange(512):
                w[b(str(i))] = empty

        with db.open() as r:
            for i in xrange(512):
                assert r[b(str(i))] == empty


@pytest.mark.parametrize("dbname", dblist)
def test_empty_out(dbname):
    with DBEnv(dbname) as db:
        with db.open(write=True) as w:
            for i in xrange(512):
                w[b(str(i))] = b"x"

        with db.open(write=True) as w:
            for i in xrange(512):
                del w[b(str(i))]

        with db.open() as r:
            assert len(r) == 0
            assert not r
            for i in xrange(512):
                assert b(str(i)) not in r


@pytest.mark.parametrize("dbname", dblist)
def test_delete_by_prefix(dbname):
    keys = b"01 02 03 04 05 11 12 13 14 15 20 21 22 23 24 25".split()

    with DBEnv(dbname) as db:
        with db.open(write=True) as w:
            for key in keys:
                w[key] = b"x"

        with db.open(write=True) as w:
            w.delete_by_prefix(b"1")
            assert b" ".join(w) == b"01 02 03 04 05 20 21 22 23 24 25"

        with db.open() as r:
            assert b" ".join(r) == b"01 02 03 04 05 20 21 22 23 24 25"
            assert b"12" not in r


@pytest.mark.parametrize("dbname", dblist)
def test_closed_attr(dbname):
    with DBEnv(dbname) as db:
        with db.open(write=True) as w:
            assert not w.closed
            w[b"foo"] = b"bar"
        assert w.closed

        with db.open() as r:
            assert r[b"foo"] == b"bar"
            assert not r.closed
        assert r.closed


@pytest.mark.parametrize("dbname", dblist)
def test_rollback(dbname):
    with DBEnv(dbname) as db:
        with db.open(write=True, create=True) as w:
            w[b"alfa"] = b"bravo"
            w[b"charlie"] = b"delta"
            w[b"echo"] = b"foxtrot"

        with db.open(write=True) as w:
            w[b"foo"] = b"bar"
            w[b"echo"] = b"echo"
            del w[b"charlie"]
            w.cancel()

        with db.open() as r:
            assert b"foo" not in r
            assert b"charlie" in r
            assert r[b"echo"] == b"foxtrot"