|
1 | 1 | import json
|
2 |
| -from typing import Dict, List |
| 2 | +from typing import Any, Dict, List |
3 | 3 |
|
4 | 4 | import pytest
|
5 | 5 |
|
@@ -48,6 +48,14 @@ def dimensions() -> List[Dict[str, str]]:
|
48 | 48 | ]
|
49 | 49 |
|
50 | 50 |
|
| 51 | +@pytest.fixture |
| 52 | +def non_str_dimensions() -> List[Dict[str, Any]]: |
| 53 | + return [ |
| 54 | + {"name": "test_dimension", "value": True}, |
| 55 | + {"name": "test_dimension_2", "value": 3}, |
| 56 | + ] |
| 57 | + |
| 58 | + |
51 | 59 | @pytest.fixture
|
52 | 60 | def namespace() -> Dict[str, str]:
|
53 | 61 | return {"name": "test_namespace"}
|
@@ -380,3 +388,27 @@ def lambda_handler(evt, context):
|
380 | 388 |
|
381 | 389 | # THEN metric set should be empty after function has been run
|
382 | 390 | assert my_metrics.metric_set == {}
|
| 391 | + |
| 392 | + |
| 393 | +def test_log_metrics_non_string_dimension_values(capsys, metrics, non_str_dimensions, namespace): |
| 394 | + # GIVEN Metrics is initialized and dimensions with non-string values are added |
| 395 | + my_metrics = Metrics() |
| 396 | + my_metrics.add_namespace(**namespace) |
| 397 | + for metric in metrics: |
| 398 | + my_metrics.add_metric(**metric) |
| 399 | + for dimension in non_str_dimensions: |
| 400 | + my_metrics.add_dimension(**dimension) |
| 401 | + |
| 402 | + # WHEN we utilize log_metrics to serialize |
| 403 | + # and flush all metrics at the end of a function execution |
| 404 | + @my_metrics.log_metrics |
| 405 | + def lambda_handler(evt, ctx): |
| 406 | + return True |
| 407 | + |
| 408 | + lambda_handler({}, {}) |
| 409 | + output = json.loads(capsys.readouterr().out.strip()) |
| 410 | + |
| 411 | + # THEN we should have no exceptions |
| 412 | + # and dimension values hould be serialized as strings |
| 413 | + for dimension in non_str_dimensions: |
| 414 | + assert isinstance(output[dimension["name"]], str) |
0 commit comments