From d150cd386c136e75b65d5272aa0f96a78e76a67c Mon Sep 17 00:00:00 2001 From: Mario Bielert Date: Tue, 22 Sep 2020 17:27:23 +0200 Subject: [PATCH] Filter design documents from docs() iteration. Fixes #30 Technically speaking, this change breaks the API. --- aiocouch/database.py | 17 +++++++++++++---- aiocouch/view.py | 6 +++++- tests/test_database.py | 12 ++++++++++++ 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/aiocouch/database.py b/aiocouch/database.py index 079ce20..bd172ed 100644 --- a/aiocouch/database.py +++ b/aiocouch/database.py @@ -109,20 +109,27 @@ 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 `. @@ -130,7 +137,9 @@ async def docs( 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]: diff --git a/aiocouch/view.py b/aiocouch/view.py index 95da7a7..b2be16e 100644 --- a/aiocouch/view.py +++ b/aiocouch/view.py @@ -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: @@ -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 diff --git a/tests/test_database.py b/tests/test_database.py index f4aab75..57709b0 100644 --- a/tests/test_database.py +++ b/tests/test_database.py @@ -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})]