def test_varchar(): varchar = t.VarChar(name='varchar', default='c') assert str(varchar) == "`varchar` varchar(255) DEFAULT 'c';" try: varchar = t.VarChar(name='varchar', default=7) assert False, 'Should be raise TypeError' except TypeError: pass
class User(People): __tablename__ = 'users' __indexes__ = [ t.K('idx_name', 'name'), t.UK('unidx_nickname', 'nickname'), ] nickname = t.VarChar(length=100) password = t.VarChar(name='pwd') lastlogin = t.DateTime(default=datetime.now, name='ll')
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')
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';"
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
class People(Model): __indexes__ = [t.K('idx_name', 'name')] id = t.Auto() name = t.VarChar(length=45) gender = t.Tinyint(length=1, unsigned=True) age = t.Tinyint(unsigned=True) create_at = t.Timestamp(default=t.ON_CREATE) update_at = t.Timestamp(default=t.ON_UPDATE) class Meta: idxes = t.K('idx_name', 'name') @classmethod def test(cls): return 1
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%'))
def test_model(): assert isinstance(People.id, types.Auto) assert str(People.name) == '`name` varchar(45) DEFAULT NULL;' assert People.__tablename__ == 'people' assert isinstance(People.__indexes__, list) assert len(People.__indexes__) == 1 assert People.__indexes__[0].name == 'idx_name' assert People.__db__ is None assert People.__auto_increment__ == 1 assert People.__engine__ == 'InnoDB' assert People.__charset__ == 'utf8' assert People.__comment__ == '' assert People.__table__.primary.auto is True assert People.test() == 1 assert People.__attrs__ == ({ 'age': 'age', 'create_at': 'create_at', 'gender': 'gender', 'id': 'id', 'name': 'name', 'update_at': 'update_at' }) assert Employee.__tablename__ == 'employee' assert isinstance(Employee.__indexes__, list) assert len(People.__indexes__) == 1 assert Employee.__indexes__[0].name == 'idx_age_salary' assert Employee.test() == 2 assert Employee.__attrs__ == ({ 'age': 'age', 'create_at': 'create_at', 'departmentid': 'departmentid', 'gender': 'gender', 'id': 'id', 'name': 'name', 'phone': 'phone', 'salary': 'salary', 'update_at': 'update_at' }) assert isinstance(User.__indexes__, list) assert len(User.__indexes__) == 2 assert User.__tablename__ == 'users' assert User.__indexes__[0].name == 'idx_name' assert User.__indexes__[1].name == 'unidx_nickname' assert User.test() == 1 assert User.__attrs__ == ({ 'age': 'age', 'create_at': 'create_at', 'gender': 'gender', 'id': 'id', 'll': 'lastlogin', 'name': 'name', 'nickname': 'nickname', 'pwd': 'password', 'update_at': 'update_at' }) assert User.__table__.fields_dict == { 'id': types.Auto( '`id` int(11) NOT NULL AUTO_INCREMENT; [PRIMARY KEY, AUTO_INCREMENT]' ), 'name': types.VarChar('`name` varchar(45) DEFAULT NULL;'), 'gender': types.Tinyint('`gender` tinyint(1) unsigned DEFAULT NULL;'), 'age': types.Tinyint('`age` tinyint(4) unsigned DEFAULT NULL;'), 'create_at': types.Timestamp('`create_at` timestamp DEFAULT CURRENT_TIMESTAMP;'), 'update_at': types.Timestamp( '`update_at` timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;' ), 'nickname': types.VarChar('`nickname` varchar(100) DEFAULT NULL;'), 'password': types.VarChar('`pwd` varchar(255) DEFAULT NULL;'), 'lastlogin': types.DateTime('`ll` datetime DEFAULT NULL;') } assert isinstance(User.nickname, types.VarChar) assert isinstance(User.password, types.VarChar) assert isinstance(User.lastlogin, types.DateTime) try: People.id = 12123123 People.name = 'at7h' del People.name assert False, 'Should be raise NotAllowedError' except err.NotAllowedError: pass try: del People.name assert False, 'Should be raise NotAllowedError' except err.NotAllowedError: pass try: class TM(People): id_ = types.BigAuto() assert False, "Should be raise DuplicatePKError" except err.DuplicatePKError: pass assert repr(User) == "Model<User>" assert str(User) == "User"