class EventEntry(Froshki): user_id = trafaret_attr(trafaret.Int(gt=0))() user_contact = trafaret_attr(trafaret.Email())() members = trafaret_attr( trafaret.String(regex=r'^\d[\d,]*$') >> (lambda m: [int(i) for i in m.string.split(',')]))( key_alias='team_members') team_name = trafaret_attr(trafaret.String())(nullable=True) @validation_hook def leader_in_team(self): try: return self.user_id in self.members except: return False
def test_email(self): res = t.Email().check('*****@*****.**') self.assertEqual(res, '*****@*****.**') res = extract_error(t.Email(),'someone@example') # try without domain-part self.assertEqual(res, 'value is not a valid email address') res = str(t.Email().check('someone@пример.рф')) # try with `idna` encoding self.assertEqual(res, '[email protected]') res = (t.Email() >> (lambda m: m.groupdict()['domain'])).check('*****@*****.**') self.assertEqual(res, 'example.net') res = extract_error(t.Email(), 'foo') self.assertEqual(res, 'value is not a valid email address') res = extract_error(t.Email(), 'f' * 10000 + '@correct.domain.edu') self.assertEqual(res, 'value is not a valid email address') res = extract_error(t.Email(), 'f' * 248 + '@x.edu') == 'f' * 248 + '@x.edu' self.assertEqual(res, True) res = extract_error(t.Email(), 123) self.assertEqual(res, 'value is not a string')
class Email(TrafaretPoweredAttribute): trafaret = trafaret.Email()
'registration_url': t.URL(allow_blank=True) | t.Null, 'image_url': t.URL(allow_blank=True) | t.String(max_length=0, allow_blank=True), 'level': t.Enum('NONE', 'TRAINEE', 'JUNIOR', 'MIDDLE', 'SENIOR'), 'when_start': t.String, 'when_end': (t.String(allow_blank=True) >> (lambda x: None if not x else x)) | t.Null, 'only_date': t.StrBool(), 'team': t.String() >> get_team_by_name, 'submitter_email': t.Email(), 'secret': t.String }).make_optional('secret', 'social') def pre_get_many(**kw): abort(405) def pre_put_single(instance_id=None, data=None, **kw): data.update(EVENT_SUGGESTION_TRAFARET.check(data)) def suggested_event_deserializer(data): try:
import trafaret as t from trafaret.contrib.object_id import MongoId from trafaret.contrib.rfc_3339 import DateTime user = t.Dict({ t.Key('_id'): MongoId(), t.Key('username'): t.String(max_length=50), t.Key('email'): t.Email(), t.Key('pw_hash'): t.String(), # t.Key('first_name'): t.String(max_length=50), # t.Key('last_name'): t.String(max_length=50), # t.Key('created'): DateTime, # t.Key('active'): t.Buol, }) message = t.Dict({ t.Key('_id'): MongoId(), t.Key('author_id'): MongoId(), t.Key('username'): t.String(max_length=50), t.Key('text'): t.String(), t.Key('pub_date'): DateTime(), # t.Key('likes'): t.Int, }) follower = t.Dict({ t.Key('_id'): MongoId(), t.Key('who_id'): MongoId(), t.Key('whom_id'): t.List(MongoId()), })
class TestScalar(unittest.TestCase): TRAFARET = T.Dict({ T.Key("a_null", optional=True): T.Null, T.Key("a_bool", optional=True): T.Bool, T.Key("a_float", optional=True): T.Float, T.Key("a_int", optional=True): T.Int, T.Key("a_atom_str", optional=True): T.Atom("hello"), T.Key("a_atom_list", optional=True): T.Atom(["x", "y"]), T.Key("a_enum_str", optional=True): T.Enum(["x", "y"]), T.Key("a_str", optional=True): T.String(max_length=12), T.Key("a_email", optional=True): T.Email(), T.Key("a_url", optional=True): T.URL(), }) def test_null(self): self.assertEqual( get_err(self.TRAFARET, u""" a_null: "hello" """), dedent(u"""\ config.yaml:2: a_null: value should be None """)) def test_bool(self): self.assertEqual( get_err(self.TRAFARET, u""" a_bool: "hello" """), dedent(u"""\ config.yaml:2: a_bool: value should be True or False """)) def test_float(self): self.assertEqual( get_err(self.TRAFARET, u""" a_float: "hello" """), dedent(u"""\ config.yaml:2: a_float: value can't be converted to float """)) def test_int(self): self.assertEqual( get_err(self.TRAFARET, u""" a_int: 2.57 """), dedent(u"""\ config.yaml:2: a_int: value is not int """)) def test_atom_str(self): self.assertEqual( get_err(self.TRAFARET, u""" a_atom_str: "xxx" """), dedent(u"""\ config.yaml:2: a_atom_str: value is not exactly 'hello' """)) def test_atom_list(self): self.assertEqual( get_err(self.TRAFARET, u""" a_atom_list: "xxx" """), dedent(u"""\ config.yaml:2: a_atom_list: value is not exactly '['x', 'y']' """)) def test_enum_str(self): self.assertEqual( get_err(self.TRAFARET, u""" a_enum_str: "hello" """), dedent(u"""\ config.yaml:2: a_enum_str: value doesn't match any variant """)) def test_string(self): self.assertEqual( get_err(self.TRAFARET, u""" a_str: 1 """), dedent(u"""\ config.yaml:2: a_str: value is not a string """)) def test_long_string(self): self.assertEqual( get_err( self.TRAFARET, u""" a_str: "hello my good friends" """), dedent(u"""\ config.yaml:2: a_str: String is longer than 12 characters """)) def test_email(self): self.assertEqual( get_err(self.TRAFARET, u""" a_email: "hello" """), dedent(u"""\ config.yaml:2: a_email: value is not a valid email address """)) def test_url(self): self.assertEqual( get_err(self.TRAFARET, u""" a_url: "hello" """), dedent(u"""\ config.yaml:2: a_url: value is not URL """))
def is_email(value): try: t.Email(value) except DataError: return False return True
def test_email(self): self.assertIn('@', generate(t.Email()))