Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow to execute one test multiple times with different parameters #12

Merged
merged 1 commit into from
Aug 30, 2024
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
24 changes: 14 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,23 +55,27 @@ If you wish to use the `healthcheck` [management command](#management-command),

The default "health" endpoint will test a simple `SELECT 1` query on the database. Additional checks can be enabled in your Django settings.

Use the `ALIVE_CHECKS` setting to configure the checks to include. It is a dictionary with the path to a Python function as a key and any keyword arguments to pass to that function as a value. A full example:
Use the `ALIVE_CHECKS` setting to configure the checks to include. It is a list of tuples with the path to a Python function as a first argiment and dict of keyword arguments to pass to that function as a second argument. A full example:

```python
ALIVE_CHECKS = {
"django_alive.checks.check_database": {},
"django_alive.checks.check_staticfile": {
ALIVE_CHECKS = [
("django_alive.checks.check_database", {}),
("django_alive.checks.check_staticfile", {
"filename": "img/favicon.ico",
},
"django_alive.checks.check_cache": {
}),
("django_alive.checks.check_cache", {
"cache": "session",
"key": "test123",
},
"django_alive.checks.check_migrations": {},
}

}),
("django_alive.checks.check_migrations", {}),
]
```

**⚠️ Warning: Changed in version 1.3.0 ⚠️**

**NOTE:** Old settings with `ALIVE_CHECKS` as dict was deprecated in favor of a list of tuples.


### Built-in Checks

Defined in `django_alive.checks`.
Expand Down
9 changes: 8 additions & 1 deletion django_alive/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import json

from django.test import TestCase
from django.test import TestCase, override_settings
from django.urls import reverse

from .side_effects import ERR_MSG, bad_database_check
Expand All @@ -22,6 +22,13 @@ def test_healthcheck(self):
self.assertEqual(response.status_code, 200)
self.assertEqual(json.loads(response.content.decode("utf8")), {"healthy": True})

@override_settings(ALIVE_CHECKS={"django_alive.checks.check_migrations": {}})
def test_deprecated_dict_format(self):
with self.assertWarns(DeprecationWarning):
response = self.client.get(reverse("alive_health"))
self.assertEqual(response.status_code, 200)
self.assertEqual(json.loads(response.content.decode("utf8")), {"healthy": True})

def test_healtcheck_failed(self):

with patch(
Expand Down
17 changes: 14 additions & 3 deletions django_alive/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import warnings
from django.conf import settings
from django.utils.module_loading import import_string

Expand All @@ -10,14 +11,24 @@
pass


DEFAULT_ALIVE_CHECKS = {"django_alive.checks.check_database": {}}
ALIVE_CHECKS = getattr(settings, "ALIVE_CHECKS", DEFAULT_ALIVE_CHECKS)
DEFAULT_ALIVE_CHECKS = [("django_alive.checks.check_database", {})]


def perform_healthchecks():
# typing: () -> (bool, List[str])
errors = []
for func, kwargs in ALIVE_CHECKS.items():
ALIVE_CHECKS = getattr(settings, "ALIVE_CHECKS", DEFAULT_ALIVE_CHECKS)
if isinstance(ALIVE_CHECKS, dict):
# Deprecated dict format
warnings.warn(
"ALIVE_CHECKS should be a list of tuples, not a dict. "
"Please update your settings.",
DeprecationWarning,
)
checks = ALIVE_CHECKS.items()
else:
checks = ALIVE_CHECKS
for func, kwargs in checks:
try:
import_string(func)(**kwargs)
except HealthcheckFailure as e:
Expand Down
Loading