From 73b843404de0976f91e5016c1d5c39ceb7b8d491 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Wed, 7 Oct 2026 03:55:39 +0000 Subject: [PATCH] test: add error and happy path tests for _as_int and _us_to_ms Added comprehensive test cases for the `_as_int` and `_us_to_ms` utility functions in `tests/test_speed.py`. These tests cover valid string numbers, non-numeric strings, empty strings, and `None` to ensure deterministic data coercion in `netsleuth.speed`. Co-authored-by: Horizontal42 <75383519+Horizontal42@users.noreply.github.com> --- tests/test_speed.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/tests/test_speed.py b/tests/test_speed.py index 3722359..6e8a9e7 100644 --- a/tests/test_speed.py +++ b/tests/test_speed.py @@ -4,6 +4,8 @@ import json from unittest.mock import AsyncMock, patch import pytest from netsleuth.speed import ( + _as_int, + _us_to_ms, bufferbloat_delta, mbps, ookla_interface_args, @@ -23,6 +25,34 @@ from netsleuth.speed import tier_fastcom from netsleuth.speed import tier_ndt7 +def test_as_int_happy_path(): + assert _as_int("123") == 123 + assert _as_int("-45") == -45 + assert _as_int("0") == 0 + + +def test_as_int_error_path(): + assert _as_int("not-a-number") is None + assert _as_int("12.34") is None + assert _as_int("") is None + assert _as_int(None) is None + + +def test_us_to_ms_happy_path(): + assert _us_to_ms("1000") == 1.0 + assert _us_to_ms("1500") == 1.5 + assert _us_to_ms("123456") == 123.456 + assert _us_to_ms("-1000") == -1.0 + assert _us_to_ms("0") == 0.0 + + +def test_us_to_ms_error_path(): + assert _us_to_ms("not-a-number") is None + assert _us_to_ms("12.34") is None + assert _us_to_ms("") is None + assert _us_to_ms(None) is None + + def test_mbps_converts_bytes_and_seconds(): assert mbps(1000000, 1.0) == pytest.approx(8.0) assert mbps(12500000, 1.0) == pytest.approx(100.0)