From 13f42e7250447640af15c3c754641fc1ea8c8211 Mon Sep 17 00:00:00 2001 From: iFuunyMan520 Date: Mon, 11 May 2020 20:19:12 +0300 Subject: [PATCH 1/4] test --- tests/test_api/test_databases.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/test_api/test_databases.py b/tests/test_api/test_databases.py index ed35d1e..992cc88 100644 --- a/tests/test_api/test_databases.py +++ b/tests/test_api/test_databases.py @@ -57,6 +57,11 @@ def test_design_docs(async_run: Callable, client: CouchClient): assert len(response.model.rows) == 1 +def test_bulk_get(async_run: Callable, client: CouchClient): + response = async_run(client.db_bulk_get(db_name, id=[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 From dfe497a34375ff04ecbb5506573b82cf4dae3569 Mon Sep 17 00:00:00 2001 From: iFuunyMan520 Date: Sun, 17 May 2020 16:45:08 +0300 Subject: [PATCH 2/4] httpx error --- async_couch/clients/database/endpoints.py | 20 ++++++++++---------- tests/test_api/test_databases.py | 12 +++++++++++- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/async_couch/clients/database/endpoints.py b/async_couch/clients/database/endpoints.py index 3cd6024..f3702f8 100644 --- a/async_couch/clients/database/endpoints.py +++ b/async_couch/clients/database/endpoints.py @@ -552,7 +552,7 @@ async def db_design_docs(self, async def db_bulk_get(self, db: str, revs: bool = None, - id: int = None) -> types.UniversalResponse: + docs: dict = 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 +581,13 @@ async def db_bulk_get(self, if revs: query['revs'] = revs - result = dict() + json_data = dict() - if id: - result['id'] = id + if docs: + json_data['docs'] = f'{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 +599,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: list = None, + new_edits: bool = True) -> types.UniversalResponse: """ The bulk document API allows you to create and update multiple @@ -648,7 +648,7 @@ async def db_bulk_docs(self, query['new_edits'] = new_edits 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', @@ -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/tests/test_api/test_databases.py b/tests/test_api/test_databases.py index 992cc88..b23ff6a 100644 --- a/tests/test_api/test_databases.py +++ b/tests/test_api/test_databases.py @@ -58,10 +58,20 @@ def test_design_docs(async_run: Callable, client: CouchClient): def test_bulk_get(async_run: Callable, client: CouchClient): - response = async_run(client.db_bulk_get(db_name, id=[doc_id])) + response = async_run(client.db_bulk_get(db_name, docs=[doc_id])) assert response.status_code == 200 +#def test_bulk_docs(async_run: Callable, client: CouchClient): + #response = async_run(client.db_bulk_docs(db_name, docs=[doc_id])) + #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 From a684f43fe4734ce011e4f0417b02174fecae6f97 Mon Sep 17 00:00:00 2001 From: Maksym Stukalo Date: Wed, 20 May 2020 21:03:09 +0200 Subject: [PATCH 3/4] general bug fixes --- async_couch/clients/database/endpoints.py | 24 +++++++++++------------ async_couch/clients/database/models.py | 22 +++++++++++++++++++++ tests/test_api/test_databases.py | 14 ++++++++----- 3 files changed, 43 insertions(+), 17 deletions(-) diff --git a/async_couch/clients/database/endpoints.py b/async_couch/clients/database/endpoints.py index f3702f8..7d0c7d6 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): @@ -537,7 +538,7 @@ async def db_design_docs(self, json_data['keys'] = keys return await self.http_client.make_request( - endpoint='/{db}/_design_docs', + endpoint=f'/{db}/_design_docs', method=types.HttpMethod.POST, statuses={ 200: 'Request completed successfully', @@ -551,8 +552,8 @@ async def db_design_docs(self, async def db_bulk_get(self, db: str, - revs: bool = None, - docs: dict = 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 @@ -582,12 +583,10 @@ async def db_bulk_get(self, query['revs'] = revs json_data = dict() - - if docs: - json_data['docs'] = f'{docs}' + json_data['docs'] = [item.dump() for item in docs] return await self.http_client.make_request( - endpoint='/{db}/_bulk_get', + endpoint=f'/{db}/_bulk_get', method=types.HttpMethod.POST, statuses={ 200: 'Request completed successfully', @@ -605,7 +604,7 @@ async def db_bulk_get(self, async def db_bulk_docs(self, db: str, - docs: list = None, + docs: typing.List[models.ExtendedDoc], new_edits: bool = True) -> types.UniversalResponse: """ @@ -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=f'/{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 ) 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 b23ff6a..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,19 +53,22 @@ 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): - response = async_run(client.db_bulk_get(db_name, docs=[doc_id])) +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): - #response = async_run(client.db_bulk_docs(db_name, docs=[doc_id])) - #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): From 13ea26c02198b857959ea9285a781b3528c1ba10 Mon Sep 17 00:00:00 2001 From: Maksym Stukalo Date: Wed, 20 May 2020 21:05:37 +0200 Subject: [PATCH 4/4] revert f-string from endpoint --- async_couch/clients/database/endpoints.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/async_couch/clients/database/endpoints.py b/async_couch/clients/database/endpoints.py index 7d0c7d6..a48ade0 100644 --- a/async_couch/clients/database/endpoints.py +++ b/async_couch/clients/database/endpoints.py @@ -538,7 +538,7 @@ async def db_design_docs(self, json_data['keys'] = keys return await self.http_client.make_request( - endpoint=f'/{db}/_design_docs', + endpoint='/{db}/_design_docs', method=types.HttpMethod.POST, statuses={ 200: 'Request completed successfully', @@ -586,7 +586,7 @@ async def db_bulk_get(self, json_data['docs'] = [item.dump() for item in docs] return await self.http_client.make_request( - endpoint=f'/{db}/_bulk_get', + endpoint='/{db}/_bulk_get', method=types.HttpMethod.POST, statuses={ 200: 'Request completed successfully', @@ -647,7 +647,7 @@ async def db_bulk_docs(self, json_data['docs'] = [item.dump() for item in docs] return await self.http_client.make_request( - endpoint=f'/{db}/_bulk_docs', + endpoint='/{db}/_bulk_docs', method=types.HttpMethod.POST, statuses={ 201: 'Document(s) have been created or updated',