Skip to content

Commit a73dbaf

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 e078bb3 commit a73dbaf

File tree

3 files changed

+189
-1
lines changed

3 files changed

+189
-1
lines changed

tests/conftest.py

Lines changed: 57 additions & 1 deletion
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 (
@@ -359,6 +360,61 @@ def streams_fixture(
359360
return deepcopy(streams)
360361

361362

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

14541510

14551511
@pytest.fixture
1456-
def stream_dict(streams_fixture: List[Subscription]) -> Dict[int, Subscription]:
1512+
def stream_dict(streams_fixture: List[Dict[str, Any]]) -> Dict[int, Any]:
14571513
return {stream["stream_id"]: stream for stream in streams_fixture}
14581514

14591515

tests/model/test_model.py

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1700,6 +1700,122 @@ def test__update_users_data_from_initial_data(
17001700
assert model.user_dict == user_dict
17011701
assert model.users == user_list
17021702

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

zulipterminal/model.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1236,6 +1236,22 @@ def _register_non_subscribed_streams(
12361236
stream["stream_id"]: stream for stream in never_subscribed_streams
12371237
}
12381238

1239+
def _get_stream_from_id(self, stream_id: int) -> Union[Subscription, Stream]:
1240+
if stream_id in self.stream_dict:
1241+
return self.stream_dict[stream_id]
1242+
elif stream_id in self._unsubscribed_streams:
1243+
return self._unsubscribed_streams[stream_id]
1244+
elif stream_id in self._never_subscribed_streams:
1245+
return self._never_subscribed_streams[stream_id]
1246+
else:
1247+
raise RuntimeError(f"Stream with id {stream_id} does not exist!")
1248+
1249+
def _get_all_stream_ids(self) -> List[int]:
1250+
id_list = list(self.stream_dict)
1251+
id_list.extend(stream_id for stream_id in self._unsubscribed_streams)
1252+
id_list.extend(stream_id for stream_id in self._never_subscribed_streams)
1253+
return id_list
1254+
12391255
def _subscribe_to_streams(self, subscriptions: List[Subscription]) -> None:
12401256
def make_reduced_stream_data(stream: Subscription) -> StreamData:
12411257
# stream_id has been changed to id.

0 commit comments

Comments
 (0)