Esempio n. 1
0
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"]
Esempio n. 2
0
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
Esempio n. 3
0
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"
Esempio n. 4
0
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"]
Esempio n. 5
0
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
Esempio n. 6
0
def make(name, prefix):
    prefix = list(prefix)
    prefix_tokens = tuple(prefix + PSTORE_SUFFIX_TOKENS)
    tokens = nstore.make('{}/token'.format(name), prefix_tokens, 2)
    out = PStore(
        name,
        # Use nstore with n=2 to be able to go from a string token to an uid,
        # and back from an uid to a token string.
        tokens,
        # TODO: Replace with a multi-dict (mstore) dedicated store.
        # The value is always empty.
        tuple(prefix + PSTORE_SUFFIX_INDEX),
        # That will map bag uid to a counter serialized to json and
        # compressed with zstd. It is a good old key-value store.
        tuple(prefix + PSTORE_SUFFIX_COUNTERS),
        None,
    )
    return out
Esempio n. 7
0
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
Esempio n. 8
0
async def test_nstore_empty():
    ntest = nstore.make("test-name", [42], 3)
    assert ntest