Skip to content

Commit

Permalink
Add invalidate_cache test
Browse files Browse the repository at this point in the history
  • Loading branch information
johanneswilm committed Aug 15, 2022
1 parent 72c1cae commit 28302c5
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Changelog
* Unreleased
* Allowed for rectangular avatars. Custom avatar tag templates now require the specification of both a ``width`` and ``height`` attribute instead of ``size``.
* Made ``True`` the default value of ``AVATAR_CLEANUP_DELETED``. (Set to ``False`` to obtain previous behavior).
* Fix invalidate_cache for on-the-fly created thumbnails

* 6.0.1 (August 12, 2022)
* Exclude tests folder from distribution.
Expand Down
1 change: 1 addition & 0 deletions avatar/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ def create_thumbnail(self, width, height=None, quality=None):
thumb = self.avatar.storage.save(
self.avatar_name(width, height), thumb_file
)
invalidate_cache(self.user, width, height)

def avatar_url(self, width, height=None):
return self.avatar.storage.url(self.avatar_name(width, height))
Expand Down
5 changes: 3 additions & 2 deletions avatar/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def get_user(userdescriptor):
return User.objects.get_by_natural_key(userdescriptor)


def get_cache_key(user_or_username, prefix, width=False, height=False):
def get_cache_key(user_or_username, prefix, width=None, height=None):
"""
Returns a cache key consisten of a username and image size.
"""
Expand Down Expand Up @@ -74,7 +74,7 @@ def cached_func(user, width=None, height=None, **kwargs):
# add image size to set of cached sizes so we can invalidate them later
sizes_key = get_cache_key(user, "cached_sizes")
sizes = cache.get(sizes_key, set())
sizes.add((width or default_size, height or width))
sizes.add((width or default_size, height or width or default_size))
cache_set(sizes_key, sizes)
return result

Expand All @@ -98,6 +98,7 @@ def invalidate_cache(user, width=None, height=None):
else:
# Size is specified with height and width.
cache.delete(get_cache_key(user, prefix, size[0], size[1]))
cache.set(sizes_key, set())


def get_default_avatar_url():
Expand Down
35 changes: 27 additions & 8 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from django.contrib.admin.sites import AdminSite
from django.core import management
from django.core.cache import cache
from django.test import TestCase
from django.test.utils import override_settings
from django.urls import reverse
Expand All @@ -15,7 +16,12 @@
from avatar.models import Avatar
from avatar.signals import avatar_deleted
from avatar.templatetags import avatar_tags
from avatar.utils import get_primary_avatar, get_user_model
from avatar.utils import (
get_cache_key,
get_primary_avatar,
get_user_model,
invalidate_cache,
)


class AssertSignal:
Expand Down Expand Up @@ -465,10 +471,23 @@ def test_rebuild_avatars(self):
self.assertMediaFileExists(avatar_80_url)
self.assertNotEqual(avatar_80_mtime, self.get_media_file_mtime(avatar_80_url))

# def testAvatarOrder
# def testReplaceAvatarWhenMaxIsOne
# def testHashFileName
# def testHashUserName
# def testChangePrimaryAvatar
# def testDeleteThumbnailAndRecreation
# def testAutomaticThumbnailCreation
def test_invalidate_cache(self):
upload_helper(self, "test.png")
sizes_key = get_cache_key(self.user, "cached_sizes")
sizes = cache.get(sizes_key, set())
# Only default 80x80 thumbnail is cached
self.assertEqual(len(sizes), 1)
# Invalidate cache
invalidate_cache(self.user)
sizes = cache.get(sizes_key, set())
# No thumbnail is cached.
self.assertEqual(len(sizes), 0)
# Create a custom 25x25 thumbnail and check that it is cached
avatar_tags.avatar(self.user, 25)
sizes = cache.get(sizes_key, set())
self.assertEqual(len(sizes), 1)
# Invalidate cache again.
invalidate_cache(self.user)
sizes = cache.get(sizes_key, set())
# It should now be empty again
self.assertEqual(len(sizes), 0)

0 comments on commit 28302c5

Please sign in to comment.