From 2aac986038cf9dd1b7ad3f3b7e360aac8ff0a2e4 Mon Sep 17 00:00:00 2001
From: heitorlessa <lessa@amazon.co.uk>
Date: Mon, 27 Apr 2020 09:50:55 +0100
Subject: [PATCH] fix: #24 correct example test and docs

---
 python/example/README.md                      |  4 +++-
 .../example/tests/{unit => }/test_handler.py  | 22 ++++++++++++++-----
 2 files changed, 19 insertions(+), 7 deletions(-)
 rename python/example/tests/{unit => }/test_handler.py (82%)

diff --git a/python/example/README.md b/python/example/README.md
index 23b21ace8cd..4fec2cad50e 100644
--- a/python/example/README.md
+++ b/python/example/README.md
@@ -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
 
diff --git a/python/example/tests/unit/test_handler.py b/python/example/tests/test_handler.py
similarity index 82%
rename from python/example/tests/unit/test_handler.py
rename to python/example/tests/test_handler.py
index 91a903330ac..f5447a8a81a 100644
--- a/python/example/tests/unit/test_handler.py
+++ b/python/example/tests/test_handler.py
@@ -5,7 +5,6 @@
 
 from hello_world import app
 
-
 @pytest.fixture()
 def apigw_event():
     """ Generates API GW Event"""
@@ -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