From 7c72d9961984c110fb493d965e12b32928002341 Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Fri, 19 Sep 2025 10:17:36 +0900 Subject: [PATCH] add pytests for seconds to human readable convert --- .../string_handling/test_seconds_to_string.py | 186 ++++++++++++++++++ 1 file changed, 186 insertions(+) create mode 100644 tests/unit/string_handling/test_seconds_to_string.py diff --git a/tests/unit/string_handling/test_seconds_to_string.py b/tests/unit/string_handling/test_seconds_to_string.py new file mode 100644 index 0000000..2645c18 --- /dev/null +++ b/tests/unit/string_handling/test_seconds_to_string.py @@ -0,0 +1,186 @@ +""" +PyTest: string_handling/timestamp_strings - seconds_to_string function +""" + +from corelibs.string_handling.timestamp_strings import seconds_to_string + + +class TestSecondsToString: + """Test suite for seconds_to_string function""" + + def test_basic_integer_seconds(self): + """Test conversion of basic integer seconds""" + assert seconds_to_string(0) == "0s" + assert seconds_to_string(1) == "1s" + assert seconds_to_string(30) == "30s" + assert seconds_to_string(59) == "59s" + + def test_minutes_conversion(self): + """Test conversion involving minutes""" + assert seconds_to_string(60) == "1m" + assert seconds_to_string(90) == "1m 30s" + assert seconds_to_string(120) == "2m" + assert seconds_to_string(3599) == "59m 59s" + + def test_hours_conversion(self): + """Test conversion involving hours""" + assert seconds_to_string(3600) == "1h" + assert seconds_to_string(3660) == "1h 1m" + assert seconds_to_string(3661) == "1h 1m 1s" + assert seconds_to_string(7200) == "2h" + assert seconds_to_string(7260) == "2h 1m" + + def test_days_conversion(self): + """Test conversion involving days""" + assert seconds_to_string(86400) == "1d" + assert seconds_to_string(86401) == "1d 1s" + assert seconds_to_string(90000) == "1d 1h" + assert seconds_to_string(90061) == "1d 1h 1m 1s" + assert seconds_to_string(172800) == "2d" + + def test_complex_combinations(self): + """Test complex time combinations""" + # 1 day, 2 hours, 3 minutes, 4 seconds + total = 86400 + 7200 + 180 + 4 + assert seconds_to_string(total) == "1d 2h 3m 4s" + + # 5 days, 23 hours, 59 minutes, 59 seconds + total = 5 * 86400 + 23 * 3600 + 59 * 60 + 59 + assert seconds_to_string(total) == "5d 23h 59m 59s" + + def test_fractional_seconds_default_precision(self): + """Test fractional seconds with default precision (3 decimal places)""" + assert seconds_to_string(0.1) == "0.1s" + assert seconds_to_string(0.123) == "0.123s" + assert seconds_to_string(0.1234) == "0.123s" + assert seconds_to_string(1.5) == "1.5s" + assert seconds_to_string(1.567) == "1.567s" + assert seconds_to_string(1.5678) == "1.568s" + + def test_fractional_seconds_microsecond_precision(self): + """Test fractional seconds with microsecond precision""" + assert seconds_to_string(0.1, show_microseconds=True) == "0.1s" + assert seconds_to_string(0.123456, show_microseconds=True) == "0.123456s" + assert seconds_to_string(0.1234567, show_microseconds=True) == "0.123457s" + assert seconds_to_string(1.5, show_microseconds=True) == "1.5s" + assert seconds_to_string(1.567890, show_microseconds=True) == "1.56789s" + + def test_fractional_seconds_with_larger_units(self): + """Test fractional seconds combined with larger time units""" + # 1 minute and 30.5 seconds + assert seconds_to_string(90.5) == "1m 30.5s" + assert seconds_to_string(90.5, show_microseconds=True) == "1m 30.5s" + + # 1 hour, 1 minute, and 1.123 seconds + total = 3600 + 60 + 1.123 + assert seconds_to_string(total) == "1h 1m 1.123s" + assert seconds_to_string(total, show_microseconds=True) == "1h 1m 1.123s" + + def test_negative_values(self): + """Test negative time values""" + assert seconds_to_string(-1) == "-1s" + assert seconds_to_string(-60) == "-1m" + assert seconds_to_string(-90) == "-1m 30s" + assert seconds_to_string(-3661) == "-1h 1m 1s" + assert seconds_to_string(-86401) == "-1d 1s" + assert seconds_to_string(-1.5) == "-1.5s" + assert seconds_to_string(-90.123) == "-1m 30.123s" + + def test_zero_handling(self): + """Test various zero values""" + assert seconds_to_string(0) == "0s" + assert seconds_to_string(0.0) == "0s" + assert seconds_to_string(-0) == "0s" + assert seconds_to_string(-0.0) == "0s" + + def test_float_input_types(self): + """Test various float input types""" + assert seconds_to_string(1.0) == "1s" + assert seconds_to_string(60.0) == "1m" + assert seconds_to_string(3600.0) == "1h" + assert seconds_to_string(86400.0) == "1d" + + def test_large_values(self): + """Test handling of large time values""" + # 365 days (1 year) + year_seconds = 365 * 86400 + assert seconds_to_string(year_seconds) == "365d" + + # 1000 days + assert seconds_to_string(1000 * 86400) == "1000d" + + # Large number with all units + large_time = 999 * 86400 + 23 * 3600 + 59 * 60 + 59.999 + result = seconds_to_string(large_time) + assert result.startswith("999d") + assert "23h" in result + assert "59m" in result + assert "59.999s" in result + + def test_rounding_behavior(self): + """Test rounding behavior for fractional seconds""" + # Default precision (3 decimal places) - values are truncated via rstrip + assert seconds_to_string(1.0004) == "1s" # Truncates trailing zeros after rstrip + assert seconds_to_string(1.0005) == "1s" # Truncates trailing zeros after rstrip + assert seconds_to_string(1.9999) == "2s" # Rounds up and strips .000 + + # Microsecond precision (6 decimal places) + assert seconds_to_string(1.0000004, show_microseconds=True) == "1s" + assert seconds_to_string(1.0000005, show_microseconds=True) == "1.000001s" + + def test_trailing_zero_removal(self): + """Test that trailing zeros are properly removed""" + assert seconds_to_string(1.100) == "1.1s" + assert seconds_to_string(1.120) == "1.12s" + assert seconds_to_string(1.123) == "1.123s" + assert seconds_to_string(1.100000, show_microseconds=True) == "1.1s" + assert seconds_to_string(1.123000, show_microseconds=True) == "1.123s" + + def test_invalid_input_types(self): + """Test handling of invalid input types""" + # String inputs should be returned as-is + assert seconds_to_string("invalid") == "invalid" + assert seconds_to_string("not a number") == "not a number" + assert seconds_to_string("") == "" + + def test_edge_cases_boundary_values(self): + """Test edge cases at unit boundaries""" + # Exactly 1 minute - 1 second + assert seconds_to_string(59) == "59s" + assert seconds_to_string(59.999) == "59.999s" + + # Exactly 1 hour - 1 second + assert seconds_to_string(3599) == "59m 59s" + assert seconds_to_string(3599.999) == "59m 59.999s" + + # Exactly 1 day - 1 second + assert seconds_to_string(86399) == "23h 59m 59s" + assert seconds_to_string(86399.999) == "23h 59m 59.999s" + + def test_very_small_fractional_seconds(self): + """Test very small fractional values""" + assert seconds_to_string(0.001) == "0.001s" + assert seconds_to_string(0.0001) == "0s" # Below default precision + assert seconds_to_string(0.000001, show_microseconds=True) == "0.000001s" + assert seconds_to_string(0.0000001, show_microseconds=True) == "0s" # Below microsecond precision + + def test_precision_consistency(self): + """Test that precision is consistent across different scenarios""" + # With other units present + assert seconds_to_string(61.123456) == "1m 1.123s" + assert seconds_to_string(61.123456, show_microseconds=True) == "1m 1.123456s" + + # Large values with fractional seconds + large_val = 90061.123456 # 1d 1h 1m 1.123456s + assert seconds_to_string(large_val) == "1d 1h 1m 1.123s" + assert seconds_to_string(large_val, show_microseconds=True) == "1d 1h 1m 1.123456s" + + def test_string_numeric_inputs(self): + """Test string inputs that represent numbers""" + # String inputs should be returned as-is, even if they look like numbers + assert seconds_to_string("60") == "60" + assert seconds_to_string("1.5") == "1.5" + assert seconds_to_string("0") == "0" + assert seconds_to_string("-60") == "-60" + +# __END__