Repository: oppia/oppia
File: core/controllers/base_test.py (Python)

Code before:
                '/mock_with_schema', self.payload, csrf_token=self.csrf_token,
                expected_status_int=500)

    def test_request_without_schema_using_payload_or_request_attr_raise_no_err(
        self
    ) -> None:
        test_app_ctx = self.swap(self, 'testapp', self.testapp)
        handler_class_still_needs_schema_list_ctx = self.swap(
            handler_schema_constants, 'HANDLER_CLASS_NAMES_WITH_NO_SCHEMA',
            ['MockHandlerWithoutSchema'])
        with test_app_ctx, handler_class_still_needs_schema_list_ctx:
            self.post_json(
                '/mock_without_schema', self.payload, csrf_token=self.csrf_token,
                expected_status_int=200)


class LogTypeTests(test_utils.GenericTestBase):
    """Tests for the LogType Enum."""

    def test_log_type_enum_values(self) -> None:
        """Test that LogType enum contains the correct values."""
        self.assertEqual(base.LogType.WARNING, 'warning')
        self.assertEqual(base.LogType.EXCEPTION, 'exception')


class LogExceptionMessageTests(test_utils.GenericTestBase):
    """Ensures that _log_exception_message correctly logs warnings and exceptions in BaseHandler."""  

    def setUp(self) -> None:
        super().setUp()
        self.mock_request = webapp2.Request.blank('/')
        self.mock_response = webapp2.Response()
        self.logged_warnings: List[str] = []
        self.logged_exceptions: List[str] = []

        def mock_logging_warning(msg: str) -> None:
            self.logged_warnings.append(msg)

        def mock_logging_exception(msg: str) -> None:
            self.logged_exceptions.append(msg)

        self.mock_logging_warning = mock_logging_warning
        self.mock_logging_exception = mock_logging_exception

    def test_log_exception_message(self) -> None:
        """Test that _log_exception_message correctly logs warning and exception messages."""
        handler: base.BaseHandler[Dict[str, str], Dict[str, str]] = base.BaseHandler(self.mock_request, self.mock_response)

        with self.swap(logging, 'warning', self.mock_logging_warning), \
             self.swap(logging, 'exception', self.mock_logging_exception):


Review comment (suggestion):
This isn't a helpful test. Look at the original issue description. Your tests (there should be more than one) should make sure that all the expected behaviours described in the original issue actually happen.

Code after:
                '/mock_with_schema', self.payload, csrf_token=self.csrf_token,
                expected_status_int=500)

    def test_request_without_schema_using_payload_or_request_attr_raise_no_err(
        self
    ) -> None:
        test_app_ctx = self.swap(self, 'testapp', self.testapp)
        handler_class_still_needs_schema_list_ctx = self.swap(
            handler_schema_constants, 'HANDLER_CLASS_NAMES_WITH_NO_SCHEMA',
            ['MockHandlerWithoutSchema'])
        with test_app_ctx, handler_class_still_needs_schema_list_ctx:
            self.post_json(
                '/mock_without_schema', self.payload, csrf_token=self.csrf_token,
                expected_status_int=200)


class LogTypeTests(test_utils.GenericTestBase):
    """Tests for the LogType Enum."""

    def test_log_type_enum_values(self) -> None:
        """Test that LogType enum contains the correct values."""
        self.assertEqual(base.LogType.WARNING, 'warning')
        self.assertEqual(base.LogType.EXCEPTION, 'exception')


class ExceptionsLoggingTests(test_utils.GenericTestBase):
    """Tests the logging behavior of handle_exception in BaseHandler."""

    def setUp(self) -> None:
        """Creates a mock request, response, and logging methods for capturing log outputs."""
        super().setUp()
        self.mock_request = webapp2.Request.blank('/')
        self.mock_request.environ['REQUEST_METHOD'] = 'POST'
        self.mock_response = webapp2.Response()

        self.logged_warnings: List[str] = []
        self.logged_exceptions: List[str] = []

        def mock_logging_warning(msg: str) -> None:
            self.logged_warnings.append(msg)

        def mock_logging_exception(msg: str) -> None:
            self.logged_exceptions.append(msg)

        self.mock_logging_warning = mock_logging_warning
        self.mock_logging_exception = mock_logging_exception

        self.handler: base.BaseHandler[Dict[str, str], Dict[str, str]] = base.BaseHandler(
            self.mock_request, self.mock_response
        )