Skip to content

Commit

Permalink
Merge pull request #110 from jazzband/96-fix-cache-invalidation
Browse files Browse the repository at this point in the history
96 fix cache invalidation
  • Loading branch information
sergei-maertens authored Dec 8, 2023
2 parents 2c90289 + 3d53512 commit fc0c6af
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 4 deletions.
31 changes: 27 additions & 4 deletions cookie_consent/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@
)


def clear_cache_after(func):
def wrapper(*args, **kwargs):
return_value = func(*args, **kwargs)
delete_cache()
return return_value

return wrapper


class CookieGroupDict(TypedDict):
varname: str
name: str
Expand All @@ -29,6 +38,16 @@ class CookieGroupDict(TypedDict):
# version: str


class BaseQueryset(models.query.QuerySet):
@clear_cache_after
def delete(self):
return super().delete()

@clear_cache_after
def update(self, **kwargs):
return super().update(**kwargs)


class CookieGroup(models.Model):
varname = models.CharField(
_("Variable name"), max_length=32, validators=[validate_cookie_name]
Expand All @@ -48,6 +67,8 @@ class CookieGroup(models.Model):
ordering = models.IntegerField(_("Ordering"), default=0)
created = models.DateTimeField(_("Created"), auto_now_add=True, blank=True)

objects = BaseQueryset.as_manager()

class Meta:
verbose_name = _("Cookie Group")
verbose_name_plural = _("Cookie Groups")
Expand All @@ -62,13 +83,13 @@ def get_version(self) -> str:
except IndexError:
return ""

@clear_cache_after
def delete(self, *args, **kwargs):
super(CookieGroup, self).delete(*args, **kwargs)
delete_cache()

@clear_cache_after
def save(self, *args, **kwargs):
super(CookieGroup, self).save(*args, **kwargs)
delete_cache()

def for_json(self) -> CookieGroupDict:
return {
Expand All @@ -92,6 +113,8 @@ class Cookie(models.Model):
domain = models.CharField(_("Domain"), max_length=250, blank=True)
created = models.DateTimeField(_("Created"), auto_now_add=True, blank=True)

objects = BaseQueryset.as_manager()

class Meta:
verbose_name = _("Cookie")
verbose_name_plural = _("Cookies")
Expand All @@ -107,13 +130,13 @@ def varname(self):
def get_version(self):
return self.created.isoformat()

@clear_cache_after
def delete(self, *args, **kwargs):
super(Cookie, self).delete(*args, **kwargs)
delete_cache()

@clear_cache_after
def save(self, *args, **kwargs):
super(Cookie, self).save(*args, **kwargs)
delete_cache()


ACTION_ACCEPTED = 1
Expand Down
37 changes: 37 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# -*- coding: utf-8 -*-
from unittest import mock

from django.core.exceptions import ValidationError
from django.test import TestCase

Expand All @@ -24,6 +26,25 @@ def test_get_version(self):
self.cookie_group.get_version(), self.cookie.created.isoformat()
)

@mock.patch("cookie_consent.models.delete_cache")
def test_bulk_delete(self, mock_delete_cache):
deleted_objs_count, _ = CookieGroup.objects.filter(
id=self.cookie_group.id
).delete()

# Deleting a CookieGroup also deletes the associated Cookies, that's why we expect a count of 2.
self.assertEqual(deleted_objs_count, 2)
self.assertEqual(mock_delete_cache.call_count, 1)

@mock.patch("cookie_consent.models.delete_cache")
def test_bulk_update(self, mock_delete_cache):
updated_objs_count = CookieGroup.objects.filter(id=self.cookie_group.id).update(
name="Optional2"
)

self.assertEqual(updated_objs_count, 1)
self.assertEqual(mock_delete_cache.call_count, 1)


class CookieTest(TestCase):
def setUp(self):
Expand All @@ -42,6 +63,22 @@ def setUp(self):
def test_varname(self):
self.assertEqual(self.cookie.varname, "optional=foo:.example.com")

@mock.patch("cookie_consent.models.delete_cache")
def test_bulk_delete(self, mock_delete_cache):
deleted_objs_count, _ = Cookie.objects.filter(id=self.cookie.id).delete()

self.assertEqual(deleted_objs_count, 1)
self.assertEqual(mock_delete_cache.call_count, 1)

@mock.patch("cookie_consent.models.delete_cache")
def test_bulk_update(self, mock_delete_cache):
updated_objs_count = Cookie.objects.filter(id=self.cookie.id).update(
name="foo2"
)

self.assertEqual(updated_objs_count, 1)
self.assertEqual(mock_delete_cache.call_count, 1)


class ValidateCookieNameTest(TestCase):
def test_valid(self):
Expand Down

0 comments on commit fc0c6af

Please sign in to comment.