Skip to content

Commit 0345e9f

Browse files
committed
model/boxes/messages: Add subscription_color_from_id method to model.
This commit introduces the subscription_color_from_id method in order to limit the direct accessing of stream_dict outside the model. The commit also replaces the access of the color attribute from stream_dict with the new method. Tests added and updated.
1 parent ac16f77 commit 0345e9f

File tree

6 files changed

+29
-10
lines changed

6 files changed

+29
-10
lines changed

tests/model/test_model.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1936,6 +1936,24 @@ def test_get_all_stream_ids(
19361936
def test_stream_name_from_id(self, model, stream_id, expected_stream_name):
19371937
assert model.stream_name_from_id(stream_id) == expected_stream_name
19381938

1939+
@pytest.mark.parametrize(
1940+
"stream_id, expected_stream_color",
1941+
[
1942+
case(
1943+
1000,
1944+
"#baf",
1945+
id="Subscribed stream",
1946+
),
1947+
case(
1948+
999,
1949+
"#ddd",
1950+
id="Unsubscribed stream",
1951+
),
1952+
],
1953+
)
1954+
def test_subscription_color_from_id(self, model, stream_id, expected_stream_color):
1955+
assert model.subscription_color_from_id(stream_id) == expected_stream_color
1956+
19391957
@pytest.mark.parametrize("muted", powerset([99, 1000]))
19401958
@pytest.mark.parametrize("visual_notification_enabled", powerset([99, 1000]))
19411959
def test__subscribe_to_streams(

tests/ui_tools/test_boxes.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1282,7 +1282,9 @@ def test__set_stream_write_box_style_markers(
12821282
expected_color: str,
12831283
) -> None:
12841284
# FIXME: Refactor when we have ~ Model.is_private_stream
1285-
write_box.model.stream_dict = stream_dict
1285+
write_box.model.subscription_color_from_id.side_effect = lambda x: stream_dict[
1286+
x
1287+
]["color"]
12861288
write_box.model.is_valid_stream.return_value = is_valid_stream
12871289
write_box.model.stream_id_from_name.return_value = stream_id
12881290
write_box.model.stream_access_type.return_value = stream_access_type

tests/ui_tools/test_messages.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,11 +1005,7 @@ def test_msg_generates_search_and_header_bar(
10051005
assert_header_bar,
10061006
assert_search_bar,
10071007
):
1008-
self.model.stream_dict = {
1009-
205: {
1010-
"color": "#bd6",
1011-
},
1012-
}
1008+
self.model.subscription_color_from_id.return_value = "#bd6"
10131009
self.model.narrow = msg_narrow
10141010
messages = messages_successful_response["messages"]
10151011
current_message = messages[msg_type]

zulipterminal/model.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1311,6 +1311,10 @@ def stream_name_from_id(self, stream_id: int) -> str:
13111311
stream = self._get_stream_from_id(stream_id)
13121312
return stream["name"]
13131313

1314+
def subscription_color_from_id(self, subscription_id: int) -> str:
1315+
subscription = self._get_subscription_from_id(subscription_id)
1316+
return subscription["color"]
1317+
13141318
def _subscribe_to_streams(self, subscriptions: List[Subscription]) -> None:
13151319
def make_reduced_stream_data(stream: Subscription) -> StreamData:
13161320
# stream_id has been changed to id.

zulipterminal/ui_tools/boxes.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -412,8 +412,7 @@ def _set_stream_write_box_style(self, widget: ReadlineEdit, new_text: str) -> No
412412
stream_id = self.model.stream_id_from_name(new_text)
413413
stream_access_type = self.model.stream_access_type(stream_id)
414414
stream_marker = STREAM_ACCESS_TYPE[stream_access_type]["icon"]
415-
stream = self.model.stream_dict[stream_id]
416-
color = stream["color"]
415+
color = self.model.subscription_color_from_id(stream_id)
417416
self.header_write_box[self.FOCUS_HEADER_PREFIX_STREAM].set_text(
418417
(color, stream_marker)
419418
)

zulipterminal/ui_tools/messages.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ def _is_private_message_to_self(self) -> bool:
152152

153153
def stream_header(self) -> Any:
154154
assert self.stream_id is not None
155-
color = self.model.stream_dict[self.stream_id]["color"]
155+
color = self.model.subscription_color_from_id(self.stream_id)
156156
bar_color = f"s{color}"
157157
stream_access_type = self.model.stream_access_type(self.stream_id)
158158
stream_icon = STREAM_ACCESS_TYPE[stream_access_type]["icon"]
@@ -220,7 +220,7 @@ def top_search_bar(self) -> Any:
220220
text_to_fill = "Mentions"
221221
elif self.message["type"] == "stream":
222222
assert self.stream_id is not None
223-
bar_color = self.model.stream_dict[self.stream_id]["color"]
223+
bar_color = self.model.subscription_color_from_id(self.stream_id)
224224
bar_color = f"s{bar_color}"
225225
if len(curr_narrow) == 2 and curr_narrow[1][0] == "topic":
226226
text_to_fill = (

0 commit comments

Comments
 (0)