tests: Factor out ZulipVerboseEqualTest class, and use more.

This commit is contained in:
Alex Vandiver
2024-07-18 14:30:58 +00:00
committed by Tim Abbott
parent 08191d3f69
commit ecdf5713c2
2 changed files with 23 additions and 22 deletions

View File

@@ -49,6 +49,7 @@ from zerver.actions.streams import bulk_add_subscriptions, bulk_remove_subscript
from zerver.decorator import do_two_factor_login from zerver.decorator import do_two_factor_login
from zerver.lib.cache import bounce_key_prefix_for_testing from zerver.lib.cache import bounce_key_prefix_for_testing
from zerver.lib.initial_password import initial_password from zerver.lib.initial_password import initial_password
from zerver.lib.mdiff import diff_strings
from zerver.lib.message import access_message from zerver.lib.message import access_message
from zerver.lib.notification_data import UserMessageNotificationsData from zerver.lib.notification_data import UserMessageNotificationsData
from zerver.lib.per_request_cache import flush_per_request_caches from zerver.lib.per_request_cache import flush_per_request_caches
@@ -2161,6 +2162,20 @@ class ZulipTestCase(ZulipTestCaseMixin, TestCase):
return message_id return message_id
class ZulipVerboseEqualTestCase(ZulipTestCase):
@override
def assertEqual(self, first: Any, second: Any, msg: str = "") -> None:
if isinstance(first, str) and isinstance(second, str):
if first != second:
raise AssertionError(
"Actual and expected outputs do not match; showing diff.\n"
+ diff_strings(first, second)
+ msg
)
else:
super().assertEqual(first, second)
def get_row_ids_in_all_tables() -> Iterator[tuple[str, set[int]]]: def get_row_ids_in_all_tables() -> Iterator[tuple[str, set[int]]]:
all_models = apps.get_models(include_auto_created=True) all_models = apps.get_models(include_auto_created=True)
ignored_tables = {"django_session"} ignored_tables = {"django_session"}

View File

@@ -60,7 +60,7 @@ from zerver.lib.mention import (
topic_wildcards, topic_wildcards,
) )
from zerver.lib.per_request_cache import flush_per_request_caches from zerver.lib.per_request_cache import flush_per_request_caches
from zerver.lib.test_classes import ZulipTestCase from zerver.lib.test_classes import ZulipVerboseEqualTestCase
from zerver.lib.tex import render_tex from zerver.lib.tex import render_tex
from zerver.models import Message, NamedUserGroup, RealmEmoji, RealmFilter, UserMessage, UserProfile from zerver.models import Message, NamedUserGroup, RealmEmoji, RealmFilter, UserMessage, UserProfile
from zerver.models.clients import get_client from zerver.models.clients import get_client
@@ -82,7 +82,7 @@ class SimulatedFencedBlockPreprocessor(FencedBlockPreprocessor):
return "**" + s.strip("\n") + "**" return "**" + s.strip("\n") + "**"
class FencedBlockPreprocessorTest(ZulipTestCase): class FencedBlockPreprocessorTest(ZulipVerboseEqualTestCase):
def test_simple_quoting(self) -> None: def test_simple_quoting(self) -> None:
processor = FencedBlockPreprocessor(Markdown()) processor = FencedBlockPreprocessor(Markdown())
markdown_input = [ markdown_input = [
@@ -207,7 +207,7 @@ def markdown_convert_wrapper(content: str) -> str:
).rendered_content ).rendered_content
class MarkdownMiscTest(ZulipTestCase): class MarkdownMiscTest(ZulipVerboseEqualTestCase):
def test_diffs_work_as_expected(self) -> None: def test_diffs_work_as_expected(self) -> None:
str1 = "<p>The quick brown fox jumps over the lazy dog. Animal stories are fun, yeah</p>" str1 = "<p>The quick brown fox jumps over the lazy dog. Animal stories are fun, yeah</p>"
str2 = "<p>The fast fox jumps over the lazy dogs and cats. Animal stories are fun</p>" str2 = "<p>The fast fox jumps over the lazy dogs and cats. Animal stories are fun</p>"
@@ -373,7 +373,7 @@ class MarkdownMiscTest(ZulipTestCase):
self.assertEqual(render_tex("foo"), "<i>html</i>") self.assertEqual(render_tex("foo"), "<i>html</i>")
class MarkdownListPreprocessorTest(ZulipTestCase): class MarkdownListPreprocessorTest(ZulipVerboseEqualTestCase):
# We test that the preprocessor inserts blank lines at correct places. # We test that the preprocessor inserts blank lines at correct places.
# We use <> to indicate that we need to insert a blank line here. # We use <> to indicate that we need to insert a blank line here.
def split_message(self, msg: str) -> tuple[list[str], list[str]]: def split_message(self, msg: str) -> tuple[list[str], list[str]]:
@@ -463,19 +463,7 @@ Outside. Should convert:<>
self.assertEqual(preprocessor.run(original), expected) self.assertEqual(preprocessor.run(original), expected)
class MarkdownTest(ZulipTestCase): class MarkdownTest(ZulipVerboseEqualTestCase):
@override
def assertEqual(self, first: Any, second: Any, msg: str = "") -> None:
if isinstance(first, str) and isinstance(second, str):
if first != second:
raise AssertionError(
"Actual and expected outputs do not match; showing diff.\n"
+ diff_strings(first, second)
+ msg
)
else:
super().assertEqual(first, second)
def load_markdown_tests(self) -> tuple[dict[str, Any], list[list[str]]]: def load_markdown_tests(self) -> tuple[dict[str, Any], list[list[str]]]:
test_fixtures = {} test_fixtures = {}
with open( with open(
@@ -578,7 +566,6 @@ class MarkdownTest(ZulipTestCase):
) )
clear_web_link_regex_for_testing() clear_web_link_regex_for_testing()
try: try:
with self.settings(ENABLE_FILE_LINKS=True): with self.settings(ENABLE_FILE_LINKS=True):
realm = do_create_realm(string_id="file_links_enabled", name="File links enabled") realm = do_create_realm(string_id="file_links_enabled", name="File links enabled")
@@ -589,7 +576,6 @@ class MarkdownTest(ZulipTestCase):
finally: finally:
clear_web_link_regex_for_testing() clear_web_link_regex_for_testing()
def test_inline_bitcoin(self) -> None: def test_inline_bitcoin(self) -> None:
msg = "To bitcoin:1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa or not to bitcoin" msg = "To bitcoin:1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa or not to bitcoin"
converted = markdown_convert_wrapper(msg) converted = markdown_convert_wrapper(msg)
@@ -3252,7 +3238,7 @@ class MarkdownTest(ZulipTestCase):
self.assertEqual(converted, dedent(expected_output)) self.assertEqual(converted, dedent(expected_output))
class MarkdownApiTests(ZulipTestCase): class MarkdownApiTests(ZulipVerboseEqualTestCase):
def test_render_message_api(self) -> None: def test_render_message_api(self) -> None:
content = "That is a **bold** statement" content = "That is a **bold** statement"
result = self.api_post( result = self.api_post(
@@ -3282,7 +3268,7 @@ class MarkdownApiTests(ZulipTestCase):
) )
class MarkdownErrorTests(ZulipTestCase): class MarkdownErrorTests(ZulipVerboseEqualTestCase):
def test_markdown_error_handling(self) -> None: def test_markdown_error_handling(self) -> None:
with self.simulated_markdown_failure(), self.assertRaises(MarkdownRenderingError): with self.simulated_markdown_failure(), self.assertRaises(MarkdownRenderingError):
markdown_convert_wrapper("") markdown_convert_wrapper("")
@@ -3348,7 +3334,7 @@ class MarkdownErrorTests(ZulipTestCase):
self.assertEqual(result, expected) self.assertEqual(result, expected)
class MarkdownEmojiTest(ZulipTestCase): class MarkdownEmojiTest(ZulipVerboseEqualTestCase):
def test_all_emoji_match_regex(self) -> None: def test_all_emoji_match_regex(self) -> None:
non_matching_emoji = [ non_matching_emoji = [
emoji emoji