Skip to content

Commit 5002e8d

Browse files
test_popups: Tests for FileUploadView.
Firstly the TestWriteBox is imported as unlike other popups FileUploadView takes a argument write_box. A fixture write_box is created which creates a instance of TestWriteBox and returns its write_box fixture. There are two cases of keypress test- one for keys which dosent make the popup exit and the other for key that exits the popup. There are 4 testcases for _handle_file_upload. The tests are based on if the return of get_file_upload_uri is None or a valid uri. On that basis the errors/append_func are called or not is asserted.
1 parent b9a618d commit 5002e8d

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

tests/ui_tools/test_popups.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
11
from collections import OrderedDict
2+
from pathlib import Path
23
from typing import Any, Callable, Dict, List, Optional, Tuple
4+
from unittest.mock import MagicMock
35

46
import pytest
57
from pytest import param as case
68
from pytest_mock import MockerFixture
79
from urwid import Columns, Pile, Text, Widget
810

11+
from tests.ui_tools.test_boxes import TestWriteBox
912
from zulipterminal.api_types import Message
1013
from zulipterminal.config.keys import is_command_key, keys_for_command
1114
from zulipterminal.config.ui_mappings import EDIT_MODE_CAPTIONS
1215
from zulipterminal.helper import CustomProfileData, TidiedUserInfo
16+
from zulipterminal.ui_tools.boxes import WriteBox
1317
from zulipterminal.ui_tools.messages import MessageBox
1418
from zulipterminal.ui_tools.views import (
1519
AboutView,
1620
EditHistoryTag,
1721
EditHistoryView,
1822
EditModeView,
1923
EmojiPickerView,
24+
FileUploadView,
2025
FullRawMsgView,
2126
FullRenderedMsgView,
2227
HelpView,
@@ -872,6 +877,90 @@ def test_keypress_exit_popup(
872877
assert self.controller.exit_popup.called
873878

874879

880+
class TestFileUploadView:
881+
@pytest.fixture(scope="class")
882+
def write_box(self) -> Any:
883+
return TestWriteBox().write_box
884+
885+
@pytest.fixture(autouse=True)
886+
def mock_external_classes(self, mocker: MockerFixture, write_box: WriteBox) -> None:
887+
self.controller = mocker.Mock()
888+
mocker.patch.object(
889+
self.controller, "maximum_popup_dimensions", return_value=(64, 64)
890+
)
891+
mocker.patch(LISTWALKER, return_value=[])
892+
self.file_upload_view = FileUploadView(
893+
self.controller, write_box, "Upload File"
894+
)
895+
896+
def test_keypress_any_key(
897+
self, widget_size: Callable[[Widget], urwid_Size]
898+
) -> None:
899+
key = "a"
900+
size = widget_size(self.file_upload_view)
901+
self.file_upload_view.keypress(size, key)
902+
assert not self.controller.exit_popup.called
903+
904+
@pytest.mark.parametrize("key", {*keys_for_command("GO_BACK")})
905+
def test_keypress_exit_popup(
906+
self, key: str, widget_size: Callable[[Widget], urwid_Size]
907+
) -> None:
908+
size = widget_size(self.file_upload_view)
909+
self.file_upload_view.keypress(size, key)
910+
assert self.controller.exit_popup.called
911+
912+
@pytest.mark.parametrize(
913+
"file_location, expected_uri, expected_error_message",
914+
[
915+
case(
916+
"example.txt",
917+
"http://example.txt/uploaded_file",
918+
None,
919+
id="txt_file_with_successful_uri",
920+
),
921+
case(
922+
"example.pdf",
923+
"http://example.pdf/uploaded_file",
924+
None,
925+
id="pdf_file_with_successful_uri",
926+
),
927+
case(
928+
"invalid.txt",
929+
"",
930+
["ERROR: Unable to get the URI"],
931+
id="invalid_txt_file_with_error",
932+
),
933+
case(
934+
"invalid.pdf",
935+
"",
936+
["ERROR: Unable to get the URI"],
937+
id="invalid_pdf_file_with_error",
938+
),
939+
],
940+
)
941+
def test_handle_file_upload(
942+
self,
943+
file_location: str,
944+
expected_uri: str,
945+
expected_error_message: Optional[str],
946+
) -> None:
947+
self.file_upload_view.write_box = MagicMock()
948+
self.controller.model.get_file_upload_uri.return_value = expected_uri
949+
950+
self.file_upload_view._handle_file_upload(file_location)
951+
952+
self.controller.model.get_file_upload_uri.assert_called_once_with(file_location)
953+
if not expected_error_message:
954+
file_name = Path(file_location).name
955+
self.file_upload_view.write_box.append_uri_and_filename.assert_called_once_with(
956+
file_name, self.file_upload_view.uri
957+
)
958+
else:
959+
self.controller.append_uri_and_filename.assert_not_called()
960+
self.controller.report_error.assert_called_with(expected_error_message)
961+
self.controller.exit_popup.assert_called()
962+
963+
875964
class TestHelpView:
876965
@pytest.fixture(autouse=True)
877966
def mock_external_classes(self, mocker: MockerFixture) -> None:

0 commit comments

Comments
 (0)