Skip to content

Commit

Permalink
views: Skip muted_streams/topics in getting next unread topic.
Browse files Browse the repository at this point in the history
Added tests.
  • Loading branch information
amanagr committed Aug 3, 2019
1 parent bb244b6 commit 899d116
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 29 deletions.
74 changes: 51 additions & 23 deletions tests/ui/test_ui_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -593,30 +593,58 @@ def test_init(self, mid_col_view):
self.super.assert_called_once_with("MSG_LIST", header=self.search_box,
footer=self.write_box)

def test_get_next_unread_topic(self, mid_col_view):
mid_col_view.model.unread_counts = {
'unread_topics': {1: 1, 2: 1}
}
return_value = mid_col_view.get_next_unread_topic()
assert return_value == 1
assert mid_col_view.last_unread_topic == 1

def test_get_next_unread_topic_again(self, mid_col_view):
mid_col_view.model.unread_counts = {
'unread_topics': {1: 1, 2: 1}
}
mid_col_view.last_unread_topic = 1
return_value = mid_col_view.get_next_unread_topic()
assert return_value == 2
assert mid_col_view.last_unread_topic == 2

def test_get_next_unread_topic_no_unread(self, mid_col_view):
mid_col_view.model.unread_counts = {
'unread_topics': {}
@pytest.mark.parametrize('last_unread_topic, next_unread_topic', [
((1, 'stream muted & unmuted topic'), (2, 'unmuted topic 1')),
((2, 'unmuted topic 1'), (2, 'unmuted topic 2')),
((2, 'unmuted topic 2'), (2, 'unmuted topic 3')),
((2, 'unmuted topic 3'), (2, 'unmuted topic 1')),
])
def test_get_next_unread_topic(self, mid_col_view, last_unread_topic,
next_unread_topic, stream_dict,
model_fixture):
mid_col_view.model = model_fixture
mid_col_view.model.unread_counts = dict()
mid_col_view.model.unread_counts['unread_topics'] = OrderedDict({
(1, 'stream muted & unmuted topic'): 1,
(1, 'muted stream muted topic'): 1,
(2, 'muted topic'): 1,
(2, 'unmuted topic 1'): 1,
(2, 'unmuted topic 2'): 1,
(2, 'unmuted topic 3'): 1,
})
mid_col_view.model.stream_dict = stream_dict
mid_col_view.model.muted_streams = [1]
mid_col_view.model.muted_topics = [
('Stream 2', 'muted topic'),
('Stream 1', 'muted stream muted topic'),
]
mid_col_view.last_unread_topic = last_unread_topic
assert mid_col_view.get_next_unread_topic() == next_unread_topic

@pytest.mark.parametrize('last_unread_topic, next_unread_topic', [
((1, 'stream muted & unmuted topic'), None),
((2, 'muted topic'), None),
(None, None),
])
def test_get_next_unread_topic_no_unread(self, mid_col_view,
stream_dict,
last_unread_topic,
next_unread_topic,
model_fixture):
mid_col_view.model = model_fixture
mid_col_view.model.unread_counts = dict()
mid_col_view.model.unread_counts['unread_topics'] = {
(1, 'stream muted & unmuted topic'): 1,
(1, 'muted stream muted topic'): 1,
(2, 'muted topic'): 1,
}
return_value = mid_col_view.get_next_unread_topic()
assert return_value is None
assert mid_col_view.last_unread_topic is None
mid_col_view.model.stream_dict = stream_dict
mid_col_view.model.muted_streams = [1]
mid_col_view.model.muted_topics = [
('Stream 2', 'muted topic'),
('Stream 1', 'muted stream muted topic'),
]
assert mid_col_view.get_next_unread_topic() == next_unread_topic

def test_get_next_unread_pm(self, mid_col_view):
mid_col_view.model.unread_counts = {
Expand Down
14 changes: 8 additions & 6 deletions zulipterminal/ui_tools/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,16 +399,18 @@ def __init__(self, view: Any, model: Any,
def get_next_unread_topic(self) -> Optional[Tuple[int, str]]:
topics = list(self.model.unread_counts['unread_topics'].keys())
next_topic = False
for topic in topics:
if next_topic is True:
if self.last_unread_topic not in topics:
next_topic = True
# loop over topics list twice
# for the case that last_unread_topic was
# the last valid unread_topic in topics list.
for topic in topics*2:
if not self.model.is_muted_topic(stream_id=topic[0],
topic=topic[1]) and next_topic:
self.last_unread_topic = topic
return topic
if topic == self.last_unread_topic:
next_topic = True
if len(topics) > 0:
topic = topics[0]
self.last_unread_topic = topic
return topic
return None

def get_next_unread_pm(self) -> Optional[int]:
Expand Down

0 comments on commit 899d116

Please sign in to comment.