refactor: improve language normalization and date handling utilities
All checks were successful
CI / lint-and-test (push) Successful in 21s

- Introduced a new `normalize_lang` function to standardize language codes across the application, ensuring consistent handling of user language preferences.
- Refactored date handling utilities by adding `parse_utc_iso` and `parse_utc_iso_naive` functions for better parsing of ISO 8601 date strings, enhancing timezone awareness.
- Updated various modules to utilize the new language normalization and date parsing functions, improving code clarity and maintainability.
- Enhanced error handling in date validation to raise specific `DateRangeValidationError` exceptions, providing clearer feedback on validation issues.
- Improved test coverage for date range validation and language normalization functionalities, ensuring robustness and reliability.
This commit is contained in:
2026-02-20 22:42:54 +03:00
parent f53ef81306
commit d02d0a1835
19 changed files with 216 additions and 158 deletions

View File

@@ -235,18 +235,15 @@ async def test_help_cmd_replies_with_help_text():
user = _make_user()
update = _make_update(message=message, effective_user=user)
with patch("duty_teller.handlers.commands.session_scope") as mock_scope:
mock_session = MagicMock()
mock_scope.return_value.__enter__.return_value = mock_session
mock_scope.return_value.__exit__.return_value = None
with patch(
"duty_teller.handlers.commands.is_admin_for_telegram_user",
return_value=False,
):
with patch("duty_teller.handlers.commands.get_lang", return_value="en"):
with patch("duty_teller.handlers.commands.t") as mock_t:
mock_t.side_effect = lambda lang, key: f"[{key}]"
await help_cmd(update, MagicMock())
with patch(
"duty_teller.handlers.commands.is_admin_async",
new_callable=AsyncMock,
return_value=False,
):
with patch("duty_teller.handlers.commands.get_lang", return_value="en"):
with patch("duty_teller.handlers.commands.t") as mock_t:
mock_t.side_effect = lambda lang, key: f"[{key}]"
await help_cmd(update, MagicMock())
message.reply_text.assert_called_once()
text = message.reply_text.call_args[0][0]
assert "help.title" in text or "[help.title]" in text

View File

@@ -5,6 +5,7 @@ from datetime import date
import pytest
from duty_teller.utils.dates import (
DateRangeValidationError,
day_start_iso,
day_end_iso,
duty_to_iso,
@@ -47,15 +48,18 @@ def test_validate_date_range_ok():
def test_validate_date_range_bad_format():
with pytest.raises(ValueError, match="формате YYYY-MM-DD"):
with pytest.raises(DateRangeValidationError) as exc_info:
validate_date_range("01-01-2025", "2025-01-31")
with pytest.raises(ValueError, match="формате YYYY-MM-DD"):
assert exc_info.value.kind == "bad_format"
with pytest.raises(DateRangeValidationError) as exc_info:
validate_date_range("2025-01-01", "invalid")
assert exc_info.value.kind == "bad_format"
def test_validate_date_range_from_after_to():
with pytest.raises(ValueError, match="from не должна быть позже"):
with pytest.raises(DateRangeValidationError) as exc_info:
validate_date_range("2025-02-01", "2025-01-01")
assert exc_info.value.kind == "from_after_to"
# --- user ---