Skip to content

Commit

Permalink
Fix storages deprecation warnings (#372)
Browse files Browse the repository at this point in the history
* ⚙️ Adds Django 4.2 support and fixes trove classifiers

* :shirts: Runs black on code to make CI happy

* :shirts: Runs isort on code to make CI happy

* 💚 Updates test to match exception copy

* 📝 mirrors old README.rst to make docs work

Not a great solution, but a workaround.

* ⚙️ Adds pip cache support

* 🔥 Removes 4.0 from grid because it's no longer supported

* ⚙️ Updates cache-key

This isn't really used, but I think having caching is a win

* ⚙️ Adds missing file

* 🔥 Removes cache line

* Fix storages deprecation warnings

---------

Co-authored-by: Jeff Triplett <[email protected]>
  • Loading branch information
browniebroke and jefftriplett authored Oct 19, 2023
1 parent 8969b61 commit 59a990a
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 6 deletions.
21 changes: 17 additions & 4 deletions health_check/storage/backends.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import uuid

import django
from django.conf import settings
from django.core.files.base import ContentFile
from django.core.files.storage import get_storage_class

if django.VERSION >= (4, 2):
from django.core.files.storage import InvalidStorageError, storages
else:
from django.core.files.storage import get_storage_class

from health_check.backends import BaseHealthCheckBackend
from health_check.exceptions import ServiceUnavailable
Expand All @@ -22,13 +27,20 @@ class MyStorageHealthCheck(StorageHealthCheck):
(e.g 'django.core.files.storage.FileSystemStorage') or a Storage instance.
"""

storage_alias = None
storage = None

def get_storage(self):
if isinstance(self.storage, str):
return get_storage_class(self.storage)()
if django.VERSION >= (4, 2):
try:
return storages[self.storage_alias]
except InvalidStorageError:
return None
else:
return self.storage
if isinstance(self.storage, str):
return get_storage_class(self.storage)()
else:
return self.storage

def get_file_name(self):
return "health_check_storage_test/test-%s.txt" % uuid.uuid4()
Expand Down Expand Up @@ -68,4 +80,5 @@ def check_status(self):


class DefaultFileStorageHealthCheck(StorageHealthCheck):
storage_alias = "default"
storage = settings.DEFAULT_FILE_STORAGE
53 changes: 51 additions & 2 deletions tests/test_storage.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import unittest
from unittest import mock

import django
from django.core.files.storage import Storage
from django.test import TestCase

Expand All @@ -10,6 +12,10 @@
)


class CustomStorage(Storage):
pass


class MockStorage(Storage):
"""
A Mock Storage backend used for testing.
Expand Down Expand Up @@ -68,6 +74,25 @@ def test_get_storage(self):
default_storage = DefaultFileStorageHealthCheck()
self.assertIsInstance(default_storage.get_storage(), Storage)

@unittest.skipUnless((4, 2) <= django.VERSION < (5, 0), "Only for Django 4.2 - 5.0")
def test_get_storage_django_between_42_and_50(self):
"""Check that the old DEFAULT_FILE_STORAGE setting keeps being supported."""
# Note: this test doesn't work on Django<4.2 because the setting value is
# evaluated when the class attribute DefaultFileStorageHealthCheck.store is
# read, which is at import time, before we can mock the setting.
with self.settings(DEFAULT_FILE_STORAGE="tests.test_storage.CustomStorage"):
default_storage = DefaultFileStorageHealthCheck()
self.assertIsInstance(default_storage.get_storage(), CustomStorage)

@unittest.skipUnless((4, 2) <= django.VERSION, "Django 4.2+ required")
def test_get_storage_django_42_plus(self):
"""Check that the new STORAGES setting is supported."""
with self.settings(
STORAGES={"default": {"BACKEND": "tests.test_storage.CustomStorage"}}
):
default_storage = DefaultFileStorageHealthCheck()
self.assertIsInstance(default_storage.get_storage(), CustomStorage)

@mock.patch(
"health_check.storage.backends.DefaultFileStorageHealthCheck.storage",
MockStorage(),
Expand All @@ -88,21 +113,45 @@ def test_check_status_working(self):
):
self.assertTrue(default_storage_health.check_status())

@unittest.skipUnless(django.VERSION <= (4, 1), "Only for Django 4.1 and earlier")
@mock.patch(
"health_check.storage.backends.DefaultFileStorageHealthCheck.storage",
MockStorage(saves=False),
)
def test_file_does_not_exist(self):
def test_file_does_not_exist_django_41_earlier(self):
"""Test check_status raises ServiceUnavailable when file is not saved."""
default_storage_health = DefaultFileStorageHealthCheck()
with self.assertRaises(ServiceUnavailable):
default_storage_health.check_status()

@unittest.skipUnless((4, 2) <= django.VERSION, "Only for Django 4.2+")
@mock.patch(
"health_check.storage.backends.storages",
{"default": MockStorage(saves=False)},
)
def test_file_does_not_exist_django_42_plus(self):
"""Test check_status raises ServiceUnavailable when file is not saved."""
default_storage_health = DefaultFileStorageHealthCheck()
with self.assertRaises(ServiceUnavailable):
default_storage_health.check_status()

@unittest.skipUnless(django.VERSION <= (4, 1), "Only for Django 4.1 and earlier")
@mock.patch(
"health_check.storage.backends.DefaultFileStorageHealthCheck.storage",
MockStorage(deletes=False),
)
def test_file_not_deleted(self):
def test_file_not_deleted_django_41_earlier(self):
"""Test check_status raises ServiceUnavailable when file is not deleted."""
default_storage_health = DefaultFileStorageHealthCheck()
with self.assertRaises(ServiceUnavailable):
default_storage_health.check_status()

@unittest.skipUnless((4, 2) <= django.VERSION, "Only for Django 4.2+")
@mock.patch(
"health_check.storage.backends.storages",
{"default": MockStorage(deletes=False)},
)
def test_file_not_deleted_django_42_plus(self):
"""Test check_status raises ServiceUnavailable when file is not deleted."""
default_storage_health = DefaultFileStorageHealthCheck()
with self.assertRaises(ServiceUnavailable):
Expand Down

0 comments on commit 59a990a

Please sign in to comment.