Skip to content

Commit ac16f77

Browse files
committed
model/boxes/buttons/views: Add stream_name_from_id method to model.
This commit introduces the 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 230a9ab commit ac16f77

File tree

8 files changed

+58
-18
lines changed

8 files changed

+58
-18
lines changed

tests/model/test_model.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1913,6 +1913,29 @@ def test_get_all_stream_ids(
19131913
model._never_subscribed_streams = never_subscribed_streams
19141914
assert model.get_all_stream_ids() == all_stream_ids
19151915

1916+
@pytest.mark.parametrize(
1917+
"stream_id, expected_stream_name",
1918+
[
1919+
case(
1920+
1000,
1921+
"Some general stream",
1922+
id="Subscribed stream",
1923+
),
1924+
case(
1925+
3,
1926+
"Stream 3",
1927+
id="Unsubscribed stream",
1928+
),
1929+
case(
1930+
5,
1931+
"Stream 5",
1932+
id="Never subscribed stream",
1933+
),
1934+
],
1935+
)
1936+
def test_stream_name_from_id(self, model, stream_id, expected_stream_name):
1937+
assert model.stream_name_from_id(stream_id) == expected_stream_name
1938+
19161939
@pytest.mark.parametrize("muted", powerset([99, 1000]))
19171940
@pytest.mark.parametrize("visual_notification_enabled", powerset([99, 1000]))
19181941
def test__subscribe_to_streams(

tests/ui/test_ui_tools.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -889,8 +889,8 @@ def test_keypress_NEXT_UNREAD_TOPIC_stream(
889889
mocker.patch(MIDCOLVIEW + ".focus_position")
890890
mocker.patch.object(self.view, "message_view")
891891

892-
mid_col_view.model.stream_dict = {1: {"name": "stream"}}
893892
mid_col_view.model.next_unread_topic_from_message_id.return_value = (1, "topic")
893+
mid_col_view.model.stream_name_from_id.return_value = "stream"
894894

895895
return_value = mid_col_view.keypress(size, key)
896896

tests/ui_tools/test_boxes.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,11 @@ def test_generic_autocomplete_stream_and_topic(
153153
is_valid_stream: bool,
154154
required_typeahead: Optional[str],
155155
topics: List[str],
156-
stream_dict: Dict[int, Dict[str, Any]],
156+
all_stream_ids: List[int],
157157
) -> None:
158158
write_box.model.topics_in_stream.return_value = topics
159159
write_box.model.is_valid_stream.return_value = is_valid_stream
160-
write_box.model.stream_dict = stream_dict
160+
write_box.model.get_all_stream_ids.return_value = all_stream_ids
161161
write_box.model.muted_streams = set()
162162
typeahead_string = write_box.generic_autocomplete(text, state)
163163

@@ -573,12 +573,12 @@ def test_generic_autocomplete_set_footer(
573573
state: Optional[int],
574574
footer_text: List[Any],
575575
text: str,
576-
stream_dict: Dict[int, Dict[str, Any]],
576+
all_stream_ids: List[int],
577577
) -> None:
578578
write_box.view.set_typeahead_footer = mocker.patch(
579579
"zulipterminal.ui.View.set_typeahead_footer"
580580
)
581-
write_box.model.stream_dict = stream_dict
581+
write_box.model.get_all_stream_ids.return_value = all_stream_ids
582582
write_box.model.muted_streams = set()
583583
write_box.generic_autocomplete(text, state)
584584

@@ -941,6 +941,7 @@ def test_generic_autocomplete_streams(
941941
state_and_required_typeahead: Dict[int, Optional[str]],
942942
stream_categories: Dict[str, Any],
943943
stream_dict: Dict[int, Dict[str, Any]],
944+
all_stream_ids: List[int],
944945
) -> None:
945946
streams_to_pin = (
946947
[{"name": stream_name} for stream_name in stream_categories["pinned"]]
@@ -951,12 +952,15 @@ def test_generic_autocomplete_streams(
951952
write_box.view.unpinned_streams.remove(stream)
952953
write_box.view.pinned_streams = streams_to_pin
953954
write_box.stream_id = stream_categories.get("current_stream", None)
954-
write_box.model.stream_dict = stream_dict
955955
write_box.model.muted_streams = {
956956
stream["stream_id"]
957957
for stream in stream_dict.values()
958958
if stream["name"] in stream_categories.get("muted", set())
959959
}
960+
write_box.model.get_all_stream_ids.return_value = all_stream_ids
961+
write_box.model.stream_name_from_id.side_effect = lambda x: stream_dict[x][
962+
"name"
963+
]
960964
states = state_and_required_typeahead.keys()
961965
required_typeaheads = list(state_and_required_typeahead.values())
962966
typeahead_strings = [

tests/ui_tools/test_buttons.py

Lines changed: 12 additions & 2 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.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+
"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
),
@@ -982,15 +989,14 @@ def test__validate_narrow_link(
982989
)
983990
def test__validate_and_patch_stream_data(
984991
self,
985-
stream_dict: Dict[int, Any],
986992
parsed_link: ParsedNarrowLink,
987993
is_user_subscribed_to_stream: Optional[bool],
988994
is_valid_stream: Optional[bool],
989995
stream_id_from_name_return_value: Optional[int],
996+
stream_name_from_id_return_value: Optional[str],
990997
expected_parsed_link: ParsedNarrowLink,
991998
expected_error: str,
992999
) -> None:
993-
self.controller.model.stream_dict = stream_dict
9941000
self.controller.model.stream_id_from_name.return_value = (
9951001
stream_id_from_name_return_value
9961002
)
@@ -1000,6 +1006,10 @@ def test__validate_and_patch_stream_data(
10001006
self.controller.model.is_valid_stream.return_value = is_valid_stream
10011007
mocked_button = self.message_link_button()
10021008

1009+
mocked_button.model.stream_name_from_id.return_value = (
1010+
stream_name_from_id_return_value
1011+
)
1012+
10031013
error = mocked_button._validate_and_patch_stream_data(parsed_link)
10041014

10051015
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.stream_name_from_id(stream_id)
894894
topic_to_search = (stream_name, topic)
895895
return topic_to_search in self._muted_topics
896896

@@ -1307,6 +1307,10 @@ def get_all_stream_ids(self) -> List[int]:
13071307
id_list.extend(stream_id for stream_id in self._never_subscribed_streams)
13081308
return id_list
13091309

1310+
def stream_name_from_id(self, stream_id: int) -> str:
1311+
stream = self._get_stream_from_id(stream_id)
1312+
return stream["name"]
1313+
13101314
def _subscribe_to_streams(self, subscriptions: List[Subscription]) -> None:
13111315
def make_reduced_stream_data(stream: Subscription) -> StreamData:
13121316
# 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.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.get_all_stream_ids():
641+
current_stream_name = self.model.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.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.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.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.stream_name_from_id(stream_id),
613613
topic_name=topic,
614614
)
615615
return key

0 commit comments

Comments
 (0)