Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix/migration: Support API migration for muted topics #744

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,28 @@ def stream_dict(streams_fixture):
return {stream['stream_id']: stream for stream in streams_fixture}


@pytest.fixture(params=[
{
('Stream 1', 'muted stream muted topic'): None,
('Stream 2', 'muted topic'): None,
},
{
('Stream 1', 'muted stream muted topic'): 1530129122,
('Stream 2', 'muted topic'): 1530129122,
},
],
ids=[
'zulip_feature_level:None',
'zulip_feature_level:1',
]
)
def processed_muted_topics(request):
"""
Locally processed muted topics data (see _muted_topics in Model.__init__).
"""
return request.param


@pytest.fixture
def classified_unread_counts():
"""
Expand Down
12 changes: 7 additions & 5 deletions tests/core/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def test_narrow_to_stream(self, mocker, controller,
}
}
controller.model.muted_streams = []
controller.model.muted_topics = []
controller.model.is_muted_topic = mocker.Mock(return_value=False)

controller.narrow_to_stream(stream_button)

Expand Down Expand Up @@ -113,7 +113,7 @@ def test_narrow_to_topic(self, mocker, controller,
}
}
controller.model.muted_streams = []
controller.model.muted_topics = []
controller.model.is_muted_topic = mocker.Mock(return_value=False)
controller.narrow_to_topic(msg_box)
assert controller.model.stream_id == msg_box.stream_id
assert controller.model.narrow == expected_narrow
Expand Down Expand Up @@ -158,7 +158,7 @@ def test_show_all_messages(self, mocker, controller, index_all_messages):
}
}
controller.model.muted_streams = []
controller.model.muted_topics = []
controller.model.is_muted_topic = mocker.Mock(return_value=False)

controller.show_all_messages('')

Expand Down Expand Up @@ -194,7 +194,8 @@ def test_show_all_starred(self, mocker, controller, index_all_starred):
controller.model.index = index_all_starred
controller.model.muted_streams = set() # FIXME Expand upon this
controller.model.user_id = 1
controller.model.muted_topics = [] # FIXME Expand upon this
# FIXME: Expand upon is_muted_topic().
controller.model.is_muted_topic = mocker.Mock(return_value=False)
controller.model.user_email = "some@email"
controller.model.stream_dict = {
205: {
Expand All @@ -218,7 +219,8 @@ def test_show_all_mentions(self, mocker, controller, index_all_mentions):
controller.model.narrow = []
controller.model.index = index_all_mentions
controller.model.muted_streams = set() # FIXME Expand upon this
controller.model.muted_topics = [] # FIXME Expand upon this
# FIXME: Expand upon is_muted_topic().
controller.model.is_muted_topic = mocker.Mock(return_value=False)
controller.model.user_email = "some@email"
controller.model.user_id = 1
controller.model.stream_dict = {
Expand Down
5 changes: 4 additions & 1 deletion tests/helper/test_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,10 @@ def test_classify_unread_counts(mocker, initial_data, stream_dict,
model = mocker.Mock()
model.stream_dict = stream_dict
model.initial_data = initial_data
model.muted_topics = muted_topics
model.is_muted_topic = mocker.Mock(side_effect=(
lambda stream_id, topic:
[model.stream_dict[stream_id]['name'], topic] in muted_topics
))
model.muted_streams = muted_streams
assert classify_unread_counts(model) == dict(classified_unread_counts,
**vary_in_unreads)
Expand Down
46 changes: 37 additions & 9 deletions tests/model/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,35 @@ def test_init(self, model, initial_data, user_profile):
self.classify_unread_counts.assert_called_once_with(model)
assert model.unread_counts == []

@pytest.mark.parametrize(['server_response', 'locally_processed_data',
'zulip_feature_level'], [
(
[['Stream 1', 'muted stream muted topic']],
{('Stream 1', 'muted stream muted topic'): None},
None,
),
(
[['Stream 2', 'muted topic', 1530129122]],
{('Stream 2', 'muted topic'): 1530129122},
1,
),
],
ids=[
'zulip_feature_level:None',
'zulip_feature_level:1',
]
)
def test_init_muted_topics(self, mocker, initial_data, server_response,
locally_processed_data, zulip_feature_level):
mocker.patch('zulipterminal.model.Model.get_messages', return_value='')
initial_data['zulip_feature_level'] = zulip_feature_level
initial_data['muted_topics'] = server_response
self.client.register = mocker.Mock(return_value=initial_data)

model = Model(self.controller)

assert model._muted_topics == locally_processed_data

def test_init_InvalidAPIKey_response(self, mocker, initial_data):
# Both network calls indicate the same response
mocker.patch('zulipterminal.model.Model.get_messages',
Expand Down Expand Up @@ -1578,17 +1607,16 @@ def test_is_muted_stream(self, muted_streams, stream_id, is_muted,
assert model.is_muted_stream(stream_id) == is_muted

@pytest.mark.parametrize('topic, is_muted', [
((1, 'stream muted & unmuted topic'), True),
((1, 'stream muted & unmuted topic'), False),
((2, 'muted topic'), True),
((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,
processed_muted_topics):
model.stream_dict = stream_dict
model.muted_streams = [1]
model.muted_topics = [
['Stream 2', 'muted topic'],
['Stream 1', 'muted stream muted topic'],
]
assert model.is_muted_topic(stream_id=topic[0],
topic=topic[1]) == is_muted
model._muted_topics = processed_muted_topics

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

assert return_value == is_muted
41 changes: 19 additions & 22 deletions tests/ui/test_ui_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,9 @@ def test_update_topics_list(self, mocker, topic_view, topic_name,
topic_view.view.controller.model.stream_dict = {
86: {'name': 'PTEST'}
}
topic_view.view.controller.model.muted_topics = []
topic_view.view.controller.model.is_muted_topic = (
mocker.Mock(return_value=False)
)
topic_view.log = [mocker.Mock(topic_name=topic_name)
for topic_name in topic_initial_log]

Expand Down Expand Up @@ -2660,7 +2662,7 @@ def test_init_calls_top_button(self, mocker, width, count, title,
86: {'name': 'Django'},
14: {'name': 'GSoC'},
}
controller.model.muted_topics = []
controller.model.is_muted_topic = mocker.Mock(return_value=False)
top_button = mocker.patch(TOPBUTTON + '.__init__')
params = dict(controller=controller,
width=width,
Expand All @@ -2677,29 +2679,24 @@ def test_init_calls_top_button(self, mocker, width, count, title,
assert topic_button.stream_id == stream_id
assert topic_button.topic_name == title

@pytest.mark.parametrize(['stream_name', 'title', 'muted_topics',
'is_muted_called'], [
('Django', 'topic1', [['Django', 'topic1']], True),
('Django', 'topic2', [['Django', 'topic1']], False),
('GSoC', 'topic1', [['Django', 'topic1']], False),
], ids=[
'stream_and_topic_match',
'topic_mismatch',
'stream_mismatch',
@pytest.mark.parametrize(['is_muted_topic_return_value',
'is_mark_muted_called'], [
(True, True),
(False, False),
])
def test_init_calls_mark_muted(self, mocker, stream_name, title,
muted_topics, is_muted_called):
mark_muted = mocker.patch(
'zulipterminal.ui_tools.buttons.TopicButton.mark_muted')
def test_init_calls_mark_muted(self, mocker, is_muted_topic_return_value,
is_mark_muted_called):
mocker.patch('zulipterminal.ui_tools.buttons.TopicButton.mark_muted')
controller = mocker.Mock()
controller.model.muted_topics = muted_topics
controller.model.is_muted_topic = (
mocker.Mock(return_value=is_muted_topic_return_value)
)
controller.model.stream_dict = {
205: {'name': stream_name}
205: {'name': 'Stream 1'}
}

topic_button = TopicButton(stream_id=205,
topic=title, controller=controller,
topic='Topic', controller=controller,
width=40, count=0)
if is_muted_called:
mark_muted.assert_called_once_with()
else:
mark_muted.assert_not_called()

assert topic_button.mark_muted.called == is_mark_muted_called
37 changes: 10 additions & 27 deletions tests/ui/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,16 @@
from zulipterminal.ui_tools.utils import create_msg_box_list, is_muted


@pytest.mark.parametrize(['msg', 'narrow', 'muted_streams', 'muted_topics',
'muted'], [
@pytest.mark.parametrize(['msg', 'narrow', 'muted_streams',
'is_muted_topic_return_value', 'muted'], [
( # PM TEST
{
'type': 'private',
# ...
},
[],
[1, 2],
[
['foo', 'boo foo'],
['boo', 'foo boo'],
],
False,
False
),
(
Expand All @@ -25,10 +22,7 @@
},
[['stream', 'foo'], ['topic', 'boo']],
[1, 2],
[
['foo', 'boo foo'],
['boo', 'foo boo'],
],
False,
False
),
(
Expand All @@ -39,52 +33,41 @@
},
[['stream', 'foo']],
[1, 2],
[
['foo', 'boo foo'],
['boo', 'foo boo'],
],
False,
True
),
(
{
'type': 'stream',
'stream_id': 2,
'display_recipient': 'boo',
'subject': 'foo boo',
# ...
},
[],
[1, 2],
[
['foo', 'boo foo'],
['boo', 'foo boo'],
],
True,
True
),
(
{
'type': 'stream',
'stream_id': 3,
'display_recipient': 'zoo',
'subject': 'foo koo',
# ...
},
[],
[1, 2],
[
['foo', 'boo foo'],
['boo', 'foo boo'],
],
False,
False
),
])
def test_is_muted(mocker, msg, narrow, muted_streams, muted_topics, muted):
def test_is_muted(mocker, msg, narrow, muted_streams,
is_muted_topic_return_value, muted):
model = mocker.Mock()
model.is_muted_stream = (
mocker.Mock(return_value=(msg.get('stream_id', '') in muted_streams))
)
model.narrow = narrow
model.muted_topics = muted_topics
model.is_muted_topic.return_value = is_muted_topic_return_value
return_value = is_muted(msg, model)
assert return_value is muted

Expand Down
6 changes: 2 additions & 4 deletions zulipterminal/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,7 @@ def _set_count_in_view(controller: Any, new_count: int,
+ new_count)
break
# FIXME: Update unread_counts['unread_topics']?
if ([message['display_recipient'], msg_topic] in
controller.model.muted_topics):
if controller.model.is_muted_topic(stream_id, msg_topic):
add_to_counts = False
if is_open_topic_view and stream_id == toggled_stream_id:
# If topic_view is open for incoming messages's stream,
Expand Down Expand Up @@ -441,8 +440,7 @@ def classify_unread_counts(model: Any) -> UnreadCounts:
# unsubscribed streams may be in unreads, but not in stream_dict
if stream_id not in model.stream_dict:
continue
if [model.stream_dict[stream_id]['name'],
stream['topic']] in model.muted_topics:
if model.is_muted_topic(stream_id, stream['topic']):
continue
stream_topic = (stream_id, stream['topic'])
unread_counts['unread_topics'][stream_topic] = count
Expand Down
21 changes: 15 additions & 6 deletions zulipterminal/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,16 @@ def __init__(self, controller: Any) -> None:
(self.stream_dict, self.muted_streams,
self.pinned_streams, self.unpinned_streams) = stream_data

self.muted_topics = (
self.initial_data['muted_topics']) # type: List[List[str]]
# NOTE: The expected response has been upgraded from
# [stream_name, topic] to [stream_name, topic, date_muted] in
# feature level 1, server version 3.0.
muted_topics = self.initial_data['muted_topics']
assert set(map(len, muted_topics)) in (set(), {2}, {3})
self._muted_topics = {
(stream_name, topic): (None if self.server_feature_level is None
else date_muted[0])
for stream_name, topic, *date_muted in muted_topics
} # type: Dict[Tuple[str, str], Optional[int]]

groups = self.initial_data['realm_user_groups']
self.user_group_by_id = {} # type: Dict[int, Dict[str, Any]]
Expand Down Expand Up @@ -411,11 +419,12 @@ def is_muted_stream(self, stream_id: int) -> bool:
return stream_id in self.muted_streams

def is_muted_topic(self, stream_id: int, topic: str) -> bool:
if stream_id in self.muted_streams:
return True
"""
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)
return topic_to_search in self._muted_topics.keys()

def _update_initial_data(self) -> None:
# Thread Processes to reduce start time.
Expand Down
3 changes: 1 addition & 2 deletions zulipterminal/ui_tools/buttons.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,7 @@ def __init__(self, stream_id: int, topic: str,
width=width,
count=count)

topic_pair = [self.stream_name, self.topic_name]
if topic_pair in controller.model.muted_topics:
preetmishra marked this conversation as resolved.
Show resolved Hide resolved
if controller.model.is_muted_topic(self.stream_id, self.topic_name):
self.mark_muted()

def mark_muted(self) -> None:
Expand Down
2 changes: 1 addition & 1 deletion zulipterminal/ui_tools/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def is_muted(msg: Message, model: Any) -> bool:
return False
elif model.is_muted_stream(msg['stream_id']):
return True
elif [msg['display_recipient'], msg['subject']] in model.muted_topics:
elif model.is_muted_topic(msg['stream_id'], msg['subject']):
return True
return False

Expand Down