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() django.setup()
from zerver.lib import event_schema from zerver.lib import event_schema
from zerver.lib.data_types import ( # force vertical from zerver.lib.data_types import (
DictType, DictType,
EnumType, EnumType,
ListType, ListType,
NumberType,
UnionType, UnionType,
make_checker, make_checker,
schema, schema,
@ -150,7 +151,7 @@ def from_openapi(node: Dict[str, Any]) -> Any:
return int return int
if node["type"] == "number": if node["type"] == "number":
return float return NumberType()
if node["type"] == "string": if node["type"] == "string":
if "enum" in node: if "enum" in node:

View File

@ -93,6 +93,16 @@ class Equals:
return f"{var_name} in {repr([self.expected_value])}" 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: class ListType:
def __init__(self, sub_type: Any, length: Optional[int] = None) -> None: def __init__(self, sub_type: Any, length: Optional[int] = None) -> None:
self.sub_type = sub_type self.sub_type = sub_type

View File

@ -12,6 +12,7 @@ from zerver.lib.data_types import (
EnumType, EnumType,
Equals, Equals,
ListType, ListType,
NumberType,
OptionalType, OptionalType,
UnionType, UnionType,
UrlType, UrlType,
@ -166,6 +167,25 @@ default_streams_event = event_dict_type(
) )
check_default_streams = make_checker(default_streams_event) 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( invites_changed_event = event_dict_type(
required_keys=[ required_keys=[
# the most boring event...no metadata # the most boring event...no metadata

View File

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

View File

@ -95,6 +95,7 @@ from zerver.lib.event_schema import (
check_default_stream_groups, check_default_stream_groups,
check_default_streams, check_default_streams,
check_events_dict, check_events_dict,
check_hotspots,
check_invites_changed, check_invites_changed,
check_message, check_message,
check_reaction, check_reaction,
@ -1607,18 +1608,9 @@ class NormalActionsTest(BaseAction):
self.user_profile.tutorial_status = UserProfile.TUTORIAL_WAITING self.user_profile.tutorial_status = UserProfile.TUTORIAL_WAITING
self.user_profile.save(update_fields=['tutorial_status']) 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( events = self.verify_action(
lambda: do_mark_hotspot_as_read(self.user_profile, 'intro_reply')) 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: def test_rename_stream(self) -> None:
stream = self.make_stream('old_name') stream = self.make_stream('old_name')