Skip to content

Commit

Permalink
conftest/model: Support API migration for muted topics.
Browse files Browse the repository at this point in the history
This reflects the expected response change for muted_topics, from
[stream_name, topic] to [stream_name, topic, date_muted] in
Zulip version 3.0, Feature level 1 (see zulip/zulip@9340cd1).

An extra attribute, _muted_topics_dict, is added to be used locally
irrespective of the server version/feature level.

Test updated along with the addition of muted_topics_dict() in
conftest.py.
  • Loading branch information
preetmishra committed Aug 6, 2020
1 parent 64cc289 commit 95e5310
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
19 changes: 19 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,25 @@ def stream_dict(streams_fixture):
return {stream['stream_id']: stream for stream in streams_fixture}


@pytest.fixture(params=[
{
('Stream 2', 'muted topic'): 1530129122,
('Stream 1', 'muted stream muted topic'): 1530129122,
},
{
('Stream 2', 'muted topic'): None,
('Stream 1', 'muted stream muted topic'): None,
},
],
ids=[
'server_feature_level:1',
'server_feature_level:None',
]
)
def muted_topics_dict(request):
return request.param


@pytest.fixture
def classified_unread_counts():
"""
Expand Down
8 changes: 3 additions & 5 deletions tests/model/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -1583,12 +1583,10 @@ def test_is_muted_stream(self, muted_streams, stream_id, is_muted,
((1, 'muted stream muted topic'), True),
((2, 'unmuted topic'), False),
])
def test_is_muted_topic(self, topic, is_muted, stream_dict, model):
def test_is_muted_topic(self, topic, is_muted, stream_dict, model,
muted_topics_dict):
model.stream_dict = stream_dict
model._muted_topics = [
['Stream 2', 'muted topic'],
['Stream 1', 'muted stream muted topic'],
]
model._muted_topics_dict = muted_topics_dict

return_value = model.is_muted_topic(stream_id=topic[0], topic=topic[1])

Expand Down
18 changes: 15 additions & 3 deletions zulipterminal/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,20 @@ def __init__(self, controller: Any) -> None:
(self.stream_dict, self.muted_streams,
self.pinned_streams, self.unpinned_streams) = stream_data

# NOTE: The expected response has been upgraded from
# [stream_name, topic] to [stream_name, topic, date_muted] in
# 3.0, Feature level 1.
self._muted_topics = (
self.initial_data['muted_topics']) # type: List[List[str]]
self.initial_data['muted_topics']
) # type: List[List[Union[str, int]]]
assert all(len(sub_list) in (2, 3) for sub_list in self._muted_topics)
# The intent is to use the following data structure locally to avoid
# server version & feature level checks throughout the codebase.
self._muted_topics_dict = {
(stream_name, topic): (None if self.server_feature_level is None
else date_muted[0])
for stream_name, topic, *date_muted in self._muted_topics
}

groups = self.initial_data['realm_user_groups']
self.user_group_by_id = {} # type: Dict[int, Dict[str, Any]]
Expand Down Expand Up @@ -415,8 +427,8 @@ def is_muted_topic(self, stream_id: int, topic: str) -> bool:
Returns True if topic is muted via muted_topics.
"""
stream_name = self.stream_dict[stream_id]['name']
topic_to_search = [stream_name, topic] # type: List[str]
return topic_to_search in self._muted_topics
topic_to_search = (stream_name, topic) # type: Tuple[str, str]
return topic_to_search in self._muted_topics_dict.keys()

def _update_initial_data(self) -> None:
# Thread Processes to reduce start time.
Expand Down

0 comments on commit 95e5310

Please sign in to comment.