diff --git a/example/README.md b/example/README.md index 8cfd1e3ee35..c5f7941765a 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..62d4133db5e 100644 --- a/example/tests/test_handler.py +++ b/example/tests/test_handler.py @@ -1,9 +1,24 @@ import json +import os +import sys 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 +76,7 @@ def apigw_event(): "path": "/examplepath", } + @dataclass class Context: function_name: str = "test" @@ -68,23 +84,25 @@ 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()) + +@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"]) 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 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 a941136407b..ec07ac86d53 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 = "*", python = "~3.6"} [tool.coverage.run] source = ["aws_lambda_powertools"] 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