Skip to content

Commit

Permalink
tests: boxes: Update on receiving feedback. To be squashed.
Browse files Browse the repository at this point in the history
- Converted list of test cases `mocked_saved_drafts` into a fixture, and
removed the respective parametrize `draft_saved_in_current_session`
- Removed all usage of if-else
    - Split `test_keypress_SAVE_AS_DRAFT_private` into 2 tests based on
    validity of recipients, removing the parametrize
    - Parametrized the function to assert, adding them to the fixture
- Added 2 new test functions for when the contents match the saved draft
  • Loading branch information
Niloth-p committed Jun 14, 2024
1 parent 7981b32 commit 8e3b967
Showing 1 changed file with 201 additions and 57 deletions.
258 changes: 201 additions & 57 deletions tests/ui_tools/test_boxes.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import datetime
from collections import OrderedDict
from typing import Any, Callable, Dict, List, Optional
from functools import reduce
from typing import Any, Callable, Dict, List, Optional, Union

import pytest
from pytest import FixtureRequest
from pytest import param as case
from pytest_mock import MockerFixture
from urwid import Widget
Expand Down Expand Up @@ -106,42 +108,68 @@ def test_not_calling_typing_method_without_recipients(

assert not write_box.model.send_typing_status_by_user_ids.called

mocked_saved_drafts = [
case(None, id="no_saved_draft_exists"),
case(
StreamComposition(
type="stream",
to="Random Stream",
content="Random stream draft",
subject="Topic name",
read_by_sender=True,
),
id="saved_stream_draft_exists",
),
case(
PrivateComposition(
type="private",
to=[5140],
content="Random private draft",
read_by_sender=True,
),
id="saved_private_draft_exists",
),
]
@pytest.fixture(
params=[
{
"draft_composition": None,
"expected_function": "model.save_draft",
},
{
"draft_composition": StreamComposition(
type="stream",
to="Random Stream",
content="Random stream draft",
subject="Topic name",
read_by_sender=True,
),
"expected_function": "view.controller.save_draft_confirmation_popup",
},
{
"draft_composition": PrivateComposition(
type="private",
to=[5140],
content="Random private draft",
read_by_sender=True,
),
"expected_function": "view.controller.save_draft_confirmation_popup",
},
{
"draft_composition": PrivateComposition(
type="private",
to=[5140, 5179],
content="Random group draft",
read_by_sender=True,
),
"expected_function": "view.controller.save_draft_confirmation_popup",
},
],
ids=[
"no_saved_draft_exists",
"saved_stream_draft_exists",
"saved_private_draft_exists",
"saved_private_group_draft_exists",
],
)
def mocked_saved_drafts(
self, request: FixtureRequest
) -> Dict[str, Union[Optional[Composition], str]]:
return request.param

@pytest.mark.parametrize("key", keys_for_command("SAVE_AS_DRAFT"))
@pytest.mark.parametrize(
"draft_saved_in_current_session",
mocked_saved_drafts,
)
def test_keypress_SAVE_AS_DRAFT_stream(
self,
key: str,
mocker: MockerFixture,
draft_saved_in_current_session: Optional[Composition],
mocked_saved_drafts: Dict[str, Union[Optional[Composition], str]],
write_box: WriteBox,
widget_size: Callable[[Widget], urwid_Size],
) -> None:
draft_saved_in_current_session = mocked_saved_drafts["draft_composition"]
assert isinstance(mocked_saved_drafts["expected_function"], str)
expected_function = reduce(
getattr, mocked_saved_drafts["expected_function"].split("."), write_box
)

mocker.patch(MODULE + ".WriteBox.update_recipients")
write_box.model.session_draft_message.return_value = (
draft_saved_in_current_session
Expand All @@ -164,13 +192,8 @@ def test_keypress_SAVE_AS_DRAFT_stream(
size = widget_size(write_box)
write_box.keypress(size, key)

assert write_box.model.session_draft_message.called
if draft_saved_in_current_session is not None:
write_box.view.controller.save_draft_confirmation_popup.assert_called_once_with(
draft_composition
)
else:
write_box.model.save_draft.assert_called_once_with(draft_composition)
write_box.model.session_draft_message.assert_called()
expected_function.assert_called_once_with(draft_composition)

@pytest.mark.parametrize("key", keys_for_command("SAVE_AS_DRAFT"))
@pytest.mark.parametrize(
Expand All @@ -196,31 +219,81 @@ def test_keypress_SAVE_AS_DRAFT_stream(
),
],
)
def test_keypress_SAVE_AS_DRAFT_private__valid_recipients(
self,
key: str,
mocker: MockerFixture,
draft_composition: PrivateComposition,
mocked_saved_drafts: Dict[str, Union[Optional[Composition], str]],
write_box: WriteBox,
widget_size: Callable[[Widget], urwid_Size],
) -> None:
draft_saved_in_current_session = mocked_saved_drafts["draft_composition"]
assert isinstance(mocked_saved_drafts["expected_function"], str)
expected_function = reduce(
getattr, mocked_saved_drafts["expected_function"].split("."), write_box
)

mocker.patch(MODULE + ".WriteBox.update_recipients")
write_box.msg_write_box = mocker.Mock(edit_text=draft_composition["content"])
write_box.to_write_box = mocker.Mock()
mocker.patch(
MODULE + ".WriteBox._tidy_valid_recipients_and_notify_invalid_ones",
return_value=True,
)

write_box.model.session_draft_message.return_value = (
draft_saved_in_current_session
)
write_box.compose_box_status = "open_with_private"
write_box.recipient_user_ids = draft_composition["to"]

size = widget_size(write_box)
write_box.keypress(size, key)

write_box.model.session_draft_message.assert_called()
expected_function.assert_called_once_with(draft_composition)

@pytest.mark.parametrize("key", keys_for_command("SAVE_AS_DRAFT"))
@pytest.mark.parametrize(
"is_every_recipient_valid",
[True, False],
ids=["valid_recipients", "invalid_recipients"],
)
@pytest.mark.parametrize(
"draft_saved_in_current_session",
mocked_saved_drafts,
"draft_composition",
[
case(
{
"type": "private",
"to": [5140],
"content": "Random message",
"read_by_sender": True,
},
id="dm",
),
case(
{
"type": "private",
"to": [5140, 5179],
"content": "Random DM group message",
"read_by_sender": True,
},
id="dm_group",
),
],
)
def test_keypress_SAVE_AS_DRAFT_private(
def test_keypress_SAVE_AS_DRAFT_private__invalid_recipients(
self,
key: str,
mocker: MockerFixture,
draft_composition: PrivateComposition,
is_every_recipient_valid: bool,
draft_saved_in_current_session: Optional[Composition],
mocked_saved_drafts: Dict[str, Union[Optional[Composition], str]],
write_box: WriteBox,
widget_size: Callable[[Widget], urwid_Size],
) -> None:
draft_saved_in_current_session = mocked_saved_drafts["draft_composition"]
mocker.patch(MODULE + ".WriteBox.update_recipients")
write_box.msg_write_box = mocker.Mock(edit_text=draft_composition["content"])
write_box.to_write_box = mocker.Mock()
mocker.patch(
MODULE + ".WriteBox._tidy_valid_recipients_and_notify_invalid_ones",
return_value=is_every_recipient_valid,
return_value=False,
)

write_box.model.session_draft_message.return_value = (
Expand All @@ -232,17 +305,88 @@ def test_keypress_SAVE_AS_DRAFT_private(
size = widget_size(write_box)
write_box.keypress(size, key)

if not is_every_recipient_valid:
write_box.model.save_draft.assert_not_called()
write_box.view.controller.save_draft_confirmation_popup.assert_not_called()
else:
assert write_box.model.session_draft_message.called
if draft_saved_in_current_session:
write_box.view.controller.save_draft_confirmation_popup.assert_called_once_with(
draft_composition
)
else:
write_box.model.save_draft.assert_called_once_with(draft_composition)
write_box.model.save_draft.assert_not_called()
write_box.view.controller.save_draft_confirmation_popup.assert_not_called()

@pytest.mark.parametrize("key", keys_for_command("SAVE_AS_DRAFT"))
def test_keypress_SAVE_AS_DRAFT_stream__already_saved(
self,
key: str,
mocker: MockerFixture,
write_box: WriteBox,
widget_size: Callable[[Widget], urwid_Size],
) -> None:
draft_composition = StreamComposition(
type="stream",
to="Random Stream",
content="Random stream draft",
subject="Topic name",
read_by_sender=True,
)
write_box.msg_write_box = mocker.Mock(edit_text=draft_composition["content"])
write_box.stream_write_box = mocker.Mock(edit_text=draft_composition["to"])
write_box.title_write_box = mocker.Mock(edit_text=draft_composition["subject"])

write_box.model.session_draft_message.return_value = draft_composition
mocker.patch(MODULE + ".WriteBox.update_recipients")
write_box.compose_box_status = "open_with_stream"
write_box.stream_id = 1

size = widget_size(write_box)
write_box.keypress(size, key)

write_box.model.session_draft_message.assert_called()
write_box.view.controller.save_draft_confirmation_popup.assert_not_called()

@pytest.mark.parametrize("key", keys_for_command("SAVE_AS_DRAFT"))
@pytest.mark.parametrize(
"draft_composition",
[
case(
{
"type": "private",
"to": [5140],
"content": "Random message",
"read_by_sender": True,
},
id="dm",
),
case(
{
"type": "private",
"to": [5140, 5179],
"content": "Random DM group message",
"read_by_sender": True,
},
id="dm_group",
),
],
)
def test_keypress_SAVE_AS_DRAFT_private__same_as_saved_draft(
self,
key: str,
mocker: MockerFixture,
draft_composition: PrivateComposition,
write_box: WriteBox,
widget_size: Callable[[Widget], urwid_Size],
) -> None:
mocker.patch(MODULE + ".WriteBox.update_recipients")
write_box.msg_write_box = mocker.Mock(edit_text=draft_composition["content"])
write_box.to_write_box = mocker.Mock()
mocker.patch(
MODULE + ".WriteBox._tidy_valid_recipients_and_notify_invalid_ones",
return_value=True,
)

write_box.model.session_draft_message.return_value = draft_composition
write_box.compose_box_status = "open_with_private"
write_box.recipient_user_ids = draft_composition["to"]

size = widget_size(write_box)
write_box.keypress(size, key)

write_box.model.session_draft_message.assert_called()
write_box.view.controller.save_draft_confirmation_popup.assert_not_called()

@pytest.mark.parametrize(
"text, state, is_valid_stream, required_typeahead",
Expand Down

0 comments on commit 8e3b967

Please sign in to comment.