Пример #1
0
async def test_constructor_with_wrong_type_for_data(database: Database,
                                                    doc: Document) -> None:
    with pytest.raises(TypeError):
        Document(database, "foo", data=cast(Any, 42))

    with pytest.raises(TypeError):
        Document(database, "foo", data=cast(Any, doc))
Пример #2
0
async def test_doc_update(doc: Document) -> None:
    assert "test" not in doc

    doc.update({"test": "value"})

    assert "test" in doc
    assert doc["test"] == "value"
Пример #3
0
async def test_get_on_unfetched(doc: Document) -> None:
    await doc.save()
    await doc.attachment("image.webp").save(image, "image/webp")

    new_doc = Document(doc._database, doc["_id"])
    att = new_doc.attachment("image.webp")
    data = await att.fetch()
    assert data == image
Пример #4
0
async def test_delete(doc: Document) -> None:
    await doc.save()
    att = doc.attachment("lipsum.txt")
    await att.save(text, "text/plain")

    await att.delete()

    att = doc.attachment("lipsum.txt")
    with pytest.raises(NotFoundError):
        await att.fetch()

    assert not await att.exists()
Пример #5
0
async def test_save_on_unfetched(doc: Document) -> None:
    await doc.save()

    new_doc = Document(doc._database, doc["_id"])

    att = new_doc.attachment("lipsum.txt")
    with pytest.raises(ValueError):
        await att.save(text, "text/plain")

    new_doc = await doc._database.get(doc["_id"])
    att = new_doc.attachment("image.webp")
    await att.save(text, "text/plain")
Пример #6
0
async def test_get(doc: Document) -> None:
    assert doc.get("foo") is None
    with pytest.raises(KeyError):
        doc["foo"]

    assert doc.get("foo", "baz") == "baz"
    with pytest.raises(KeyError):
        doc["foo"]

    assert doc.setdefault("foo") is None
    assert doc["foo"] is None

    assert doc.setdefault("baz", "bar") == "bar"
    assert doc["baz"] == "bar"

    assert doc.setdefault("baz", "kitty") == "bar"
    assert doc["baz"] == "bar"
Пример #7
0
async def test_constructor(database: Database) -> None:
    from aiocouch.document import Document

    doc = Document(database, "foo", data={"foo": 42})

    assert doc.id == "foo"
    assert doc["_id"] == "foo"
    assert doc["foo"] == 42
Пример #8
0
async def test_get(doc: Document) -> None:
    await doc.save()
    await doc.attachment("image.webp").save(image, "image/webp")

    att = doc.attachment("image.webp")
    assert await att.exists()
    data = await att.fetch()
    assert data == image
    assert att.content_type == "image/webp"
Пример #9
0
async def test_rev(doc: Document) -> None:
    assert doc.rev is None
    await doc.save()
    assert doc.rev is not None

    rev = doc.rev
    assert rev.startswith("1-")
    with pytest.raises(TypeError):
        doc.rev = 42
    assert doc.rev == rev
Пример #10
0
async def test_update(doc: Document) -> None:
    await doc.save()

    doc["value"] = 42
    await doc.save()

    att = doc.attachment("lipsum.txt")
    await att.save(text, "text/plain")

    att = doc.attachment("image.webp")
    await att.save(image, "image/webp")

    doc["value"] = 43
    await doc.save()

    new_doc = await doc._database.get(doc["_id"])
    assert new_doc["value"] == 43
    assert await new_doc.attachment("lipsum.txt").fetch() == text
    assert await new_doc.attachment("image.webp").fetch() == image
Пример #11
0
async def test_conflict_on_update(doc: Document) -> None:
    await doc.save()

    outdated = await doc._database.get(doc["_id"])

    att = doc.attachment("lipsum.txt")
    await att.save(text, "text/plain")

    with pytest.raises(ConflictError):
        await outdated.attachment("image.webp").save(image, "image/webp")

    await doc.attachment("image.webp").save(image, "image/webp")
Пример #12
0
async def test_dict_from_doc(filled_database: Database) -> None:
    doc = await filled_database["foo"]

    doc_as_dict = doc.data
    assert isinstance(doc_as_dict, dict)

    doc = Document(filled_database, "buzz")
    doc_as_dict = doc.data
    assert doc_as_dict is None

    await doc.save()
    doc_as_dict = doc.data
    assert isinstance(doc_as_dict, dict)
Пример #13
0
async def test_context_manager_by_retrieving_existing_doc(
    filled_database: Database, ) -> None:
    """Test async context manager by retrieving an existing doc from
    filled_database fixture & verify that data was actually written to server"""

    from aiocouch.document import Document

    async with Document(database=filled_database, id="foo") as document:
        doc_keys = document.keys()
        assert len(doc_keys) == 4
        assert document["_id"] == document.id == "foo"
        assert document["_rev"] == document.rev
        assert "bar" in doc_keys
        assert document["bar"] is True
        assert document["bar2"] == 3
Пример #14
0
async def test_context_manager_by_creating_new_doc(database: Database) -> None:
    from aiocouch.document import Document

    new_doc_id = "new_doc"

    # Use context manager to create & save new doc with some data in it
    async with Document(database=database, id=new_doc_id) as document:
        assert (len(document.keys())) == 1
        assert document["_id"] == document.id == new_doc_id
        document["king"] = "elvis"

    # Verify whether the data was actually written to the server or not
    saved_doc = await database.get(new_doc_id)

    assert saved_doc["_id"] == saved_doc.id == new_doc_id
    assert saved_doc.rev is not None
    assert saved_doc.rev.startswith("1-")
    assert "king" in saved_doc
    assert saved_doc["king"] == "elvis"
Пример #15
0
async def test_context_manager_with_data_parameter(database: Database) -> None:
    from aiocouch.document import Document

    new_doc_id = "new_doc"

    doc_data = {"king": "elvis"}

    # Create and save a new document with the data given to data parameter
    async with Document(database=database, id=new_doc_id,
                        data=doc_data) as document:
        assert (len(document.keys())) == 2
        assert document.rev is None
        assert document["_id"] == document.id == new_doc_id
        assert "king" in document
        assert document["king"] == "elvis"

    # Verify that the data was actually written to server by context manager
    saved_doc = await database.get(new_doc_id)

    assert saved_doc["_id"] == saved_doc.id == new_doc_id
    assert saved_doc.rev is not None
    assert saved_doc.rev.startswith("1-")
    assert "king" in saved_doc
    assert saved_doc["king"] == "elvis"
Пример #16
0
async def test_fetch_clean_document(filled_database: Database) -> None:
    from aiocouch.document import Document

    doc = Document(filled_database, "foo")
    await doc.fetch()
Пример #17
0
async def test_save_text(doc: Document) -> None:
    await doc.save()
    att = doc.attachment("lipsum.txt")
    await att.save(text, "text/plain")
Пример #18
0
async def test_filled_doc_items_keys_values(doc: Document) -> None:
    doc.update({"test": "value"})

    assert list(doc.keys()) == ["_id", "test"]
    assert list(doc.values()) == ["foo", "value"]
    assert dict(doc.items()) == {"_id": "foo", "test": "value"}
Пример #19
0
async def test_doc_items_keys_values(doc: Document) -> None:
    assert list(doc.keys()) == ["_id"]
    assert list(doc.values()) == ["foo"]
    assert dict(doc.items()) == {"_id": "foo"}
Пример #20
0
async def test_save_binary(doc: Document) -> None:
    await doc.save()
    att = doc.attachment("image.webp")
    await att.save(image, "image/webp")