Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
event_schema: Extract event_schema.py.
Steve Howelle49acfa637c0
diff --git a/zerver/lib/event_schema.py b/zerver/lib/event_schema.pynew file mode 100644index 00000000000..c891b969489--- /dev/null+++ b/zerver/lib/event_schema.py@@ -0,0 +1,37 @@+"""+This is new module that we intend to GROW from test_events.py.++It will contain schemas (aka validators) for Zulip events.++Right now it's only intended to be used by test code.+"""+from typing import Dict, Sequence, Tuple++from zerver.lib.validator import Validator, check_dict_only, check_int+++def check_events_dict(+ required_keys: Sequence[Tuple[str, Validator[object]]],+ optional_keys: Sequence[Tuple[str, Validator[object]]] = [],+) -> Validator[Dict[str, object]]:+ """+ This is just a tiny wrapper on check_dict, but it provides+ some minor benefits:++ - mark clearly that the schema is for a Zulip event+ - make sure there's a type field+ - add id field automatically+ - sanity check that we have no duplicate keys (we+ should just make check_dict do that, eventually)++ """+ rkeys = [key[0] for key in required_keys]+ okeys = [key[0] for key in optional_keys]+ keys = rkeys + okeys+ assert len(keys) == len(set(keys))+ assert "type" in rkeys+ assert "id" not in keys+ return check_dict_only(+ required_keys=list(required_keys) + [("id", check_int)],+ optional_keys=optional_keys,+ )diff --git a/zerver/tests/test_events.py b/zerver/tests/test_events.pyindex 42ef9dace75..3615dcd66cd 100644--- a/zerver/tests/test_events.py+++ b/zerver/tests/test_events.py@@ -4,7 +4,7 @@ import copyimport sysimport timefrom io import StringIO-from typing import Any, Callable, Dict, List, Optional, Sequence, Set, Tuple+from typing import Any, Callable, Dict, List, Optional, Set, Tuplefrom unittest import mockimport ujson@@ -89,6 +89,7 @@ from zerver.lib.actions import (remove_members_from_user_group,try_update_realm_custom_profile_field,)+from zerver.lib.event_schema import check_events_dictfrom zerver.lib.events import apply_events, fetch_initial_state_data, post_process_statefrom zerver.lib.markdown import MentionDatafrom zerver.lib.message import render_markdown@@ -139,33 +140,6 @@ from zerver.tornado.event_queue import (clear_client_event_queues_for_testing,)--def check_events_dict(- required_keys: Sequence[Tuple[str, Validator[object]]],- optional_keys: Sequence[Tuple[str, Validator[object]]]=[]-) -> Validator[Dict[str, object]]:- '''- This is just a tiny wrapper on check_dict, but it provides- some minor benefits:-- - mark clearly that the schema is for a Zulip event- - make sure there's a type field- - add id field automatically- - sanity check that we have no duplicate keys (we- should just make check_dict do that, eventually)-- '''- rkeys = [key[0] for key in required_keys]- okeys = [key[0] for key in optional_keys]- keys = rkeys + okeys- assert len(keys) == len(set(keys))- assert 'type' in rkeys- assert 'id' not in keys- return check_dict_only(- required_keys=list(required_keys) + [('id', check_int)],- optional_keys=optional_keys,- )-# These fields are used for "stream" events, and are included in the# larger "subscription" events that also contain personal settings.basic_stream_fields = [