def test_func(self): field = Field().func(uuid4_verify) # Assign valid value. field.value = str(uuid.uuid4()) # Assign not valid value. with self.assertRaises(Field.VerifyError): field.value = 1
class NoteJSON(Model): title = Field(required=True).instance(str) content = Field().instance(str) datetime = Field(default=lambda: datetime.utcnow().isoformat())\ .func(datetime_verify) user = Field(required=True).model(UserJSON) comments = Field().listof(CommentJSON)
class Note(Model): title = Field(required=True).instance(str) content = Field().instance(str) datetime = Field(default=lambda: datetime.utcnow()).instance(datetime) user = Field(required=True).instance(User) comments = Field().listof(Comment) def post_validate(self): assert self.get('title') != self.get('content')
def test_init(self): # 1. Field with no options field = Field() self.assertIsInstance(field, Field) self.assertEqual(field._value, UNDEF) # 2. Field with options. field = Field(required=True, default='default', grant=[None]) self.assertEqual(field.required, True) self.assertEqual(field._default, 'default')
def test_listof(self): def less_than_five(value): assert value < 5 field = Field().listof((int, float), validate=less_than_five) str_list = [1, 2] field.value = str_list with self.assertRaises(Field.VerifyError): field.value = ['a', 'b'] with self.assertRaises(Field.VerifyError): field.value = [1, 7] # Field's value should be instance of `ListOf` self.assertIsInstance(field.value, ListOf)
def test_model(self): field = Field().model(NoteJSON) note = NoteJSON({ 'title': 'Note', 'user': UserJSON({'name': 'user-1'}) }) # 1. Set field value to NoteJSON() instance field.value = note # 2. Set field value to dict() which is JSON compatible. note = json.dumps(note) note = json.loads(note) field.value = note # 3. Error if assign invalid value. with self.assertRaises(Field.VerifyError): field.value = 1
def test_value(self): field = Field(required=True, grant=[None])\ .instance(str).verify(lambda value: len(value) <= 10) # Required Field should raise RequiredError if ask for value # before assigned. with self.assertRaises(Field.RequiredError): field.value # Assign valid value. field.value = 'test' self.assertEqual(field.value, 'test') # Assign not valid value. with self.assertRaises(Field.VerifyError): field.value = 'word-length-more-than-ten' # field.value should be unmodifed if errors. self.assertEqual(field.value, 'test') # Assign ignored value. field.value = None assert field.value is None
class UserJSON(Model): id = Field(default=lambda: str(uuid.uuid4())).func(uuid4_verify) name = Field(required=True).instance(str)
class Content(Model): content_type = Field().instance(str)
def test_search(self): field = Field().search(r'\w+', re.I) field.value = '0123456789' with self.assertRaises(Field.VerifyError): field.value = '?' self.assertEqual(field.value, '0123456789')
def test_match(self): field = Field().match('012', re.I) field.value = '0123456789' with self.assertRaises(Field.VerifyError): field.value = '123' self.assertEqual(field.value, '0123456789')
class User(Model): id = Field(default=lambda: uuid.uuid4()).instance(uuid.UUID) name = Field(required=True).instance(str)
def test_instance(self): field = Field().instance(str) field.value = 'test' with self.assertRaises(Field.VerifyError): field.value = 1
def test_reset(self): field = Field(default='default') field.value = 1 field.reset() self.assertEqual(field.value, 'default')
class Comment(Model): content = Field().instance(str) datetime = Field(default=lambda: datetime.utcnow()).instance(datetime) user = Field(required=True).instance(User)
def test_default(self): field = Field(default='default') self.assertEqual(field.value, 'default') field = Field(default=datetime.utcnow) self.assertIsInstance(field.default, datetime)
class Note(Model): title = Field().required().type(str) note = Field().type(str) datetime = Field().default(datetime.utcnow)
class Note(Model): title = Field().required().type(str) content = Field().type(str)