|
|
|
|
@@ -0,0 +1,205 @@
|
|
|
|
|
"""
|
|
|
|
|
Unit tests for convert_to_seconds function from timestamp_strings module.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
from corelibs.string_handling.timestamp_strings import convert_to_seconds, TimeParseError, TimeUnitError
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestConvertToSeconds:
|
|
|
|
|
"""Test class for convert_to_seconds function."""
|
|
|
|
|
|
|
|
|
|
def test_numeric_input_int(self):
|
|
|
|
|
"""Test with integer input."""
|
|
|
|
|
assert convert_to_seconds(42) == 42
|
|
|
|
|
assert convert_to_seconds(0) == 0
|
|
|
|
|
assert convert_to_seconds(-5) == -5
|
|
|
|
|
|
|
|
|
|
def test_numeric_input_float(self):
|
|
|
|
|
"""Test with float input."""
|
|
|
|
|
assert convert_to_seconds(42.7) == 43 # rounds to 43
|
|
|
|
|
assert convert_to_seconds(42.3) == 42 # rounds to 42
|
|
|
|
|
assert convert_to_seconds(42.5) == 42 # rounds to 42 (banker's rounding)
|
|
|
|
|
assert convert_to_seconds(0.0) == 0
|
|
|
|
|
assert convert_to_seconds(-5.7) == -6
|
|
|
|
|
|
|
|
|
|
def test_numeric_string_input(self):
|
|
|
|
|
"""Test with numeric string input."""
|
|
|
|
|
assert convert_to_seconds("42") == 42
|
|
|
|
|
assert convert_to_seconds("42.7") == 43
|
|
|
|
|
assert convert_to_seconds("42.3") == 42
|
|
|
|
|
assert convert_to_seconds("0") == 0
|
|
|
|
|
assert convert_to_seconds("-5.7") == -6
|
|
|
|
|
|
|
|
|
|
def test_single_unit_seconds(self):
|
|
|
|
|
"""Test with seconds unit."""
|
|
|
|
|
assert convert_to_seconds("30s") == 30
|
|
|
|
|
assert convert_to_seconds("1s") == 1
|
|
|
|
|
assert convert_to_seconds("0s") == 0
|
|
|
|
|
|
|
|
|
|
def test_single_unit_minutes(self):
|
|
|
|
|
"""Test with minutes unit."""
|
|
|
|
|
assert convert_to_seconds("5m") == 300 # 5 * 60
|
|
|
|
|
assert convert_to_seconds("1m") == 60
|
|
|
|
|
assert convert_to_seconds("0m") == 0
|
|
|
|
|
|
|
|
|
|
def test_single_unit_hours(self):
|
|
|
|
|
"""Test with hours unit."""
|
|
|
|
|
assert convert_to_seconds("2h") == 7200 # 2 * 3600
|
|
|
|
|
assert convert_to_seconds("1h") == 3600
|
|
|
|
|
assert convert_to_seconds("0h") == 0
|
|
|
|
|
|
|
|
|
|
def test_single_unit_days(self):
|
|
|
|
|
"""Test with days unit."""
|
|
|
|
|
assert convert_to_seconds("1d") == 86400 # 1 * 86400
|
|
|
|
|
assert convert_to_seconds("2d") == 172800 # 2 * 86400
|
|
|
|
|
assert convert_to_seconds("0d") == 0
|
|
|
|
|
|
|
|
|
|
def test_single_unit_months(self):
|
|
|
|
|
"""Test with months unit (30 days * 12 = 1 year)."""
|
|
|
|
|
# Note: The code has M: 2592000 * 12 which is 1 year, not 1 month
|
|
|
|
|
# This seems like a bug in the original code, but testing what it actually does
|
|
|
|
|
assert convert_to_seconds("1M") == 31104000 # 2592000 * 12
|
|
|
|
|
assert convert_to_seconds("2M") == 62208000 # 2 * 2592000 * 12
|
|
|
|
|
|
|
|
|
|
def test_single_unit_years(self):
|
|
|
|
|
"""Test with years unit."""
|
|
|
|
|
assert convert_to_seconds("1Y") == 31536000 # 365 * 86400
|
|
|
|
|
assert convert_to_seconds("2Y") == 63072000 # 2 * 365 * 86400
|
|
|
|
|
|
|
|
|
|
def test_long_unit_names(self):
|
|
|
|
|
"""Test with long unit names."""
|
|
|
|
|
assert convert_to_seconds("1year") == 31536000
|
|
|
|
|
assert convert_to_seconds("2years") == 63072000
|
|
|
|
|
assert convert_to_seconds("1month") == 31104000
|
|
|
|
|
assert convert_to_seconds("2months") == 62208000
|
|
|
|
|
assert convert_to_seconds("1day") == 86400
|
|
|
|
|
assert convert_to_seconds("2days") == 172800
|
|
|
|
|
assert convert_to_seconds("1hour") == 3600
|
|
|
|
|
assert convert_to_seconds("2hours") == 7200
|
|
|
|
|
assert convert_to_seconds("1minute") == 60
|
|
|
|
|
assert convert_to_seconds("2minutes") == 120
|
|
|
|
|
assert convert_to_seconds("30min") == 1800
|
|
|
|
|
assert convert_to_seconds("1second") == 1
|
|
|
|
|
assert convert_to_seconds("2seconds") == 2
|
|
|
|
|
assert convert_to_seconds("30sec") == 30
|
|
|
|
|
|
|
|
|
|
def test_multiple_units(self):
|
|
|
|
|
"""Test with multiple units combined."""
|
|
|
|
|
assert convert_to_seconds("1h30m") == 5400 # 3600 + 1800
|
|
|
|
|
assert convert_to_seconds("1d2h") == 93600 # 86400 + 7200
|
|
|
|
|
assert convert_to_seconds("1h30m45s") == 5445 # 3600 + 1800 + 45
|
|
|
|
|
assert convert_to_seconds("2d3h4m5s") == 183845 # 172800 + 10800 + 240 + 5
|
|
|
|
|
|
|
|
|
|
def test_multiple_units_with_spaces(self):
|
|
|
|
|
"""Test with multiple units and spaces."""
|
|
|
|
|
assert convert_to_seconds("1h 30m") == 5400
|
|
|
|
|
assert convert_to_seconds("1d 2h") == 93600
|
|
|
|
|
assert convert_to_seconds("1h 30m 45s") == 5445
|
|
|
|
|
assert convert_to_seconds("2d 3h 4m 5s") == 183845
|
|
|
|
|
|
|
|
|
|
def test_mixed_unit_formats(self):
|
|
|
|
|
"""Test with mixed short and long unit names."""
|
|
|
|
|
assert convert_to_seconds("1hour 30min") == 5400
|
|
|
|
|
assert convert_to_seconds("1day 2hours") == 93600
|
|
|
|
|
assert convert_to_seconds("1h 30minutes 45sec") == 5445
|
|
|
|
|
|
|
|
|
|
def test_negative_values(self):
|
|
|
|
|
"""Test with negative time strings."""
|
|
|
|
|
assert convert_to_seconds("-30s") == -30
|
|
|
|
|
assert convert_to_seconds("-1h") == -3600
|
|
|
|
|
assert convert_to_seconds("-1h30m") == -5400
|
|
|
|
|
assert convert_to_seconds("-2d3h4m5s") == -183845
|
|
|
|
|
|
|
|
|
|
def test_case_insensitive_long_names(self):
|
|
|
|
|
"""Test that long unit names are case insensitive."""
|
|
|
|
|
assert convert_to_seconds("1Hour") == 3600
|
|
|
|
|
assert convert_to_seconds("1MINUTE") == 60
|
|
|
|
|
assert convert_to_seconds("1Day") == 86400
|
|
|
|
|
assert convert_to_seconds("2YEARS") == 63072000
|
|
|
|
|
|
|
|
|
|
def test_duplicate_units_error(self):
|
|
|
|
|
"""Test that duplicate units raise TimeParseError."""
|
|
|
|
|
with pytest.raises(TimeParseError, match="Unit 'h' appears more than once"):
|
|
|
|
|
convert_to_seconds("1h2h")
|
|
|
|
|
|
|
|
|
|
with pytest.raises(TimeParseError, match="Unit 's' appears more than once"):
|
|
|
|
|
convert_to_seconds("30s45s")
|
|
|
|
|
|
|
|
|
|
with pytest.raises(TimeParseError, match="Unit 'm' appears more than once"):
|
|
|
|
|
convert_to_seconds("1m30m")
|
|
|
|
|
|
|
|
|
|
def test_invalid_units_error(self):
|
|
|
|
|
"""Test that invalid units raise TimeUnitError."""
|
|
|
|
|
with pytest.raises(TimeUnitError, match="Unit 'x' is not a valid unit name"):
|
|
|
|
|
convert_to_seconds("30x")
|
|
|
|
|
|
|
|
|
|
with pytest.raises(TimeUnitError, match="Unit 'invalid' is not a valid unit name"):
|
|
|
|
|
convert_to_seconds("1invalid")
|
|
|
|
|
|
|
|
|
|
with pytest.raises(TimeUnitError, match="Unit 'z' is not a valid unit name"):
|
|
|
|
|
convert_to_seconds("1h30z")
|
|
|
|
|
|
|
|
|
|
def test_empty_string(self):
|
|
|
|
|
"""Test with empty string."""
|
|
|
|
|
assert convert_to_seconds("") == 0
|
|
|
|
|
|
|
|
|
|
def test_no_matches(self):
|
|
|
|
|
"""Test with string that has no time units."""
|
|
|
|
|
assert convert_to_seconds("hello") == 0
|
|
|
|
|
assert convert_to_seconds("no time here") == 0
|
|
|
|
|
|
|
|
|
|
def test_zero_values(self):
|
|
|
|
|
"""Test with zero values for different units."""
|
|
|
|
|
assert convert_to_seconds("0s") == 0
|
|
|
|
|
assert convert_to_seconds("0m") == 0
|
|
|
|
|
assert convert_to_seconds("0h") == 0
|
|
|
|
|
assert convert_to_seconds("0d") == 0
|
|
|
|
|
assert convert_to_seconds("0h0m0s") == 0
|
|
|
|
|
|
|
|
|
|
def test_large_values(self):
|
|
|
|
|
"""Test with large time values."""
|
|
|
|
|
assert convert_to_seconds("999d") == 86313600 # 999 * 86400
|
|
|
|
|
assert convert_to_seconds("100Y") == 3153600000 # 100 * 31536000
|
|
|
|
|
|
|
|
|
|
def test_order_independence(self):
|
|
|
|
|
"""Test that order of units doesn't matter."""
|
|
|
|
|
assert convert_to_seconds("30m1h") == 5400 # same as 1h30m
|
|
|
|
|
assert convert_to_seconds("45s30m1h") == 5445 # same as 1h30m45s
|
|
|
|
|
assert convert_to_seconds("5s4m3h2d") == 183845 # same as 2d3h4m5s
|
|
|
|
|
|
|
|
|
|
def test_whitespace_handling(self):
|
|
|
|
|
"""Test various whitespace scenarios."""
|
|
|
|
|
assert convert_to_seconds("1 h") == 3600
|
|
|
|
|
assert convert_to_seconds("1h 30m") == 5400
|
|
|
|
|
assert convert_to_seconds(" 1h30m ") == 5400
|
|
|
|
|
assert convert_to_seconds("1h\t30m") == 5400
|
|
|
|
|
|
|
|
|
|
def test_mixed_case_short_units(self):
|
|
|
|
|
"""Test that short units work with different cases."""
|
|
|
|
|
# Note: The regex only matches [a-zA-Z]+ so case matters for the lookup
|
|
|
|
|
with pytest.raises(TimeUnitError, match="Unit 'H' is not a valid unit name"):
|
|
|
|
|
convert_to_seconds("1H") # 'H' is not in unit_factors, raises error
|
|
|
|
|
assert convert_to_seconds("1h") == 3600 # lowercase works
|
|
|
|
|
|
|
|
|
|
def test_boundary_conditions(self):
|
|
|
|
|
"""Test boundary conditions and edge cases."""
|
|
|
|
|
# Test with leading zeros
|
|
|
|
|
assert convert_to_seconds("01h") == 3600
|
|
|
|
|
assert convert_to_seconds("001m") == 60
|
|
|
|
|
|
|
|
|
|
# Test very small values
|
|
|
|
|
assert convert_to_seconds("1s") == 1
|
|
|
|
|
|
|
|
|
|
def test_negative_with_multiple_units(self):
|
|
|
|
|
"""Test negative values with multiple units."""
|
|
|
|
|
assert convert_to_seconds("-1h30m45s") == -5445
|
|
|
|
|
assert convert_to_seconds("-2d3h") == -183600
|
|
|
|
|
|
|
|
|
|
def test_duplicate_with_long_names(self):
|
|
|
|
|
"""Test duplicate detection with long unit names."""
|
|
|
|
|
with pytest.raises(TimeParseError, match="Unit 'h' appears more than once"):
|
|
|
|
|
convert_to_seconds("1hour2h") # both resolve to 'h'
|
|
|
|
|
|
|
|
|
|
with pytest.raises(TimeParseError, match="Unit 's' appears more than once"):
|
|
|
|
|
convert_to_seconds("1second30sec") # both resolve to 's'
|