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_list_type():
class SchemaExample(schemas.Model):
area_list = schemas.StringListType(schemas.StringModelType(schemas.Area), separator="+")
string_list = schemas.StringListType(schemas.StringType, separator="+")
string_data = {"string_list": "a+b+c", "area_list": "10km@-36.847585,174.765742+10km@-41.288058,174.778265"}
list_data = {"string_list": ["a", "b", "c"], "area_list": ["10km@-36.847585,174.765742", "10km@-41.288058,174.778265"]}
dict_data = {"string_list": ["a", "b", "c"], "area_list": [{"radius": "10km", "latitude": -36.847585, "longitude": 174.765742}, {"radius": "10km", "latitude": -41.288058, "longitude": 174.778265}]}
expected_data = {"string_list": "a+b+c", "area_list": "10km@-36.847585,174.765742+10km@-41.288058,174.778265"}
m = SchemaExample()
assert m.import_data(string_data).to_primitive() == expected_data
assert m.import_data(list_data).to_primitive() == expected_data
assert m.import_data(dict_data).to_primitive() == expected_data
unique_item_data = {"string_list": "a", "area_list": "10km@-36.847585,174.765742"}
unique_item_dict_data = {"string_list": "a", "area_list": {"radius": "10km", "latitude": -36.847585, "longitude": 174.765742}}
assert m.import_data(unique_item_data).to_primitive() == unique_item_data
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
assert m.import_data(model_data).to_primitive() == expected_data
assert m.import_data(short_data).to_dict() == expected_data
assert m.import_data(long_data).to_dict() == expected_data
def test_area_model():
class SchemaExample(schemas.Model):
area = schemas.StringModelType(schemas.Area)
short_data = {"area": "10km@-36.847585,174.765742"}
long_data = {"area": {"radius": "10km", "latitude": -36.847585, "longitude": 174.765742}}
model_data = {"area": schemas.Area("10km@-36.847585,174.765742")}
invalid_data = {"area": "10k@-36.847585,174.765742"}
expected_expected = {"area": "10km@-36.847585,174.765742"}
m = SchemaExample()
assert m.import_data(short_data).to_primitive() == expected_expected
assert m.import_data(long_data).to_primitive() == expected_expected
assert m.import_data(model_data).to_primitive() == expected_expected
assert m.import_data(short_data).to_dict() == expected_expected
assert m.import_data(long_data).to_dict() == expected_expected
assert m.import_data(model_data).to_dict() == expected_expected
def test_location_model():
class SchemaExample(schemas.Model):
location = schemas.StringModelType(schemas.Location)
short_data = {"location": "@-36.847585,174.765742"}
long_data = {"location": {"latitude": -36.847585, "longitude": 174.765742}}
model_data = {"location": schemas.Location("@-36.847585,174.765742")}
invalid_data = {"location": "-36.847585,174.765742"}
expected_expected = {"location": "@-36.847585,174.765742"}
m = SchemaExample()
assert m.import_data(short_data).to_primitive() == expected_expected
assert m.import_data(long_data).to_primitive() == expected_expected
assert m.import_data(model_data).to_primitive() == expected_expected
assert m.import_data(short_data).to_dict() == expected_expected
assert m.import_data(long_data).to_dict() == expected_expected
assert m.import_data(model_data).to_dict() == expected_expected
from predicthq.endpoints.schemas import (
LimitMixin, Model, ResultSet, ListType, StringType, GeoJSONPointType, StringListType,
StringModelType, Location, DateTimeType, ResultType, SchematicsValidationError
)
class SearchParams(LimitMixin, Model):
class Options:
serialize_when_none = False
q = StringType()
id = ListType(StringType)
location = StringListType(StringModelType(Location), separator="+")
country = ListType(StringType)
type = ListType(StringType(choices=('planet', 'continent', 'country', 'region', 'county', 'local', 'major', 'metro', 'all')))
def validate(self, *args, **kwargs):
super(SearchParams, self).validate(*args, **kwargs)
if not any((self.q, self.id, self.location, self.country)):
raise SchematicsValidationError("Places search requires one of q, id, location or country")
class Place(Model):
id = StringType()
type = StringType()
name = StringType()
county = StringType()
region = StringType()
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:
serialize_when_none = True
entity_id = StringType()
name = StringType()
type = StringType()
formatted_address = StringType()
class AnalysisResultSet(ResultSet):
results = ResultType(DailyAnalysis)
class AnalysisParams(PaginatedMixin, SortableMixin, Model):
class Options:
serialize_when_none = False
id = StringType(required=True)
date = ModelType(DateTimeRange)
initiated = ModelType(DateTimeRange)
completed = ModelType(DateTimeRange)
within = StringListType(StringModelType(Area), separator="+")
significance = FloatType(min_value=0, max_value=100)
place = ModelType(Place)
def _convert(self, value, context):
if isinstance(value, six.string_types):
return self.model_class._convert(value, context=context)
else:
return super(StringModelType, self)._convert(value, context=context)