diff --git a/async_couch/clients/database/endpoints.py b/async_couch/clients/database/endpoints.py index 3cd6024..a48ade0 100644 --- a/async_couch/clients/database/endpoints.py +++ b/async_couch/clients/database/endpoints.py @@ -4,6 +4,7 @@ from async_couch.clients.designs.responses import ExecuteViewResponse from async_couch.http_clients.base_client import BaseEndpoint from . import responses as resp +from . import models class DatabaseEndpoint(BaseEndpoint): @@ -551,8 +552,8 @@ async def db_design_docs(self, async def db_bulk_get(self, db: str, - revs: bool = None, - id: int = None) -> types.UniversalResponse: + docs: typing.List[models.Doc], + revs: bool = None) -> types.UniversalResponse: """ This method can be called to query several documents in bulk. It is well suited for fetching a specific revision of documents, as @@ -581,13 +582,11 @@ async def db_bulk_get(self, if revs: query['revs'] = revs - result = dict() - - if id: - result['id'] = id + json_data = dict() + json_data['docs'] = [item.dump() for item in docs] return await self.http_client.make_request( - endpoint='/db/_bulk_get', + endpoint='/{db}/_bulk_get', method=types.HttpMethod.POST, statuses={ 200: 'Request completed successfully', @@ -599,14 +598,14 @@ async def db_bulk_get(self, }, query=query, path={'db': db}, - json_data=result, + json_data=json_data, response_model=ExecuteViewResponse ) async def db_bulk_docs(self, db: str, - docs: list, - new_edits: bool=True) -> types.UniversalResponse: + docs: typing.List[models.ExtendedDoc], + new_edits: bool = True) -> types.UniversalResponse: """ The bulk document API allows you to create and update multiple @@ -641,14 +640,14 @@ async def db_bulk_docs(self, """ query = dict() - - if docs: - query['docs'] = docs if new_edits: query['new_edits'] = new_edits + json_data = dict() + json_data['docs'] = [item.dump() for item in docs] + return await self.http_client.make_request( - endpoint='/db/_bulk_docs', + endpoint='/{db}/_bulk_docs', method=types.HttpMethod.POST, statuses={ 201: 'Document(s) have been created or updated', @@ -656,6 +655,7 @@ async def db_bulk_docs(self, 404: 'Requested database not found' }, query=query, + json_data=json_data, path={'db': db}, response_model=ExecuteViewResponse ) @@ -777,7 +777,7 @@ async def db_find(self, json_data['selector'] = selector return await self.http_client.make_request( - endpoint='/db/_find', + endpoint='/{db}/_find', method=types.HttpMethod.POST, statuses={ 200: 'Request completed successfully', diff --git a/async_couch/clients/database/models.py b/async_couch/clients/database/models.py index 96ca91b..d2cdfe9 100644 --- a/async_couch/clients/database/models.py +++ b/async_couch/clients/database/models.py @@ -1,3 +1,6 @@ +from dataclasses import dataclass + + class ClusterObject: n: int # Replicas. The number of copies of every document. @@ -30,3 +33,22 @@ class DatabaseProps: partitioned: bool # (optional) If present and true, this indicates that the database # is partitioned. + + +@dataclass() +class Doc: + id: str + rev: str = None + + def dump(self): + return {'id': self.id, 'rev': self.rev} + + +@dataclass() +class ExtendedDoc: + _id: str + _rev: str = None + _deleted: bool = None + + def dump(self): + return {'_id': self._id, '_rev': self._rev, '_deleted': self._deleted} diff --git a/tests/test_api/test_databases.py b/tests/test_api/test_databases.py index ed35d1e..c2a90a3 100644 --- a/tests/test_api/test_databases.py +++ b/tests/test_api/test_databases.py @@ -1,6 +1,7 @@ from typing import Callable from async_couch import CouchClient +from async_couch.clients.database import models as db_models db_name = 'test_db_01' @@ -52,11 +53,29 @@ def test_all_docs(async_run: Callable, client: CouchClient): def test_design_docs(async_run: Callable, client: CouchClient): + # todo: type of keys is invalid response = async_run(client.db_design_docs(db_name, keys=[doc_id])) assert response.status_code == 200 assert len(response.model.rows) == 1 +def __test_bulk_get(async_run: Callable, client: CouchClient): + docs = [db_models.Doc(id=doc_id)] + response = async_run(client.db_bulk_get(db_name, docs=docs)) + assert response.status_code == 200 + + +def test_bulk_docs(async_run: Callable, client: CouchClient): + docs = [db_models.ExtendedDoc(_id=doc_id)] + response = async_run(client.db_bulk_docs(db_name, docs=docs)) + assert response.status_code == 200 + + +#def test_find(async_run: Callable, client: CouchClient): + #response = async_run(client.db_find(db_name, selector=[doc_id])) + #assert response.status_code == 200 + + def test_delete(async_run: Callable, client: CouchClient): response = async_run(client.db_delete(db_name)) assert response.status_code == 200