Files
mostovik-backend/tests/apps/core/test_exception_handler.py
Aleksandr Meshchriakov ee95628a0a
Some checks failed
CI/CD Pipeline / Run Tests (push) Failing after 37s
CI/CD Pipeline / Code Quality Checks (push) Failing after 43s
CI/CD Pipeline / Build & Push Images (push) Has been skipped
CI/CD Pipeline / Deploy (dev) (push) Has been skipped
CI/CD Pipeline / Deploy (prod) (push) Has been skipped
CI/CD Pipeline / Code Quality Checks (pull_request) Failing after 0s
CI/CD Pipeline / Run Tests (pull_request) Failing after 0s
CI/CD Pipeline / Build & Push Images (pull_request) Has been skipped
CI/CD Pipeline / Deploy (dev) (pull_request) Has been skipped
CI/CD Pipeline / Deploy (prod) (pull_request) Has been skipped
feat: обновления парсеров, тестов и миграций
- Обновлены клиенты парсеров (checko, fns, minpromtorg, proverki, zakupki)
- Добавлены новые миграции для моделей
- Расширено покрытие тестами
- Обновлены конфигурации и настройки проекта
- Добавлены утилиты для тестирования

Co-Authored-By: Warp <agent@warp.dev>
2026-02-10 10:17:47 +01:00

59 lines
2.5 KiB
Python

"""Tests for custom exception handler."""
from django.core.exceptions import PermissionDenied
from django.http import Http404
from django.test import SimpleTestCase
from rest_framework.exceptions import APIException, ValidationError
from apps.core.exception_handler import custom_exception_handler
from apps.core.exceptions import BaseAPIException
class CustomExceptionHandlerTest(SimpleTestCase):
def _context(self):
return {"request": None, "view": None}
def test_base_api_exception(self):
class CustomAPIException(BaseAPIException):
status_code = 418
exc = CustomAPIException(message="oops", code="oops_code")
response = custom_exception_handler(exc, self._context())
self.assertEqual(response.status_code, 418)
self.assertFalse(response.data["success"])
self.assertEqual(response.data["errors"][0]["code"], "oops_code")
def test_http404_exception(self):
response = custom_exception_handler(Http404("missing"), self._context())
self.assertEqual(response.status_code, 404)
self.assertEqual(response.data["errors"][0]["code"], "not_found")
def test_permission_denied_exception(self):
response = custom_exception_handler(PermissionDenied("no"), self._context())
self.assertEqual(response.status_code, 403)
self.assertEqual(response.data["errors"][0]["code"], "permission_denied")
def test_api_exception_with_detail(self):
exc = APIException("bad")
response = custom_exception_handler(exc, self._context())
self.assertIsNotNone(response)
self.assertEqual(response.status_code, 500)
self.assertFalse(response.data["success"])
def test_validation_error_fields(self):
exc = ValidationError({"field": ["invalid"]})
response = custom_exception_handler(exc, self._context())
self.assertEqual(response.status_code, 400)
self.assertEqual(response.data["errors"][0]["code"], "validation_error")
def test_validation_error_list(self):
exc = ValidationError(["item1", "item2"])
response = custom_exception_handler(exc, self._context())
self.assertEqual(response.status_code, 400)
self.assertEqual(len(response.data["errors"]), 2)
def test_unhandled_exception(self):
response = custom_exception_handler(RuntimeError("boom"), self._context())
self.assertEqual(response.status_code, 500)
self.assertEqual(response.data["errors"][0]["code"], "internal_error")