From 64371880cbfd06d73d58fab5bcf3185dd11feee2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Dlouh=C3=BD?= Date: Tue, 23 Jan 2024 16:11:35 +0100 Subject: [PATCH] add tests for the THUMBNAIL_STORAGE and THUMBNAIL_STORAGE_ALIAS --- categories/tests/test_models.py | 9 ++++++++- example/settings-testing.py | 16 ++++++++++++++++ example/settings.py | 1 + example/test_storages.py | 10 ++++++++++ 4 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 example/test_storages.py diff --git a/categories/tests/test_models.py b/categories/tests/test_models.py index ed5d0d0..d54092a 100644 --- a/categories/tests/test_models.py +++ b/categories/tests/test_models.py @@ -1,19 +1,26 @@ import os +import django from django.core.files import File from django.core.files.uploadedfile import UploadedFile from django.test import TestCase from categories.models import Category +from example.test_storages import MyTestStorage, MyTestStorageAlias class TestCategoryThumbnail(TestCase): def test_thumbnail(self): file_name = "test_image.jpg" - with open(os.path.join(os.path.dirname(__file__), file_name), "rb") as f: test_image = UploadedFile(File(f), content_type="image/jpeg") category = Category.objects.create(name="Test Category", slug="test-category", thumbnail=test_image) + self.assertEqual(category.pk, 1) self.assertEqual(category.thumbnail_width, 640) self.assertEqual(category.thumbnail_height, 480) + + if django.VERSION >= (4, 2): + self.assertEqual(category.thumbnail.storage.__class__, MyTestStorageAlias) + else: + self.assertEqual(category.thumbnail.storage.__class__, MyTestStorage) diff --git a/example/settings-testing.py b/example/settings-testing.py index 2105458..2af0702 100644 --- a/example/settings-testing.py +++ b/example/settings-testing.py @@ -98,6 +98,20 @@ TEMPLATES = [ }, ] + +STORAGES = { + "default": { + "BACKEND": "django.core.files.storage.FileSystemStorage", + }, + "staticfiles": { + "BACKEND": "django.core.files.storage.FileSystemStorage", + }, + "thumbnails": { + "BACKEND": "example.test_storages.MyTestStorageAlias", + }, +} + + CATEGORIES_SETTINGS = { "ALLOW_SLUG_CHANGE": True, "RELATION_MODELS": ["simpletext.simpletext", "flatpages.flatpage"], @@ -115,6 +129,8 @@ CATEGORIES_SETTINGS = { {"name": "more_categories", "related_name": "more_cats"}, ), }, + "THUMBNAIL_STORAGE": "example.test_storages.MyTestStorage", + "THUMBNAIL_STORAGE_ALIAS": "thumbnails", } TEST_RUNNER = "django.test.runner.DiscoverRunner" diff --git a/example/settings.py b/example/settings.py index ababf90..5bc6203 100644 --- a/example/settings.py +++ b/example/settings.py @@ -99,6 +99,7 @@ TEMPLATES = [ } ] + CATEGORIES_SETTINGS = { "ALLOW_SLUG_CHANGE": True, "RELATION_MODELS": ["simpletext.simpletext", "flatpages.flatpage"], diff --git a/example/test_storages.py b/example/test_storages.py new file mode 100644 index 0000000..eb10fd5 --- /dev/null +++ b/example/test_storages.py @@ -0,0 +1,10 @@ +# Storages used for testing THUMBNAIL_STORAGE and THUMBNAIL_STORAGE_ALIAS +from django.core.files.storage import FileSystemStorage + + +class MyTestStorage(FileSystemStorage): + pass + + +class MyTestStorageAlias(FileSystemStorage): + pass