Skip to content

Commit 7b641e4

Browse files
committed
model: boxes: refactor: Store emoji data in model.
Save the data from emoji_data.py as a model attribute, instead of loading them directly from the file. This will make it easy, in the future, to use emoji data when it is fetched from different sources, for example - custom emojis that will be fetched using a server endpoint. Tests amended.
1 parent 2913474 commit 7b641e4

File tree

4 files changed

+12
-6
lines changed

4 files changed

+12
-6
lines changed

tests/model/test_model.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ def mock_external_classes(self, mocker: Any) -> None:
2121
mocker.patch('zulipterminal.model.Model._start_presence_updates')
2222

2323
@pytest.fixture
24-
def model(self, mocker, initial_data, user_profile):
24+
def model(self, mocker, initial_data, user_profile,
25+
unicode_emojis):
2526
mocker.patch('zulipterminal.model.Model.get_messages',
2627
return_value='')
2728
self.client.register.return_value = initial_data
@@ -35,10 +36,13 @@ def model(self, mocker, initial_data, user_profile):
3536
'zulipterminal.model.classify_unread_counts',
3637
return_value=[])
3738
self.client.get_profile.return_value = user_profile
39+
mocker.patch('zulipterminal.model.emoji_data',
40+
EMOJI_DATA=unicode_emojis)
3841
model = Model(self.controller)
3942
return model
4043

41-
def test_init(self, model, initial_data, user_profile):
44+
def test_init(self, model, initial_data, user_profile,
45+
unicode_emojis):
4246
assert hasattr(model, 'controller')
4347
assert hasattr(model, 'client')
4448
assert model.msg_view is None
@@ -70,6 +74,7 @@ def test_init(self, model, initial_data, user_profile):
7074
assert model.unpinned_streams == []
7175
self.classify_unread_counts.assert_called_once_with(model)
7276
assert model.unread_counts == []
77+
assert model.emoji_data == unicode_emojis
7378

7479
def test_init_InvalidAPIKey_response(self, mocker, initial_data):
7580
# Both network calls indicate the same response

tests/ui_tools/test_boxes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ def mock_external_classes(self, mocker, initial_index):
1616
@pytest.fixture()
1717
def write_box(self, mocker, users_fixture, user_groups_fixture,
1818
streams_fixture, unicode_emojis):
19+
self.view.model.emoji_data = unicode_emojis
20+
1921
write_box = WriteBox(self.view)
2022
write_box.view.users = users_fixture
2123
write_box.model.user_group_names = [
@@ -26,8 +28,6 @@ def write_box(self, mocker, users_fixture, user_groups_fixture,
2628
[stream['name']] for stream in
2729
streams_fixture], key=lambda stream: stream[0].lower())
2830

29-
mocker.patch('zulipterminal.ui_tools.boxes.emoji_data',
30-
EMOJI_DATA=unicode_emojis)
3131
return write_box
3232

3333
def test_init(self, write_box):

zulipterminal/model.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import zulip
1212
from mypy_extensions import TypedDict
1313

14+
from zulipterminal import emoji_data
1415
from zulipterminal.config.keys import keys_for_command
1516
from zulipterminal.helper import (
1617
Message, asynch, canonicalize_color, classify_unread_counts,
@@ -120,6 +121,7 @@ def __init__(self, controller: Any) -> None:
120121
self.unread_counts = classify_unread_counts(self)
121122

122123
self.fetch_all_topics(workers=5)
124+
self.emoji_data = emoji_data.EMOJI_DATA
123125

124126
self.new_user_input = True
125127
self._start_presence_updates()

zulipterminal/ui_tools/boxes.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from bs4.element import NavigableString
1111
from urwid_readline import ReadlineEdit
1212

13-
from zulipterminal import emoji_data
1413
from zulipterminal.config.keys import is_command_key, keys_for_command
1514
from zulipterminal.helper import (
1615
Message, format_string, match_emoji, match_group, match_stream, match_user,
@@ -156,7 +155,7 @@ def autocomplete_streams(self, text: str, prefix_string: str
156155

157156
def autocomplete_emojis(self, text: str, prefix_string: str
158157
) -> Tuple[List[str], List[str]]:
159-
emoji_list = list(emoji_data.EMOJI_DATA.keys())
158+
emoji_list = list(self.model.emoji_data.keys())
160159
emojis = [emoji
161160
for emoji in emoji_list
162161
if match_emoji(emoji, text[1:])]

0 commit comments

Comments
 (0)