Skip to content

Commit

Permalink
model: Improve typing of stream_dict.
Browse files Browse the repository at this point in the history
With the revamp of the Subscription TypedDict in an earlier commit, we
use it to improve the typing of stream_dict in this commit.

Tests and fixtures updated to support the new typing.
  • Loading branch information
theViz343 committed Aug 26, 2023
1 parent 27ecc64 commit 1ced8ec
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 26 deletions.
46 changes: 33 additions & 13 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
CustomProfileField,
Message,
MessageType,
Subscription,
)
from zulipterminal.config.keys import (
ZT_TO_URWID_CMD_MAPPING,
Expand Down Expand Up @@ -231,7 +232,7 @@ def logged_on_user() -> Dict[str, Any]:


@pytest.fixture
def general_stream() -> Dict[str, Any]:
def general_stream() -> Subscription:
return {
"name": "Some general stream",
"date_created": 1472091253,
Expand All @@ -243,21 +244,26 @@ def general_stream() -> Dict[str, Any]:
"audible_notifications": False,
"description": "General Stream",
"rendered_description": "General Stream",
"is_old_stream": True,
"desktop_notifications": False,
"stream_weekly_traffic": 0,
"push_notifications": False,
"email_address": "general@example.comm",
"message_retention_days": 10,
"subscribers": [1001, 11, 12],
"history_public_to_subscribers": True,
"is_announcement_only": False,
"stream_post_policy": 1,
"first_message_id": 1,
"email_notifications": False,
"wildcard_mentions_notify": False,
"is_web_public": False,
}


# This is a private stream;
# only description/stream_id/invite_only/name/color vary from above
@pytest.fixture
def secret_stream() -> Dict[str, Any]:
def secret_stream() -> Subscription:
return {
"description": "Some private stream",
"stream_id": 99,
Expand All @@ -270,19 +276,24 @@ def secret_stream() -> Dict[str, Any]:
"color": "#ccc", # Color in '#xxx' format
"is_muted": False,
"audible_notifications": False,
"is_old_stream": True,
"desktop_notifications": False,
"stream_weekly_traffic": 0,
"message_retention_days": -1,
"push_notifications": False,
"subscribers": [1001, 11],
"history_public_to_subscribers": False,
"is_announcement_only": False,
"stream_post_policy": 1,
"first_message_id": 1,
"email_notifications": False,
"wildcard_mentions_notify": False,
"is_web_public": False,
}


# Like public stream but with is_web_public=True
@pytest.fixture
def web_public_stream() -> Dict[str, Any]:
def web_public_stream() -> Subscription:
return {
"description": "Some web public stream",
"stream_id": 999,
Expand All @@ -295,23 +306,27 @@ def web_public_stream() -> Dict[str, Any]:
"color": "#ddd", # Color in '#xxx' format
"is_muted": False,
"audible_notifications": False,
"is_old_stream": True,
"desktop_notifications": False,
"stream_weekly_traffic": 0,
"message_retention_days": -1,
"push_notifications": False,
"subscribers": [1001, 11],
"history_public_to_subscribers": False,
"is_web_public": True,
"is_announcement_only": False,
"stream_post_policy": 1,
"first_message_id": 1,
"email_notifications": False,
"wildcard_mentions_notify": False,
}


@pytest.fixture
def streams_fixture(
general_stream: Dict[str, Any],
secret_stream: Dict[str, Any],
web_public_stream: Dict[str, Any],
) -> List[Dict[str, Any]]:
general_stream: Subscription,
secret_stream: Subscription,
web_public_stream: Subscription,
) -> List[Subscription]:
streams = [general_stream, secret_stream, web_public_stream]
for i in range(1, 3):
streams.append(
Expand All @@ -326,14 +341,19 @@ def streams_fixture(
"audible_notifications": False,
"description": f"A description of stream {i}",
"rendered_description": f"A description of stream {i}",
"is_old_stream": True,
"desktop_notifications": False,
"stream_weekly_traffic": 0,
"push_notifications": False,
"message_retention_days": i + 30,
"email_address": f"stream{i}@example.com",
"subscribers": [1001, 11, 12],
"history_public_to_subscribers": True,
"is_announcement_only": False,
"stream_post_policy": 1,
"first_message_id": 1,
"email_notifications": False,
"wildcard_mentions_notify": False,
"is_web_public": False,
}
)
return deepcopy(streams)
Expand Down Expand Up @@ -872,7 +892,7 @@ def clean_custom_profile_data_fixture() -> List[CustomProfileData]:
def initial_data(
logged_on_user: Dict[str, Any],
users_fixture: List[Dict[str, Any]],
streams_fixture: List[Dict[str, Any]],
streams_fixture: List[Subscription],
realm_emojis: Dict[str, Dict[str, Any]],
custom_profile_fields_fixture: List[Dict[str, Union[str, int]]],
) -> Dict[str, Any]:
Expand Down Expand Up @@ -1433,7 +1453,7 @@ def user_id(logged_on_user: Dict[str, Any]) -> int:


@pytest.fixture
def stream_dict(streams_fixture: List[Dict[str, Any]]) -> Dict[int, Any]:
def stream_dict(streams_fixture: List[Subscription]) -> Dict[int, Subscription]:
return {stream["stream_id"]: stream for stream in streams_fixture}


Expand Down
51 changes: 39 additions & 12 deletions tests/core/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from pytest import param as case
from pytest_mock import MockerFixture

from zulipterminal.api_types import Subscription
from zulipterminal.config.themes import generate_theme
from zulipterminal.core import Controller
from zulipterminal.helper import Index
Expand Down Expand Up @@ -124,18 +125,22 @@ def test_narrow_to_stream(
mocker: MockerFixture,
controller: Controller,
index_stream: Index,
general_stream: Subscription,
stream_id: int = 205,
stream_name: str = "PTEST",
) -> None:
controller.model.narrow = []
controller.model.index = index_stream
controller.view.message_view = mocker.patch("urwid.ListBox")
controller.model.stream_dict = {
stream_id: {
stream_id: general_stream,
}
controller.model.stream_dict[stream_id].update(
{
"color": "#ffffff",
"name": stream_name,
}
}
)
controller.model.muted_streams = set()
mocker.patch(MODEL + ".is_muted_topic", return_value=False)

Expand Down Expand Up @@ -171,6 +176,7 @@ def test_narrow_to_topic(
initial_stream_id: Optional[int],
anchor: Optional[int],
expected_final_focus: int,
general_stream: Subscription,
stream_name: str = "PTEST",
topic_name: str = "Test",
stream_id: int = 205,
Expand All @@ -184,11 +190,14 @@ def test_narrow_to_topic(
controller.model.stream_id = initial_stream_id
controller.view.message_view = mocker.patch("urwid.ListBox")
controller.model.stream_dict = {
stream_id: {
stream_id: general_stream,
}
controller.model.stream_dict[stream_id].update(
{
"color": "#ffffff",
"name": stream_name,
}
}
)
controller.model.muted_streams = set()
mocker.patch(MODEL + ".is_muted_topic", return_value=False)

Expand Down Expand Up @@ -253,6 +262,7 @@ def test_narrow_to_all_messages(
controller: Controller,
index_all_messages: Index,
anchor: Optional[int],
general_stream: Subscription,
expected_final_focus_msg_id: int,
) -> None:
controller.model.narrow = [["stream", "PTEST"]]
Expand All @@ -261,10 +271,13 @@ def test_narrow_to_all_messages(
controller.model.user_email = "some@email"
controller.model.user_id = 1
controller.model.stream_dict = {
205: {
205: general_stream,
}
controller.model.stream_dict[205].update(
{
"color": "#ffffff",
}
}
)
controller.model.muted_streams = set()
mocker.patch(MODEL + ".is_muted_topic", return_value=False)

Expand Down Expand Up @@ -300,7 +313,11 @@ def test_narrow_to_all_pm(
assert msg_ids == id_list

def test_narrow_to_all_starred(
self, mocker: MockerFixture, controller: Controller, index_all_starred: Index
self,
mocker: MockerFixture,
controller: Controller,
index_all_starred: Index,
general_stream: Subscription,
) -> None:
controller.model.narrow = []
controller.model.index = index_all_starred
Expand All @@ -310,10 +327,13 @@ def test_narrow_to_all_starred(
mocker.patch(MODEL + ".is_muted_topic", return_value=False)
controller.model.user_email = "some@email"
controller.model.stream_dict = {
205: {
205: general_stream,
}
controller.model.stream_dict[205].update(
{
"color": "#ffffff",
}
}
)
controller.view.message_view = mocker.patch("urwid.ListBox")

controller.narrow_to_all_starred() # FIXME: Add id narrowing test
Expand All @@ -327,7 +347,11 @@ def test_narrow_to_all_starred(
assert msg_ids == id_list

def test_narrow_to_all_mentions(
self, mocker: MockerFixture, controller: Controller, index_all_mentions: Index
self,
mocker: MockerFixture,
controller: Controller,
index_all_mentions: Index,
general_stream: Subscription,
) -> None:
controller.model.narrow = []
controller.model.index = index_all_mentions
Expand All @@ -337,10 +361,13 @@ def test_narrow_to_all_mentions(
controller.model.user_email = "some@email"
controller.model.user_id = 1
controller.model.stream_dict = {
205: {
205: general_stream,
}
controller.model.stream_dict[205].update(
{
"color": "#ffffff",
}
}
)
controller.view.message_view = mocker.patch("urwid.ListBox")

controller.narrow_to_all_mentions() # FIXME: Add id narrowing test
Expand Down
2 changes: 1 addition & 1 deletion zulipterminal/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ def __init__(self, controller: Any) -> None:
self.users: List[MinimalUserData] = []
self._update_users_data_from_initial_data()

self.stream_dict: Dict[int, Any] = {}
self.stream_dict: Dict[int, Subscription] = {}
self._unsubscribed_streams: Dict[int, Subscription] = {}
self._never_subscribed_streams: Dict[int, Stream] = {}
self.muted_streams: Set[int] = set()
Expand Down

0 comments on commit 1ced8ec

Please sign in to comment.