Skip to content

Migrate to black #1039

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

Merged
merged 33 commits into from
Jun 1, 2021
Merged
Changes from 2 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
1445b54
refactor: tests: model: Extract CONTROLLER constant to simplify patches.
neiljp May 25, 2021
7e441e0
refactor: tests: model: Extract MODEL constant to simplify patches.
neiljp May 25, 2021
2ef40d8
refactor: tests: model/boxes: Extract variables to simplify tests.
neiljp May 25, 2021
2127faa
refactor: tests: Simplify parametrize names into single strings.
neiljp May 25, 2021
f8b9e55
refactor: tests: Simplify parametrize names & minimally indent.
neiljp May 25, 2021
b5246ab
refactor: tests: run/ui/model/core: Simplify multi-line strings.
neiljp May 23, 2021
a8488ef
refactor: tests: ui/ui_tools/model: Remove unnecessary parentheses.
neiljp May 25, 2021
3347195
refactor: tests: model/run/popups/boxes/ui_tools: Remove commas.
neiljp May 23, 2021
cefcd55
refactor: tests: boxes: Use standard 'case' for param import.
neiljp May 27, 2021
3f5ab97
refactor: tests: model/boxes/buttons/ui_tools: Inline test ids.
neiljp May 24, 2021
e638572
refactor: tests: ui_tools: Reformat soup2markup parametrize entries.
neiljp May 23, 2021
3a1847e
refactor: tests: ui_tools: Inline soup2markup test ids.
neiljp May 23, 2021
399493f
refactor: tests: helper: Indent outside-narrow test cases & add `case`.
neiljp May 24, 2021
3185035
tests: helper: Add inline outside-narrow test ids.
neiljp May 24, 2021
6117cf4
refactor: tests: model/ui_tools: Adjust comment placement.
neiljp May 24, 2021
2c4170b
refactor: tests: ui_tools: Add parentheses around multi-line string.
neiljp May 24, 2021
85533b8
refactor: tests: ui_tools: Add commas to ids for consistent formatting.
neiljp May 24, 2021
3d51bcd
refactor: tests: run: Disable formatting around test parametrize grid.
neiljp May 23, 2021
e878e20
refactor: tests: ui_tools: Indent & disable formatting for blockquote.
neiljp May 24, 2021
572369f
refactor: core/ui/model/tables/boxes: Remove trailing commas.
neiljp May 1, 2021
26081f3
refactor: run: Add trailing commas to argparse entries for consistency.
neiljp May 22, 2021
5487ac6
refactor: core/ui/boxes/run: Simplify implicitly joined strings.
neiljp May 21, 2021
937a82f
refactor: model/run/boxes/helper: Remove unnecessary parentheses.
neiljp May 22, 2021
6ee42d0
refactor: buttons/boxes: Reformat to maintain comment locations.
neiljp May 22, 2021
bc0aa3a
refactor: keys/themes: Disable reformatting for config datastructures.
neiljp May 31, 2021
579bf27
unicode_emojis: Update list of unicode emojis from server using script.
neiljp May 31, 2021
10a9605
unicode_emojis/tools: Update & run tool to make black-compatible data.
neiljp May 31, 2021
7cd6d00
linting/requirements: Add linting dependency on black & configuration.
neiljp May 23, 2021
89a1925
linting: Stop enforcing E203 via flake8, which conflicts with black.
neiljp May 23, 2021
7321281
refactor: Reformat zulipterminal/ and tests/ with black, except quotes.
neiljp May 31, 2021
17c9959
refactor: Reformat zulipterminal/ and tests/ with black (incl quotes).
neiljp May 31, 2021
49e1d40
makefile/lint-all: Add support for linting & fixing formatting by black.
neiljp May 26, 2021
b8e910d
lint-and-test: Check conformance with black in CI.
neiljp May 26, 2021
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
102 changes: 45 additions & 57 deletions tests/model/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,18 @@
)


CONTROLLER = 'zulipterminal.core.Controller'
MODEL = 'zulipterminal.model.Model'
Copy link
Member

@Ezio-Sarthak Ezio-Sarthak May 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just wanted to point that I had a PR #959 that does a general simplification of patches in all tests. Let me know if I should make it review-ready if we wish to merge it first.

EDIT: I just updated that PR after rebasing and resolving conflicts.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Ezio-Sarthak Thanks for reminding me of your PR 👍

I welcome your intent to add more constants to improve readability; the main challenge with using these is that we don't currently have much of a "standard" way of applying these constants - or it seems that way, but isn't quite. In some pre-existing cases we have files referred to, others to classes, and I think this is where we would benefit from standardizing and clarying. You'll note that in my commit I only extracted the classes, since they were both the longest - so more likely to benefit from compacting the long lines - but also that it made clear that others were module-specific, either imports or not in the main class.



class TestModel:
@pytest.fixture(autouse=True)
def mock_external_classes(self, mocker: Any) -> None:
self.urlparse = mocker.patch('urllib.parse.urlparse')
self.controller = mocker.patch('zulipterminal.core.'
'Controller',
return_value=None)
self.client = mocker.patch('zulipterminal.core.'
'Controller.client')
self.controller = mocker.patch(CONTROLLER, return_value=None)
self.client = mocker.patch(CONTROLLER + '.client')
self.client.base_url = 'chat.zulip.zulip'
mocker.patch('zulipterminal.model.Model._start_presence_updates')
mocker.patch(MODEL + '._start_presence_updates')
self.display_error_if_present = mocker.patch(
'zulipterminal.model.display_error_if_present')
self.notify_if_message_sent_outside_narrow = mocker.patch(
Expand All @@ -35,11 +36,9 @@ def mock_external_classes(self, mocker: Any) -> None:
@pytest.fixture
def model(self, mocker, initial_data, user_profile,
unicode_emojis):
mocker.patch('zulipterminal.model.Model.get_messages',
return_value='')
mocker.patch(MODEL + '.get_messages', return_value='')
self.client.register.return_value = initial_data
mocker.patch('zulipterminal.model.Model.get_all_users',
return_value=[])
mocker.patch(MODEL + '.get_all_users', return_value=[])
# NOTE: PATCH WHERE USED NOT WHERE DEFINED
self.classify_unread_counts = mocker.patch(
'zulipterminal.model.classify_unread_counts',
Expand Down Expand Up @@ -110,7 +109,7 @@ def test_init(self, model, initial_data, user_profile,
)
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='')
mocker.patch(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)
Expand All @@ -121,14 +120,12 @@ def test_init_muted_topics(self, mocker, initial_data, server_response,

def test_init_InvalidAPIKey_response(self, mocker, initial_data):
# Both network calls indicate the same response
mocker.patch('zulipterminal.model.Model.get_messages',
return_value='Invalid API key')
mocker.patch('zulipterminal.model.Model._register_desired_events',
mocker.patch(MODEL + '.get_messages', return_value='Invalid API key')
mocker.patch(MODEL + '._register_desired_events',
return_value='Invalid API key')

mocker.patch('zulipterminal.model.Model.get_all_users',
return_value=[])
mocker.patch('zulipterminal.model.Model._subscribe_to_streams')
mocker.patch(MODEL + '.get_all_users', return_value=[])
mocker.patch(MODEL + '._subscribe_to_streams')
self.classify_unread_counts = mocker.patch(
'zulipterminal.model.classify_unread_counts',
return_value=[])
Expand All @@ -141,14 +138,12 @@ def test_init_InvalidAPIKey_response(self, mocker, initial_data):
def test_init_ZulipError_exception(self, mocker, initial_data,
exception_text="X"):
# Both network calls fail, resulting in exceptions
mocker.patch('zulipterminal.model.Model.get_messages',
side_effect=ZulipError(exception_text))
mocker.patch('zulipterminal.model.Model._register_desired_events',
mocker.patch(MODEL + '.get_messages', side_effect=ZulipError(exception_text))
mocker.patch(MODEL + '._register_desired_events',
side_effect=ZulipError(exception_text))

mocker.patch('zulipterminal.model.Model.get_all_users',
return_value=[])
mocker.patch('zulipterminal.model.Model._subscribe_to_streams')
mocker.patch(MODEL + '.get_all_users', return_value=[])
mocker.patch(MODEL + '._subscribe_to_streams')
self.classify_unread_counts = mocker.patch(
'zulipterminal.model.classify_unread_counts',
return_value=[])
Expand All @@ -159,9 +154,8 @@ def test_init_ZulipError_exception(self, mocker, initial_data,
assert str(e.value) == exception_text + ' (get_messages, register)'

def test_register_initial_desired_events(self, mocker, initial_data):
mocker.patch('zulipterminal.model.Model.get_messages',
return_value='')
mocker.patch('zulipterminal.model.Model.get_all_users')
mocker.patch(MODEL + '.get_messages', return_value='')
mocker.patch(MODEL + '.get_all_users')
self.client.register.return_value = initial_data

model = Model(self.controller)
Expand Down Expand Up @@ -441,8 +435,7 @@ def test_react_to_message_for_not_thumbs_up(self, model):
def test_send_typing_status_by_user_ids(self, mocker, model, status,
recipient_user_ids):
response = mocker.Mock()
mock_api_query = mocker.patch('zulipterminal.core.Controller'
'.client.set_typing_status',
mock_api_query = mocker.patch(CONTROLLER + '.client.set_typing_status',
return_value=response)

model.send_typing_status_by_user_ids(recipient_user_ids,
Expand Down Expand Up @@ -577,9 +570,8 @@ def test_success_get_messages(self, mocker, messages_successful_response,
index_all_messages, initial_data,
num_before=30, num_after=10):
self.client.register.return_value = initial_data
mocker.patch('zulipterminal.model.Model.get_all_users',
return_value=[])
mocker.patch('zulipterminal.model.Model._subscribe_to_streams')
mocker.patch(MODEL + '.get_all_users', return_value=[])
mocker.patch(MODEL + '._subscribe_to_streams')
self.classify_unread_counts = mocker.patch(
'zulipterminal.model.classify_unread_counts',
return_value=[])
Expand Down Expand Up @@ -677,9 +669,8 @@ def test_get_message_false_first_anchor(

# Initialize Model
self.client.register.return_value = initial_data
mocker.patch('zulipterminal.model.Model.get_all_users',
return_value=[])
mocker.patch('zulipterminal.model.Model._subscribe_to_streams')
mocker.patch(MODEL + '.get_all_users', return_value=[])
mocker.patch(MODEL + '._subscribe_to_streams')
self.classify_unread_counts = mocker.patch(
'zulipterminal.model.classify_unread_counts',
return_value=[])
Expand Down Expand Up @@ -709,9 +700,8 @@ def test_fail_get_messages(self, mocker, error_response,
initial_data, num_before=30, num_after=10):
# Initialize Model
self.client.register.return_value = initial_data
mocker.patch('zulipterminal.model.Model.get_all_users',
return_value=[])
mocker.patch('zulipterminal.model.Model._subscribe_to_streams')
mocker.patch(MODEL + '.get_all_users', return_value=[])
mocker.patch(MODEL + '._subscribe_to_streams')
self.classify_unread_counts = mocker.patch(
'zulipterminal.model.classify_unread_counts',
return_value=[])
Expand Down Expand Up @@ -774,8 +764,7 @@ def test_toggle_message_star_status(self, mocker, model, flags_before,
self.controller)

def test_mark_message_ids_as_read(self, model, mocker: Any) -> None:
mock_api_query = mocker.patch('zulipterminal.core.Controller'
'.client.update_message_flags')
mock_api_query = mocker.patch(CONTROLLER + '.client.update_message_flags')

model.mark_message_ids_as_read([1, 2])

Expand All @@ -791,10 +780,9 @@ def test__update_initial_data(self, model, initial_data):

def test__update_initial_data_raises_exception(self, mocker, initial_data):
# Initialize Model
mocker.patch('zulipterminal.model.Model.get_messages', return_value='')
mocker.patch('zulipterminal.model.Model.get_all_users',
return_value=[])
mocker.patch('zulipterminal.model.Model._subscribe_to_streams')
mocker.patch(MODEL + '.get_messages', return_value='')
mocker.patch(MODEL + '.get_all_users', return_value=[])
mocker.patch(MODEL + '._subscribe_to_streams')
self.classify_unread_counts = mocker.patch(
'zulipterminal.model.classify_unread_counts',
return_value=[])
Expand Down Expand Up @@ -824,9 +812,9 @@ def test__group_info_from_realm_user_groups(self, model,

def test_get_all_users(self, mocker, initial_data, user_list, user_dict,
user_id):
mocker.patch('zulipterminal.model.Model.get_messages', return_value='')
mocker.patch(MODEL + '.get_messages', return_value='')
self.client.register.return_value = initial_data
mocker.patch('zulipterminal.model.Model._subscribe_to_streams')
mocker.patch(MODEL + '._subscribe_to_streams')
self.classify_unread_counts = mocker.patch(
'zulipterminal.model.classify_unread_counts',
return_value=[])
Expand All @@ -851,7 +839,7 @@ def test__subscribe_to_streams(self, initial_data, muted, model):
def test__handle_message_event_with_Falsey_log(self, mocker,
model, message_fixture):
model._have_last_message[repr([])] = True
mocker.patch('zulipterminal.model.Model._update_topic_index')
mocker.patch(MODEL + '._update_topic_index')
index_msg = mocker.patch('zulipterminal.model.index_messages',
return_value={})
self.controller.view.message_view = mocker.Mock(log=[])
Expand All @@ -872,7 +860,7 @@ def test__handle_message_event_with_Falsey_log(self, mocker,
def test__handle_message_event_with_valid_log(self, mocker,
model, message_fixture):
model._have_last_message[repr([])] = True
mocker.patch('zulipterminal.model.Model._update_topic_index')
mocker.patch(MODEL + '._update_topic_index')
index_msg = mocker.patch('zulipterminal.model.index_messages',
return_value={})
self.controller.view.message_view = mocker.Mock(log=[mocker.Mock()])
Expand All @@ -896,7 +884,7 @@ def test__handle_message_event_with_valid_log(self, mocker,
def test__handle_message_event_with_flags(self, mocker,
model, message_fixture):
model._have_last_message[repr([])] = True
mocker.patch('zulipterminal.model.Model._update_topic_index')
mocker.patch(MODEL + '._update_topic_index')
index_msg = mocker.patch('zulipterminal.model.index_messages',
return_value={})
self.controller.view.message_view = mocker.Mock(log=[mocker.Mock()])
Expand Down Expand Up @@ -972,7 +960,7 @@ def test__handle_message_event_with_flags(self, mocker,
def test__handle_message_event(self, mocker, user_profile, response,
narrow, recipients, model, log):
model._have_last_message[repr(narrow)] = True
mocker.patch('zulipterminal.model.Model._update_topic_index')
mocker.patch(MODEL + '._update_topic_index')
index_msg = mocker.patch('zulipterminal.model.index_messages',
return_value={})
create_msg_box_list = mocker.patch('zulipterminal.model.'
Expand Down Expand Up @@ -1305,12 +1293,12 @@ def test__handle_update_message_event(self, mocker, model,
'edited_messages': set(),
'topics': {10: ['old subject']},
}
mocker.patch('zulipterminal.model.Model._update_rendered_view')
mocker.patch(MODEL + '._update_rendered_view')

def _set_topics_to_old_and_new(event):
model.index['topics'][10] = ['new subject', 'old subject']
fetch_topics = mocker.patch(
'zulipterminal.model.Model._fetch_topics_in_streams',
MODEL + '._fetch_topics_in_streams',
side_effect=_set_topics_to_old_and_new)

(model.controller.view.left_panel.is_in_topic_view_with_stream_id.
Expand Down Expand Up @@ -1520,7 +1508,7 @@ def test_update_star_status_no_index(self, mocker, model,
'all': False,
operation: 'add',
}
mocker.patch('zulipterminal.model.Model._update_rendered_view')
mocker.patch(MODEL + '._update_rendered_view')
set_count = mocker.patch('zulipterminal.model.set_count')

model._handle_update_message_flags_event(event)
Expand All @@ -1543,7 +1531,7 @@ def test_update_star_status_invalid_operation(
operation: 'OTHER', # not 'add' or 'remove'
'all': False,
}
mocker.patch('zulipterminal.model.Model._update_rendered_view')
mocker.patch(MODEL + '._update_rendered_view')
set_count = mocker.patch('zulipterminal.model.set_count')
with pytest.raises(RuntimeError):
model._handle_update_message_flags_event(event)
Expand Down Expand Up @@ -1589,7 +1577,7 @@ def test_update_star_status(self, mocker, model, event_op,
'all': False,
}
self.controller.view.starred_button.count = 0
mocker.patch('zulipterminal.model.Model._update_rendered_view')
mocker.patch(MODEL + '._update_rendered_view')
set_count = mocker.patch('zulipterminal.model.set_count')
update_star_count = self.controller.view.starred_button.update_count

Expand Down Expand Up @@ -1655,7 +1643,7 @@ def test_update_read_status(self, mocker, model, event_op,
'all': False,
}

mocker.patch('zulipterminal.model.Model._update_rendered_view')
mocker.patch(MODEL + '._update_rendered_view')
set_count = mocker.patch('zulipterminal.model.set_count')

model._handle_update_message_flags_event(event)
Expand Down Expand Up @@ -2219,7 +2207,7 @@ def raiser(*args):

def test_poll_for_events__no_disconnect(self, mocker, model,
raising_event):
mocker.patch("zulipterminal.model.Model._register_desired_events")
mocker.patch(MODEL + "._register_desired_events")
sleep = mocker.patch("zulipterminal.model.time.sleep")

self.client.get_events.side_effect = [
Expand All @@ -2243,7 +2231,7 @@ def test_poll_for_events__no_disconnect(self, mocker, model,
])
def test_poll_for_events__reconnect_ok(self, mocker, model, raising_event,
register_return_value):
mocker.patch("zulipterminal.model.Model._register_desired_events",
mocker.patch(MODEL + "._register_desired_events",
side_effect=register_return_value)
sleep = mocker.patch("zulipterminal.model.time.sleep")

Expand Down