event_schema: Extract check_hotspots.

This forces us to introduce a NumberType.
This commit is contained in:
Steve Howell 2020-08-05 17:56:34 +00:00 committed by Tim Abbott
parent cf26151cea
commit d28c01284c
5 changed files with 38 additions and 12 deletions

View File

@ -42,10 +42,11 @@ os.environ["DJANGO_SETTINGS_MODULE"] = "zproject.test_settings"
django.setup()
from zerver.lib import event_schema
from zerver.lib.data_types import ( # force vertical
from zerver.lib.data_types import (
DictType,
EnumType,
ListType,
NumberType,
UnionType,
make_checker,
schema,
@ -150,7 +151,7 @@ def from_openapi(node: Dict[str, Any]) -> Any:
return int
if node["type"] == "number":
return float
return NumberType()
if node["type"] == "string":
if "enum" in node:

View File

@ -93,6 +93,16 @@ class Equals:
return f"{var_name} in {repr([self.expected_value])}"
class NumberType:
def check_data(self, var_name: str, val: Optional[Any]) -> None:
if isinstance(val, int) or isinstance(val, float):
return
raise AssertionError(f"{var_name} is not a number")
def schema(self, var_name: str) -> str:
return f"{var_name}: number"
class ListType:
def __init__(self, sub_type: Any, length: Optional[int] = None) -> None:
self.sub_type = sub_type

View File

@ -12,6 +12,7 @@ from zerver.lib.data_types import (
EnumType,
Equals,
ListType,
NumberType,
OptionalType,
UnionType,
UrlType,
@ -166,6 +167,25 @@ default_streams_event = event_dict_type(
)
check_default_streams = make_checker(default_streams_event)
_hotspot = DictType(
required_keys=[
# force vertical
("name", str),
("title", str),
("description", str),
("delay", NumberType()),
]
)
hotspots_event = event_dict_type(
required_keys=[
# force vertical
("type", Equals("hotspots")),
("hotspots", ListType(_hotspot),),
]
)
check_hotspots = make_checker(hotspots_event)
invites_changed_event = event_dict_type(
required_keys=[
# the most boring event...no metadata

View File

@ -3,6 +3,7 @@ from zerver.lib.data_types import (
EnumType,
Equals,
ListType,
NumberType,
OptionalType,
UnionType,
UrlType,
@ -22,6 +23,7 @@ class MiscTest(ZulipTestCase):
("type", Equals("realm")),
("maybe_n", OptionalType(int)),
("s", str),
("timestamp", NumberType()),
("flag", bool),
("level", EnumType([1, 2, 3])),
("lst", ListType(int)),
@ -37,6 +39,7 @@ test (dict):
type: int
maybe_n: int
s: str
timestamp: number
type in ['realm']
url: str
value (union):

View File

@ -95,6 +95,7 @@ from zerver.lib.event_schema import (
check_default_stream_groups,
check_default_streams,
check_events_dict,
check_hotspots,
check_invites_changed,
check_message,
check_reaction,
@ -1607,18 +1608,9 @@ class NormalActionsTest(BaseAction):
self.user_profile.tutorial_status = UserProfile.TUTORIAL_WAITING
self.user_profile.save(update_fields=['tutorial_status'])
schema_checker = check_events_dict([
('type', equals('hotspots')),
('hotspots', check_list(check_dict_only([
('name', check_string),
('title', check_string),
('description', check_string),
('delay', check_float),
]))),
])
events = self.verify_action(
lambda: do_mark_hotspot_as_read(self.user_profile, 'intro_reply'))
schema_checker('events[0]', events[0])
check_hotspots('events[0]', events[0])
def test_rename_stream(self) -> None:
stream = self.make_stream('old_name')