Skip to content

Commit

Permalink
add tests for the THUMBNAIL_STORAGE and THUMBNAIL_STORAGE_ALIAS
Browse files Browse the repository at this point in the history
  • Loading branch information
PetrDlouhy committed Jan 23, 2024
1 parent 5d83c0a commit fc489a8
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 1 deletion.
9 changes: 8 additions & 1 deletion categories/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -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)
16 changes: 16 additions & 0 deletions example/settings-testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,20 @@
},
]


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"],
Expand All @@ -115,6 +129,8 @@
{"name": "more_categories", "related_name": "more_cats"},
),
},
"THUMBNAIL_STORAGE": "example.test_storages.MyTestStorage",
"THUMBNAIL_STORAGE_ALIAS": "thumbnails",
}

TEST_RUNNER = "django.test.runner.DiscoverRunner"
1 change: 1 addition & 0 deletions example/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
}
]


CATEGORIES_SETTINGS = {
"ALLOW_SLUG_CHANGE": True,
"RELATION_MODELS": ["simpletext.simpletext", "flatpages.flatpage"],
Expand Down
10 changes: 10 additions & 0 deletions example/test_storages.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit fc489a8

Please sign in to comment.