Skip to content

Commit dee7e2b

Browse files
committed
refactor: model: Use get_subscription_color instead of stream_dict.
This commit replaces the usage of directly indexing stream_dict for "color" data with the new stream property accessor method "get_subscription_color". Test added.
1 parent 5e5bd20 commit dee7e2b

File tree

6 files changed

+29
-5
lines changed

6 files changed

+29
-5
lines changed

tests/model/test_model.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2279,6 +2279,21 @@ def test_get_stream_rendered_description(
22792279
assert model.get_stream_rendered_description(stream_id) == expected_value
22802280
model._get_stream_from_id.assert_called_once()
22812281

2282+
@pytest.mark.parametrize(
2283+
"stream_id, expected_value",
2284+
[
2285+
case(
2286+
1,
2287+
"#baf",
2288+
),
2289+
],
2290+
)
2291+
def test_get_subscription_color(
2292+
self, model, stream_dict, stream_id, expected_value
2293+
):
2294+
model.stream_dict = stream_dict
2295+
assert model.get_subscription_color(stream_id) == expected_value
2296+
22822297
def test_get_all_subscription_ids(
22832298
self,
22842299
model,

tests/ui_tools/test_boxes.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,6 +1284,7 @@ def test__set_stream_write_box_style_markers(
12841284
) -> None:
12851285
# FIXME: Refactor when we have ~ Model.is_private_stream
12861286
write_box.model.stream_dict = stream_dict
1287+
write_box.model.get_subscription_color.return_value = expected_color
12871288
write_box.model.is_valid_stream.return_value = is_valid_stream
12881289
write_box.model.stream_id_from_name.return_value = stream_id
12891290
write_box.model.stream_access_type.return_value = stream_access_type

tests/ui_tools/test_messages.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -951,13 +951,15 @@ def test_msg_generates_search_and_header_bar(
951951
msg_narrow,
952952
assert_header_bar,
953953
assert_search_bar,
954+
stream_color="#bd6",
954955
):
955956
self.model.stream_dict = {
956957
205: {
957-
"color": "#bd6",
958+
"color": stream_color,
958959
},
959960
}
960961
self.model.narrow = msg_narrow
962+
self.model.get_subscription_color.return_value = stream_color
961963
messages = messages_successful_response["messages"]
962964
current_message = messages[msg_type]
963965
msg_box = MessageBox(current_message, self.model, messages[0])

zulipterminal/model.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1236,6 +1236,13 @@ def get_stream_weekly_traffic(self, stream_id: int) -> Optional[int]:
12361236
def get_stream_rendered_description(self, stream_id: int) -> str:
12371237
return self._get_stream_from_id(stream_id)["rendered_description"]
12381238

1239+
def get_subscription_color(self, stream_id: int) -> Optional[str]:
1240+
if stream_id in self.stream_dict:
1241+
return self.stream_dict[stream_id]["color"]
1242+
elif stream_id in self._unsubscribed_streams:
1243+
return self._unsubscribed_streams[stream_id]["color"]
1244+
return None
1245+
12391246
def _subscribe_to_streams(self, subscriptions: List[Subscription]) -> None:
12401247
def make_reduced_stream_data(stream: Subscription) -> StreamData:
12411248
# 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.get_subscription_color(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
@@ -150,7 +150,7 @@ def _is_private_message_to_self(self) -> bool:
150150

151151
def stream_header(self) -> Any:
152152
assert self.stream_id is not None
153-
color = self.model.stream_dict[self.stream_id]["color"]
153+
color = self.model.get_subscription_color(self.stream_id)
154154
bar_color = f"s{color}"
155155
stream_title_markup = (
156156
"bar",
@@ -211,7 +211,7 @@ def top_search_bar(self) -> Any:
211211
text_to_fill = "Mentions"
212212
elif self.message["type"] == "stream":
213213
assert self.stream_id is not None
214-
bar_color = self.model.stream_dict[self.stream_id]["color"]
214+
bar_color = self.model.get_subscription_color(self.stream_id)
215215
bar_color = f"s{bar_color}"
216216
if len(curr_narrow) == 2 and curr_narrow[1][0] == "topic":
217217
text_to_fill = (

0 commit comments

Comments
 (0)