Skip to content

Commit

Permalink
fix: #24 correct example test and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
heitorlessa committed Apr 27, 2020
1 parent 91d8953 commit a8c74e7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
4 changes: 3 additions & 1 deletion python/example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ This example uses both [tracing](https://github.com/awslabs/aws-lambda-powertool
* **Deploy**: `sam deploy --guided`
* **Unit Tests**: We recommend proceeding with the following commands in a virtual environment
- **Install deps**: `pip install -r hello_world/requirements.txt && pip install -r requirements-dev.txt`
- **Run tests with tracing disabled**: `POWERTOOLS_TRACE_DISABLED=1 python -m pytest`
- **Run tests with tracing disabled and namespace set**
- `POWERTOOLS_METRICS_NAMESPACE="Example" POWERTOOLS_TRACE_DISABLED=1 python -m pytest`
- Both are necessary because `app.py` initializes them in the global scope, since both Tracer and Metrics will be initialized and configured during import time. For unit tests, we could always patch and explicitly config but env vars do just fine for this example.

# Example code

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

from hello_world import app


@pytest.fixture()
def apigw_event():
""" Generates API GW Event"""
Expand Down Expand Up @@ -69,13 +68,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):


def test_lambda_handler(apigw_event, mocker, capsys):
ret = app.lambda_handler(apigw_event, Context())
data = json.loads(ret["body"])

output = capsys.readouterr()
output = output.out.split('\n')
stdout_one_string = '\t'.join(output)

assert ret["statusCode"] == 200
assert "message" in ret["body"]
assert data["message"] == "hello world"
# assert "location" in data.dict_keys()
assert "location" in data
assert "message" in ret["body"]

# assess custom metric was flushed in stdout/logs
assert "SuccessfulLocations" in stdout_one_string
assert "ColdStart" in stdout_one_string
assert "UniqueMetricDimension" in stdout_one_string

# assess our custom middleware ran
assert "Logging response after Handler is called" in stdout_one_string
assert "Logging event before Handler is called" in stdout_one_string

0 comments on commit a8c74e7

Please sign in to comment.