|
1 | 1 | import json
|
| 2 | +import os |
| 3 | +import sys |
2 | 4 | from dataclasses import dataclass
|
3 | 5 |
|
4 | 6 | import pytest
|
5 | 7 |
|
6 |
| -from hello_world import app |
| 8 | + |
| 9 | +@pytest.fixture() |
| 10 | +def env_vars(monkeypatch): |
| 11 | + monkeypatch.setenv("POWERTOOLS_METRICS_NAMESPACE", "example_namespace") |
| 12 | + monkeypatch.setenv("POWERTOOLS_SERVICE_NAME", "example_service") |
| 13 | + monkeypatch.setenv("POWERTOOLS_TRACE_DISABLED", "1") |
| 14 | + |
| 15 | + |
| 16 | +@pytest.fixture() |
| 17 | +def lambda_handler(env_vars): |
| 18 | + from hello_world import app |
| 19 | + |
| 20 | + return app.lambda_handler |
| 21 | + |
7 | 22 |
|
8 | 23 | @pytest.fixture()
|
9 | 24 | def apigw_event():
|
@@ -61,30 +76,33 @@ def apigw_event():
|
61 | 76 | "path": "/examplepath",
|
62 | 77 | }
|
63 | 78 |
|
| 79 | + |
64 | 80 | @dataclass
|
65 | 81 | class Context:
|
66 | 82 | function_name: str = "test"
|
67 | 83 | memory_limit_in_mb: int = 128
|
68 | 84 | invoked_function_arn: str = "arn:aws:lambda:eu-west-1:298026489:function:test"
|
69 | 85 | aws_request_id: str = "5b441b59-a550-11c8-6564-f1c833cf438c"
|
70 | 86 |
|
71 |
| -def test_lambda_handler(apigw_event, mocker, capsys): |
72 |
| - ret = app.lambda_handler(apigw_event, Context()) |
| 87 | + |
| 88 | +@pytest.mark.skipif(sys.version_info < (3, 7), reason="requires python3.7 or higher") |
| 89 | +def test_lambda_handler(lambda_handler, apigw_event, mocker, capsys): |
| 90 | + ret = lambda_handler(apigw_event, Context()) |
73 | 91 | data = json.loads(ret["body"])
|
74 | 92 |
|
75 | 93 | output = capsys.readouterr()
|
76 |
| - output = output.out.split('\n') |
77 |
| - stdout_one_string = '\t'.join(output) |
| 94 | + output = output.out.split("\n") |
| 95 | + stdout_one_string = "\t".join(output) |
78 | 96 |
|
79 | 97 | assert ret["statusCode"] == 200
|
80 | 98 | assert data["message"] == "hello world"
|
81 | 99 | assert "location" in data
|
82 | 100 | assert "message" in ret["body"]
|
83 | 101 | assert "async_http" in data
|
84 |
| - |
| 102 | + |
85 | 103 | # assess custom metric was flushed in stdout/logs
|
86 |
| - assert "SuccessfulLocations" in stdout_one_string |
87 |
| - assert "ColdStart" in stdout_one_string |
| 104 | + assert "SuccessfulLocations" in stdout_one_string |
| 105 | + assert "ColdStart" in stdout_one_string |
88 | 106 | assert "UniqueMetricDimension" in stdout_one_string
|
89 | 107 |
|
90 | 108 | # assess our custom middleware ran
|
|
0 commit comments