from unittest import mock import pytest from pydantic import ValidationError from index import handler @mock.patch("index.run") def test_handler_passes_event_to_run(mock_run: mock.MagicMock) -> None: expected = { "dry_run": True, "scanned": 0, "changed": 0, "unchanged": 0, "failed": 0, "updates": [], } mock_run.return_value = expected event = {"limit": 100, "dry_run": False, "request_timeout_seconds": 5} result = handler(event, mock.MagicMock()) assert result == expected call_args = mock_run.call_args[0][0] assert call_args["limit"] == 100 assert call_args["dry_run"] is False assert call_args["request_timeout_seconds"] == 5 @mock.patch("index.run") def test_handler_applies_schema_defaults(mock_run: mock.MagicMock) -> None: mock_run.return_value = {} handler({}, mock.MagicMock()) call_args = mock_run.call_args[0][0] assert call_args["limit"] == 500 assert call_args["dry_run"] is True assert call_args["request_timeout_seconds"] == 10 @pytest.mark.parametrize( "invalid_event,expected_field", [ ({"limit": 0}, "limit"), ({"limit": 5001}, "limit"), ({"request_timeout_seconds": 0}, "request_timeout_seconds"), ({"request_timeout_seconds": 121}, "request_timeout_seconds"), ], ) def test_handler_rejects_invalid_event( invalid_event: dict[str, object], expected_field: str ) -> None: with pytest.raises(ValidationError) as exc_info: handler(invalid_event, mock.MagicMock()) error_fields = [e["loc"][0] for e in exc_info.value.errors()] assert expected_field in error_fields