Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
test_events: Extract BaseAction.
Steve Howell987408db680a
diff --git a/docs/subsystems/events-system.md b/docs/subsystems/events-system.mdindex 23fe3ae0188..32132c3edd2 100644--- a/docs/subsystems/events-system.md+++ b/docs/subsystems/events-system.md@@ -219,7 +219,7 @@ The design above achieves everything we desire, at the cost that weneed to write a correct `apply_events` function. This is a difficultfunction to implement correctly, because the situations that it testsfor almost never happen (being race conditions). So we have a special-test class, `EventsRegisterTest`, that is specifically designed to+helper test class, `BaseAction`, that is specifically designed totest this function by ensuring the possible race condition alwayshappens. In particular, it does the following:@@ -231,7 +231,7 @@ e.g. `do_change_full_name`, and capture any events that are generated.`fetch_initial_state_data` again now.The `apply_events` code is correct if those two results are identical.-The `EventsRegisterTest` tests in `test_events` will print a diff+The `BaseAction` tests in `test_events` will print a diffbetween the "hybrid state" and the "normal state" obtained fromcalling `fetch_initial_state_data` after the changes. Those testsalso inspect the events generated to ensure they have the expected@@ -242,9 +242,9 @@ to differ what what it was before (which help catch common classes ofbugs).The final detail we need to ensure that `apply_events` always works-correctly is to make sure that we have `EventsRegisterTest` tests for+correctly is to make sure that we have relevant tests forevery event type that can be generated by Zulip. This can be tested-manually using `test-backend --coverage EventsRegisterTest` and then+manually using `test-backend --coverage BaseAction` and thenchecking that all the calls to `send_event` are covered. Somedaywe'll add automation that verifies this directly by inspecting thecoverage data.diff --git a/docs/testing/testing-with-django.md b/docs/testing/testing-with-django.mdindex 201de9a1722..68b8edd75c1 100644--- a/docs/testing/testing-with-django.md+++ b/docs/testing/testing-with-django.md@@ -398,7 +398,7 @@ below some threshold.The Zulip back end has a mechanism where it will fetch initial datafor a client from the database, and then it will subsequently applysome queued up events to that data to the data structure before notifying-the client. The `EventsRegisterTest.do_test()` helper helps tests+the client. The `BaseAction.do_test()` helper helps testsverify that the application of those events via apply_events() producesthe same data structure as performing an action that generates said event.diff --git a/zerver/lib/events.py b/zerver/lib/events.pyindex ec3506959ff..9e1545574ba 100644--- a/zerver/lib/events.py+++ b/zerver/lib/events.py@@ -84,7 +84,7 @@ def always_want(msg_type: str) -> bool:# Fetch initial data. When event_types is not specified, clients want# all event types. Whenever you add new code to this function, you# should also add corresponding events for changes in the data-# structures and new code to apply_events (and add a test in EventsRegisterTest).+# structures and new code to apply_events (and add a test in test_events.py).def fetch_initial_state_data(user_profile: UserProfile,event_types: Optional[Iterable[str]],queue_id: str, client_gravatar: bool,diff --git a/zerver/tests/test_events.py b/zerver/tests/test_events.pyindex cfe0b85b41e..84c72860ceb 100644--- a/zerver/tests/test_events.py+++ b/zerver/tests/test_events.py@@ -164,24 +164,11 @@ basic_stream_fields = [('stream_post_policy', check_int),]-class EventsRegisterTest(ZulipTestCase):+class BaseAction(ZulipTestCase):def setUp(self) -> None:super().setUp()self.user_profile = self.example_user('hamlet')- def create_bot(self, email: str, **extras: Any) -> Optional[UserProfile]:- return self.create_test_bot(email, self.user_profile, **extras)-- def realm_bot_schema(self, field_name: str, check: Validator[object]) -> Validator[Dict[str, object]]:- return check_events_dict([- ('type', equals('realm_bot')),- ('op', equals('update')),- ('bot', check_dict_only([- ('user_id', check_int),- (field_name, check),- ])),- ])-def do_test(self, action: Callable[[], object], event_types: Optional[List[str]]=None,include_subscribers: bool=True, state_change_expected: bool=True,notification_settings_null: bool=False, client_gravatar: bool=True,@@ -310,6 +297,20 @@ class EventsRegisterTest(ZulipTestCase):sys.stdout.flush()raise AssertionError('Mismatching states')+class NormalActionsTest(BaseAction):+ def create_bot(self, email: str, **extras: Any) -> Optional[UserProfile]:+ return self.create_test_bot(email, self.user_profile, **extras)++ def realm_bot_schema(self, field_name: str, check: Validator[object]) -> Validator[Dict[str, object]]:+ return check_events_dict([+ ('type', equals('realm_bot')),+ ('op', equals('update')),+ ('bot', check_dict_only([+ ('user_id', check_int),+ (field_name, check),+ ])),+ ])+def test_mentioned_send_message_events(self) -> None:user = self.example_user('hamlet')