| 
4 | 4 | 
 
  | 
5 | 5 | import pytest  | 
6 | 6 | 
 
  | 
7 |  | -# from unittest.mock import Mock, MagicMock, patch  | 
8 | 7 | from prometheus_client import CollectorRegistry, Gauge  | 
9 | 8 | from src.service.constants import PROMETHEUS_METRIC_PREFIX  | 
10 | 9 | from src.service.payloads.metrics.base_metric_request import BaseMetricRequest  | 
@@ -246,3 +245,57 @@ def test_invalid_gauge_parameters(  | 
246 | 245 |             match="Either 'request' or 'metric_name' & 'value' must be provided",  | 
247 | 246 |         ):  | 
248 | 247 |             publisher.gauge(model_name="test_model", id=test_id, value=0.5)  | 
 | 248 | +      | 
 | 249 | +    def test_valid_metric_names(self, publisher: PrometheusPublisher):  | 
 | 250 | +        """Test validation of valid Prometheus metric names."""  | 
 | 251 | +        valid_names = [  | 
 | 252 | +            "simple_metric",  | 
 | 253 | +            "metric_with_numbers123",  | 
 | 254 | +            "UPPERCASE_METRIC", # we convert to lowercase  | 
 | 255 | +            "metric:with:colons",  | 
 | 256 | +            "_underscore_start",  | 
 | 257 | +            "a1b2c3",  | 
 | 258 | +            "metric_name_with_3underscores"  | 
 | 259 | +        ]  | 
 | 260 | +          | 
 | 261 | +        for name in valid_names:  | 
 | 262 | +            full_name = publisher._get_full_metric_name(name)  | 
 | 263 | +            expected = f"{PROMETHEUS_METRIC_PREFIX}{name.lower()}"  | 
 | 264 | +            assert full_name == expected  | 
 | 265 | + | 
 | 266 | +    def test_invalid_metric_names(self, publisher: PrometheusPublisher):  | 
 | 267 | +        """Test validation of invalid Prometheus metric names."""  | 
 | 268 | +        invalid_names = [  | 
 | 269 | +            "metric-with-hyphens",  | 
 | 270 | +            "metric with spaces",  | 
 | 271 | +            "metric@with@symbols",  | 
 | 272 | +            "metric.with.dots",  | 
 | 273 | +            "metric#with#hash",  | 
 | 274 | +            "metric%with%percent",  | 
 | 275 | +            "-starts_with_hyphen",  | 
 | 276 | +            "metric/with/slashes"  | 
 | 277 | +        ]  | 
 | 278 | +          | 
 | 279 | +        for name in invalid_names:  | 
 | 280 | +            with pytest.raises(ValueError, match="Invalid Prometheus metric name"):  | 
 | 281 | +                publisher._get_full_metric_name(name)  | 
 | 282 | + | 
 | 283 | +    def test_empty_metric_name_validation(self, publisher: PrometheusPublisher):  | 
 | 284 | +        with pytest.raises(ValueError, match="Metric name cannot be empty"):  | 
 | 285 | +            publisher._get_full_metric_name("")  | 
 | 286 | + | 
 | 287 | +    def test_gauge_creation_with_invalid_metric_name(  | 
 | 288 | +        self, publisher: PrometheusPublisher, mock_request: MockMetricRequest  | 
 | 289 | +    ):  | 
 | 290 | +        mock_request.metric_name = "invalid-metric-name"  | 
 | 291 | +          | 
 | 292 | +        test_id = uuid.uuid4()  | 
 | 293 | +        test_value = 0.5  | 
 | 294 | +          | 
 | 295 | +        with pytest.raises(ValueError, match="Invalid Prometheus metric name"):  | 
 | 296 | +            publisher.gauge(  | 
 | 297 | +                model_name="test_model",   | 
 | 298 | +                id=test_id,   | 
 | 299 | +                value=test_value,   | 
 | 300 | +                request=mock_request  | 
 | 301 | +            )  | 
0 commit comments