Skip to content

Commit 6db6378

Browse files
committed
messages: Add tests for transform_content.
This commit introduces tests for the `transform_content` class method, specifically focusing on the `message_links` and `time_mentions`. These enhancements improve the reliability and testing coverage of the `transform_content` method.
1 parent d65cbee commit 6db6378

File tree

1 file changed

+109
-1
lines changed

1 file changed

+109
-1
lines changed

tests/ui_tools/test_messages.py

+109-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
from collections import OrderedDict, defaultdict
22
from datetime import date
3+
from typing import Any, Dict, List, Tuple
34

45
import pytest
56
import pytz
67
from bs4 import BeautifulSoup
78
from pytest import param as case
9+
from pytest_mock import MockerFixture
810
from urwid import Columns, Divider, Padding, Text
911

1012
from zulipterminal.config.keys import keys_for_command
@@ -1585,14 +1587,120 @@ def test_keypress_EDIT_MESSAGE(
15851587
# fmt: on
15861588
],
15871589
)
1588-
def test_transform_content(self, mocker, raw_html, expected_content):
1590+
def test_transform_content(self, raw_html: str, expected_content: Any) -> None:
15891591
expected_content = expected_content.replace("{}", QUOTED_TEXT_MARKER)
15901592

15911593
content, *_ = MessageBox.transform_content(raw_html, SERVER_URL)
15921594

15931595
rendered_text = Text(content)
15941596
assert rendered_text.text == expected_content
15951597

1598+
@pytest.mark.parametrize(
1599+
"raw_html, expected_message_links",
1600+
[
1601+
(
1602+
"""
1603+
<p><a href="https://github.com/zulip/zulip-terminal/pull/1">https://github.com/zulip/zulip-terminal/pull/1</a></p>
1604+
""",
1605+
{
1606+
"https://github.com/zulip/zulip-terminal/pull/1": (
1607+
"github.com",
1608+
1,
1609+
True,
1610+
)
1611+
},
1612+
),
1613+
(
1614+
"""
1615+
<p><a href="https://foo.com">https://foo.com</a></p>
1616+
""",
1617+
{"https://foo.com": ("https://foo.com", 1, False)},
1618+
),
1619+
(
1620+
"""
1621+
<p><a href="#narrow/stream/206-zulip-terminal/topic/announce">https://chat.zulip.zulip/#narrow/stream/206-zulip-terminal/topic/announce</a></p>
1622+
""",
1623+
{
1624+
"https://chat.zulip.zulip#narrow/stream/206-zulip-terminal/topic/announce": ( # noqa: E501
1625+
"https://chat.zulip.zulip/#narrow/stream/206-zulip-terminal/topic/announce",
1626+
1,
1627+
True,
1628+
)
1629+
},
1630+
),
1631+
(
1632+
"""
1633+
<p><a href="https://github.com/zulip/zulip-terminal">test</a></p>
1634+
""",
1635+
{"https://github.com/zulip/zulip-terminal": ("test", 1, True)},
1636+
),
1637+
],
1638+
ids=[
1639+
"github_pr",
1640+
"external_link_no_footlink",
1641+
"internal_link",
1642+
"github_link_with_text",
1643+
],
1644+
)
1645+
def test_transform_content_message_links(
1646+
self, raw_html: str, expected_message_links: Dict[str, Tuple[str, int, bool]]
1647+
) -> None:
1648+
_, message_links, *_ = MessageBox.transform_content(raw_html, SERVER_URL)
1649+
1650+
assert message_links == expected_message_links
1651+
1652+
@pytest.mark.parametrize(
1653+
"raw_html, expected_time_mentions",
1654+
[
1655+
(
1656+
"""
1657+
<p><time datetime="2024-05-29T11:30:00Z">2024-05-29T17:00:00+05:30</time></p>
1658+
""", # noqa: E501
1659+
[
1660+
(
1661+
"Wed, May 29 2024, 17:00 (IST)",
1662+
"Original text was 2024-05-29T17:00:00+05:30",
1663+
)
1664+
],
1665+
),
1666+
(
1667+
"""
1668+
<p><time datetime="3000-06-01T14:00:00Z">3000-06-01T19:30:00+05:30</time></p>
1669+
""", # noqa: E501
1670+
[
1671+
(
1672+
"Sun, Jun 1 3000, 19:30 (IST)",
1673+
"Original text was 3000-06-01T19:30:00+05:30",
1674+
)
1675+
],
1676+
),
1677+
(
1678+
"""
1679+
<p><time datetime="1947-08-14T19:47:00Z">1947-08-15T01:17:00+05:30</time></p>
1680+
""", # noqa: E501
1681+
[
1682+
(
1683+
"Fri, Aug 15 1947, 1:17 (IST)",
1684+
"Original text was 1947-08-15T01:17:00+05:30",
1685+
)
1686+
],
1687+
),
1688+
],
1689+
ids=["test_date", "distant_future_date", "past_date"],
1690+
)
1691+
def test_transform_content_time_mentions(
1692+
self,
1693+
mocker: MockerFixture,
1694+
raw_html: str,
1695+
expected_time_mentions: List[Tuple[str, str]],
1696+
) -> None:
1697+
mocker.patch(
1698+
MODULE + ".get_localzone", return_value=pytz.timezone("Asia/Kolkata")
1699+
)
1700+
_, _, time_mentions, *_ = MessageBox.transform_content(raw_html, SERVER_URL)
1701+
1702+
assert time_mentions == expected_time_mentions
1703+
15961704
# FIXME This is the same parametrize as MsgInfoView:test_height_reactions
15971705
@pytest.mark.parametrize(
15981706
"to_vary_in_each_message, expected_text, expected_attributes",

0 commit comments

Comments
 (0)