Skip to content

Commit 2254409

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. c
1 parent b2ba590 commit 2254409

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 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,
@@ -812,6 +817,90 @@ def test_keypress_exit_popup(
812817
assert self.controller.exit_popup.called
813818

814819

820+
class TestFileUploadView:
821+
@pytest.fixture(scope="class")
822+
def write_box(self) -> Any:
823+
return TestWriteBox().write_box
824+
825+
@pytest.fixture(autouse=True)
826+
def mock_external_classes(self, mocker: MockerFixture, write_box: WriteBox) -> None:
827+
self.controller = mocker.Mock()
828+
mocker.patch.object(
829+
self.controller, "maximum_popup_dimensions", return_value=(64, 64)
830+
)
831+
mocker.patch(LISTWALKER, return_value=[])
832+
self.file_upload_view = FileUploadView(
833+
self.controller, write_box, "Upload File"
834+
)
835+
836+
def test_keypress_any_key(
837+
self, widget_size: Callable[[Widget], urwid_Size]
838+
) -> None:
839+
key = "a"
840+
size = widget_size(self.file_upload_view)
841+
self.file_upload_view.keypress(size, key)
842+
assert not self.controller.exit_popup.called
843+
844+
@pytest.mark.parametrize("key", {*keys_for_command("GO_BACK")})
845+
def test_keypress_exit_popup(
846+
self, key: str, widget_size: Callable[[Widget], urwid_Size]
847+
) -> None:
848+
size = widget_size(self.file_upload_view)
849+
self.file_upload_view.keypress(size, key)
850+
assert self.controller.exit_popup.called
851+
852+
@pytest.mark.parametrize(
853+
"file_location, expected_uri, expected_error_message",
854+
[
855+
case(
856+
"example.txt",
857+
"http://example.txt/uploaded_file",
858+
None,
859+
id="txt_file_with_successful_uri",
860+
),
861+
case(
862+
"example.pdf",
863+
"http://example.pdf/uploaded_file",
864+
None,
865+
id="pdf_file_with_unsuccessful_uri",
866+
),
867+
case(
868+
"invalid.txt",
869+
"",
870+
["ERROR: Unable to get the URI"],
871+
id="invalid_txt_file_with_error",
872+
),
873+
case(
874+
"invalid.pdf",
875+
"",
876+
["ERROR: Unable to get the URI"],
877+
id="invalid_pdf_file_with_error",
878+
),
879+
],
880+
)
881+
def test_handle_file_upload(
882+
self,
883+
file_location: str,
884+
expected_uri: str,
885+
expected_error_message: Optional[str],
886+
) -> None:
887+
self.file_upload_view.write_box = MagicMock()
888+
self.controller.model.get_file_upload_uri.return_value = expected_uri
889+
890+
self.file_upload_view._handle_file_upload(file_location)
891+
892+
self.controller.model.get_file_upload_uri.assert_called_once_with(file_location)
893+
if expected_uri is not None:
894+
file_name = Path(file_location).name
895+
self.file_upload_view.write_box.append_uri_and_filename.assert_called_once_with(
896+
file_name, self.file_upload_view.uri
897+
)
898+
else:
899+
self.controller.append_uri_and_filename.assert_not_called()
900+
self.controller.report_error.assert_called_with(expected_error_message)
901+
self.controller.exit_popup.assert_called()
902+
903+
815904
class TestHelpView:
816905
@pytest.fixture(autouse=True)
817906
def mock_external_classes(self, mocker: MockerFixture) -> None:

0 commit comments

Comments
 (0)