Ejemplo n.º 1
0
async def test_update_docs_on_empty(database: Database) -> None:
    async with database.update_docs([]) as bulk:
        pass

    assert bulk._docs == []
    keys = [key async for key in database.akeys()]
    assert len(keys) == 0
Ejemplo n.º 2
0
async def test_update_docs_creating(database: Database) -> None:
    async with database.update_docs(["foobar"], create=True):
        pass

    keys = [key async for key in database.akeys()]

    assert len(keys) == 1
    assert sorted(keys) == ["foobar"]
Ejemplo n.º 3
0
async def test_create_document(database: Database) -> None:
    doc = await database.create("test_document")

    # doc not yet saved, so it shouldn't be listed in the database
    assert doc.id not in [key async for key in database.akeys()]

    await doc.save()

    # doc was saved, so it should be listed in the database
    assert doc.id in [key async for key in database.akeys()]
Ejemplo n.º 4
0
async def test_create_docs_mixed(database: Database) -> None:
    async with database.create_docs(["foo"]) as bulk:
        bulk.create("baz")

    assert bulk.response is not None
    assert len(bulk.response) == 2

    keys = [key async for key in database.akeys()]

    assert len(keys) == 2
    assert sorted(keys) == ["baz", "foo"]
Ejemplo n.º 5
0
async def test_docs_on_deleted(filled_database: Database) -> None:
    doc = await filled_database["foo"]
    await doc.delete()

    with pytest.raises(KeyError):
        async for doc in filled_database.docs(["foo"]):
            assert False

    async for doc in filled_database.docs(["foo"], create=True):
        assert doc.id == "foo"
        assert doc.exists is False
Ejemplo n.º 6
0
async def test_create_docs_mixed(database: Database) -> None:
    async with database.create_docs(["foo"]) as docs:
        docs.create("baz")

    assert docs.status is not None
    assert len(docs.status) == 2

    keys = [key async for key in database.akeys()]

    assert len(keys) == 2
    assert sorted(keys) == ["baz", "foo"]
Ejemplo n.º 7
0
async def test_create_document_with_data(database: Database) -> None:
    doc = await database.create("test_document", data={"foo": 1234})

    assert doc["foo"] == 1234

    # doc not yet saved, so it shouldn't be listed in the database
    assert doc.id not in [key async for key in database.akeys()]

    await doc.save()

    # doc was saved, so it should be listed in the database
    assert doc.id in [key async for key in database.akeys()]
Ejemplo n.º 8
0
async def test_many_docs(large_filled_database: Database) -> None:
    keys = [key async for key in large_filled_database.akeys()]
    assert len(keys) == 2000

    docs = [doc async for doc in large_filled_database.docs()]
    assert len(docs) == 2000

    find_docs = [
        doc async for doc in large_filled_database.find(
            selector={"llama": "awesome"})
    ]
    assert len(find_docs) == 2000
Ejemplo n.º 9
0
async def test_update_docs(database: Database) -> None:
    async with database.update_docs(["foo", "baz"], create=True) as docs:
        async for doc in docs:
            doc["llama"] = "awesome"

    keys = [key async for key in database.akeys()]

    assert len(keys) == 2
    assert sorted(keys) == ["baz", "foo"]

    async for doc in database.values():
        assert "llama" in doc
        assert doc["llama"] == "awesome"
Ejemplo n.º 10
0
async def test_update_docs_for_errored(filled_database: Database) -> None:
    doc = await filled_database["foo"]
    doc["something"] = 42
    async with filled_database.update_docs(["foo", "baz"]) as docs:
        # provoke a conflict for document foo
        await doc.save()

        async for doc in docs:
            doc["thing"] = 42

    assert docs.status is not None
    assert len(docs.status) == 2

    assert docs.ok is not None
    assert len(docs.ok) == 1
    doc_ok = docs.ok[0]
    assert doc_ok.id == "baz"
    assert doc_ok["thing"] == 42
    assert doc_ok["_rev"].startswith("2-")

    assert docs.error is not None
    assert len(docs.error) == 1
    doc_err = docs.error[0]
    assert doc_err.id == "foo"
    assert doc_err["_rev"].startswith("1-")
    assert "something" not in doc_err
Ejemplo n.º 11
0
async def test_docs_on_non_existant(database: Database) -> None:
    docs = [doc async for doc in database.docs(["foo"], create=True)]

    assert len(docs) == 1
    doc = docs[0]
    assert doc._dirty_cache is True
    assert doc.id == "foo"
Ejemplo n.º 12
0
async def listen_for_first_change(database: Database,
                                  **kwargs: Any) -> BaseChangeEvent:
    async for event in database.changes(**kwargs,
                                        feed="continuous",
                                        since="now"):
        return event

    assert False
Ejemplo n.º 13
0
async def test_update_dont_crash_on_pristine_doc(filled_database: Database) -> None:
    doc = await filled_database["foo"]
    doc["llama"] = "awesome"
    await doc.save()

    async with filled_database.update_docs(["foo", "baz"], create=True) as docs:
        async for doc in docs:
            doc["llama"] = "awesome"
Ejemplo n.º 14
0
async def test_docs_contain_ddocs_with_param(
    filled_database_with_view: Database, ) -> None:
    keys = [
        doc.id
        async for doc in filled_database_with_view.docs(include_ddocs=True)
    ]

    assert "_design/test_ddoc" in keys
Ejemplo n.º 15
0
async def test_akeys_with_keys(filled_database: Database) -> None:
    keys = [
        key async for key in filled_database.akeys(
            keys=["foo", "baz", "halloween"])
    ]

    assert len(keys) == 2
    assert keys == ["foo", "baz"]
Ejemplo n.º 16
0
async def test_delete(filled_database: Database) -> None:
    doc = await filled_database["foo"]

    await doc.delete()

    keys = [key async for key in filled_database.akeys()]

    assert len(keys) == 3
    assert sorted(keys) == ["baz", "baz2", "foo2"]
Ejemplo n.º 17
0
async def test_save(database: Database) -> None:
    doc = await database.create("foo42")
    doc["bar"] = True
    await doc.save()

    keys = [key async for key in database.akeys()]

    assert "foo42" in keys
    assert len(keys) == 1
Ejemplo n.º 18
0
async def test_create_docs_with_create_duplicate(database: Database) -> None:
    async with database.create_docs() as docs:
        foo = docs.create("foo")

        # DO NOT DO THIS! This is just using the private interface to test conflict handling.
        assert docs._docs is not None
        docs._docs.append(foo)

    assert docs.status is not None
    assert len(docs.status) == 2

    assert "ok" in docs.status[0]
    assert "error" in docs.status[1]
    assert docs.status[1]["error"] == "conflict"

    keys = [key async for key in database.akeys()]

    assert len(keys) == 1
    assert sorted(keys) == ["foo"]
Ejemplo n.º 19
0
async def test_create_docs_with_create(database: Database) -> None:
    async with database.create_docs() as docs:
        docs.create("foo", data={"counter": 42})
        docs.create("baz")

        with pytest.raises(ValueError):
            docs.create("foo")

    assert docs.status is not None
    assert len(docs.status) == 2

    keys = [key async for key in database.akeys()]

    assert len(keys) == 2
    assert sorted(keys) == ["baz", "foo"]

    foo = await database["foo"]
    assert "counter" in foo
    assert foo["counter"] == 42
Ejemplo n.º 20
0
async def test_find_sorted(filled_database: Database) -> None:
    await filled_database.index({"fields": ["bar2"]})

    matching_docs = [
        doc async for doc in filled_database.find({"bar": True}, sort=["bar2"])
    ]

    assert len(matching_docs) == 3
    assert matching_docs[0]["bar2"] == 1
    assert matching_docs[1]["bar2"] == 2
    assert matching_docs[2]["bar2"] == 3
Ejemplo n.º 21
0
async def test_create_docs_for_existing(filled_database: Database) -> None:
    async with filled_database.create_docs(["new", "foo"]) as bulk:
        bulk.create("baz")

    assert bulk.response is not None
    assert bulk.error is not None
    assert bulk.ok is not None

    assert len(bulk.response) == 3
    assert len(bulk.ok) == 1
    assert len(bulk.error) == 2
    assert bulk.response[1]["error"] == "conflict"
    assert bulk.response[2]["error"] == "conflict"
Ejemplo n.º 22
0
async def test_update_external_documents(filled_database: Database) -> None:
    foo = await filled_database.get("foo")

    assert "zebras" not in foo

    async with filled_database.update_docs() as bulk:
        foo["zebras"] = "awesome 🦓"
        bulk.update(foo)

        with pytest.raises(ValueError):
            bulk.update(foo)

    foo = await filled_database.get("foo")
    assert foo["zebras"] == "awesome 🦓"
Ejemplo n.º 23
0
async def test_update_docs_for_deleted(filled_database: Database) -> None:
    doc = await filled_database["foo"]
    await doc.delete()

    async with filled_database.update_docs(["foo"], create=True) as docs:
        async for doc in docs:
            doc["llama"] = "awesome"

    doc = await filled_database["foo"]

    assert "_deleted" not in doc
    assert "_rev" in doc
    assert doc["_rev"].startswith("3-")
    assert doc["llama"] == "awesome"
Ejemplo n.º 24
0
async def test_find_limited(filled_database: Database) -> None:
    matching_docs = [
        doc async for doc in filled_database.find({"bar": True}, limit=1)
    ]

    assert len(matching_docs) == 1

    from aiocouch.document import Document

    matching_keys = []
    for doc in matching_docs:
        assert isinstance(doc, Document)
        matching_keys.append(doc.id)

    assert "baz2" in matching_keys
Ejemplo n.º 25
0
async def test_values_for_filled_limited(filled_database: Database) -> None:
    keys = [doc.id async for doc in filled_database.values(limit=1)]

    assert len(keys) == 1
    assert keys == ["baz"]
Ejemplo n.º 26
0
async def test_values_for_filled(filled_database: Database) -> None:
    keys = [doc.id async for doc in filled_database.values()]

    assert len(keys) == 4
    assert sorted(keys) == ["baz", "baz2", "foo", "foo2"]
Ejemplo n.º 27
0
async def test_find_fields_parameter_gets_rejected(database: Database) -> None:
    with pytest.raises(ValueError):
        [doc async for doc in database.find({"bar": True}, fields="anything")]
Ejemplo n.º 28
0
async def test_docs_do_not_contain_ddocs(
        filled_database_with_view: Database) -> None:
    keys = [doc.id async for doc in filled_database_with_view.docs()]

    assert "_design/test_ddoc" not in keys
Ejemplo n.º 29
0
async def test_docs_with_no_ids(filled_database: Database) -> None:
    keys = [doc.id async for doc in filled_database.docs()]

    assert len(keys) == 4
    assert sorted(keys) == ["baz", "baz2", "foo", "foo2"]
Ejemplo n.º 30
0
async def test_docs_with_prefix(filled_database: Database) -> None:
    keys = [doc.id async for doc in filled_database.docs(prefix="ba")]

    assert len(keys) == 2
    assert (sorted(keys)) == ["baz", "baz2"]