[Tests][Added][Time Parser] now tested

This commit is contained in:
Salvador E. Tropea
2025-07-16 07:17:21 -03:00
parent c9aaa0b341
commit 6dc0502582
+89
View File
@@ -0,0 +1,89 @@
"""
Tests for miscellaneous utility functions found in utils/misc.py.
"""
import bootstrap # noqa: F401
import pytest
# Import the function to be tested
from nodes.utils.misc import parse_time_to_seconds
# --- Test Cases for parse_time_to_seconds ---
# Use pytest.mark.parametrize to test many valid inputs efficiently
@pytest.mark.parametrize("time_str, expected_seconds", [
# Basic cases
("10", 10.0),
("15.5", 15.5),
("0", 0.0),
# MM:SS format
("1:30", 90.0),
("01:30", 90.0),
("00:45", 45.0),
("10:00", 600.0),
# MM:SS.ss format
("1:30.5", 90.5),
("0:05.25", 5.25),
# HH:MM:SS format
("1:00:00", 3600.0),
("01:00:00", 3600.0),
("0:10:20", 620.0),
# HH:MM:SS.ss format
("1:01:01.5", 3661.5),
("00:00:00.123", 0.123),
# Edge cases
("", 0.0), # Empty string should default to 0
(" 30.5 ", 30.5), # Should handle whitespace
(None, 0.0), # None should default to 0
])
def test_parse_time_valid_inputs(time_str, expected_seconds):
"""Tests various valid time string formats."""
assert parse_time_to_seconds(time_str) == pytest.approx(expected_seconds)
# Use pytest.mark.parametrize for invalid inputs that should raise ValueError
@pytest.mark.parametrize("invalid_time_str, expected_error_msg_part", [
# Invalid characters
("abc", "Invalid time format"),
("10s", "Invalid time format"),
("1:20s", "Invalid time format"),
# Malformed structures
("1:2:3:4", "Invalid time format"), # Too many colons
("1::30", "Empty part in time string"), # Empty part
(":", "Empty part in time string"),
(":30", "Empty part in time string"),
# Invalid numbers within segments
("1:70", "Minutes or seconds segment is out of range (0-59)"), # Seconds > 59
("1:60", "Minutes or seconds segment is out of range (0-59)"), # Seconds == 60
("70:30", "Minutes or seconds segment is out of range (0-59)"), # Minutes > 59
("1:30:80", "Minutes or seconds segment is out of range (0-59)"), # Seconds > 59 in HH:MM:SS
])
def test_parse_time_invalid_inputs_raise_error(invalid_time_str, expected_error_msg_part):
"""
Tests that invalid time string formats raise a ValueError
with an informative message.
"""
with pytest.raises(ValueError) as excinfo:
parse_time_to_seconds(invalid_time_str)
# Check that the exception message contains the expected substring
assert expected_error_msg_part in str(excinfo.value)
def test_parse_time_non_string_input_raises_error():
"""Tests that non-string, non-None inputs raise a TypeError."""
with pytest.raises(TypeError):
parse_time_to_seconds(123) # Pass an integer
with pytest.raises(TypeError):
parse_time_to_seconds([1, 2, 3]) # Pass a list
with pytest.raises(TypeError):
parse_time_to_seconds({"time": "10"}) # Pass a dict