Skip to content

Commit 5a78f8d

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 9ca2802 commit 5a78f8d

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
@@ -22,7 +22,8 @@ def mock_external_classes(self, mocker: Any) -> None:
2222
mocker.patch('zulipterminal.model.Model._start_presence_updates')
2323

2424
@pytest.fixture
25-
def model(self, mocker, initial_data, user_profile):
25+
def model(self, mocker, initial_data, user_profile,
26+
unicode_emojis):
2627
mocker.patch('zulipterminal.model.Model.get_messages',
2728
return_value='')
2829
self.client.register.return_value = initial_data
@@ -36,10 +37,13 @@ def model(self, mocker, initial_data, user_profile):
3637
'zulipterminal.model.classify_unread_counts',
3738
return_value=[])
3839
self.client.get_profile.return_value = user_profile
40+
mocker.patch('zulipterminal.model.emoji_data',
41+
EMOJI_DATA=unicode_emojis)
3942
model = Model(self.controller)
4043
return model
4144

42-
def test_init(self, model, initial_data, user_profile):
45+
def test_init(self, model, initial_data, user_profile,
46+
unicode_emojis):
4347
assert hasattr(model, 'controller')
4448
assert hasattr(model, 'client')
4549
assert model.narrow == []
@@ -69,6 +73,7 @@ def test_init(self, model, initial_data, user_profile):
6973
assert model.unpinned_streams == []
7074
self.classify_unread_counts.assert_called_once_with(model)
7175
assert model.unread_counts == []
76+
assert model.emoji_data == unicode_emojis
7277

7378
def test_init_InvalidAPIKey_response(self, mocker, initial_data):
7479
# 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,
@@ -125,6 +126,7 @@ def __init__(self, controller: Any) -> None:
125126
self.unread_counts = classify_unread_counts(self)
126127

127128
self.fetch_all_topics(workers=5)
129+
self.emoji_data = emoji_data.EMOJI_DATA
128130

129131
self.new_user_input = True
130132
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.config.symbols import (
1615
MESSAGE_CONTENT_MARKER, MESSAGE_HEADER_DIVIDER, QUOTED_TEXT_MARKER,
@@ -180,7 +179,7 @@ def autocomplete_streams(self, text: str, prefix_string: str
180179

181180
def autocomplete_emojis(self, text: str, prefix_string: str
182181
) -> Tuple[List[str], List[str]]:
183-
emoji_list = list(emoji_data.EMOJI_DATA.keys())
182+
emoji_list = list(self.model.emoji_data.keys())
184183
emojis = [emoji
185184
for emoji in emoji_list
186185
if match_emoji(emoji, text[1:])]

0 commit comments

Comments
 (0)