Skip to content

Commit 230a9ab

Browse files
committed
model: Add stream and subscription id helper methods.
This commit adds three helper methods - _get_stream_from_id, _get_subscription_from_id, and get_all_stream_ids. These three helper methods help in preparation for adding stream and subscription property accessor methods in the next commits. Tests and fixture added.
1 parent 1190b33 commit 230a9ab

File tree

3 files changed

+250
-0
lines changed

3 files changed

+250
-0
lines changed

tests/conftest.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,20 @@ def never_subscribed_streams_fixture() -> List[Stream]:
415415
return deepcopy(never_subscribed_streams)
416416

417417

418+
@pytest.fixture
419+
def all_stream_ids(
420+
streams_fixture: List[Subscription],
421+
unsubscribed_streams_fixture: List[Subscription],
422+
never_subscribed_streams_fixture: List[Stream],
423+
) -> List[int]:
424+
return [
425+
stream["stream_id"]
426+
for stream in streams_fixture
427+
+ unsubscribed_streams_fixture
428+
+ never_subscribed_streams_fixture
429+
]
430+
431+
418432
@pytest.fixture
419433
def realm_emojis() -> Dict[str, Dict[str, Any]]:
420434
# Omitting source_url, author_id (server version 3.0),

tests/model/test_model.py

Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1703,6 +1703,216 @@ def test__update_users_data_from_initial_data(
17031703
assert model.user_dict == user_dict
17041704
assert model.users == user_list
17051705

1706+
@pytest.mark.parametrize(
1707+
"stream_id, expected_value",
1708+
[
1709+
case(
1710+
1000,
1711+
{
1712+
"name": "Some general stream",
1713+
"date_created": None,
1714+
"invite_only": False,
1715+
"color": "#baf",
1716+
"pin_to_top": False,
1717+
"stream_id": 1000,
1718+
"is_muted": False,
1719+
"audible_notifications": False,
1720+
"description": "General Stream",
1721+
"rendered_description": "General Stream",
1722+
"desktop_notifications": False,
1723+
"stream_weekly_traffic": 0,
1724+
"push_notifications": False,
1725+
"email_address": "[email protected]",
1726+
"message_retention_days": None,
1727+
"subscribers": [1001, 11, 12],
1728+
"history_public_to_subscribers": True,
1729+
"is_announcement_only": False,
1730+
"first_message_id": 1,
1731+
"email_notifications": False,
1732+
"wildcard_mentions_notify": False,
1733+
"is_web_public": False,
1734+
},
1735+
),
1736+
case(
1737+
3,
1738+
{
1739+
"name": "Stream 3",
1740+
"date_created": 1472047127,
1741+
"invite_only": False,
1742+
"color": "#b0a5fd",
1743+
"pin_to_top": False,
1744+
"stream_id": 3,
1745+
"is_muted": False,
1746+
"audible_notifications": False,
1747+
"description": "A description of stream 3",
1748+
"rendered_description": "A description of stream 3",
1749+
"desktop_notifications": False,
1750+
"stream_weekly_traffic": 0,
1751+
"push_notifications": False,
1752+
"message_retention_days": 33,
1753+
"email_address": "[email protected]",
1754+
"email_notifications": False,
1755+
"wildcard_mentions_notify": False,
1756+
"subscribers": [1001, 11, 12],
1757+
"history_public_to_subscribers": True,
1758+
"is_announcement_only": True,
1759+
"stream_post_policy": 0,
1760+
"is_web_public": True,
1761+
"first_message_id": None,
1762+
},
1763+
),
1764+
case(
1765+
5,
1766+
{
1767+
"name": "Stream 5",
1768+
"date_created": 1472047129,
1769+
"invite_only": False,
1770+
"stream_id": 5,
1771+
"description": "A description of stream 5",
1772+
"rendered_description": "A description of stream 5",
1773+
"stream_weekly_traffic": 0,
1774+
"message_retention_days": 35,
1775+
"subscribers": [1001, 11, 12],
1776+
"history_public_to_subscribers": True,
1777+
"is_announcement_only": True,
1778+
"stream_post_policy": 0,
1779+
"is_web_public": True,
1780+
"first_message_id": None,
1781+
},
1782+
),
1783+
],
1784+
)
1785+
def test__get_stream_from_id(
1786+
self,
1787+
model,
1788+
stream_id,
1789+
expected_value,
1790+
stream_dict,
1791+
unsubscribed_streams,
1792+
never_subscribed_streams,
1793+
):
1794+
model.stream_dict = stream_dict
1795+
model._unsubscribed_streams = unsubscribed_streams
1796+
model._never_subscribed_streams = never_subscribed_streams
1797+
assert model._get_stream_from_id(stream_id) == expected_value
1798+
1799+
def test__get_stream_from_id__nonexistent_stream(
1800+
self,
1801+
model,
1802+
stream_dict,
1803+
unsubscribed_streams,
1804+
never_subscribed_streams,
1805+
stream_id=231, # id 231 does not belong to any stream
1806+
):
1807+
model.stream_dict = stream_dict
1808+
model._unsubscribed_streams = unsubscribed_streams
1809+
model._never_subscribed_streams = never_subscribed_streams
1810+
with pytest.raises(RuntimeError):
1811+
model._get_stream_from_id(stream_id)
1812+
1813+
@pytest.mark.parametrize(
1814+
"stream_id, expected_value",
1815+
[
1816+
case(
1817+
1000,
1818+
{
1819+
"name": "Some general stream",
1820+
"date_created": None,
1821+
"invite_only": False,
1822+
"color": "#baf",
1823+
"pin_to_top": False,
1824+
"stream_id": 1000,
1825+
"is_muted": False,
1826+
"audible_notifications": False,
1827+
"description": "General Stream",
1828+
"rendered_description": "General Stream",
1829+
"desktop_notifications": False,
1830+
"stream_weekly_traffic": 0,
1831+
"push_notifications": False,
1832+
"email_address": "[email protected]",
1833+
"message_retention_days": None,
1834+
"subscribers": [1001, 11, 12],
1835+
"history_public_to_subscribers": True,
1836+
"is_announcement_only": False,
1837+
"first_message_id": 1,
1838+
"email_notifications": False,
1839+
"wildcard_mentions_notify": False,
1840+
"is_web_public": False,
1841+
},
1842+
),
1843+
case(
1844+
3,
1845+
{
1846+
"name": "Stream 3",
1847+
"date_created": 1472047127,
1848+
"invite_only": False,
1849+
"color": "#b0a5fd",
1850+
"pin_to_top": False,
1851+
"stream_id": 3,
1852+
"is_muted": False,
1853+
"audible_notifications": False,
1854+
"description": "A description of stream 3",
1855+
"rendered_description": "A description of stream 3",
1856+
"desktop_notifications": False,
1857+
"stream_weekly_traffic": 0,
1858+
"push_notifications": False,
1859+
"message_retention_days": 33,
1860+
"email_address": "[email protected]",
1861+
"email_notifications": False,
1862+
"wildcard_mentions_notify": False,
1863+
"subscribers": [1001, 11, 12],
1864+
"history_public_to_subscribers": True,
1865+
"is_announcement_only": True,
1866+
"stream_post_policy": 0,
1867+
"is_web_public": True,
1868+
"first_message_id": None,
1869+
},
1870+
),
1871+
],
1872+
)
1873+
def test__get_subscription_from_id(
1874+
self,
1875+
model,
1876+
stream_id,
1877+
expected_value,
1878+
stream_dict,
1879+
unsubscribed_streams,
1880+
):
1881+
model.stream_dict = stream_dict
1882+
model._unsubscribed_streams = unsubscribed_streams
1883+
assert model._get_subscription_from_id(stream_id) == expected_value
1884+
1885+
@pytest.mark.parametrize(
1886+
"stream_id",
1887+
[case(5, id="never_subscribed_stream"), case(231, id="non-existent_stream")],
1888+
)
1889+
def test__get_subscription_from_id__nonexistent_subscription(
1890+
self,
1891+
model,
1892+
stream_dict,
1893+
unsubscribed_streams,
1894+
never_subscribed_streams,
1895+
stream_id,
1896+
):
1897+
model.stream_dict = stream_dict
1898+
model._unsubscribed_streams = unsubscribed_streams
1899+
model._never_subscribed_streams = never_subscribed_streams
1900+
with pytest.raises(RuntimeError):
1901+
model._get_subscription_from_id(stream_id)
1902+
1903+
def test_get_all_stream_ids(
1904+
self,
1905+
model,
1906+
stream_dict,
1907+
unsubscribed_streams,
1908+
never_subscribed_streams,
1909+
all_stream_ids,
1910+
):
1911+
model.stream_dict = stream_dict
1912+
model._unsubscribed_streams = unsubscribed_streams
1913+
model._never_subscribed_streams = never_subscribed_streams
1914+
assert model.get_all_stream_ids() == all_stream_ids
1915+
17061916
@pytest.mark.parametrize("muted", powerset([99, 1000]))
17071917
@pytest.mark.parametrize("visual_notification_enabled", powerset([99, 1000]))
17081918
def test__subscribe_to_streams(

zulipterminal/model.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1281,6 +1281,32 @@ 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) -> Stream:
1285+
if stream_id in self.stream_dict:
1286+
return cast(Stream, self.stream_dict[stream_id])
1287+
elif stream_id in self._unsubscribed_streams:
1288+
return cast(Stream, 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_subscription_from_id(self, subscription_id: int) -> Subscription:
1295+
if subscription_id in self.stream_dict:
1296+
return self.stream_dict[subscription_id]
1297+
elif subscription_id in self._unsubscribed_streams:
1298+
return self._unsubscribed_streams[subscription_id]
1299+
else:
1300+
raise RuntimeError(
1301+
f"Stream with id {subscription_id} does not exist or not subscribed to!"
1302+
)
1303+
1304+
def get_all_stream_ids(self) -> List[int]:
1305+
id_list = list(self.stream_dict)
1306+
id_list.extend(stream_id for stream_id in self._unsubscribed_streams)
1307+
id_list.extend(stream_id for stream_id in self._never_subscribed_streams)
1308+
return id_list
1309+
12841310
def _subscribe_to_streams(self, subscriptions: List[Subscription]) -> None:
12851311
def make_reduced_stream_data(stream: Subscription) -> StreamData:
12861312
# stream_id has been changed to id.

0 commit comments

Comments
 (0)