Skip to content

Commit

Permalink
refactor: model: Rename Model.get_all_users to reflect functionality.
Browse files Browse the repository at this point in the history
This method does currently return some data, but also updates other data
as a side-effect, and is now only used internally to synchronize users
data from 'initial data' from API register() calls, or subsequent
updates to that data from events.

The revised name of _update_users_data_from_initial_data reflects these
factors.

Tests updated.
  • Loading branch information
neiljp committed Jul 14, 2023
1 parent a79451b commit cdceb62
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
20 changes: 11 additions & 9 deletions tests/model/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def mock_external_classes(self, mocker: Any) -> None:
def model(self, mocker, initial_data, user_profile, unicode_emojis):
mocker.patch(MODEL + ".get_messages", return_value="")
self.client.register.return_value = initial_data
mocker.patch(MODEL + ".get_all_users", return_value=[])
mocker.patch(MODEL + "._update_users_data_from_initial_data", return_value=[])
# NOTE: PATCH WHERE USED NOT WHERE DEFINED
self.classify_unread_counts = mocker.patch(
MODULE + ".classify_unread_counts", return_value=[]
Expand Down Expand Up @@ -85,7 +85,7 @@ def test_init(
assert model.user_email == user_profile["email"]
assert model.server_name == initial_data["realm_name"]
# FIXME Add test here for model.server_url
model.get_all_users.assert_called_once_with()
model._update_users_data_from_initial_data.assert_called_once_with()
assert model.users == []
self.classify_unread_counts.assert_called_once_with(model)
assert model.unread_counts == []
Expand Down Expand Up @@ -192,7 +192,7 @@ def test_init_InvalidAPIKey_response(self, mocker, initial_data):
MODEL + "._register_desired_events", return_value="Invalid API key"
)

mocker.patch(MODEL + ".get_all_users", return_value=[])
mocker.patch(MODEL + "._update_users_data_from_initial_data", return_value=[])
mocker.patch(MODEL + "._subscribe_to_streams")
self.classify_unread_counts = mocker.patch(
MODULE + ".classify_unread_counts", return_value=[]
Expand All @@ -210,7 +210,7 @@ def test_init_ZulipError_exception(self, mocker, initial_data, exception_text="X
MODEL + "._register_desired_events", side_effect=ZulipError(exception_text)
)

mocker.patch(MODEL + ".get_all_users", return_value=[])
mocker.patch(MODEL + "._update_users_data_from_initial_data", return_value=[])
mocker.patch(MODEL + "._subscribe_to_streams")
self.classify_unread_counts = mocker.patch(
MODULE + ".classify_unread_counts", return_value=[]
Expand All @@ -223,7 +223,7 @@ def test_init_ZulipError_exception(self, mocker, initial_data, exception_text="X

def test_register_initial_desired_events(self, mocker, initial_data):
mocker.patch(MODEL + ".get_messages", return_value="")
mocker.patch(MODEL + ".get_all_users")
mocker.patch(MODEL + "._update_users_data_from_initial_data")
self.client.register.return_value = initial_data

model = Model(self.controller)
Expand Down Expand Up @@ -1283,7 +1283,7 @@ def test_success_get_messages(
num_after=10,
):
self.client.register.return_value = initial_data
mocker.patch(MODEL + ".get_all_users", return_value=[])
mocker.patch(MODEL + "._update_users_data_from_initial_data", return_value=[])
mocker.patch(MODEL + "._subscribe_to_streams")
self.classify_unread_counts = mocker.patch(
MODULE + ".classify_unread_counts", return_value=[]
Expand Down Expand Up @@ -1406,7 +1406,7 @@ def test_get_message_false_first_anchor(

# Initialize Model
self.client.register.return_value = initial_data
mocker.patch(MODEL + ".get_all_users", return_value=[])
mocker.patch(MODEL + "._update_users_data_from_initial_data", return_value=[])
mocker.patch(MODEL + "._subscribe_to_streams")
self.classify_unread_counts = mocker.patch(
MODULE + ".classify_unread_counts", return_value=[]
Expand Down Expand Up @@ -1434,7 +1434,7 @@ def test_fail_get_messages(
):
# Initialize Model
self.client.register.return_value = initial_data
mocker.patch(MODEL + ".get_all_users", return_value=[])
mocker.patch(MODEL + "._update_users_data_from_initial_data", return_value=[])
mocker.patch(MODEL + "._subscribe_to_streams")
self.classify_unread_counts = mocker.patch(
MODULE + ".classify_unread_counts", return_value=[]
Expand Down Expand Up @@ -1687,7 +1687,9 @@ def test_get_user_info_sample_response(
model._all_users_by_id = _all_users_by_id
assert model.get_user_info(12) == tidied_user_info_response

def test_get_all_users(self, mocker, initial_data, user_list, user_dict, user_id):
def test__update_users_data_from_initial_data(
self, mocker, initial_data, user_list, user_dict, user_id
):
mocker.patch(MODEL + ".get_messages", return_value="")
self.client.register.return_value = initial_data
mocker.patch(MODEL + "._subscribe_to_streams")
Expand Down
6 changes: 3 additions & 3 deletions zulipterminal/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ def __init__(self, controller: Any) -> None:
self.user_id_email_dict: Dict[int, str] = {}
self._all_users_by_id: Dict[int, RealmUser] = {}
self._cross_realm_bots_by_id: Dict[int, RealmUser] = {}
self.users = self.get_all_users()
self.users = self._update_users_data_from_initial_data()

self.stream_dict: Dict[int, Any] = {}
self.muted_streams: Set[int] = set()
Expand Down Expand Up @@ -437,7 +437,7 @@ def _start_presence_updates(self) -> None:
response = self._notify_server_of_presence()
if response["result"] == "success":
self.initial_data["presences"] = response["presences"]
self.users = self.get_all_users()
self.users = self._update_users_data_from_initial_data()
if hasattr(self.controller, "view"):
view = self.controller.view
view.users_view.update_user_list(user_list=self.users)
Expand Down Expand Up @@ -1088,7 +1088,7 @@ def get_user_info(self, user_id: int) -> Optional[TidiedUserInfo]:

return user_info

def get_all_users(self) -> List[Dict[str, Any]]:
def _update_users_data_from_initial_data(self) -> List[Dict[str, Any]]:
# Dict which stores the active/idle status of users (by email)
presences = self.initial_data["presences"]

Expand Down

0 comments on commit cdceb62

Please sign in to comment.