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
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"]
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()]
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"]
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
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"]
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()]
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
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"
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
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"
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
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"
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
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"]
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"]
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
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"]
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
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
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"
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 🦓"
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"
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
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"]
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"]
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")]
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
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"]
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"]