Skip to content

Commit

Permalink
Filter design documents from docs() iteration. Fixes #30
Browse files Browse the repository at this point in the history
Technically speaking, this change breaks the API.
  • Loading branch information
bmario committed Sep 22, 2020
1 parent 9639459 commit d150cd3
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
17 changes: 13 additions & 4 deletions aiocouch/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,28 +109,37 @@ async def delete(self):
await self._delete()

async def docs(
self, ids: list = None, create: bool = False, prefix: str = None, **params
self,
ids: list = None,
create: bool = False,
prefix: str = None,
include_ddocs: bool = False,
**params,
) -> AsyncGenerator["Document", None]:
"""A generator to iterator over all or a subset of documents in the database.
When neither of ``ids`` nor ``prefix`` are specified, all documents will be
iterated. Only one of ``ids`` and ``prefix`` can be specified.
iterated. Only one of ``ids`` and ``prefix`` can be specified. By default, design
documents are not included.
:param ids: Allows to iterate over a subset of documents by passing a list of
document ids
:param create: If ``True``, every document contained in `ids`, which doesn't
exists, will be represented by an empty
:class:`~aiocouch.document.Document` instance.
:param prefix: Allows to iterator over a subset of documents by specifing a
:param prefix: Allows to iterator over a subset of documents by specifing a
prefix that the documents must match.
:param include_ddocs: Include the design documents of the database.
:param params: Additional query parameters,
see :ref:`CouchDB view endpoint <couchdb:api/ddoc/view>`.
"""
if ids is not None and len(ids) == 0:
return

async for doc in self.all_docs.docs(ids, create, prefix, **params):
async for doc in self.all_docs.docs(
ids, create, prefix, include_ddocs, **params
):
yield doc

async def values(self, **params) -> AsyncGenerator["Document", None]:
Expand Down
6 changes: 5 additions & 1 deletion aiocouch/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ async def avalues(self, **params):
async for res in self.get(**params):
yield res["value"]

async def docs(self, ids=None, create=False, prefix=None, **params):
async def docs(
self, ids=None, create=False, prefix=None, include_ddocs=False, **params
):
params["include_docs"] = True
if prefix is None:
if ids is None:
Expand All @@ -99,6 +101,8 @@ async def docs(self, ids=None, create=False, prefix=None, **params):

async for res in iter:
if "error" not in res and res["doc"] is not None:
if res["id"].startswith("_design/") and not include_ddocs:
continue
doc = Document(self._database, res["id"])
doc._update_cache(res["doc"])
yield doc
Expand Down
12 changes: 12 additions & 0 deletions tests/test_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,18 @@ async def test_docs_with_no_ids(filled_database):
assert sorted(keys) == ["baz", "baz2", "foo", "foo2"]


async def test_docs_do_not_contain_ddocs(filled_database_with_view):
keys = [doc.id async for doc in filled_database_with_view.docs()]

assert "_design/test_ddoc" not in keys


async def test_docs_contain_ddocs_with_param(filled_database_with_view):
keys = [doc.id async for doc in filled_database_with_view.docs(include_ddocs=True)]

assert "_design/test_ddoc" in keys


async def test_find(filled_database):
matching_docs = [doc async for doc in filled_database.find({"bar": True})]

Expand Down

0 comments on commit d150cd3

Please sign in to comment.