Skip to content

Commit 3cabb87

Browse files
committed
model: Add stream id helper methods.
This commit adds two helper methods - _get_stream_from_id and _get_all_stream_ids. These two helper methods help in preparation for adding stream and subscription property accessor methods in the next commit. Tests added.
1 parent 6e7e612 commit 3cabb87

File tree

3 files changed

+192
-0
lines changed

3 files changed

+192
-0
lines changed

tests/conftest.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
CustomProfileField,
1212
Message,
1313
MessageType,
14+
Stream,
1415
Subscription,
1516
)
1617
from zulipterminal.config.keys import (
@@ -355,6 +356,61 @@ def streams_fixture(
355356
return deepcopy(streams)
356357

357358

359+
@pytest.fixture
360+
def unsubscribed_streams_fixture() -> Dict[int, Subscription]:
361+
unsubscribed_streams: Dict[int, Subscription] = {}
362+
for i in range(3, 5):
363+
unsubscribed_streams[i] = {
364+
"name": f"Stream {i}",
365+
"date_created": 1472047124 + i,
366+
"invite_only": False,
367+
"color": "#b0a5fd",
368+
"pin_to_top": False,
369+
"stream_id": i,
370+
"is_muted": False,
371+
"audible_notifications": False,
372+
"description": f"A description of stream {i}",
373+
"rendered_description": f"A description of stream {i}",
374+
"desktop_notifications": False,
375+
"stream_weekly_traffic": 0,
376+
"push_notifications": False,
377+
"message_retention_days": i + 30,
378+
"email_address": f"stream{i}@example.com",
379+
"email_notifications": False,
380+
"wildcard_mentions_notify": False,
381+
"subscribers": [1001, 11, 12],
382+
"history_public_to_subscribers": True,
383+
"is_announcement_only": True,
384+
"stream_post_policy": 0,
385+
"is_web_public": True,
386+
"first_message_id": None,
387+
}
388+
return deepcopy(unsubscribed_streams)
389+
390+
391+
@pytest.fixture
392+
def never_subscribed_streams_fixture() -> Dict[int, Stream]:
393+
never_subscribed_streams: Dict[int, Stream] = {}
394+
for i in range(5, 7):
395+
never_subscribed_streams[i] = {
396+
"name": f"Stream {i}",
397+
"date_created": 1472047124 + i,
398+
"invite_only": False,
399+
"stream_id": i,
400+
"description": f"A description of stream {i}",
401+
"rendered_description": f"A description of stream {i}",
402+
"stream_weekly_traffic": 0,
403+
"message_retention_days": i + 30,
404+
"subscribers": [1001, 11, 12],
405+
"history_public_to_subscribers": True,
406+
"is_announcement_only": True,
407+
"stream_post_policy": 0,
408+
"is_web_public": True,
409+
"first_message_id": None,
410+
}
411+
return deepcopy(never_subscribed_streams)
412+
413+
358414
@pytest.fixture
359415
def realm_emojis() -> Dict[str, Dict[str, Any]]:
360416
# Omitting source_url, author_id (server version 3.0),

tests/model/test_model.py

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1699,6 +1699,126 @@ def test__update_users_data_from_initial_data(
16991699
assert model.user_dict == user_dict
17001700
assert model.users == user_list
17011701

1702+
@pytest.mark.parametrize(
1703+
"stream_id, expected_value",
1704+
[
1705+
case(
1706+
1000,
1707+
{
1708+
"name": "Some general stream",
1709+
"date_created": None,
1710+
"invite_only": False,
1711+
"color": "#baf",
1712+
"pin_to_top": False,
1713+
"stream_id": 1000,
1714+
"is_muted": False,
1715+
"audible_notifications": False,
1716+
"description": "General Stream",
1717+
"rendered_description": "General Stream",
1718+
"desktop_notifications": False,
1719+
"stream_weekly_traffic": 0,
1720+
"push_notifications": False,
1721+
"email_address": "general@example.comm",
1722+
"message_retention_days": None,
1723+
"subscribers": [1001, 11, 12],
1724+
"history_public_to_subscribers": True,
1725+
"is_announcement_only": False,
1726+
"first_message_id": 1,
1727+
"email_notifications": False,
1728+
"wildcard_mentions_notify": False,
1729+
"is_web_public": False,
1730+
},
1731+
),
1732+
case(
1733+
3,
1734+
{
1735+
"name": "Stream 3",
1736+
"date_created": 1472047127,
1737+
"invite_only": False,
1738+
"color": "#b0a5fd",
1739+
"pin_to_top": False,
1740+
"stream_id": 3,
1741+
"is_muted": False,
1742+
"audible_notifications": False,
1743+
"description": "A description of stream 3",
1744+
"rendered_description": "A description of stream 3",
1745+
"desktop_notifications": False,
1746+
"stream_weekly_traffic": 0,
1747+
"push_notifications": False,
1748+
"message_retention_days": 33,
1749+
"email_address": "stream3@example.com",
1750+
"email_notifications": False,
1751+
"wildcard_mentions_notify": False,
1752+
"subscribers": [1001, 11, 12],
1753+
"history_public_to_subscribers": True,
1754+
"is_announcement_only": True,
1755+
"stream_post_policy": 0,
1756+
"is_web_public": True,
1757+
"first_message_id": None,
1758+
},
1759+
),
1760+
case(
1761+
5,
1762+
{
1763+
"name": "Stream 5",
1764+
"date_created": 1472047129,
1765+
"invite_only": False,
1766+
"stream_id": 5,
1767+
"description": "A description of stream 5",
1768+
"rendered_description": "A description of stream 5",
1769+
"stream_weekly_traffic": 0,
1770+
"message_retention_days": 35,
1771+
"subscribers": [1001, 11, 12],
1772+
"history_public_to_subscribers": True,
1773+
"is_announcement_only": True,
1774+
"stream_post_policy": 0,
1775+
"is_web_public": True,
1776+
"first_message_id": None,
1777+
},
1778+
),
1779+
],
1780+
)
1781+
def test__get_stream_from_id(
1782+
self,
1783+
model,
1784+
stream_id,
1785+
expected_value,
1786+
stream_dict,
1787+
unsubscribed_streams_fixture,
1788+
never_subscribed_streams_fixture,
1789+
):
1790+
model.stream_dict = stream_dict
1791+
model._unsubscribed_streams = unsubscribed_streams_fixture
1792+
model._never_subscribed_streams = never_subscribed_streams_fixture
1793+
assert model._get_stream_from_id(stream_id) == expected_value
1794+
1795+
def test__get_stream_from_id__nonexistent_stream(
1796+
self,
1797+
model,
1798+
stream_dict,
1799+
unsubscribed_streams_fixture,
1800+
never_subscribed_streams_fixture,
1801+
stream_id=231, # id 231 does not belong to any stream
1802+
):
1803+
model.stream_dict = stream_dict
1804+
model._unsubscribed_streams = unsubscribed_streams_fixture
1805+
model._never_subscribed_streams = never_subscribed_streams_fixture
1806+
with pytest.raises(RuntimeError):
1807+
model._get_stream_from_id(stream_id)
1808+
1809+
def test__get_all_stream_ids(
1810+
self,
1811+
model,
1812+
stream_dict,
1813+
unsubscribed_streams_fixture,
1814+
never_subscribed_streams_fixture,
1815+
expected_value=[1000, 99, 999, 1, 2, 3, 4, 5, 6],
1816+
):
1817+
model.stream_dict = stream_dict
1818+
model._unsubscribed_streams = unsubscribed_streams_fixture
1819+
model._never_subscribed_streams = never_subscribed_streams_fixture
1820+
assert model._get_all_stream_ids() == expected_value
1821+
17021822
@pytest.mark.parametrize("muted", powerset([99, 1000]))
17031823
@pytest.mark.parametrize("visual_notification_enabled", powerset([99, 1000]))
17041824
def test__subscribe_to_streams(

zulipterminal/model.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1281,6 +1281,22 @@ def _register_non_subscribed_streams(
12811281
stream["stream_id"]: stream for stream in never_subscribed_streams
12821282
}
12831283

1284+
def _get_stream_from_id(self, stream_id: int) -> Union[Subscription, Stream]:
1285+
if stream_id in self.stream_dict:
1286+
return self.stream_dict[stream_id]
1287+
elif stream_id in self._unsubscribed_streams:
1288+
return self._unsubscribed_streams[stream_id]
1289+
elif stream_id in self._never_subscribed_streams:
1290+
return self._never_subscribed_streams[stream_id]
1291+
else:
1292+
raise RuntimeError(f"Stream with id {stream_id} does not exist!")
1293+
1294+
def _get_all_stream_ids(self) -> List[int]:
1295+
id_list = list(self.stream_dict)
1296+
id_list.extend(stream_id for stream_id in self._unsubscribed_streams)
1297+
id_list.extend(stream_id for stream_id in self._never_subscribed_streams)
1298+
return id_list
1299+
12841300
def _subscribe_to_streams(self, subscriptions: List[Subscription]) -> None:
12851301
def make_reduced_stream_data(stream: Subscription) -> StreamData:
12861302
# stream_id has been changed to id.

0 commit comments

Comments
 (0)