"""Tests for utility functions (backend/utils.py).""" import pytest from datetime import datetime, timezone, timedelta from utils import utc_to_ist, format_ist_timestamp IST = timezone(timedelta(hours=5, minutes=30)) class TestUtcToIst: def test_midnight_utc_becomes_530_ist(self): utc = datetime(2024, 1, 1, 0, 0, 0) ist = utc_to_ist(utc) assert ist.hour == 5 assert ist.minute == 30 def test_adds_five_hours_thirty_minutes(self): utc = datetime(2024, 6, 15, 10, 0, 0) ist = utc_to_ist(utc) assert ist.hour == 15 assert ist.minute == 30 def test_rolls_over_to_next_day(self): utc = datetime(2024, 1, 1, 22, 0, 0) # 22:00 UTC → 03:30 next day IST ist = utc_to_ist(utc) assert ist.day == 2 assert ist.hour == 3 assert ist.minute == 30 def test_rolls_over_to_next_month(self): utc = datetime(2024, 1, 31, 23, 0, 0) # Jan 31 → Feb 1 IST ist = utc_to_ist(utc) assert ist.month == 2 assert ist.day == 1 def test_output_has_ist_timezone_offset(self): utc = datetime(2024, 1, 1, 12, 0, 0) ist = utc_to_ist(utc) assert ist.utcoffset() == timedelta(hours=5, minutes=30) def test_preserves_seconds(self): utc = datetime(2024, 3, 15, 8, 45, 30) ist = utc_to_ist(utc) assert ist.second == 30 def test_noon_utc_is_1730_ist(self): utc = datetime(2024, 7, 4, 12, 0, 0) ist = utc_to_ist(utc) assert ist.hour == 17 assert ist.minute == 30 class TestFormatIstTimestamp: def test_converts_z_suffix_timestamp(self): result = format_ist_timestamp("2024-01-01T00:00:00Z") assert "+05:30" in result def test_converts_explicit_utc_offset_timestamp(self): result = format_ist_timestamp("2024-01-01T00:00:00+00:00") assert "+05:30" in result def test_midnight_utc_produces_0530_ist(self): result = format_ist_timestamp("2024-01-01T00:00:00Z") assert "05:30:00+05:30" in result def test_noon_utc_produces_1730_ist(self): result = format_ist_timestamp("2024-01-01T12:00:00Z") assert "17:30:00+05:30" in result def test_returns_iso_format_string(self): result = format_ist_timestamp("2024-06-15T08:00:00Z") # Should be parseable as ISO datetime parsed = datetime.fromisoformat(result) assert parsed is not None def test_invalid_text_raises_value_error(self): with pytest.raises(ValueError): format_ist_timestamp("not-a-date") def test_invalid_month_raises_value_error(self): with pytest.raises(ValueError): format_ist_timestamp("2024-13-01T00:00:00Z") def test_empty_string_raises_value_error(self): with pytest.raises(ValueError): format_ist_timestamp("") def test_slash_separated_date_raises_value_error(self): with pytest.raises(ValueError): format_ist_timestamp("2024/01/01T00:00:00") # Slashes not valid ISO format