Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
danirus committed Dec 31, 2022
1 parent c2c770c commit f601f3b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
12 changes: 5 additions & 7 deletions django_comments_ink/caching.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,16 @@ def clear_comment_cache(content_type_id, object_pk, site_id):
return False

keys = []
for key_pattern in [
"comment_qs",
"comment_count",
"comments_paged",
"comment_list_auth",
"comment_list_anon",
]:
for key_pattern in ["comment_qs", "comment_count", "comments_paged"]:
key = settings.COMMENTS_INK_CACHE_KEYS[key_pattern].format(
ctype_pk=content_type_id, object_pk=object_pk, site_id=site_id
)
keys.append(key)

for key_pattern in ["comment_list_auth", "comment_list_anon"]:
key = settings.COMMENTS_INK_CACHE_KEYS[key_pattern].format(path="*")
keys.append(key)

for key in keys:
if dci_cache.get(key):
logger.debug("Delete cached key %s" % key)
Expand Down
27 changes: 20 additions & 7 deletions django_comments_ink/tests/test_templatetags.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,9 +453,15 @@ class FakeRequest:
def __init__(self, **kwargs):
self.kwargs = kwargs

def __getattr__(self, name):
return self.kwargs.get(name, None)

def get_property(self):
return self.kwargs

def get_full_path(self):
return self.path if self.path else ""

GET = property(get_property)


Expand Down Expand Up @@ -704,21 +710,28 @@ def test_render_inkcomment_list_raises_TemplateSyntaxError(an_article):
@pytest.mark.django_db
def test_render_inkcomment_list_uses_cached_list(monkeypatch, an_article):
fake_cache = FakeCache()
fake_request = FakeRequest(
path="/comment_list/15/1/1", user=AnonymousUser()
)
monkeypatch.setattr(comments_ink.caching, "get_cache", lambda: fake_cache)
setup_paginator_example_1(an_article)

t = "{% load comments_ink %}" "{% render_inkcomment_list for object %}"

assert len(fake_cache.store) == 0
assert not "/comment_list/15/1/1/anon" in fake_cache.store
assert not "/comment_list/15/1/1|anon" in fake_cache.store

result_1 = Template(t).render(Context({"object": an_article}))
assert "/comment_list/15/1/1/anon" in fake_cache.store
assert fake_cache.found["/comment_list/15/1/1/anon"] == False
result_1 = Template(t).render(
Context({"request": fake_request, "object": an_article})
)
assert "/comment_list/15/1/1|anon" in fake_cache.store
assert fake_cache.found["/comment_list/15/1/1|anon"] == False

result_2 = Template(t).render(Context({"object": an_article}))
assert "/comment_list/15/1/1/anon" in fake_cache.store
assert fake_cache.found["/comment_list/15/1/1/anon"] == True
result_2 = Template(t).render(
Context({"request": fake_request, "object": an_article})
)
assert "/comment_list/15/1/1|anon" in fake_cache.store
assert fake_cache.found["/comment_list/15/1/1|anon"] == True

assert result_1 == result_2

Expand Down

0 comments on commit f601f3b

Please sign in to comment.