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>
This commit is contained in:
google-labs-jules[bot] 2026-10-07 03:55:39 +00:00
parent 8c99ac78b7
commit 73b843404d

View file

@ -4,6 +4,8 @@ import json
from unittest.mock import AsyncMock, patch from unittest.mock import AsyncMock, patch
import pytest import pytest
from netsleuth.speed import ( from netsleuth.speed import (
_as_int,
_us_to_ms,
bufferbloat_delta, bufferbloat_delta,
mbps, mbps,
ookla_interface_args, ookla_interface_args,
@ -23,6 +25,34 @@ from netsleuth.speed import tier_fastcom
from netsleuth.speed import tier_ndt7 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(): def test_mbps_converts_bytes_and_seconds():
assert mbps(1000000, 1.0) == pytest.approx(8.0) assert mbps(1000000, 1.0) == pytest.approx(8.0)
assert mbps(12500000, 1.0) == pytest.approx(100.0) assert mbps(12500000, 1.0) == pytest.approx(100.0)