Skip to content

Commit 62f7883

Browse files
committed
model/boxes/buttons/views: Add get_stream_name_from_id method to model.
This commit introduces the get_stream_name_from_id method in order to limit the direct accessing of stream_dict outside the model. The commit also replaces the access of the name attribute from stream_dict with the new method. Tests added and updated.
1 parent 3cabb87 commit 62f7883

File tree

8 files changed

+59
-10
lines changed

8 files changed

+59
-10
lines changed

tests/model/test_model.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1819,6 +1819,36 @@ def test__get_all_stream_ids(
18191819
model._never_subscribed_streams = never_subscribed_streams_fixture
18201820
assert model._get_all_stream_ids() == expected_value
18211821

1822+
@pytest.mark.parametrize(
1823+
"stream_id, expected_stream_name",
1824+
[
1825+
case(
1826+
1000,
1827+
"Some general stream",
1828+
id="Subcribed stream",
1829+
),
1830+
case(
1831+
3,
1832+
"Stream 3",
1833+
id="Unsubcribed stream",
1834+
),
1835+
case(
1836+
5,
1837+
"Stream 5",
1838+
id="Never subcribed stream",
1839+
),
1840+
],
1841+
)
1842+
def test_get_stream_name_from_id(
1843+
self, mocker, model, stream_id, expected_stream_name
1844+
):
1845+
mocker.patch(
1846+
MODEL + "._get_stream_from_id", return_value={"name": expected_stream_name}
1847+
)
1848+
1849+
assert model.get_stream_name_from_id(stream_id) == expected_stream_name
1850+
model._get_stream_from_id.assert_called_once()
1851+
18221852
@pytest.mark.parametrize("muted", powerset([99, 1000]))
18231853
@pytest.mark.parametrize("visual_notification_enabled", powerset([99, 1000]))
18241854
def test__subscribe_to_streams(

tests/ui/test_ui_tools.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -891,6 +891,7 @@ def test_keypress_NEXT_UNREAD_TOPIC_stream(
891891

892892
mid_col_view.model.stream_dict = {1: {"name": "stream"}}
893893
mid_col_view.model.next_unread_topic_from_message_id.return_value = (1, "topic")
894+
mid_col_view.model.get_stream_name_from_id.return_value = "stream"
894895

895896
return_value = mid_col_view.keypress(size, key)
896897

tests/ui_tools/test_boxes.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -957,6 +957,9 @@ def test_generic_autocomplete_streams(
957957
for stream in stream_dict.values()
958958
if stream["name"] in stream_categories.get("muted", set())
959959
}
960+
write_box.model.get_stream_name_from_id.side_effect = lambda x: stream_dict[x][
961+
"name"
962+
]
960963
states = state_and_required_typeahead.keys()
961964
required_typeaheads = list(state_and_required_typeahead.values())
962965
typeahead_strings = [

tests/ui_tools/test_buttons.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,8 @@ def test_init_calls_top_button(
444444
top_button = mocker.patch(MODULE + ".TopButton.__init__")
445445
params = dict(controller=controller, count=count)
446446

447+
controller.model.get_stream_name_from_id.return_value = stream_name
448+
447449
topic_button = TopicButton(
448450
stream_id=stream_id, topic=title, view=view, **params
449451
)
@@ -922,6 +924,7 @@ def test__validate_narrow_link(
922924
"is_user_subscribed_to_stream",
923925
"is_valid_stream",
924926
"stream_id_from_name_return_value",
927+
"get_stream_name_from_id_return_value",
925928
"expected_parsed_link",
926929
"expected_error",
927930
],
@@ -933,6 +936,7 @@ def test__validate_narrow_link(
933936
True,
934937
None,
935938
None,
939+
"Stream 1",
936940
ParsedNarrowLink(
937941
stream=DecodedStream(stream_id=1, stream_name="Stream 1")
938942
),
@@ -945,6 +949,7 @@ def test__validate_narrow_link(
945949
False,
946950
None,
947951
None,
952+
None,
948953
ParsedNarrowLink(stream=DecodedStream(stream_id=462, stream_name=None)),
949954
"The stream seems to be either unknown or unsubscribed",
950955
),
@@ -955,6 +960,7 @@ def test__validate_narrow_link(
955960
None,
956961
True,
957962
1,
963+
None,
958964
ParsedNarrowLink(
959965
stream=DecodedStream(stream_id=1, stream_name="Stream 1")
960966
),
@@ -967,6 +973,7 @@ def test__validate_narrow_link(
967973
None,
968974
False,
969975
None,
976+
"foo",
970977
ParsedNarrowLink(
971978
stream=DecodedStream(stream_id=None, stream_name="foo")
972979
),
@@ -987,6 +994,7 @@ def test__validate_and_patch_stream_data(
987994
is_user_subscribed_to_stream: Optional[bool],
988995
is_valid_stream: Optional[bool],
989996
stream_id_from_name_return_value: Optional[int],
997+
get_stream_name_from_id_return_value: Optional[str],
990998
expected_parsed_link: ParsedNarrowLink,
991999
expected_error: str,
9921000
) -> None:
@@ -1000,6 +1008,10 @@ def test__validate_and_patch_stream_data(
10001008
self.controller.model.is_valid_stream.return_value = is_valid_stream
10011009
mocked_button = self.message_link_button()
10021010

1011+
mocked_button.model.get_stream_name_from_id.return_value = (
1012+
get_stream_name_from_id_return_value
1013+
)
1014+
10031015
error = mocked_button._validate_and_patch_stream_data(parsed_link)
10041016

10051017
assert parsed_link == expected_parsed_link

zulipterminal/model.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -890,7 +890,7 @@ def is_muted_topic(self, stream_id: int, topic: str) -> bool:
890890
"""
891891
Returns True if topic is muted via muted_topics.
892892
"""
893-
stream_name = self.stream_dict[stream_id]["name"]
893+
stream_name = self.get_stream_name_from_id(stream_id)
894894
topic_to_search = (stream_name, topic)
895895
return topic_to_search in self._muted_topics
896896

@@ -1297,6 +1297,10 @@ def _get_all_stream_ids(self) -> List[int]:
12971297
id_list.extend(stream_id for stream_id in self._never_subscribed_streams)
12981298
return id_list
12991299

1300+
def get_stream_name_from_id(self, stream_id: int) -> str:
1301+
stream = self._get_stream_from_id(stream_id)
1302+
return stream["name"]
1303+
13001304
def _subscribe_to_streams(self, subscriptions: List[Subscription]) -> None:
13011305
def make_reduced_stream_data(stream: Subscription) -> StreamData:
13021306
# stream_id has been changed to id.

zulipterminal/ui_tools/boxes.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@ def autocomplete_streams(
618618
)
619619

620620
muted_streams = [
621-
self.model.stream_dict[stream_id]["name"]
621+
self.model.get_stream_name_from_id(stream_id)
622622
for stream_id in self.model.muted_streams
623623
]
624624
matching_muted_streams = [
@@ -637,9 +637,8 @@ def autocomplete_streams(
637637
else:
638638
matched_streams.append(matching_muted_stream)
639639

640-
current_stream = self.model.stream_dict.get(self.stream_id, None)
641-
if current_stream is not None:
642-
current_stream_name = current_stream["name"]
640+
if self.stream_id in self.model.stream_dict:
641+
current_stream_name = self.model.get_stream_name_from_id(self.stream_id)
643642
if current_stream_name in matched_streams:
644643
matched_streams.remove(current_stream_name)
645644
matched_streams.insert(0, current_stream_name)

zulipterminal/ui_tools/buttons.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ def __init__(
306306
view: Any,
307307
count: int,
308308
) -> None:
309-
self.stream_name = controller.model.stream_dict[stream_id]["name"]
309+
self.stream_name = controller.model.get_stream_name_from_id(stream_id)
310310
self.topic_name = topic
311311
self.stream_id = stream_id
312312
self.model = controller.model
@@ -574,7 +574,7 @@ def _validate_and_patch_stream_data(self, parsed_link: ParsedNarrowLink) -> str:
574574
stream_id = cast(int, model.stream_id_from_name(stream_name))
575575
parsed_link["stream"]["stream_id"] = stream_id
576576
else:
577-
stream_name = cast(str, model.stream_dict[stream_id]["name"])
577+
stream_name = cast(str, model.get_stream_name_from_id(stream_id))
578578
parsed_link["stream"]["stream_name"] = stream_name
579579

580580
return ""

zulipterminal/ui_tools/views.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -578,8 +578,8 @@ def keypress(self, size: urwid_Size, key: str) -> Optional[str]:
578578
# For new streams with no previous conversation.
579579
if self.footer.focus is None:
580580
stream_id = self.model.stream_id
581-
stream_dict = self.model.stream_dict
582-
self.footer.stream_box_view(caption=stream_dict[stream_id]["name"])
581+
stream_name = self.model.get_stream_name_from_id(stream_id)
582+
self.footer.stream_box_view(caption=stream_name)
583583
self.set_focus("footer")
584584
self.footer.focus_position = 0
585585
return key
@@ -609,7 +609,7 @@ def keypress(self, size: urwid_Size, key: str) -> Optional[str]:
609609

610610
stream_id, topic = stream_topic
611611
self.controller.narrow_to_topic(
612-
stream_name=self.model.stream_dict[stream_id]["name"],
612+
stream_name=self.model.get_stream_name_from_id(stream_id),
613613
topic_name=topic,
614614
)
615615
return key

0 commit comments

Comments
 (0)