~amirouche/asyncio-foundationdb

ref: e24a647742c28f2e7885b444b4983c0f19c8c57a asyncio-foundationdb/tests.py -rw-r--r-- 16.2 KiB
e24a6477Amirouche release v0.10.4 8 months ago
                                                                                
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
581
import asyncio
from uuid import uuid4

import pytest
from fdb.tuple import SingleFloat

import found
import found.base
from found import nstore
from found.nstore import var
from found import bstore
from found import eavstore


def test_pack_unpack():
    value = (
        (uuid4(), None, SingleFloat(3.1415), b"x42", 1, -1, 3.1415, -3.1415, ("abc",)),
        ("d", "e", "f"),
        2.718281828459045,
    )  # noqa
    assert found.unpack(found.pack(value)) == value


async def open():
    # XXX: hack around the fact that the loop is cached in found
    loop = asyncio.get_event_loop()
    found.base._loop = loop

    db = await found.open()

    async def purge(tx):
        found.clear(tx, b"", b"\xff")

    await found.transactional(db, purge)

    return db


@pytest.mark.asyncio
async def test_get():
    # prepare
    db = await open()

    async def test0(tx):
        out = await found.get(tx, b"test")
        # check
        assert out is None

    # exec
    await found.transactional(db, test0)

    async def test1(tx):
        found.set(tx, b"test", b"test")
        out = await found.get(tx, b"test")
        # check
        assert out == b"test"

    # exec
    await found.transactional(db, test1)


async def aiolist(aiogenerator):
    out = []
    async for item in aiogenerator:
        out.append(item)
    return out


@pytest.mark.asyncio
async def test_query():
    # prepare
    db = await open()

    async def set(tx):
        for number in range(10):
            found.set(tx, found.pack((number,)), found.pack((str(number),)))

    await found.transactional(db, set)

    async def query(tx):
        out = found.query(tx, found.pack((1,)), found.pack((8,)))
        out = await aiolist(out)
        return out

    out = await found.transactional(db, query)
    for (key, value), index in zip(out, range(10)[1:-1]):
        assert found.unpack(key)[0] == index
        assert found.unpack(value)[0] == str(index)


@pytest.mark.asyncio
async def test_next_prefix():
    # prepare
    db = await open()

    prefix_zero = b"\x00"
    prefix_one = b"\x01"

    # exec
    async def test(tx):
        found.set(tx, prefix_zero + b"\x01", found.pack((1,)))
        found.set(tx, prefix_zero + b"\x02", found.pack((2,)))
        found.set(tx, prefix_zero + b"\x03", found.pack((3,)))
        found.set(tx, prefix_one + b"\x42", found.pack((42,)))

    await found.transactional(db, test)

    # check
    async def query0(tx):
        everything = found.query(tx, b'', b'\xFF')
        everything = await aiolist(everything)
        assert len(everything) == 4

    await found.transactional(db, query0)

    # check
    async def query1(tx):
        everything = found.query(tx, prefix_zero, found.next_prefix(prefix_zero))
        everything = await aiolist(everything)
        assert len(everything) == 3

    await found.transactional(db, query1)


@pytest.mark.asyncio
async def test_read_version():
    # prepare
    db = await open()

    # exec
    async def read_version(tx):
        out = await found.read_version(tx)
        assert out

    await found.transactional(db, read_version)


@pytest.mark.asyncio
async def test_estimated_size_bytes():
    # prepare
    db = await open()

    # initial check

    out = await found.transactional(db, found.estimated_size_bytes, b'\x00', b'\xFF')
    assert out == 0

    # populate with a small set of keys and check

    for i in range(10):
        await found.transactional(db, found.co(found.set), bytes((i,)), b'\xFF' * found.MAX_SIZE_VALUE)

    out = await found.transactional(db, found.estimated_size_bytes, b'\x00', b'\xFF')
    # below 3 MB the estimated size is less accurate, only check it is positive
    assert 0 < out

    # populate with a large set of keys and check
    for i in range(100):
        await found.transactional(db, found.co(found.set), bytes((i,)), b'\xFF' * found.MAX_SIZE_VALUE)

    out = await found.transactional(db, found.estimated_size_bytes, b'\x00', b'\xFF')
    # estimated size hence approximate check
    assert abs(out - (found.MAX_SIZE_VALUE * 100)) < found.MAX_SIZE_VALUE


# nstore tests

@pytest.mark.asyncio
async def test_nstore_empty():
    ntest = nstore.make("test-name", [42], 3)
    assert ntest


@pytest.mark.asyncio
async def test_nstore_simple_single_item_db_subject_lookup():
    db = await open()
    ntest = nstore.make("test-name", [42], 3)

    expected = uuid4()

    async def prepare(tx):
        nstore.add(tx, ntest, expected, "title", "hyper.dev")

    await found.transactional(db, prepare)

    async def query(tx):
        out = await aiolist(nstore.select(tx, ntest, var("subject"), "title", "hyper.dev"))
        return out

    out = await found.transactional(db, query)
    out = out[0]["subject"]

    assert out == expected


@pytest.mark.asyncio
async def test_nstore_ask_rm_and_ask():
    db = await open()
    ntest = nstore.make("test-name", [42], 3)

    expected = uuid4()

    async def get(tx):
        out = await nstore.get(tx, ntest, expected, "title", "hyper.dev")
        return out

    out = await found.transactional(db, get)

    assert out is None

    async def add(tx):
        nstore.add(tx, ntest, expected, "title", "hyper.dev")

    await found.transactional(db, add)
    out = await found.transactional(db, get)
    assert out == b''

    async def remove(tx):
        nstore.remove(tx, ntest, expected, "title", "hyper.dev")

    await found.transactional(db, remove)

    out = await found.transactional(db, get)
    assert out is None


@pytest.mark.asyncio
async def test_nstore_simple_multiple_items_db_subject_lookup():
    db = await open()

    ntest = nstore.make("test-name", [42], 3)

    expected = uuid4()

    async def prepare(tx):
        nstore.add(tx, ntest, expected, "title", "hyper.dev")
        nstore.add(tx, ntest, uuid4(), "title", "blog.copernic.com")

    async def query(tx):
        out = await aiolist(nstore.select(tx, ntest, var("subject"), "title", "hyper.dev"))
        return out

    await found.transactional(db, prepare)
    out = await found.transactional(db, query)
    out = out[0]["subject"]
    assert out == expected


@pytest.mark.asyncio
async def test_nstore_complex():
    db = await open()

    ntest = nstore.make("test-name", [42], 3)

    async def prepare(tx):
        hyperdev = uuid4()
        nstore.add(tx, ntest, hyperdev, "title", "hyper.dev")
        nstore.add(tx, ntest, hyperdev, "keyword", "scheme")
        nstore.add(tx, ntest, hyperdev, "keyword", "hacker")
        copernic = uuid4()
        nstore.add(tx, ntest, copernic, "title", "blog.copernic.com")
        nstore.add(tx, ntest, copernic, "keyword", "corporate")

    async def query(tx):
        seed = nstore.select(tx, ntest, var("identifier"), "keyword", "hacker")
        out = await aiolist(nstore.where(
            tx, ntest, seed, var("identifier"), "title", var("blog")
        ))
        return out

    await found.transactional(db, prepare)
    out = await found.transactional(db, query)
    out = sorted([x["blog"] for x in out])
    assert out == ["hyper.dev"]


@pytest.mark.asyncio
async def test_nstore_seed_subject_variable():
    db = await open()
    ntest = nstore.make("test-name", [42], 3)

    hyperdev = uuid4()
    copernic = uuid4()
    hypersocial = uuid4()

    async def prepare(tx):
        nstore.add(tx, ntest, hyperdev, "title", "hyper.dev")
        nstore.add(tx, ntest, hyperdev, "keyword", "scheme")
        nstore.add(tx, ntest, hyperdev, "keyword", "hacker")

        nstore.add(tx, ntest, copernic, "title", "copernic.space")
        nstore.add(tx, ntest, copernic, "keyword", "corporate")

        nstore.add(tx, ntest, hypersocial, "title", "hypersocial.space")
        nstore.add(tx, ntest, hypersocial, "keyword", "python")
        nstore.add(tx, ntest, hypersocial, "keyword", "hacker")

    async def query(tx):
        out = await aiolist(nstore.select(tx, ntest, var("subject"), "keyword", "corporate"))
        return out

    await found.transactional(db, prepare)
    out = await found.transactional(db, query)
    out = out[0]["subject"]

    assert out == copernic


@pytest.mark.asyncio
async def test_nstore_seed_subject_lookup():
    db = await open()

    ntest = nstore.make("test-name", [42], 3)

    hyperdev = uuid4()
    copernic = uuid4()
    hypersocial = uuid4()

    async def prepare(tx):
        nstore.add(tx, ntest, hyperdev, "title", "hyper.dev")
        nstore.add(tx, ntest, hyperdev, "keyword", "scheme")
        nstore.add(tx, ntest, hyperdev, "keyword", "hacker")

        nstore.add(tx, ntest, copernic, "title", "blog.copernic.com")
        nstore.add(tx, ntest, copernic, "keyword", "corporate")

        nstore.add(tx, ntest, hypersocial, "title", "hypersocial.space")
        nstore.add(tx, ntest, hypersocial, "keyword", "python")
        nstore.add(tx, ntest, hypersocial, "keyword", "hacker")

    async def query(tx):
        out = await aiolist(nstore.select(tx, ntest, copernic, var("key"), var("value")))
        return out

    await found.transactional(db, prepare)
    out = await found.transactional(db, query)
    out = [dict(x) for x in out]

    expected = [
        {"key": "keyword", "value": "corporate"},
        {"key": "title", "value": "blog.copernic.com"},
    ]
    assert out == expected


@pytest.mark.asyncio
async def test_nstore_seed_object_variable():
    db = await open()
    ntest = nstore.make("test-name", [42], 3)

    hyperdev = uuid4()
    copernic = uuid4()
    hypersocial = uuid4()

    async def prepare(tx):
        nstore.add(tx, ntest, hyperdev, "title", "hyper.dev")
        nstore.add(tx, ntest, hyperdev, "keyword", "scheme")
        nstore.add(tx, ntest, hyperdev, "keyword", "hacker")

        nstore.add(tx, ntest, copernic, "title", "blog.copernic.com")
        nstore.add(tx, ntest, copernic, "keyword", "corporate")

        nstore.add(tx, ntest, hypersocial, "title", "hypersocial.space")
        nstore.add(tx, ntest, hypersocial, "keyword", "python")
        nstore.add(tx, ntest, hypersocial, "keyword", "hacker")

    async def query(tx):
        out = await aiolist(nstore.select(tx, ntest, hyperdev, "title", var("title")))
        return out

    await found.transactional(db, prepare)
    out = await found.transactional(db, query)
    out = out[0]["title"]
    assert out == "hyper.dev"


@pytest.mark.asyncio
async def test_nstore_subject_variable():
    db = await open()
    ntest = nstore.make("test-name", [42], 3)
    hyperdev = uuid4()
    post1 = uuid4()
    post2 = uuid4()

    async def prepare(tx):
        nstore.add(tx, ntest, hyperdev, "title", "hyper.dev")
        nstore.add(tx, ntest, hyperdev, "keyword", "scheme")
        nstore.add(tx, ntest, hyperdev, "keyword", "hacker")
        nstore.add(tx, ntest, post1, "blog", hyperdev)
        nstore.add(tx, ntest, post1, "title", "hoply is awesome")
        nstore.add(tx, ntest, post2, "blog", hyperdev)
        nstore.add(tx, ntest, post2, "title", "hoply foundiple store")

    # exec, fetch all blog title from hyper.dev

    async def query(tx):
        query = nstore.select(tx, ntest, var("blog"), "title", "hyper.dev")
        query = nstore.where(tx, ntest, query, var("post"), "blog", var("blog"))
        out = await aiolist(nstore.where(tx, ntest, query, var("post"), "title", var("title")))
        return out

    await found.transactional(db, prepare)
    out = await found.transactional(db, query)

    out = sorted([x["title"] for x in out])
    assert out == ["hoply foundiple store", "hoply is awesome"]


@pytest.mark.asyncio
async def test_nstore_query():
    db = await open()

    ntest = nstore.make("test-name", [42], 3)

    async def prepare(tx):
        hyperdev = uuid4()
        post1 = uuid4()
        post2 = uuid4()
        nstore.add(tx, ntest, hyperdev, "title", "hyper.dev")
        nstore.add(tx, ntest, hyperdev, "keyword", "scheme")
        nstore.add(tx, ntest, hyperdev, "keyword", "hacker")
        nstore.add(tx, ntest, post1, "blog", hyperdev)
        nstore.add(tx, ntest, post1, "title", "hoply is awesome")
        nstore.add(tx, ntest, post2, "blog", hyperdev)
        nstore.add(tx, ntest, post2, "title", "hoply foundiple store")

    # exec, fetch all blog title from hyper.dev

    async def query(tx):
        query = [
            [var("blog"), "title", "hyper.dev"],
            [var("post"), "blog", var("blog")],
            [var("post"), "title", var("title")],
        ]
        out = await aiolist(nstore.query(tx, ntest, *query))
        return out

    await found.transactional(db, prepare)
    out = await found.transactional(db, query)
    out = sorted([x["title"] for x in out])
    assert out == ["hoply foundiple store", "hoply is awesome"]


# bstore tests

@pytest.mark.asyncio
async def test_bstore_small():
    db = await open()

    store = bstore.make('bstore-test', (42,))

    expected = b'\xBE\xEF'

    uid = await found.transactional(db, bstore.get_or_create, store, expected)
    out = await found.transactional(db, bstore.get, store, uid)

    assert out == expected


@pytest.mark.asyncio
async def test_bstore_large():
    db = await open()

    store = bstore.make('bstore-test', (42,))

    expected = b'\xBE\xEF' * found.MAX_SIZE_VALUE

    uid = await found.transactional(db, bstore.get_or_create, store, expected)
    out = await found.transactional(db, bstore.get, store, uid)

    assert out == expected

@pytest.mark.asyncio
async def test_bstore_idempotent():
    db = await open()

    store = bstore.make('bstore-test', (42,))

    blob = b'\xBE\xEF' * found.MAX_SIZE_VALUE

    expected = await found.transactional(db, bstore.get_or_create, store, blob)
    out = await found.transactional(db, bstore.get_or_create, store, blob)

    assert out == expected

# eavstore tests

@pytest.mark.asyncio
async def test_eavstore_crud():
    db = await open()

    store = eavstore.make('eavstore-test', (42,))

    expected = dict(hello="world")

    uid = await found.transactional(db, found.co(eavstore.create), store, expected)
    out = await found.transactional(db, eavstore.get, store, uid)
    assert out == expected

    other = dict(hello="world", who="me")
    await found.transactional(db, found.co(eavstore.update), store, other)
    out = await found.transactional(db, eavstore.get, store, uid)
    assert out == other

    await found.transactional(db, found.co(eavstore.remove), uid)
    out = await found.transactional(db, eavstore.get, store, uid)
    assert not out


@pytest.mark.asyncio
async def test_eavstore_crud():
    db = await open()

    store = eavstore.make('eavstore-test', (42,))

    expected = set()
    mydict = dict(key=42)
    for _ in range(10):
        uid = await found.transactional(db, found.co(eavstore.create), store, mydict)
        expected.add(uid)

    # add some unwanted dictionaries, with key != 42
    # before 42
    for value in range(42):
        mydict = dict(key=value)
        uid = await found.transactional(db, found.co(eavstore.create), store, mydict)
    # and after 42
    for value in range(43, 84):
        mydict = dict(key=value)
        uid = await found.transactional(db, found.co(eavstore.create), store, mydict)

    # query
    async def query(tx, store, key, value):
        out = set()
        async for uid in eavstore.query(tx, store, key, value):
            out.add(uid)
        return out

    out = await found.transactional(db, query, store, 'key', 42)

    assert out == expected


@pytest.mark.asyncio
async def test_peace_search_store():
    import multiprocessing
    from found import pstore
    from concurrent import futures

    db = await open()

    with futures.ProcessPoolExecutor(max_workers=multiprocessing.cpu_count()) as pool:
        store = pstore.make('test-pstore', (42,), pool)

        DOC0 = dict(
            foundationdb=1,
            okvs=2,
            database=42,
        )
        DOC1 = dict(
            sqlite=1,
            sql=2,
            database=3
        )
        DOC2 = dict(
            spam=42,
        )
        for uid, doc in enumerate((DOC0, DOC1, DOC2)):
            await found.transactional(db, pstore.index, store, uid, doc)

        expected = [(0, 1)]
        out = await found.transactional(db, pstore.search, store, ["foundationdb"], 10)
        assert out == expected

        expected = [(2, 42)]
        out = await found.transactional(db, pstore.search, store, ["spam"], 10)
        assert out == expected

        expected = [(0, 42), (1, 3)]
        out = await found.transactional(db, pstore.search, store, ["database"], 10)
        assert out == expected