Skip to content

Commit 5ca5ee6

Browse files
committed
refactor: model: Improve stream_dict typing.
This commit improves the typing of stream_dict by using Subscription TypedDict instead of Dict[str, Any]. Tests updated.
1 parent e39bbe3 commit 5ca5ee6

File tree

7 files changed

+94
-42
lines changed

7 files changed

+94
-42
lines changed

tests/conftest.py

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ def logged_on_user() -> Dict[str, Any]:
207207

208208

209209
@pytest.fixture
210-
def general_stream() -> Dict[str, Any]:
210+
def general_stream() -> Subscription:
211211
return {
212212
"name": "Some general stream",
213213
"date_created": 1472091253,
@@ -220,21 +220,25 @@ def general_stream() -> Dict[str, Any]:
220220
"audible_notifications": False,
221221
"description": "General Stream",
222222
"rendered_description": "General Stream",
223-
"is_old_stream": True,
224223
"desktop_notifications": False,
225224
"stream_weekly_traffic": 0,
226225
"push_notifications": False,
227226
"email_address": "[email protected]",
228227
"message_retention_days": 10,
229228
"subscribers": [1001, 11, 12],
230229
"history_public_to_subscribers": True,
230+
"is_announcement_only": False,
231+
"stream_post_policy": 0,
232+
"first_message_id": 1,
233+
"email_notifications": False,
234+
"wildcard_mentions_notify": False,
231235
}
232236

233237

234238
# This is a private stream;
235239
# only description/stream_id/invite_only/name/color vary from above
236240
@pytest.fixture
237-
def secret_stream() -> Dict[str, Any]:
241+
def secret_stream() -> Subscription:
238242
return {
239243
"description": "Some private stream",
240244
"stream_id": 99,
@@ -248,19 +252,23 @@ def secret_stream() -> Dict[str, Any]:
248252
"is_muted": False,
249253
"is_web_public": False,
250254
"audible_notifications": False,
251-
"is_old_stream": True,
252255
"desktop_notifications": False,
253256
"stream_weekly_traffic": 0,
254257
"message_retention_days": -1,
255258
"push_notifications": False,
256259
"subscribers": [1001, 11],
257260
"history_public_to_subscribers": False,
261+
"is_announcement_only": False,
262+
"stream_post_policy": 0,
263+
"first_message_id": 1,
264+
"email_notifications": False,
265+
"wildcard_mentions_notify": False,
258266
}
259267

260268

261269
# Like public stream but with is_web_public=True
262270
@pytest.fixture
263-
def web_public_stream() -> Dict[str, Any]:
271+
def web_public_stream() -> Subscription:
264272
return {
265273
"description": "Some web public stream",
266274
"stream_id": 999,
@@ -273,24 +281,28 @@ def web_public_stream() -> Dict[str, Any]:
273281
"color": "#ddd", # Color in '#xxx' format
274282
"is_muted": False,
275283
"audible_notifications": False,
276-
"is_old_stream": True,
277284
"desktop_notifications": False,
278285
"stream_weekly_traffic": 0,
279286
"message_retention_days": -1,
280287
"push_notifications": False,
281288
"subscribers": [1001, 11],
282289
"history_public_to_subscribers": False,
283290
"is_web_public": True,
291+
"is_announcement_only": False,
292+
"stream_post_policy": 0,
293+
"first_message_id": 1,
294+
"email_notifications": False,
295+
"wildcard_mentions_notify": False,
284296
}
285297

286298

287299
@pytest.fixture
288300
def get_stream_from_id_fixture(
289301
stream_id: int,
290-
stream_dict: Dict[int, Any],
302+
stream_dict: Dict[int, Subscription],
291303
unsubscribed_streams_fixture: Dict[int, Subscription],
292304
never_subscribed_streams_fixture: Dict[int, Stream],
293-
) -> Union[Subscription, Stream, Any]:
305+
) -> Union[Subscription, Stream]:
294306
if stream_id in stream_dict:
295307
return stream_dict[stream_id]
296308
elif stream_id in unsubscribed_streams_fixture:
@@ -301,10 +313,10 @@ def get_stream_from_id_fixture(
301313

302314
@pytest.fixture
303315
def streams_fixture(
304-
general_stream: Dict[str, Any],
305-
secret_stream: Dict[str, Any],
306-
web_public_stream: Dict[str, Any],
307-
) -> List[Dict[str, Any]]:
316+
general_stream: Subscription,
317+
secret_stream: Subscription,
318+
web_public_stream: Subscription,
319+
) -> List[Subscription]:
308320
streams = [general_stream, secret_stream, web_public_stream]
309321
for i in range(1, 3):
310322
streams.append(
@@ -319,7 +331,6 @@ def streams_fixture(
319331
"audible_notifications": False,
320332
"description": f"A description of stream {i}",
321333
"rendered_description": f"A description of stream {i}",
322-
"is_old_stream": True,
323334
"desktop_notifications": False,
324335
"stream_weekly_traffic": 0,
325336
"push_notifications": False,
@@ -328,6 +339,11 @@ def streams_fixture(
328339
"subscribers": [1001, 11, 12],
329340
"history_public_to_subscribers": True,
330341
"is_web_public": False,
342+
"is_announcement_only": False,
343+
"stream_post_policy": 0,
344+
"first_message_id": 1,
345+
"email_notifications": False,
346+
"wildcard_mentions_notify": False,
331347
}
332348
)
333349
return deepcopy(streams)
@@ -648,7 +664,7 @@ def mentioned_messages_combination(request: Any) -> Tuple[Set[int], Set[int]]:
648664
def initial_data(
649665
logged_on_user: Dict[str, Any],
650666
users_fixture: List[Dict[str, Any]],
651-
streams_fixture: List[Dict[str, Any]],
667+
streams_fixture: List[Dict[str, Subscription]],
652668
realm_emojis: Dict[str, Dict[str, Any]],
653669
) -> Dict[str, Any]:
654670
"""
@@ -1262,7 +1278,7 @@ def never_subscribed_streams_fixture() -> Dict[int, Stream]:
12621278

12631279

12641280
@pytest.fixture
1265-
def stream_dict(streams_fixture: List[Dict[str, Any]]) -> Dict[int, Any]:
1281+
def stream_dict(streams_fixture: List[Subscription]) -> Dict[int, Subscription]:
12661282
return {stream["stream_id"]: stream for stream in streams_fixture}
12671283

12681284

tests/core/test_core.py

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from pytest import param as case
1010
from pytest_mock import MockerFixture
1111

12+
from zulipterminal.api_types import Subscription
1213
from zulipterminal.config.themes import generate_theme
1314
from zulipterminal.core import Controller
1415
from zulipterminal.helper import Index
@@ -124,18 +125,22 @@ def test_narrow_to_stream(
124125
mocker: MockerFixture,
125126
controller: Controller,
126127
index_stream: Index,
128+
general_stream: Subscription,
127129
stream_id: int = 205,
128130
stream_name: str = "PTEST",
129131
) -> None:
130132
controller.model.narrow = []
131133
controller.model.index = index_stream
132134
controller.view.message_view = mocker.patch("urwid.ListBox")
133135
controller.model.stream_dict = {
134-
stream_id: {
136+
stream_id: general_stream,
137+
}
138+
controller.model.stream_dict[stream_id].update(
139+
{
135140
"color": "#ffffff",
136141
"name": stream_name,
137142
}
138-
}
143+
)
139144
controller.model._unsubscribed_streams = {}
140145
controller.model._never_subscribed_streams = {}
141146
controller.model.muted_streams = set()
@@ -173,6 +178,7 @@ def test_narrow_to_topic(
173178
initial_stream_id: Optional[int],
174179
anchor: Optional[int],
175180
expected_final_focus: int,
181+
general_stream: Subscription,
176182
stream_name: str = "PTEST",
177183
topic_name: str = "Test",
178184
stream_id: int = 205,
@@ -186,11 +192,14 @@ def test_narrow_to_topic(
186192
controller.model.stream_id = initial_stream_id
187193
controller.view.message_view = mocker.patch("urwid.ListBox")
188194
controller.model.stream_dict = {
189-
stream_id: {
195+
stream_id: general_stream,
196+
}
197+
controller.model.stream_dict[stream_id].update(
198+
{
190199
"color": "#ffffff",
191200
"name": stream_name,
192201
}
193-
}
202+
)
194203
controller.model._unsubscribed_streams = {}
195204
controller.model._never_subscribed_streams = {}
196205
controller.model.muted_streams = set()
@@ -250,6 +259,7 @@ def test_narrow_to_all_messages(
250259
controller: Controller,
251260
index_all_messages: Index,
252261
anchor: Optional[int],
262+
general_stream: Subscription,
253263
expected_final_focus_msg_id: int,
254264
) -> None:
255265
controller.model.narrow = [["stream", "PTEST"]]
@@ -258,10 +268,13 @@ def test_narrow_to_all_messages(
258268
controller.model.user_email = "some@email"
259269
controller.model.user_id = 1
260270
controller.model.stream_dict = {
261-
205: {
271+
205: general_stream,
272+
}
273+
controller.model.stream_dict[205].update(
274+
{
262275
"color": "#ffffff",
263276
}
264-
}
277+
)
265278
controller.model.muted_streams = set()
266279
mocker.patch(MODEL + ".is_muted_topic", return_value=False)
267280

@@ -297,7 +310,11 @@ def test_narrow_to_all_pm(
297310
assert msg_ids == id_list
298311

299312
def test_narrow_to_all_starred(
300-
self, mocker: MockerFixture, controller: Controller, index_all_starred: Index
313+
self,
314+
mocker: MockerFixture,
315+
controller: Controller,
316+
index_all_starred: Index,
317+
general_stream: Subscription,
301318
) -> None:
302319
controller.model.narrow = []
303320
controller.model.index = index_all_starred
@@ -307,10 +324,13 @@ def test_narrow_to_all_starred(
307324
mocker.patch(MODEL + ".is_muted_topic", return_value=False)
308325
controller.model.user_email = "some@email"
309326
controller.model.stream_dict = {
310-
205: {
327+
205: general_stream,
328+
}
329+
controller.model.stream_dict[205].update(
330+
{
311331
"color": "#ffffff",
312332
}
313-
}
333+
)
314334
controller.view.message_view = mocker.patch("urwid.ListBox")
315335

316336
controller.narrow_to_all_starred() # FIXME: Add id narrowing test
@@ -324,7 +344,11 @@ def test_narrow_to_all_starred(
324344
assert msg_ids == id_list
325345

326346
def test_narrow_to_all_mentions(
327-
self, mocker: MockerFixture, controller: Controller, index_all_mentions: Index
347+
self,
348+
mocker: MockerFixture,
349+
controller: Controller,
350+
index_all_mentions: Index,
351+
general_stream: Subscription,
328352
) -> None:
329353
controller.model.narrow = []
330354
controller.model.index = index_all_mentions
@@ -334,10 +358,13 @@ def test_narrow_to_all_mentions(
334358
controller.model.user_email = "some@email"
335359
controller.model.user_id = 1
336360
controller.model.stream_dict = {
337-
205: {
361+
205: general_stream,
362+
}
363+
controller.model.stream_dict[205].update(
364+
{
338365
"color": "#ffffff",
339366
}
340-
}
367+
)
341368
controller.view.message_view = mocker.patch("urwid.ListBox")
342369

343370
controller.narrow_to_all_mentions() # FIXME: Add id narrowing test

tests/helper/test_helper.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from pytest import param as case
55
from pytest_mock import MockerFixture
66

7-
from zulipterminal.api_types import Composition
7+
from zulipterminal.api_types import Composition, Subscription
88
from zulipterminal.config.keys import primary_key_for_command
99
from zulipterminal.helper import (
1010
Index,
@@ -278,7 +278,7 @@ def test_powerset(
278278
def test_classify_unread_counts(
279279
mocker: MockerFixture,
280280
initial_data: Dict[str, Any],
281-
stream_dict: Dict[int, Dict[str, Any]],
281+
stream_dict: Dict[int, Subscription],
282282
classified_unread_counts: Dict[str, Any],
283283
muted_topics: List[List[str]],
284284
muted_streams: Set[int],

tests/model/test_model.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1680,14 +1680,18 @@ def test_get_all_users(self, mocker, initial_data, user_list, user_dict, user_id
16801680
"audible_notifications": False,
16811681
"description": "General Stream",
16821682
"rendered_description": "General Stream",
1683-
"is_old_stream": True,
16841683
"desktop_notifications": False,
16851684
"stream_weekly_traffic": 0,
16861685
"push_notifications": False,
16871686
"email_address": "[email protected]",
16881687
"message_retention_days": None,
16891688
"subscribers": [1001, 11, 12],
16901689
"history_public_to_subscribers": True,
1690+
"is_announcement_only": False,
1691+
"stream_post_policy": 0,
1692+
"first_message_id": 1,
1693+
"email_notifications": False,
1694+
"wildcard_mentions_notify": False,
16911695
},
16921696
),
16931697
case(
@@ -2096,8 +2100,10 @@ def test_is_stream_invite_only(
20962100
[
20972101
case(
20982102
1000,
2099-
{},
2100-
None,
2103+
{
2104+
"stream_post_policy": 0,
2105+
},
2106+
0,
21012107
),
21022108
case(
21032109
3,
@@ -2136,8 +2142,10 @@ def test_get_stream_post_policy(
21362142
[
21372143
case(
21382144
1000,
2139-
{},
2140-
None,
2145+
{
2146+
"is_announcement_only": True,
2147+
},
2148+
True,
21412149
),
21422150
case(
21432151
3,

tests/ui_tools/test_boxes.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from pytest_mock import MockerFixture
88
from urwid import Widget
99

10+
from zulipterminal.api_types import Subscription
1011
from zulipterminal.config.keys import keys_for_command, primary_key_for_command
1112
from zulipterminal.config.symbols import (
1213
INVALID_MARKER,
@@ -153,7 +154,7 @@ def test_generic_autocomplete_stream_and_topic(
153154
is_valid_stream: bool,
154155
required_typeahead: Optional[str],
155156
topics: List[str],
156-
stream_dict: Dict[int, Dict[str, Any]],
157+
stream_dict: Dict[int, Subscription],
157158
) -> None:
158159
write_box.model.topics_in_stream.return_value = topics
159160
write_box.model.is_valid_stream.return_value = is_valid_stream
@@ -573,7 +574,7 @@ def test_generic_autocomplete_set_footer(
573574
state: Optional[int],
574575
footer_text: List[Any],
575576
text: str,
576-
stream_dict: Dict[int, Dict[str, Any]],
577+
stream_dict: Dict[int, Subscription],
577578
) -> None:
578579
write_box.view.set_typeahead_footer = mocker.patch(
579580
"zulipterminal.ui.View.set_typeahead_footer"
@@ -940,7 +941,7 @@ def test_generic_autocomplete_streams(
940941
text: str,
941942
state_and_required_typeahead: Dict[int, Optional[str]],
942943
stream_categories: Dict[str, Any],
943-
stream_dict: Dict[int, Dict[str, Any]],
944+
stream_dict: Dict[int, Subscription],
944945
) -> None:
945946
streams_to_pin = (
946947
[{"name": stream_name} for stream_name in stream_categories["pinned"]]
@@ -1279,7 +1280,7 @@ def test__set_stream_write_box_style_markers(
12791280
is_valid_stream: bool,
12801281
stream_access_type: StreamAccessType,
12811282
expected_marker: str,
1282-
stream_dict: Dict[int, Any],
1283+
stream_dict: Dict[int, Subscription],
12831284
expected_color: str,
12841285
) -> None:
12851286
# FIXME: Refactor when we have ~ Model.is_private_stream

0 commit comments

Comments
 (0)