Beispiel #1
0
    def test_datetime(self):
        check = DateTime()
        self.assertEqual(check('2017-09-01 23:59'),
                         datetime.datetime(2017, 9, 1, 23, 59))
        self.assertEqual(
            check('Fri Sep 1 23:59:59 UTC 2017'),
            datetime.datetime(2017, 9, 1, 23, 59, 59, tzinfo=tzutc()))
        self.assertEqual(
            check('Fri Sep 1 23:59:59 2017'),
            datetime.datetime(2017, 9, 1, 23, 59, 59))
        self.assertEqual(
            check('Fri, 1 Sep 2017 23:59:59 -0300'),
            datetime.datetime(2017, 9, 1, 23, 59, 59,
                              tzinfo=tzoffset(None, -10800)))
        self.assertEqual(
            check('2017-09-01T23:59:59.5-03:00'),
            datetime.datetime(2017, 9, 1, 23, 59, 59, 500000,
                              tzinfo=tzoffset(None, -10800)))
        self.assertEqual(
            check('20170901T235959.5-0300'),
            datetime.datetime(2017, 9, 1, 23, 59, 59, 500000,
                              tzinfo=tzoffset(None, -10800)))
        self.assertEqual(
            check('20170901T235959-0300'),
            datetime.datetime(2017, 9, 1, 23, 59, 59,
                              tzinfo=tzoffset(None, -10800)))
        self.assertEqual(check('2017-09-01T23:59:59'),
                         datetime.datetime(2017, 9, 1, 23, 59, 59))
        self.assertEqual(check('20170901T235959'),
                         datetime.datetime(2017, 9, 1, 23, 59, 59))
        self.assertEqual(check('20170901235959'),
                         datetime.datetime(2017, 9, 1, 23, 59, 59))
        self.assertEqual(check('2017-09-01T23:59'),
                         datetime.datetime(2017, 9, 1, 23, 59))
        self.assertEqual(check('20170901T2359'),
                         datetime.datetime(2017, 9, 1, 23, 59))
        self.assertEqual(check('2017-09-01T23'),
                         datetime.datetime(2017, 9, 1, 23))
        self.assertEqual(check('20170901T23'),
                         datetime.datetime(2017, 9, 1, 23))
        self.assertEqual(check('2017-09-01'),
                         datetime.datetime(2017, 9, 1))
        self.assertEqual(check('20170901'),
                         datetime.datetime(2017, 9, 1))
        self.assertEqual(check('09-01-2017'),
                         datetime.datetime(2017, 9, 1))
        self.assertEqual(check('09-01-17'),
                         datetime.datetime(2017, 9, 1))
        self.assertEqual(check('2017.Sep.01'),
                         datetime.datetime(2017, 9, 1))
        self.assertEqual(check('2017/09/01'),
                         datetime.datetime(2017, 9, 1))
        self.assertEqual(check('2017 09 01'),
                         datetime.datetime(2017, 9, 1))
        self.assertEqual(check('1st of September 2017'),
                         datetime.datetime(2017, 9, 1))

        # Note: to equality here we need to pass extra params to parse() method
        self.assertNotEqual(check('01-09-2017'),
                            datetime.datetime(2017, 9, 1))
Beispiel #2
0
def build_trafaret(sa_type, **kwargs):

    if isinstance(sa_type, sa.sql.sqltypes.Enum):
        trafaret = t.Enum(*sa_type.enums, **kwargs)

    # check for Text should be before String
    elif isinstance(sa_type, sa.sql.sqltypes.Text):
        trafaret = t.String(**kwargs)

    elif isinstance(sa_type, sa.sql.sqltypes.String):
        trafaret = t.String(max_length=sa_type.length, **kwargs)

    elif isinstance(sa_type, sa.sql.sqltypes.Integer):
        trafaret = t.Int(**kwargs)

    elif isinstance(sa_type, sa.sql.sqltypes.Float):
        trafaret = t.Float(**kwargs)

    elif isinstance(sa_type, sa.sql.sqltypes.DateTime):
        trafaret = DateTime(**kwargs)  # RFC3339

    elif isinstance(sa_type, sa.sql.sqltypes.Date):
        trafaret = DateTime(**kwargs)  # RFC3339

    elif isinstance(sa_type, sa.sql.sqltypes.Boolean):
        trafaret = t.StrBool(**kwargs)

    # Add PG related JSON and ARRAY
    elif isinstance(sa_type, postgresql.JSON):
        trafaret = AnyDict | t.List(AnyDict)

    # Add PG related JSON and ARRAY
    elif isinstance(sa_type, postgresql.ARRAY):
        item_trafaret = build_trafaret(sa_type.item_type)
        trafaret = t.List(item_trafaret)

    elif isinstance(sa_type, postgresql.INET):
        # TODO: depend on "ipaddress" module?
        trafaret = t.String(**kwargs)

    else:
        type_ = str(sa_type)
        msg = 'Validator for type {} not implemented'.format(type_)
        raise NotImplementedError(msg)
    return trafaret
 def from_row(cls, row, books: List['Book'], alias=None):
     table = cls.Options.db_table if alias is None else alias
     return cls(
         id=t.Int().check(row[table.c.id]),
         name=t.String().check(row[table.c.name]),
         books=t.List(t.Type(Book)).check(books),
         created_at=DateTime().check(row[table.c.created_at]),
         updated_at=t.Or(DateTime, t.Null).check(row[table.c.updated_at]),
         is_populated=True
     )
 def from_row(cls, row, book: 'Book', alias=None):
     table = cls.Options.db_table if alias is None else alias
     return cls(
         id=t.Int().check(row[table.c.id]),
         title=t.String().check(row[table.c.title]),
         ordering=t.Int().check(row[table.c.ordering]),
         book=t.Type(Book).check(book),
         created_at=DateTime().check(row[table.c.created_at]),
         updated_at=t.Or(DateTime, t.Null).check(row[table.c.updated_at]),
         is_populated=True
     )
 def from_row(cls, row, imageable: ImageableType, alias=None):
     table = cls.Options.db_table if alias is None else alias
     return cls(
         id=t.Int().check(row[table.c.id]),
         title=t.String().check(row[table.c.title]),
         uri=str(t.URL.check(row[table.c.uri])),
         imageable=t.Or(t.Type(Author),
                        t.Type(Book),
                        t.Type(Series)).check(imageable),
         created_at=DateTime().check(row[table.c.created_at]),
         updated_at=t.Or(DateTime, t.Null).check(row[table.c.updated_at]),
         is_populated=True
     )
 def from_row(cls, row, author: 'Author', photos: List['Photo'],
              chapters: List['Chapter'], series: 'Series' = None,
              alias=None):
     table = cls.Options.db_table if alias is None else alias
     return cls(
         id=t.Int().check(row[table.c.id]),
         title=t.String().check(row[table.c.title]),
         date_published=Date().check(row[table.c.date_published]),
         author=t.Type(Author).check(author),
         photos=t.List(t.Type(Photo)).check(photos),
         chapters=t.List(t.Type(Chapter)).check(chapters),
         series=t.Or(t.Type(Series), t.Null).check(series),
         created_at=DateTime().check(row[table.c.created_at]),
         updated_at=t.Or(DateTime, t.Null).check(row[table.c.updated_at]),
         is_populated=True
     )
Beispiel #7
0
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()),
})


async def get_user_id(user_collection, username):
    rv = await user_collection.find_one({'username': username}, {'_id': 1})
    return rv['_id'] if rv else None
Beispiel #8
0
 def test_datetime(self):
     check = DateTime()
     assert check('2017-09-01 23:59') == datetime.datetime(2017, 9, 1, 23, 59)
Beispiel #9
0
 def test_repr(self):
     assert repr(DateTime()) == '<DateTime>'
     assert repr(DateTime(allow_blank=True)) == '<DateTime(blank)>'
Beispiel #10
0
 def test_datetime_blank(self):
     check = DateTime(allow_blank=True)
     with pytest.raises(DataError):
         check('')
Beispiel #11
0
 def _datetime_col(self, column, **kwargs):
     return DateTime(**kwargs)  # RFC3339
Beispiel #12
0
 def test_datetime_blank(self):
     check = DateTime(allow_blank=True)
     with self.assertRaises(DataError):
         check('')