| |
| """ |
| Simple test script to verify timezone functionality in the scheduling system. |
| """ |
|
|
| import sys |
| import os |
| sys.path.append(os.path.join(os.path.dirname(__file__), 'backend')) |
|
|
| from backend.utils.timezone_utils import ( |
| validate_timezone, |
| format_timezone_schedule, |
| parse_timezone_schedule, |
| calculate_adjusted_time_with_timezone, |
| get_server_timezone |
| ) |
|
|
| def test_timezone_validation(): |
| """Test timezone validation functionality.""" |
| print("Testing timezone validation...") |
| |
| |
| valid_timezones = [ |
| "UTC", |
| "America/New_York", |
| "Europe/London", |
| "Asia/Tokyo", |
| "Africa/Porto-Novo" |
| ] |
| |
| for tz in valid_timezones: |
| assert validate_timezone(tz), f"Should validate {tz}" |
| print(f"[OK] {tz} - Valid") |
| |
| |
| invalid_timezones = [ |
| "Invalid/Timezone", |
| "America/Bogus", |
| "Not/A_Timezone", |
| "" |
| ] |
| |
| for tz in invalid_timezones: |
| assert not validate_timezone(tz), f"Should invalidate {tz}" |
| print(f"[FAIL] {tz} - Invalid (as expected)") |
| |
| print("[OK] Timezone validation tests passed!\n") |
|
|
| def test_timezone_formatting(): |
| """Test timezone formatting functionality.""" |
| print("Testing timezone formatting...") |
| |
| |
| schedule_time = "Monday 14:30" |
| timezone = "America/New_York" |
| formatted = format_timezone_schedule(schedule_time, timezone) |
| expected = "Monday 14:30::::America/New_York" |
| |
| assert formatted == expected, f"Expected '{expected}', got '{formatted}'" |
| print(f"[OK] Formatted: {formatted}") |
| |
| |
| formatted_no_tz = format_timezone_schedule(schedule_time, None) |
| assert formatted_no_tz == schedule_time, f"Expected '{schedule_time}', got '{formatted_no_tz}'" |
| print(f"[OK] No timezone: {formatted_no_tz}") |
| |
| print("[OK] Timezone formatting tests passed!\n") |
|
|
| def test_timezone_parsing(): |
| """Test timezone parsing functionality.""" |
| print("Testing timezone parsing...") |
| |
| |
| schedule_with_tz = "Monday 14:30::::America/New_York" |
| time_part, tz_part = parse_timezone_schedule(schedule_with_tz) |
| |
| assert time_part == "Monday 14:30", f"Expected 'Monday 14:30', got '{time_part}'" |
| assert tz_part == "America/New_York", f"Expected 'America/New_York', got '{tz_part}'" |
| print(f"[OK] Parsed time: {time_part}, timezone: {tz_part}") |
| |
| |
| schedule_without_tz = "Monday 14:30" |
| time_part_no_tz, tz_part_no_tz = parse_timezone_schedule(schedule_without_tz) |
| |
| assert time_part_no_tz == "Monday 14:30", f"Expected 'Monday 14:30', got '{time_part_no_tz}'" |
| assert tz_part_no_tz is None, f"Expected None, got '{tz_part_no_tz}'" |
| print(f"[OK] Parsed time: {time_part_no_tz}, timezone: {tz_part_no_tz}") |
| |
| print("[OK] Timezone parsing tests passed!\n") |
|
|
| def test_adjusted_time_calculation(): |
| """Test adjusted time calculation with timezone.""" |
| print("Testing adjusted time calculation...") |
| |
| |
| schedule_time = "Monday 14:30::::America/New_York" |
| adjusted_time = calculate_adjusted_time_with_timezone(schedule_time, "America/New_York") |
| expected = "Monday 14:25::::America/New_York" |
| |
| assert adjusted_time == expected, f"Expected '{expected}', got '{adjusted_time}'" |
| print(f"[OK] Adjusted with timezone: {adjusted_time}") |
| |
| |
| schedule_time_no_tz = "Monday 14:30" |
| adjusted_time_no_tz = calculate_adjusted_time_with_timezone(schedule_time_no_tz, None) |
| expected_no_tz = "Monday 14:25" |
| |
| assert adjusted_time_no_tz == expected_no_tz, f"Expected '{expected_no_tz}', got '{adjusted_time_no_tz}'" |
| print(f"[OK] Adjusted without timezone: {adjusted_time_no_tz}") |
| |
| print("[OK] Adjusted time calculation tests passed!\n") |
|
|
| def test_server_timezone(): |
| """Test server timezone detection.""" |
| print("Testing server timezone detection...") |
| |
| server_tz = get_server_timezone() |
| print(f"[OK] Server timezone: {server_tz}") |
| |
| |
| assert validate_timezone(server_tz), f"Server timezone {server_tz} should be valid" |
| print("[OK] Server timezone is valid!") |
| |
| print("[OK] Server timezone tests passed!\n") |
|
|
| def test_frontend_compatibility(): |
| """Test frontend compatibility with timezone data.""" |
| print("Testing frontend compatibility...") |
| |
| |
| schedule_data = { |
| "id": "123", |
| "schedule_time": "Monday 14:30::::America/New_York", |
| "adjusted_time": "Monday 14:25::::America/New_York" |
| } |
| |
| |
| display_time = schedule_data["schedule_time"].split("::::")[0] |
| print(f"[OK] Display time (no timezone): {display_time}") |
| |
| |
| if "::::" in schedule_data["schedule_time"]: |
| timezone = schedule_data["schedule_time"].split("::::")[1] |
| print(f"[OK] Extracted timezone: {timezone}") |
| |
| print("[OK] Frontend compatibility tests passed!\n") |
|
|
| def main(): |
| """Run all timezone tests.""" |
| print("Starting timezone functionality tests...\n") |
| |
| try: |
| test_timezone_validation() |
| test_timezone_formatting() |
| test_timezone_parsing() |
| test_adjusted_time_calculation() |
| test_server_timezone() |
| test_frontend_compatibility() |
| |
| print("[OK] All timezone tests passed successfully!") |
| return True |
| |
| except Exception as e: |
| print(f"[FAIL] Test failed with error: {e}") |
| import traceback |
| traceback.print_exc() |
| return False |
|
|
| if __name__ == "__main__": |
| success = main() |
| sys.exit(0 if success else 1) |