예제 #1
0
def test_int():
    int_ = t.Int(name='int', comment="int")
    assert str(int_) == "`int` int(11) DEFAULT NULL COMMENT 'int';"
    try:
        int_ = t.Int(name='int', default='xxx')
        assert str(int_)
        assert False, "Should be raise TypeError"
    except TypeError:
        pass
    try:
        int_ = t.Int(name='int', primary_key=True, default=1)
        int_ = t.Int(name='int', auto=True)
        assert False, "Should be raise ProgrammingError"
    except err.ProgrammingError:
        pass
예제 #2
0
class TypesModel(Model):

    __tablename__ = 'test_types_table'
    __comment__ = 'type case table'
    __auto_increment__ = 7
    __indexes__ = (
        t.K('key', ['tinyint', 'datetime_'], comment='key test'),
        t.UK('ukey', 'varchar', comment='unique key test'),
    )

    id = t.Auto(comment='permary_key')
    tinyint = t.Tinyint(1, unsigned=True, zerofill=True, comment='tinyint')
    smallint = t.Smallint(null=False, default=0, comment='smallint')
    int_ = t.Int(unsigned=True, null=False, default=0, comment='int')
    bigint = t.Bigint(45, null=False, default=0, comment='bigint')
    text = t.Text(encoding=t.ENCODING.utf8mb4, null=False, comment='text')
    char = t.Char(45, null=False, default='', comment='char')
    varchar = t.VarChar(45, null=False, default='', comment='varchar')
    uuid = t.UUID(comment='uuid test')
    float_ = t.Float((3, 3), default=0, comment='float')
    double_ = t.Double((4, 4), unsigned=True, default=0, comment='double')
    decimal = t.Decimal((10, 2), unsigned=True, default=0, comment='decimal')
    time_ = t.Time(default=datetime.now, comment='time')
    date_ = t.Date(default=datetime.now, comment='date')
    datetime_ = t.DateTime(default=datetime.now, comment='datetime')
    now_ts = t.Timestamp(default=datetime.now, comment='now ts')
    created_at = t.Timestamp(default=t.ON_CREATE, comment='created_at')
    updated_at = t.Timestamp(default=t.ON_UPDATE, comment='updated_at')
예제 #3
0
def test_funs():
    age = t.Int(name='age')
    s = t.F.SUM(age).as_('age_sum')
    assert helper.parse(s).sql == 'SUM(`age`) AS `age_sum` ;'

    m_ = t.F.MAX(age).as_('age_max')
    assert helper.parse(m_).sql == 'MAX(`age`) AS `age_max` ;'
    try:
        t.F.STR(age).as_('age_str')
        assert False, 'Should be raise RuntimeError'
    except RuntimeError:
        pass
예제 #4
0
def test_key():
    age = t.Int(name='age')
    name = t.Char(name='name')
    phone = t.VarChar(name='phone')
    key = t.K('idx_name', name)
    assert str(key) == 'KEY `idx_name` (`name`);'
    key = t.K('idx_name_age', (name, age))
    assert str(key) == 'KEY `idx_name_age` (`name`, `age`);'
    key = t.K('idx_name_age', ('name', 'age'))
    assert str(key) == 'KEY `idx_name_age` (`name`, `age`);'
    key = t.UK('uk_phone', phone, comment='phone')
    assert str(key) == "UNIQUE KEY `uk_phone` (`phone`) COMMENT 'phone';"
예제 #5
0
class Employee(People):

    __indexes__ = [t.K('idx_age_salary', ['age', 'salary'])]

    salary = t.Float()
    departmentid = t.Int()
    phone = t.VarChar(default='')

    # class Table:
    #     idxes = t.K('idx_age_salary', ['age', 'salary'])

    @classmethod
    def test(cls):
        return 2
예제 #6
0
def test_expr():
    age = t.Int(name='age')
    name = t.Char(name='name')
    phone = t.VarChar(name='phone')

    e = (age > 10) | (name == 'test')
    assert helper.parse(e) == helper.Query('((`age` > %s) OR (`name` = %s));',
                                           (10, 'test'))
    e = (name == 'test') | (age > 10)
    assert helper.parse(e) == helper.Query('((`name` = %s) OR (`age` > %s));',
                                           ('test', 10))

    e = age + 1
    assert helper.parse(e) == helper.Query('(`age` + %s);', (1, ))
    e = 1 + age
    assert helper.parse(e) == helper.Query('(%s + `age`);', (1, ))

    e = age + '20'
    assert helper.parse(e) == helper.Query('(`age` + %s);', (20, ))
    e = 20 + age
    assert helper.parse(e) == helper.Query('(%s + `age`);', (20, ))

    e = age * '2'
    assert helper.parse(e) == helper.Query('(`age` * %s);', (2, ))
    e = 2 * age
    assert helper.parse(e) == helper.Query('(%s * `age`);', (2, ))

    e = 1000 / age
    assert helper.parse(e) == helper.Query('(%s / `age`);', (1000, ))
    e = age / 2
    assert helper.parse(e) == helper.Query('(`age` / %s);', (2, ))

    e = age ^ name
    assert helper.parse(e) == helper.Query('(`age` # `name`);', ())
    e = name ^ age
    assert helper.parse(e) == helper.Query('(`name` # `age`);', ())

    e = name == 'at7h'
    assert helper.parse(e) == helper.Query('(`name` = %s);', ('at7h', ))
    e = name != 'at7h'
    assert helper.parse(e) == helper.Query('(`name` != %s);', ('at7h', ))
    e = name <= 'at7h'
    assert helper.parse(e) == helper.Query('(`name` <= %s);', ('at7h', ))
    e = name >= 'at7h'
    assert helper.parse(e) == helper.Query('(`name` >= %s);', ('at7h', ))
    e = age < 90
    assert helper.parse(e) == helper.Query('(`age` < %s);', (90, ))
    e = age > 20
    assert helper.parse(e) == helper.Query('(`age` > %s);', (20, ))
    e = name >> None
    assert helper.parse(e) == helper.Query('(`name` IS %s);', (None, ))
    e = name << ['at7h', 'mejer']
    assert helper.parse(e) == helper.Query('(`name` IN %s);',
                                           (('at7h', 'mejer'), ))
    e = name % 'at'
    assert helper.parse(e) == helper.Query('(`name` LIKE BINARY %s);',
                                           ('at', ))
    e = name**'at'
    assert helper.parse(e) == helper.Query('(`name` LIKE %s);', ('at', ))
    e = age[slice(20, 30)]
    assert helper.parse(e) == helper.Query('(`age` BETWEEN %s AND %s);', (
        20,
        30,
    ))

    e = name.concat(10)
    assert helper.parse(e) == helper.Query('(`name` || %s);', ('10', ))
    e = name.binand('at7h')
    assert helper.parse(e) == helper.Query('(`name` & %s);', ('at7h', ))
    e = name.binor('at7h')
    assert helper.parse(e) == helper.Query('(`name` | %s);', ('at7h', ))
    e = name.in_(['at7h', 'mejor'])
    assert helper.parse(e) == helper.Query('(`name` IN %s);',
                                           (('at7h', 'mejor'), ))
    e = name.nin_(['at7h', 'mejor'])
    assert helper.parse(e) == helper.Query('(`name` NOT IN %s);',
                                           (('at7h', 'mejor'), ))
    e = name.exists(['at7h', 'mejor'])
    assert helper.parse(e) == helper.Query('(`name` EXISTS %s);',
                                           (('at7h', 'mejor'), ))
    e = name.nexists(['at7h', 'mejor'])
    assert helper.parse(e) == helper.Query('(`name` NOT EXISTS %s);',
                                           (('at7h', 'mejor'), ))
    e = name.isnull()
    assert helper.parse(e) == helper.Query('(`name` IS %s);', (None, ))
    e = name.isnull(False)
    assert helper.parse(e) == helper.Query('(`name` IS NOT %s);', (None, ))
    e = name.regexp('at.*')
    assert helper.parse(e) == helper.Query('(`name` REGEXP %s);', ('at.*', ))
    e = name.regexp('at.*', i=False)
    assert helper.parse(e) == helper.Query('(`name` REGEXP BINARY %s);',
                                           ('at.*', ))
    e = phone.like(177)
    assert helper.parse(e) == helper.Query('(`phone` LIKE %s);', ('177', ))
    e = phone.like(177, i=False)
    assert helper.parse(e) == helper.Query('(`phone` LIKE BINARY %s);',
                                           ('177', ))
    e = phone.contains(7867)
    assert helper.parse(e) == helper.Query('(`phone` LIKE %s);', ('%7867%', ))
    e = phone.contains(7867, i=False)
    assert helper.parse(e) == helper.Query('(`phone` LIKE BINARY %s);',
                                           ('%7867%', ))
    e = name.endswith('7h')
    assert helper.parse(e) == helper.Query('(`name` LIKE %s);', ('%7h', ))
    e = name.endswith('7h', i=False)
    assert helper.parse(e) == helper.Query('(`name` LIKE BINARY %s);',
                                           ('%7h', ))
    e = name.startswith('at')
    assert helper.parse(e) == helper.Query('(`name` LIKE %s);', ('at%', ))
    e = name.startswith('at', i=False)
    assert helper.parse(e) == helper.Query('(`name` LIKE BINARY %s);',
                                           ('at%', ))
    e = age.between(10, 30)
    assert helper.parse(e) == helper.Query('(`age` BETWEEN %s AND %s);',
                                           (10, 30))
    e = age.nbetween(10, 30)
    assert helper.parse(e) == helper.Query('(`age` NOT BETWEEN %s AND %s);',
                                           (10, 30))
    e = age.asc()
    assert helper.parse(e) == helper.Query('`age` ASC ;', ())
    e = age.desc()
    assert helper.parse(e) == helper.Query('`age` DESC ;', ())
    e = age.as_('a')
    assert helper.parse(e) == helper.Query('`age` AS `a` ;', ())

    e = (age > 10) & (name == 'test')
    assert helper.parse(e) == helper.Query('((`age` > %s) AND (`name` = %s));',
                                           (10, 'test'))
    e = (name == 'test') & (age > 10)
    assert helper.parse(e) == helper.Query('((`name` = %s) AND (`age` > %s));',
                                           ('test', 10))

    e = (age >= '20') & name.in_(['at7h', 'mejor']) | phone.startswith('153')
    assert helper.parse(e) == helper.Query(
        '(((`age` >= %s) AND (`name` IN %s)) OR (`phone` LIKE %s));',
        (20, ('at7h', 'mejor'), '153%'))