test_helpers: Remove capture_event helper.

We now consistently use the `tornado_redirected_to_list`
helper, which asserts the number of events sent too.
This commit is contained in:
Abhijeet Prasad Bodas 2021-05-27 19:45:34 +05:30 committed by Tim Abbott
parent d2bc93f6dd
commit 090f2d6664
2 changed files with 4 additions and 29 deletions

View File

@ -92,30 +92,6 @@ def simulated_queue_client(client: Callable[[], object]) -> Iterator[None]:
yield yield
class EventInfo:
def populate(self, call_args_list: List[Any]) -> None:
args = call_args_list[0][0]
self.realm_id = args[0]
self.payload = args[1]
self.user_ids = args[2]
@contextmanager
def capture_event(event_info: EventInfo) -> Iterator[None]:
# Use this for simple endpoints that throw a single event
# in zerver.lib.actions.
with mock.patch("zerver.lib.actions.send_event") as m:
yield
if len(m.call_args_list) == 0:
raise AssertionError("No event was sent inside actions.py")
if len(m.call_args_list) > 1:
raise AssertionError("Too many events sent by action")
event_info.populate(m.call_args_list)
@contextmanager @contextmanager
def cache_tries_captured() -> Iterator[List[Tuple[str, Union[str, List[str]], Optional[str]]]]: def cache_tries_captured() -> Iterator[List[Tuple[str, Union[str, List[str]], Optional[str]]]]:
cache_queries: List[Tuple[str, Union[str, List[str]], Optional[str]]] = [] cache_queries: List[Tuple[str, Union[str, List[str]], Optional[str]]] = []

View File

@ -1,9 +1,8 @@
from typing import Any, Dict, Set from typing import Any, Dict, List, Mapping, Set
import orjson import orjson
from zerver.lib.test_classes import ZulipTestCase from zerver.lib.test_classes import ZulipTestCase
from zerver.lib.test_helpers import EventInfo, capture_event
from zerver.lib.user_status import get_user_info_dict, update_user_status from zerver.lib.user_status import get_user_info_dict, update_user_status
from zerver.models import UserProfile, UserStatus, get_client from zerver.models import UserProfile, UserStatus, get_client
@ -142,11 +141,11 @@ class UserStatusTest(ZulipTestCase):
def update_status_and_assert_event( def update_status_and_assert_event(
self, payload: Dict[str, Any], expected_event: Dict[str, Any] self, payload: Dict[str, Any], expected_event: Dict[str, Any]
) -> None: ) -> None:
event_info = EventInfo() events: List[Mapping[str, Any]] = []
with capture_event(event_info): with self.tornado_redirected_to_list(events):
result = self.client_post("/json/users/me/status", payload) result = self.client_post("/json/users/me/status", payload)
self.assert_json_success(result) self.assert_json_success(result)
self.assertEqual(event_info.payload, expected_event) self.assertEqual(events[0]["event"], expected_event)
def test_endpoints(self) -> None: def test_endpoints(self) -> None:
hamlet = self.example_user("hamlet") hamlet = self.example_user("hamlet")