class BlogPost(Model): id = Uuid(default_value=uuid4) title = String(validators=[ min_length(3), max_length(100), regex(r'^([A-Za-z0-9- !.]*)$') ]) body = String(default_value=u'Lorem ipsum', error_key='text') meta_data = Dict(mapping={'corrector': String(), 'reviewer': String()}) published = Boolean() likes = Integer(required=False) category = String(required=False) tags = List(property=String(validators=[min_length(3)]), error_key='category') author = Object(model_class=Author) created_on = DateTime(default_value=datetime.now) updated_on = DateTime(default_value=datetime.now) revisions = List(required=False, property=Object(model_class=Revision)) @model_validator(error_key='category') def category_or_tags(self): assert self.tags is not None or self.category is not None, ERROR_REQUIRED def __init__(self, **kwargs): super(BlogPost, self).__init__(**kwargs) self.foo = "bar"
def test_datetime_invalid(): """A string is not a valid datetime""" test_datetime = DateTime() with assert_raises(InvalidPropertyError) as cm: test_datetime.validate('not a datetime') assert cm.exception.error == 'invalid'
def test_datetime_valid_1(): """Should be ok (without microseconds)""" last_order_property = DateTime(include_microseconds=False) last_order_property.validate(datetime.now()) class Bar(Model): last_order = last_order_property now = datetime.now().replace(microsecond=123456) some_bar = Bar(last_order=now) assert some_bar.last_order.microsecond == 0