From 123d6923be935db8ff24cfadae191f2134b5c103 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Fri, 4 Sep 2026 07:01:49 +0000 Subject: [PATCH] test: add coverage for typeerror and valueerror handling in _add Added a dedicated test function `test_add_catches_type_and_value_errors` to `tests/test_metrics.py` to ensure that calling `_add` with invalid inputs (which raise `TypeError` or `ValueError`) correctly suppresses the exceptions and avoids appending invalid metrics. Co-authored-by: Horizontal42 <75383519+Horizontal42@users.noreply.github.com> --- tests/test_metrics.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/tests/test_metrics.py b/tests/test_metrics.py index 0d42121..d855882 100644 --- a/tests/test_metrics.py +++ b/tests/test_metrics.py @@ -1,6 +1,6 @@ from __future__ import annotations -from netsleuth.metrics import Metric, collect_metrics, render_csv, render_prometheus +from netsleuth.metrics import Metric, _add, collect_metrics, render_csv, render_prometheus def _report(**overrides) -> dict: @@ -84,3 +84,17 @@ def test_render_csv_has_a_header_row_and_quotes_a_comma_in_a_label(): lines = text.strip().splitlines() assert lines[0] == "metric,labels,value" assert '"target=a, b"' in lines[1] + + +def test_add_catches_type_and_value_errors(): + metrics: list[Metric] = [] + _add(metrics, "test_value_error", "not_a_float") + assert len(metrics) == 0 + + _add(metrics, "test_type_error", {"invalid": "dict"}) + assert len(metrics) == 0 + + # Sanity check that a valid float still works + _add(metrics, "test_valid", "12.5") + assert len(metrics) == 1 + assert metrics[0].value == 12.5