Some checks failed
Integration Tests / integration-tests (push) Failing after 19s
Integration Tests / performance-tests (push) Has been skipped
Service Adapters (Python FastAPI) / test (3.11) (push) Successful in 1m13s
Service Adapters (Python FastAPI) / test (3.12) (push) Successful in 1m19s
Service Adapters (Python FastAPI) / test (3.13) (push) Successful in 1m17s
Service Adapters (Python FastAPI) / build (push) Successful in 16s
### Summary of Changes - Refactored the `test_get_services` method to enhance the organization of mock responses and improve test clarity. - Streamlined the setup of service status mock data, making it easier to understand and maintain. ### Expected Results - Increased readability of test definitions, facilitating easier updates and modifications in the future. - Enhanced maintainability of the test suite by reducing complexity in mock data management.
237 lines
8.6 KiB
Python
237 lines
8.6 KiB
Python
"""
|
|
Tests for time_formatter utility
|
|
"""
|
|
|
|
from datetime import datetime, timedelta, timezone
|
|
|
|
from utils.time_formatter import (
|
|
_format_duration_string,
|
|
_format_epoch_uptime,
|
|
_format_timedelta_from_seconds,
|
|
_format_timestamp_uptime,
|
|
_is_duration_string,
|
|
_is_epoch,
|
|
_is_numeric_seconds,
|
|
_is_timestamp,
|
|
_parse_duration_string,
|
|
format_response_time,
|
|
format_uptime_for_frontend,
|
|
)
|
|
|
|
|
|
class TestFormatUptimeForFrontend:
|
|
"""Test format_uptime_for_frontend function"""
|
|
|
|
def test_format_timestamp_recent(self):
|
|
"""Test formatting recent timestamp"""
|
|
recent_time = datetime.now(timezone.utc) - timedelta(hours=2, minutes=30)
|
|
timestamp = recent_time.isoformat()
|
|
|
|
result = format_uptime_for_frontend(timestamp)
|
|
assert "2h 30m" in result
|
|
|
|
def test_format_timestamp_old(self):
|
|
"""Test formatting old timestamp"""
|
|
old_time = datetime.now(timezone.utc) - timedelta(days=2, hours=5)
|
|
timestamp = old_time.isoformat()
|
|
|
|
result = format_uptime_for_frontend(timestamp)
|
|
assert "2d 5h" in result
|
|
|
|
def test_format_timestamp_with_z(self):
|
|
"""Test formatting timestamp with Z suffix"""
|
|
recent_time = datetime.now(timezone.utc) - timedelta(minutes=45)
|
|
timestamp = recent_time.isoformat().replace("+00:00", "Z")
|
|
|
|
result = format_uptime_for_frontend(timestamp)
|
|
assert "45m" in result
|
|
|
|
def test_format_epoch_timestamp(self):
|
|
"""Test formatting epoch timestamp"""
|
|
recent_time = datetime.now(timezone.utc) - timedelta(hours=1)
|
|
epoch = str(int(recent_time.timestamp()))
|
|
|
|
result = format_uptime_for_frontend(epoch)
|
|
assert "1h" in result
|
|
|
|
def test_format_duration_string(self):
|
|
"""Test formatting duration string"""
|
|
result = format_uptime_for_frontend("2h 30m")
|
|
assert result == "2h 30m"
|
|
|
|
result = format_uptime_for_frontend("5d 2h 15m")
|
|
assert result == "5d 2h 15m"
|
|
|
|
result = format_uptime_for_frontend("45m")
|
|
assert result == "45m"
|
|
|
|
def test_format_numeric_seconds(self):
|
|
"""Test formatting numeric seconds"""
|
|
result = format_uptime_for_frontend("3600")
|
|
assert result == "1h 0m"
|
|
|
|
result = format_uptime_for_frontend("86400")
|
|
assert result == "1d 0h 0m"
|
|
|
|
result = format_uptime_for_frontend("150")
|
|
assert result == "2m"
|
|
|
|
def test_format_edge_cases(self):
|
|
"""Test formatting edge cases"""
|
|
assert format_uptime_for_frontend("") == "0d 0h"
|
|
assert format_uptime_for_frontend(None) == "0d 0h"
|
|
assert format_uptime_for_frontend("invalid") == "invalid"
|
|
assert format_uptime_for_frontend("0") == "0m"
|
|
|
|
def test_format_long_string(self):
|
|
"""Test formatting very long string"""
|
|
long_string = "x" * 100
|
|
result = format_uptime_for_frontend(long_string)
|
|
assert result == "0d 0h"
|
|
|
|
|
|
class TestFormatResponseTime:
|
|
"""Test format_response_time function"""
|
|
|
|
def test_format_response_time_seconds(self):
|
|
"""Test formatting response time in seconds"""
|
|
assert format_response_time(1.5) == "1.50s"
|
|
assert format_response_time(0.5) == "500ms"
|
|
|
|
def test_format_response_time_milliseconds(self):
|
|
"""Test formatting response time in milliseconds"""
|
|
assert format_response_time(0.5) == "500ms"
|
|
assert format_response_time(0.001) == "1ms"
|
|
|
|
def test_format_response_time_none(self):
|
|
"""Test formatting None response time"""
|
|
assert format_response_time(None) == "N/A"
|
|
|
|
|
|
class TestHelperFunctions:
|
|
"""Test helper functions"""
|
|
|
|
def test_is_timestamp(self):
|
|
"""Test _is_timestamp function"""
|
|
assert _is_timestamp("2025-09-18T10:00:00+00:00") is True
|
|
assert _is_timestamp("2025-09-18T10:00:00Z") is True
|
|
assert _is_timestamp("2025-09-18T10:00:00") is True
|
|
assert _is_timestamp("invalid") is False
|
|
assert _is_timestamp("1234567890") is False
|
|
|
|
def test_is_epoch(self):
|
|
"""Test _is_epoch function"""
|
|
assert _is_epoch("1726640562") is True
|
|
assert _is_epoch("1726640562.123") is True
|
|
assert _is_epoch("123") is False # Too small
|
|
assert _is_epoch("invalid") is False
|
|
|
|
def test_is_duration_string(self):
|
|
"""Test _is_duration_string function"""
|
|
assert _is_duration_string("2h 30m") is True
|
|
assert _is_duration_string("5d 2h 15m") is True
|
|
assert _is_duration_string("1d 2h 3m 4s") is True
|
|
assert _is_duration_string("45m") is True
|
|
assert _is_duration_string("2h") is True
|
|
assert _is_duration_string("invalid") is False
|
|
assert _is_duration_string("2 hours") is False
|
|
|
|
def test_is_numeric_seconds(self):
|
|
"""Test _is_numeric_seconds function"""
|
|
assert _is_numeric_seconds("3600") is True
|
|
assert _is_numeric_seconds("3600.5") is True
|
|
assert _is_numeric_seconds("invalid") is False
|
|
assert _is_numeric_seconds("") is False
|
|
|
|
def test_format_timestamp_uptime(self):
|
|
"""Test _format_timestamp_uptime function"""
|
|
recent_time = datetime.now(timezone.utc) - timedelta(hours=2, minutes=30)
|
|
timestamp = recent_time.isoformat()
|
|
|
|
result = _format_timestamp_uptime(timestamp)
|
|
assert "2h 30m" in result
|
|
|
|
def test_format_epoch_uptime(self):
|
|
"""Test _format_epoch_uptime function"""
|
|
recent_time = datetime.now(timezone.utc) - timedelta(hours=1)
|
|
epoch = str(int(recent_time.timestamp()))
|
|
|
|
result = _format_epoch_uptime(epoch)
|
|
assert "1h" in result
|
|
|
|
def test_format_duration_string(self):
|
|
"""Test _format_duration_string function"""
|
|
result = _format_duration_string("2h 30m")
|
|
assert result == "2h 30m"
|
|
|
|
result = _format_duration_string("5d 2h 15m")
|
|
assert result == "5d 2h 15m"
|
|
|
|
def test_parse_duration_string(self):
|
|
"""Test _parse_duration_string function"""
|
|
assert _parse_duration_string("2h 30m") == 9000 # 2*3600 + 30*60
|
|
assert _parse_duration_string("5d 2h 15m") == 440100 # 5*86400 + 2*3600 + 15*60
|
|
assert _parse_duration_string("1d 2h 3m 4s") == 93784 # 1*86400 + 2*3600 + 3*60 + 4
|
|
assert _parse_duration_string("45m") == 2700 # 45*60
|
|
assert _parse_duration_string("2h") == 7200 # 2*3600
|
|
assert _parse_duration_string("30s") == 30
|
|
assert _parse_duration_string("1d") == 86400
|
|
|
|
def test_format_timedelta_from_seconds(self):
|
|
"""Test _format_timedelta_from_seconds function"""
|
|
assert _format_timedelta_from_seconds(3600) == "1h 0m"
|
|
assert _format_timedelta_from_seconds(86400) == "1d 0h 0m"
|
|
assert _format_timedelta_from_seconds(150) == "2m"
|
|
assert _format_timedelta_from_seconds(0) == "0m"
|
|
assert _format_timedelta_from_seconds(-100) == "0d 0h" # Negative time
|
|
|
|
def test_format_timedelta_from_seconds_with_decimals(self):
|
|
"""Test _format_timedelta_from_seconds with decimal input"""
|
|
assert _format_timedelta_from_seconds(3600.5) == "1h 0m"
|
|
assert _format_timedelta_from_seconds(150.7) == "2m"
|
|
|
|
|
|
class TestTimeFormatterEdgeCases:
|
|
"""Test edge cases and error handling"""
|
|
|
|
def test_malformed_timestamp(self):
|
|
"""Test malformed timestamp handling"""
|
|
result = format_uptime_for_frontend("2025-13-45T25:70:90+00:00")
|
|
assert result == "2025-13-45T25:70:90+00:00" # Returns as-is for invalid timestamps
|
|
|
|
def test_very_old_timestamp(self):
|
|
"""Test very old timestamp"""
|
|
old_time = datetime(1900, 1, 1, tzinfo=timezone.utc)
|
|
timestamp = old_time.isoformat()
|
|
|
|
result = format_uptime_for_frontend(timestamp)
|
|
assert "d" in result # Should be many days
|
|
|
|
def test_future_timestamp(self):
|
|
"""Test future timestamp"""
|
|
future_time = datetime.now(timezone.utc) + timedelta(days=1)
|
|
timestamp = future_time.isoformat()
|
|
|
|
result = format_uptime_for_frontend(timestamp)
|
|
assert result == "0d 0h" # Future times should be 0
|
|
|
|
def test_empty_duration_string(self):
|
|
"""Test empty duration string"""
|
|
result = _parse_duration_string("")
|
|
assert result == 0
|
|
|
|
def test_invalid_duration_string(self):
|
|
"""Test invalid duration string"""
|
|
result = _parse_duration_string("invalid")
|
|
assert result == 0
|
|
|
|
def test_duration_string_with_spaces(self):
|
|
"""Test duration string with extra spaces"""
|
|
result = _parse_duration_string(" 2h 30m ")
|
|
assert result == 9000
|
|
|
|
def test_mixed_case_duration_string(self):
|
|
"""Test duration string with mixed case"""
|
|
result = _parse_duration_string("2H 30M")
|
|
assert result == 0 # Should not match due to case sensitivity
|