def test_url(self): res = t.URL().check('http://example.net/resource/?param=value#anchor') self.assertEqual(res, 'http://example.net/resource/?param=value#anchor') res = str( t.URL().check('http://пример.рф/resource/?param=value#anchor')) self.assertEqual( res, 'http://xn--e1afmkfd.xn--p1ai/resource/?param=value#anchor')
async def add_post(request): data = await request.post() url = data["url"] try: url = t.URL(url) except t.DataError: raise web.HTTPBadRequest() feed = await parse(request.app["session"], url) await add(request.app["db"], request.app["sparky"], feed) name = feed["title"] log.debug("added stream name=%r", name) location = "/stream/{}".format(name) raise web.HTTPSeeOther(location=location)
class Reaction( namedtuple( 'BaseReaction', 'id,patterns,image_url,image_id,text,created_at,created_by,last_used' ), StorableMix): collection = 'reactions' trafaret = t.Dict({ 'id': t.Or(t.String | MongoId(allow_blank=True)), 'patterns': t.List(t.String, min_length=1), 'image_url': t.URL(allow_blank=True), 'image_id': t.String(allow_blank=True), 'text': t.String(allow_blank=True), 'created_at': t.Int, 'created_by': User.trafaret, t.Key('last_used', default=0): t.Int, }).make_optional('image_id', 'image_url', 'text', 'last_used') @classmethod @inject.params(db=AsyncIOMotorDatabase) def find_by_pattern(cls, patterns, db=None): return db[cls.collection].find({'patterns': {'$in': patterns}}) @inject.params(db=AsyncIOMotorDatabase) def update_usage(self, db=None): epoch_now = int(time.time()) return db[self.collection].update({'_id': self.id}, {'$set': { 'last_used': epoch_now }}) @property @inject.params(config=Config) def on_hold(self, config=None): epoch_now = int(time.time()) return self.last_used >= (epoch_now - config.reaction_threshold * 60)
return Team.query.filter(Team.name == name).one().id except NoResultFound: raise t.DataError('Team not found') EVENT_SUGGESTION_TRAFARET = t.Dict({ 'title': t.String, 'agenda': t.String, 'social': t.String(allow_blank=True) | t.Null, 'place': t.String(allow_blank=True), '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':
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 test_url(self): self.assertIn('://', generate(t.URL()))