From ec0480d06b52c3420b138e3bc5de0e7435c91563 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.py | 13 +++++++++++++ example/test_storages.py | 10 ++++++++++ 3 files changed, 31 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 ed5d0d08..d54092aa 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.py b/example/settings.py index ababf90b..d0ae1f71 100644 --- a/example/settings.py +++ b/example/settings.py @@ -99,6 +99,17 @@ } ] + +STORAGES = { + "default": { + "BACKEND": "django.core.files.storage.FileSystemStorage", + }, + "thumbnails": { + "BACKEND": "example.test_storages.MyTestStorageAlias", + }, +} + + CATEGORIES_SETTINGS = { "ALLOW_SLUG_CHANGE": True, "RELATION_MODELS": ["simpletext.simpletext", "flatpages.flatpage"], @@ -116,6 +127,8 @@ {"name": "more_categories", "related_name": "more_cats"}, ), }, + "THUMBNAIL_STORAGE": "example.test_storages.MyTestStorage", + "THUMBNAIL_STORAGE_ALIAS": "thumbnails", } if django.VERSION[1] > 5: diff --git a/example/test_storages.py b/example/test_storages.py new file mode 100644 index 00000000..eb10fd53 --- /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