Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions health_check/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import re

from django.db import transaction
from django.http import HttpResponse, JsonResponse
from django.utils.decorators import method_decorator
from django.views.decorators.cache import never_cache
Expand Down Expand Up @@ -79,6 +80,7 @@ def __lt__(self, other):
return self.weight.__lt__(other.weight)


@method_decorator(transaction.non_atomic_requests, name="dispatch")
class MainView(CheckMixin, TemplateView):
template_name = "health_check/index.html"

Expand Down
14 changes: 14 additions & 0 deletions tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import json
from unittest.mock import Mock

import pytest
from django.db import DatabaseError
from django.urls import reverse

from health_check.backends import BaseHealthCheckBackend
Expand Down Expand Up @@ -309,3 +311,15 @@ def run_check(self):
assert response.status_code == 500, response.content.decode("utf-8")
assert response["content-type"] == "application/json"
assert "JSON Error" in json.loads(response.content.decode("utf-8"))[JSONErrorBackend().identifier()]

@pytest.mark.django_db(transaction=True)
def test_non_native_atomic_request(self, settings, monkeypatch, client):
# See also: https://github.com/revsys/django-health-check/pull/469
settings.DATABASES["default"]["ATOMIC_REQUESTS"] = True
# disable the ensure_connection
monkeypatch.setattr(
"django.db.backends.base.base.BaseDatabaseWrapper.ensure_connection", Mock(side_effect=DatabaseError())
)
response = client.get(self.url)
assert response.status_code == 500
assert b"<title>System status</title>" in response.content