|
24 | 24 | ) |
25 | 25 | from aws_lambda_powertools.metrics.provider.cloudwatch_emf.constants import ( |
26 | 26 | MAX_DIMENSIONS, |
| 27 | + MAX_METRIC_NAME_LENGTH, |
| 28 | + MIN_METRIC_NAME_LENGTH, |
27 | 29 | ) |
| 30 | +from aws_lambda_powertools.metrics.provider.cloudwatch_emf.exceptions import MetricNameError |
28 | 31 |
|
29 | 32 | if TYPE_CHECKING: |
30 | 33 | from aws_lambda_powertools.metrics.provider.cloudwatch_emf.types import ( |
@@ -443,6 +446,38 @@ def test_schema_validation_no_namespace(metric, dimension): |
443 | 446 | my_metric.add_dimension(**dimension) |
444 | 447 |
|
445 | 448 |
|
| 449 | +def test_schema_validation_empty_metric_name(metric, dimension, namespace): |
| 450 | + # GIVEN we pass an empty metric name |
| 451 | + my_metrics = AmazonCloudWatchEMFProvider(namespace=namespace) |
| 452 | + metric["name"] = "" |
| 453 | + |
| 454 | + # WHEN we attempt to add a metric |
| 455 | + # THEN it should fail validation and raise MetricNameError |
| 456 | + with pytest.raises( |
| 457 | + MetricNameError, |
| 458 | + match=f"The metric name should be between {MIN_METRIC_NAME_LENGTH} and {MAX_METRIC_NAME_LENGTH} characters", |
| 459 | + ): |
| 460 | + my_metrics.add_metric(**metric) |
| 461 | + |
| 462 | + |
| 463 | +def test_schema_validation_long_metric_name(metric, dimension, namespace): |
| 464 | + # GIVEN we pass a metric name outside the maximum length constraint |
| 465 | + my_metrics = AmazonCloudWatchEMFProvider(namespace=namespace) |
| 466 | + metric[ |
| 467 | + "name" |
| 468 | + ] = """Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. |
| 469 | + Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, |
| 470 | + ultricies nec, pellentesque eu, pretium quis,.""" |
| 471 | + |
| 472 | + # WHEN we attempt to serialize a valid EMF object |
| 473 | + # THEN it should fail validation and raise SchemaValidationError |
| 474 | + with pytest.raises( |
| 475 | + MetricNameError, |
| 476 | + match=f"The metric name should be between {MIN_METRIC_NAME_LENGTH} and {MAX_METRIC_NAME_LENGTH} characters", |
| 477 | + ): |
| 478 | + my_metrics.add_metric(**metric) |
| 479 | + |
| 480 | + |
446 | 481 | def test_schema_validation_incorrect_metric_value(metric, dimension, namespace): |
447 | 482 | # GIVEN we pass an incorrect metric value (non-numeric) |
448 | 483 | metric["value"] = "some_value" |
|
0 commit comments