mirror of https://github.com/zulip/zulip.git
tests: Improve linkifiers test.
We test at a higher level now.
This commit is contained in:
parent
549891266d
commit
730ae61ce5
|
@ -3,7 +3,7 @@ import os
|
||||||
import re
|
import re
|
||||||
from html import escape
|
from html import escape
|
||||||
from textwrap import dedent
|
from textwrap import dedent
|
||||||
from typing import Any, Dict, List, Optional, Set, Tuple, cast
|
from typing import Any, Dict, List, Optional, Set, Tuple
|
||||||
from unittest import mock
|
from unittest import mock
|
||||||
|
|
||||||
import orjson
|
import orjson
|
||||||
|
@ -63,13 +63,11 @@ from zerver.models import (
|
||||||
UserGroup,
|
UserGroup,
|
||||||
UserMessage,
|
UserMessage,
|
||||||
UserProfile,
|
UserProfile,
|
||||||
flush_linkifiers,
|
|
||||||
flush_per_request_caches,
|
flush_per_request_caches,
|
||||||
get_client,
|
get_client,
|
||||||
get_realm,
|
get_realm,
|
||||||
get_stream,
|
get_stream,
|
||||||
linkifiers_for_realm,
|
linkifiers_for_realm,
|
||||||
realm_in_local_linkifiers_cache,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -1453,43 +1451,6 @@ class MarkdownTest(ZulipTestCase):
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_flush_linkifier(self) -> None:
|
|
||||||
realm = get_realm("zulip")
|
|
||||||
|
|
||||||
def flush() -> None:
|
|
||||||
"""
|
|
||||||
flush_linkifiers is a post-save hook, so calling it
|
|
||||||
directly for testing is kind of awkward
|
|
||||||
"""
|
|
||||||
|
|
||||||
class Instance:
|
|
||||||
realm_id: Optional[int] = None
|
|
||||||
|
|
||||||
instance = Instance()
|
|
||||||
instance.realm_id = realm.id
|
|
||||||
flush_linkifiers(sender=RealmFilter, instance=cast(RealmFilter, instance))
|
|
||||||
|
|
||||||
def save_new_linkifier() -> None:
|
|
||||||
linkifier = RealmFilter(realm=realm, pattern=r"whatever", url_template="whatever")
|
|
||||||
linkifier.save()
|
|
||||||
|
|
||||||
# start fresh for our realm
|
|
||||||
flush()
|
|
||||||
self.assertFalse(realm_in_local_linkifiers_cache(realm.id))
|
|
||||||
|
|
||||||
# call this just for side effects of populating the cache
|
|
||||||
linkifiers_for_realm(realm.id)
|
|
||||||
self.assertTrue(realm_in_local_linkifiers_cache(realm.id))
|
|
||||||
|
|
||||||
# Saving a new RealmFilter should have the side effect of
|
|
||||||
# flushing the cache.
|
|
||||||
save_new_linkifier()
|
|
||||||
self.assertFalse(realm_in_local_linkifiers_cache(realm.id))
|
|
||||||
|
|
||||||
# and flush it one more time, to make sure we don't get a KeyError
|
|
||||||
flush()
|
|
||||||
self.assertFalse(realm_in_local_linkifiers_cache(realm.id))
|
|
||||||
|
|
||||||
def test_linkifier_precedence(self) -> None:
|
def test_linkifier_precedence(self) -> None:
|
||||||
realm = self.example_user("hamlet").realm
|
realm = self.example_user("hamlet").realm
|
||||||
# The insertion order should not affect the fact that the linkifiers are ordered by id.
|
# The insertion order should not affect the fact that the linkifiers are ordered by id.
|
||||||
|
@ -1547,6 +1508,36 @@ class MarkdownTest(ZulipTestCase):
|
||||||
)
|
)
|
||||||
self.assertTrue(Message.is_status_message(content, rendering_result.rendered_content))
|
self.assertTrue(Message.is_status_message(content, rendering_result.rendered_content))
|
||||||
|
|
||||||
|
def test_linkifier_caching(self) -> None:
|
||||||
|
realm = get_realm("zulip")
|
||||||
|
|
||||||
|
RealmFilter.objects.all().delete()
|
||||||
|
|
||||||
|
with self.assert_database_query_count(1):
|
||||||
|
self.assertEqual(linkifiers_for_realm(realm.id), [])
|
||||||
|
|
||||||
|
# Verify that our in-memory cache avoids round trips.
|
||||||
|
with self.assert_database_query_count(0, keep_cache_warm=True):
|
||||||
|
with self.assert_memcached_count(0):
|
||||||
|
self.assertEqual(linkifiers_for_realm(realm.id), [])
|
||||||
|
|
||||||
|
linkifier = RealmFilter(realm=realm, pattern=r"whatever", url_template="whatever")
|
||||||
|
linkifier.save()
|
||||||
|
|
||||||
|
# cache gets properly invalidated by virtue of our save
|
||||||
|
self.assertEqual(
|
||||||
|
linkifiers_for_realm(realm.id),
|
||||||
|
[{"id": linkifier.id, "pattern": "whatever", "url_template": "whatever"}],
|
||||||
|
)
|
||||||
|
|
||||||
|
# And the in-process cache works again.
|
||||||
|
with self.assert_database_query_count(0, keep_cache_warm=True):
|
||||||
|
with self.assert_memcached_count(0):
|
||||||
|
self.assertEqual(
|
||||||
|
linkifiers_for_realm(realm.id),
|
||||||
|
[{"id": linkifier.id, "pattern": "whatever", "url_template": "whatever"}],
|
||||||
|
)
|
||||||
|
|
||||||
def test_alert_words(self) -> None:
|
def test_alert_words(self) -> None:
|
||||||
user_profile = self.example_user("othello")
|
user_profile = self.example_user("othello")
|
||||||
do_add_alert_words(user_profile, ["ALERTWORD", "scaryword"])
|
do_add_alert_words(user_profile, ["ALERTWORD", "scaryword"])
|
||||||
|
|
Loading…
Reference in New Issue