Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_string_model_and_string_model_type():
class MyModel(schemas.StringModel):
import_format = r"(?P.*)==(?P\d*)"
export_format = "{left}=={right}"
left = schemas.StringType()
right = schemas.IntType()
class SchemaExample(schemas.Model):
my_model = schemas.StringModelType(MyModel)
short_data = {"my_model": "ten==10"}
long_data = {"my_model": {"left": "ten", "right": 10}}
model_data = {"my_model": MyModel("ten==10")}
invalid_data = {"my_model": "10==ten"}
expected_data = {"my_model": "ten==10"}
m = SchemaExample()
assert m.import_data(short_data).to_primitive() == expected_data
assert m.import_data(long_data).to_primitive() == expected_data
def test_returns(self):
class SchemaExample(schemas.Model):
arg1 = schemas.StringType(required=True)
arg2 = schemas.ListType(schemas.IntType)
class EndpointExample(BaseEndpoint):
@decorators.returns(SchemaExample)
def func(self, **kwargs):
return kwargs
endpoint = EndpointExample(None)
self.assertEqual(endpoint.func(arg1="test", arg2=[1, 2]), SchemaExample({'arg1': 'test', 'arg2': [1, 2]}))
with self.assertRaises(ValidationError):
endpoint.func(arg2=[1, 2])
with self.assertRaises(ValidationError):
endpoint.func(arg1="value", arg2="invalid")
class TopEventsSearchParams(SortableMixin, Model):
limit = IntType(min_value=0, max_value=10)
class CalendarParams(SearchParams):
top_events = ModelType(TopEventsSearchParams)
class CalendarDay(Model):
date = DateType()
count = IntType()
top_rank = FloatType()
rank_levels = DictType(IntType)
categories = DictType(IntType)
labels = DictType(IntType)
top_events = ModelType(EventResultSet)
class CalendarResultSet(ResultSet):
results = ResultType(CalendarDay)
class ImpactParams(SearchParams):
top_events = ModelType(TopEventsSearchParams)
impact_rank = StringType(choices=('rank', 'aviation_rank'))
start = ModelType(DateTimeRange)
start_around = ModelType(DateAround)
end = ModelType(DateTimeRange)
end_around = ModelType(DateAround)
active = ModelType(DateTimeRange)
updated = ModelType(DateTimeRange)
state = StringType(choices=('active', 'deleted'))
deleted_reason = StringType(choices=('cancelled', 'duplicate', 'invalid', 'postponed'))
rank = ModelType(IntRange)
rank_level = ListType(IntType(min_value=1, max_value=5))
# `local_rank`, `aviation_rank`, and `phq_attendance` are paid features.
# If you haven't subscribed to a paid feature, using it as a
# search param will have no effect on your search results.
local_rank = ModelType(IntRange)
local_rank_level = ListType(IntType(min_value=1, max_value=5))
aviation_rank = ModelType(IntRange)
aviation_rank_level = ListType(IntType(min_value=1, max_value=5))
phq_attendance = ModelType(IntRange)
country = ListType(StringType)
location_around = ModelType(LocationAround)
within = StringListType(StringModelType(Area), separator="+")
place = ModelType(Place)
relevance = ListType(StringType)
brand_unsafe = ModelType(BrandUnsafe)
entity = ModelType(Entity)
class Entities(Model):
class Options:
class DataPoint(Model):
uid = StringType(required=True)
date = DateTimeType(required=True)
latitude = FloatType(min_value=-90, max_value=90, required=True)
longitude = FloatType(min_value=-180, max_value=180, required=True)
initiated = DateTimeType()
completed = DateTimeType()
# @todo: Support custom dimensions from signal
class SignalDataPoints(Model):
id = StringType(required=True)
data_points = ListType(ModelType(DataPoint), required=True)
chunk_size = IntType(default=1000, max_value=5000, required=True)
class DateDimension(Model):
type = StringType()
count = IntType()
min = DateTimeType()
max = DateTimeType()
class GeoDimension(Model):
type = StringType()
bbox = ListType(FloatType)
class CalendarResultSet(ResultSet):
results = ResultType(CalendarDay)
class ImpactParams(SearchParams):
top_events = ModelType(TopEventsSearchParams)
impact_rank = StringType(choices=('rank', 'aviation_rank'))
class ImpactDay(Model):
date = DateType()
count = IntType()
impact = IntType()
rank_levels = DictType(IntType, export_level=NONEMPTY)
rank_levels_impact = DictType(IntType, export_level=NONEMPTY)
aviation_rank_levels = DictType(IntType, export_level=NONEMPTY)
aviation_rank_levels_impact = DictType(IntType, export_level=NONEMPTY)
categories = DictType(IntType)
categories_impact = DictType(IntType)
class ImpactResultSet(ResultSet):
results = ResultType(ImpactDay)
type = StringType()
count = IntType()
min = DateTimeType()
max = DateTimeType()
class GeoDimension(Model):
type = StringType()
bbox = ListType(FloatType)
class CategoryDimensionComponent(Model):
name = StringType()
count = IntType()
class CategoryDimension(Model):
type = StringType()
options = ListType(ModelType(CategoryDimensionComponent))
class NumberDimension(Model):
type = StringType()
count = IntType()
min = IntType()
max = IntType()
avg = FloatType()
std_deviation = FloatType()
results = ResultType(Event)
class CountResultSet(Model):
count = IntType()
top_rank = FloatType()
rank_levels = DictType(IntType)
categories = DictType(IntType)
labels = DictType(IntType)
class TopEventsSearchParams(SortableMixin, Model):
limit = IntType(min_value=0, max_value=10)
class CalendarParams(SearchParams):
top_events = ModelType(TopEventsSearchParams)
class CalendarDay(Model):
date = DateType()
count = IntType()
top_rank = FloatType()
rank_levels = DictType(IntType)
categories = DictType(IntType)
labels = DictType(IntType)
top_events = ModelType(EventResultSet)
type = StringType()
formatted_address = StringType()
class Event(Model):
class Options:
serialize_when_none = True
id = StringType()
title = StringType()
description = StringType()
start = DateTimeType()
end = DateTimeType()
timezone = StringType()
duration = IntType()
category = StringType()
labels = ListType(StringType())
country = StringType()
rank = IntType()
# `local_rank`, `aviation_rank`, and `phq_attendance` are paid features.
# They will only show up in your response body if you
# have subscribed to them.
local_rank = IntType()
aviation_rank = IntType()
phq_attendance = IntType()
entities = ListType(ModelType(Entities))
location = GeoJSONPointType()
place_hierarchies = ListType(ListType(StringType()))
scope = StringType()