From 8f7cd2bbd36c5a797a2ca1d6dcf3ee82bac2064f Mon Sep 17 00:00:00 2001 From: Tom McCarthy Date: Sun, 7 Jun 2020 14:23:42 +0200 Subject: [PATCH 1/5] chore: add example/tests to test paths --- pytest.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pytest.ini b/pytest.ini index 75891e4f024..800a6cf0a8d 100644 --- a/pytest.ini +++ b/pytest.ini @@ -1,3 +1,3 @@ [pytest] addopts = -ra --cov --cov-config=.coveragerc -testpaths = ./tests \ No newline at end of file +testpaths = ./tests ./example/tests \ No newline at end of file From 7e84ab82c0ea8d5f0b402bd1e90c003307be82e3 Mon Sep 17 00:00:00 2001 From: Tom McCarthy Date: Sun, 7 Jun 2020 14:24:34 +0200 Subject: [PATCH 2/5] chore: update example tests to remove requirement to manually specify env vars --- example/README.md | 4 ++-- example/tests/__init__.py | 0 example/tests/test_handler.py | 30 ++++++++++++++++++++++-------- 3 files changed, 24 insertions(+), 10 deletions(-) create mode 100644 example/tests/__init__.py diff --git a/example/README.md b/example/README.md index 98e6d9d7339..61767e1c785 100644 --- a/example/README.md +++ b/example/README.md @@ -1,6 +1,6 @@ # Summary -This example uses both [tracing](https://github.com/awslabs/aws-lambda-powertools/tree/develop/python#tracing) and [logging](https://github.com/awslabs/aws-lambda-powertools/tree/develop/python#logging) features, includes all environment variables that can be used, and demonstrates how to explicitly disable tracing while running unit tests - That is not necessary when running within SAM CLI as it detects the local env automatically. +This example uses [tracer](https://awslabs.github.io/aws-lambda-powertools-python/core/tracer/), [metrics](https://awslabs.github.io/aws-lambda-powertools-python/core/metrics/),and [logger](https://awslabs.github.io/aws-lambda-powertools-python/core/logger/) features, includes all environment variables that can be used, and demonstrates how to explicitly disable tracing while running unit tests - That is not necessary when running within SAM CLI as it detects the local env automatically. **Quick commands** @@ -118,7 +118,7 @@ Tests are defined in the `tests` folder in this project. Use PIP to install the ```bash example$ pip install -r hello_world/requirements.txt example$ pip install -r requirements-dev.txt -example$ POWERTOOLS_TRACE_DISABLED=1 python -m pytest tests/ -v +example$ pytest -v ``` ## Cleanup diff --git a/example/tests/__init__.py b/example/tests/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/example/tests/test_handler.py b/example/tests/test_handler.py index 909bb224c9a..0875177c71f 100644 --- a/example/tests/test_handler.py +++ b/example/tests/test_handler.py @@ -1,9 +1,21 @@ import json +import os + from dataclasses import dataclass import pytest -from hello_world import app + +@pytest.fixture() +def env_vars(monkeypatch): + monkeypatch.setenv("POWERTOOLS_METRICS_NAMESPACE", "example_namespace") + monkeypatch.setenv("POWERTOOLS_SERVICE_NAME", "example_service") + monkeypatch.setenv("POWERTOOLS_TRACE_DISABLED", "1") + +@pytest.fixture() +def lambda_handler(env_vars): + from hello_world import app + return app.lambda_handler @pytest.fixture() def apigw_event(): @@ -61,6 +73,7 @@ def apigw_event(): "path": "/examplepath", } + @dataclass class Context: function_name: str = "test" @@ -68,23 +81,24 @@ class Context: invoked_function_arn: str = "arn:aws:lambda:eu-west-1:298026489:function:test" aws_request_id: str = "5b441b59-a550-11c8-6564-f1c833cf438c" -def test_lambda_handler(apigw_event, mocker, capsys): - ret = app.lambda_handler(apigw_event, Context()) + +def test_lambda_handler(lambda_handler, apigw_event, mocker, capsys): + ret = lambda_handler(apigw_event, Context()) data = json.loads(ret["body"]) output = capsys.readouterr() - output = output.out.split('\n') - stdout_one_string = '\t'.join(output) + output = output.out.split("\n") + stdout_one_string = "\t".join(output) assert ret["statusCode"] == 200 assert data["message"] == "hello world" assert "location" in data assert "message" in ret["body"] assert "async_http" in data - + # assess custom metric was flushed in stdout/logs - assert "SuccessfulLocations" in stdout_one_string - assert "ColdStart" in stdout_one_string + assert "SuccessfulLocations" in stdout_one_string + assert "ColdStart" in stdout_one_string assert "UniqueMetricDimension" in stdout_one_string # assess our custom middleware ran From e3926d36ec7aa1d4a4874e415dac86c9ece86605 Mon Sep 17 00:00:00 2001 From: Tom McCarthy Date: Sun, 7 Jun 2020 15:05:27 +0200 Subject: [PATCH 3/5] chore: try to fix build for python3.6 (install dataclasses backport) --- pyproject.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/pyproject.toml b/pyproject.toml index a941136407b..96963891840 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -46,6 +46,7 @@ radon = "^4.1.0" xenon = "^0.7.0" flake8-bugbear = "^20.1.4" flake8-eradicate = "^0.3.0" +dataclasses = {version = "^0.7", python = "~3.6"} [tool.coverage.run] source = ["aws_lambda_powertools"] From 7ecd6f5f71bfdbbad8542e74b04995582cc3ab45 Mon Sep 17 00:00:00 2001 From: Tom McCarthy Date: Sun, 7 Jun 2020 15:07:41 +0200 Subject: [PATCH 4/5] chore: fix build for python 3.6 --- poetry.lock | 15 ++++++++++++++- pyproject.toml | 2 +- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/poetry.lock b/poetry.lock index b5ec9af256f..33e5188f53e 100644 --- a/poetry.lock +++ b/poetry.lock @@ -239,6 +239,15 @@ version = "*" [package.extras] toml = ["toml"] +[[package]] +category = "dev" +description = "A backport of the dataclasses module for Python 3.6" +marker = "python_version >= \"3.6\" and python_version < \"3.7\"" +name = "dataclasses" +optional = false +python-versions = ">=3.6, <3.7" +version = "0.7" + [[package]] category = "main" description = "Docutils -- Python Documentation Utilities" @@ -949,7 +958,7 @@ docs = ["sphinx", "jaraco.packaging (>=3.2)", "rst.linker (>=1.9)"] testing = ["jaraco.itertools", "func-timeout"] [metadata] -content-hash = "11587b6860e090c953a7e70372c266723b32848accdde9e3546e7ed9d5208a9f" +content-hash = "68009b53f884801ad967ab41923a811dd51d61c28979658545b1db8e7ee1588a" python-versions = "^3.6" [metadata.files] @@ -1064,6 +1073,10 @@ coverage = [ {file = "coverage-5.1-cp39-cp39-win_amd64.whl", hash = "sha256:bb28a7245de68bf29f6fb199545d072d1036a1917dca17a1e75bbb919e14ee8e"}, {file = "coverage-5.1.tar.gz", hash = "sha256:f90bfc4ad18450c80b024036eaf91e4a246ae287701aaa88eaebebf150868052"}, ] +dataclasses = [ + {file = "dataclasses-0.7-py3-none-any.whl", hash = "sha256:3459118f7ede7c8bea0fe795bff7c6c2ce287d01dd226202f7c9ebc0610a7836"}, + {file = "dataclasses-0.7.tar.gz", hash = "sha256:494a6dcae3b8bcf80848eea2ef64c0cc5cd307ffc263e17cdf42f3e5420808e6"}, +] docutils = [ {file = "docutils-0.15.2-py2-none-any.whl", hash = "sha256:9e4d7ecfc600058e07ba661411a2b7de2fd0fafa17d1a7f7361cd47b1175c827"}, {file = "docutils-0.15.2-py3-none-any.whl", hash = "sha256:6c4f696463b79f1fb8ba0c594b63840ebd41f059e92b31957c46b74a4599b6d0"}, diff --git a/pyproject.toml b/pyproject.toml index 96963891840..ec07ac86d53 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -46,7 +46,7 @@ radon = "^4.1.0" xenon = "^0.7.0" flake8-bugbear = "^20.1.4" flake8-eradicate = "^0.3.0" -dataclasses = {version = "^0.7", python = "~3.6"} +dataclasses = {version = "*", python = "~3.6"} [tool.coverage.run] source = ["aws_lambda_powertools"] From 85462236eaf4cc178df739f785cd4180029bde19 Mon Sep 17 00:00:00 2001 From: Tom McCarthy Date: Sun, 7 Jun 2020 15:14:02 +0200 Subject: [PATCH 5/5] chore: dont run example test on python3.6 since it requires asyncio --- example/tests/test_handler.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/example/tests/test_handler.py b/example/tests/test_handler.py index 0875177c71f..62d4133db5e 100644 --- a/example/tests/test_handler.py +++ b/example/tests/test_handler.py @@ -1,6 +1,6 @@ import json import os - +import sys from dataclasses import dataclass import pytest @@ -12,11 +12,14 @@ def env_vars(monkeypatch): monkeypatch.setenv("POWERTOOLS_SERVICE_NAME", "example_service") monkeypatch.setenv("POWERTOOLS_TRACE_DISABLED", "1") + @pytest.fixture() def lambda_handler(env_vars): from hello_world import app + return app.lambda_handler + @pytest.fixture() def apigw_event(): """ Generates API GW Event""" @@ -82,6 +85,7 @@ class Context: aws_request_id: str = "5b441b59-a550-11c8-6564-f1c833cf438c" +@pytest.mark.skipif(sys.version_info < (3, 7), reason="requires python3.7 or higher") def test_lambda_handler(lambda_handler, apigw_event, mocker, capsys): ret = lambda_handler(apigw_event, Context()) data = json.loads(ret["body"])