diff options
author | Jarosław Wygoda <jaroslaw@wygoda.me> | 2023-01-11 10:48:57 +0100 |
---|---|---|
committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2023-01-12 06:20:57 +0100 |
commit | 1ec3f0961fedbe01f174b78ef2805a9d4f3844b1 (patch) | |
tree | 58c346b0abf71be4cee2e8d07ed1ddc4be744740 /tests/file_storage | |
parent | d02a9f0cee84e3d23f676bdf2ab6aadbf4a5bfe8 (diff) | |
download | django-1ec3f0961fedbe01f174b78ef2805a9d4f3844b1.tar.gz |
Fixed #26029 -- Allowed configuring custom file storage backends.
Diffstat (limited to 'tests/file_storage')
-rw-r--r-- | tests/file_storage/tests.py | 48 |
1 files changed, 46 insertions, 2 deletions
diff --git a/tests/file_storage/tests.py b/tests/file_storage/tests.py index 5c7190d698..d4e5969519 100644 --- a/tests/file_storage/tests.py +++ b/tests/file_storage/tests.py @@ -14,9 +14,14 @@ from urllib.request import urlopen from django.core.cache import cache from django.core.exceptions import SuspiciousFileOperation from django.core.files.base import ContentFile, File -from django.core.files.storage import FileSystemStorage +from django.core.files.storage import FileSystemStorage, InvalidStorageError from django.core.files.storage import Storage as BaseStorage -from django.core.files.storage import default_storage, get_storage_class +from django.core.files.storage import ( + StorageHandler, + default_storage, + get_storage_class, + storages, +) from django.core.files.uploadedfile import ( InMemoryUploadedFile, SimpleUploadedFile, @@ -1157,3 +1162,42 @@ class FileLikeObjectTestCase(LiveServerTestCase): remote_file = urlopen(self.live_server_url + "/") with self.storage.open(stored_filename) as stored_file: self.assertEqual(stored_file.read(), remote_file.read()) + + +class StorageHandlerTests(SimpleTestCase): + @override_settings( + STORAGES={ + "custom_storage": { + "BACKEND": "django.core.files.storage.FileSystemStorage", + }, + } + ) + def test_same_instance(self): + cache1 = storages["custom_storage"] + cache2 = storages["custom_storage"] + self.assertIs(cache1, cache2) + + def test_defaults(self): + storages = StorageHandler() + self.assertEqual(storages.backends, {}) + + def test_nonexistent_alias(self): + msg = "Could not find config for 'nonexistent' in settings.STORAGES." + storages = StorageHandler() + with self.assertRaisesMessage(InvalidStorageError, msg): + storages["nonexistent"] + + def test_nonexistent_backend(self): + test_storages = StorageHandler( + { + "invalid_backend": { + "BACKEND": "django.nonexistent.NonexistentBackend", + }, + } + ) + msg = ( + "Could not find backend 'django.nonexistent.NonexistentBackend': " + "No module named 'django.nonexistent'" + ) + with self.assertRaisesMessage(InvalidStorageError, msg): + test_storages["invalid_backend"] |