event_schema: Extract event_schema.py.

Obviously, this file will soon grow--this
was the easiest way to start without introducing
noise into other commits.

It will soon be structurally similar
to frontend_tests/node_tests/lib/events.js--I
have some ideas there.  But this should also
help for things like API docs.
This commit is contained in:
Steve Howell
2020-06-30 16:38:42 +00:00
committed by Tim Abbott
parent a908f5a693
commit e49acfa637
2 changed files with 39 additions and 28 deletions

View File

@@ -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,
)

View File

@@ -4,7 +4,7 @@ import copy
import sys import sys
import time import time
from io import StringIO from io import StringIO
from typing import Any, Callable, Dict, List, Optional, Sequence, Set, Tuple from typing import Any, Callable, Dict, List, Optional, Set, Tuple
from unittest import mock from unittest import mock
import ujson import ujson
@@ -89,6 +89,7 @@ from zerver.lib.actions import (
remove_members_from_user_group, remove_members_from_user_group,
try_update_realm_custom_profile_field, try_update_realm_custom_profile_field,
) )
from zerver.lib.event_schema import check_events_dict
from zerver.lib.events import apply_events, fetch_initial_state_data, post_process_state from zerver.lib.events import apply_events, fetch_initial_state_data, post_process_state
from zerver.lib.markdown import MentionData from zerver.lib.markdown import MentionData
from zerver.lib.message import render_markdown from zerver.lib.message import render_markdown
@@ -139,33 +140,6 @@ from zerver.tornado.event_queue import (
clear_client_event_queues_for_testing, 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 # These fields are used for "stream" events, and are included in the
# larger "subscription" events that also contain personal settings. # larger "subscription" events that also contain personal settings.
basic_stream_fields = [ basic_stream_fields = [