From f1f507ad5ccc5e41ab32d1b3a736b512e6dd879c Mon Sep 17 00:00:00 2001 From: Jye Cusch Date: Fri, 30 Jul 2021 15:56:38 +1000 Subject: [PATCH 1/3] ci: add code coverage --- .github/workflows/test.yaml | 5 +++++ nitric/api/documents.py | 10 +++++++++- nitric/faas/faas.py | 10 +++++++--- nitric/faas/response.py | 7 +++++++ tox.ini | 2 +- 5 files changed, 29 insertions(+), 5 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 6da3f81..9449035 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -27,3 +27,8 @@ jobs: - name: Run Tox # Run tox using the version of Python in `PATH` run: tox -e py + - name: Upload coverage to Codecov + uses: codecov/codecov-action@v1 + with: + fail_ci_if_error: true + token: ${{ secrets.CODECOV_TOKEN }} diff --git a/nitric/api/documents.py b/nitric/api/documents.py index c8c3d0b..d600847 100644 --- a/nitric/api/documents.py +++ b/nitric/api/documents.py @@ -151,6 +151,7 @@ def query( ) def sub_collection_depth(self) -> int: + """Return the depth of this collection, which is a count of the parents above this collection.""" if not self.is_sub_collection(): return 0 else: @@ -271,14 +272,17 @@ class Document: @property def id(self): + """Return the document's unique id.""" return self._ref.id @property def collection(self) -> CollectionRef: + """Return the CollectionRef for the collection that contains this document.""" return self._ref.parent @property def ref(self): + """Return the DocumentRef for this document.""" return self._ref @@ -289,6 +293,10 @@ class QueryResultsPage: paging_token: any = field(default_factory=lambda: None) documents: List[Document] = field(default_factory=lambda: []) + def has_more_pages(self) -> bool: + """Return false if the page token is None or empty (both represent no more pages).""" + return bool(self.paging_token) + class QueryBuilder: """Document query builder for retrieving documents from a collection based on filters.""" @@ -416,7 +424,7 @@ async def fetch(self) -> QueryResultsPage: ) return QueryResultsPage( - paging_token=results.paging_token, + paging_token=results.paging_token if results.paging_token else None, documents=[_document_from_wire(documents=self._documents, message=result) for result in results.documents], ) diff --git a/nitric/faas/faas.py b/nitric/faas/faas.py index 3da49fd..e654ae4 100644 --- a/nitric/faas/faas.py +++ b/nitric/faas/faas.py @@ -31,7 +31,11 @@ async def _register_faas_worker( func: Callable[ - [Trigger], Union[Coroutine[Any, Any, Union[Response, None, dict, bytes]], Union[Response, None, dict, bytes]] + [Trigger], + Union[ + Coroutine[Any, Any, Union[Response, None, dict, list, set, bytes]], + Union[Response, None, dict, list, set, bytes], + ], ] ): """ @@ -74,7 +78,7 @@ async def _register_faas_worker( if isinstance(response, bytes): full_response.data = response # convert dict responses to JSON - elif isinstance(response, dict): + elif isinstance(response, (dict, list, set)): full_response.data = bytes(json.dumps(response), "utf-8") if full_response.context.is_http(): full_response.context.as_http().headers["Content-Type"] = "application/json" @@ -106,7 +110,7 @@ async def _register_faas_worker( channel.close() -def start(handler: Callable[[Trigger], Coroutine[Any, Any, Union[Response, None, dict, bytes]]]): +def start(handler: Callable[[Trigger], Coroutine[Any, Any, Union[Response, None, dict, list, set, bytes]]]): """ Register the provided function as the trigger handler and starts handling new trigger requests. diff --git a/nitric/faas/response.py b/nitric/faas/response.py index df7576c..6d56e74 100644 --- a/nitric/faas/response.py +++ b/nitric/faas/response.py @@ -93,6 +93,13 @@ class Response(object): data: Union[bytes, str, None, Any] = field(default=None) def data_to_bytes(self) -> bytes: + """ + Return the data from this response. If not already bytes, the data will be converted. + + None returns an empty byte array. + str will be converted directly to bytes + All other types are converted to JSON using json.dumps, then to bytes from the JSON string. + """ if self.data is None: return bytes() elif isinstance(self.data, bytes): diff --git a/tox.ini b/tox.ini index c6195b8..058d485 100644 --- a/tox.ini +++ b/tox.ini @@ -5,7 +5,7 @@ envlist = linter,py3.8 deps = -e .[dev] commands = - pytest . + pytest --cov=./nitric --cov-report=xml [testenv:linter] deps = From 77321528736affc07057e0b150fc82a6f7fa3da8 Mon Sep 17 00:00:00 2001 From: Jye Cusch Date: Fri, 30 Jul 2021 16:08:45 +1000 Subject: [PATCH 2/3] ci: add test workflow to main branch --- .github/workflows/test.yaml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 9449035..bd8314f 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -1,6 +1,10 @@ name: Tests -on: pull_request +on: + push: + branches: + - main + pull_request: jobs: build: From a947dbb45b438c9c542a10e37ce94301881ed48d Mon Sep 17 00:00:00 2001 From: Jye Cusch Date: Fri, 30 Jul 2021 16:18:48 +1000 Subject: [PATCH 3/3] docs: add README badges --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index c0b1000..5b4eda8 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,10 @@ Nitric Python SDK Logo

+![test status](https://github.com/nitrictech/python-sdk/actions/workflows/test.yaml/badge.svg?branch=main) +[![codecov](https://codecov.io/gh/nitrictech/python-sdk/branch/main/graph/badge.svg?token=SBFRNSZ4ZF)](https://codecov.io/gh/nitrictech/python-sdk) +[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=nitrictech_python-sdk&metric=alert_status)](https://sonarcloud.io/dashboard?id=nitrictech_python-sdk) + # Nitric Python SDK The Python SDK supports the use of the cloud-portable [Nitric](https://nitric.io) framework with Python 3.7+.