Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improv: include example tests in make tests #63

Merged
merged 6 commits into from
Jun 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions example/README.md
Original file line number Diff line number Diff line change
@@ -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**

Expand Down Expand Up @@ -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
Expand Down
Empty file added example/tests/__init__.py
Empty file.
34 changes: 26 additions & 8 deletions example/tests/test_handler.py
Original file line number Diff line number Diff line change
@@ -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():
Expand Down Expand Up @@ -61,30 +76,33 @@ def apigw_event():
"path": "/examplepath",
}


@dataclass
class Context:
function_name: str = "test"
memory_limit_in_mb: int = 128
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
Expand Down
15 changes: 14 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
2 changes: 1 addition & 1 deletion pytest.ini
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[pytest]
addopts = -ra --cov --cov-config=.coveragerc
testpaths = ./tests
testpaths = ./tests ./example/tests