
在实际测试中发现,需要插入大量的测试数据或者有依赖关系的数据 来达到测试的目的,这时我们可以使用python来简化和规范化该 *** 作。
采用 peewee 来将SQL代码转化为Python代码实现ORM
peewee 是一个轻量级的 python ORM 库。内建对 SQLite、MySQL 和 PostgreSQL 的支持
peewee文档地址:
http://docs.peewee-orm.com/en/latest/
中文版文档:
https://www.osgeo.cn/peewee/
生成数据模型在安装peewee后可以在命令行中输入指令,快速的将已有的数据库转化成数据模型
python -m pwiz -e 数据库类型 -p 端口 -u 账号 -H IP地址 --password 密码 -o -i 库名 > 生成的py文件名.py
例如
python -m pwiz -e mysql -p 3306 -u doctor_core -H 数据库ip --password 数据库密码 -o -i 数据库账号 > 导出的文件.py
查看doctor_info表
-- auto-generated definition create table doctor_info ( id bigint(22) unsigned auto_increment comment '主键' primary key, user_id bigint unsigned not null comment '用户编号', login_id varchar(128) null comment '登录名', mobile_no varchar(11) null comment '手机号码', telephone_no varchar(50) null comment '医生固话', title_name varchar(200) null comment '行政职称名', dept_uuid varchar(40) null comment '科室uuid', dept_name varchar(100) null comment '科室名称', hospital_uuid varchar(40) null comment '医院uuid', hospital_name varchar(100) null comment '医院名称', property tinyint(1) unsigned null comment '医院性质', expert_uuid varchar(45) null comment '专家uuid', expert_name varchar(40) null comment '专家姓名', favorite_dept varchar(1000) null comment '医生擅长科室,多个以英文逗号分隔', job_number varchar(40) null comment '医院端医生工号', canal_online tinyint(1) unsigned default 1 null comment '渠道:1为线上,0为线下,2为兼职,3为赠险注册', source bigint(11) unsigned null comment '应用来源', reg_type tinyint(2) unsigned not null comment '注册来源: ', state tinyint(4) unsigned default 0 not null comment '状态,0:未认证,1:已认证', is_close tinyint(1) unsigned default 1 not null comment ' 0,关闭,1,开通', is_deleted tinyint unsigned default 0 not null comment '是否删除', gmt_created datetime not null comment '创建时间', gmt_modified datetime not null comment '修改时间', project_source_id int null comment '对应项目来源表的外键', source_group_id bigint null comment '来源分组', source_group_name varchar(200) null comment '来源分组名称', role_id varchar(100) null comment '医生关联角色', grade tinyint(3) default 0 null comment '医生资质等级', is_special tinyint(2) default 0 null comment '是否特批医生', relation_state tinyint(4) unsigned null comment '关联状态', relation_remark varchar(256) null comment '关联备注', gmt_relation datetime null comment '关联时间', is_auto_relation tinyint(1) unsigned null comment '是否自动关联', supervise_record_status tinyint(2) unsigned default 0 not null comment '监管备案状态', supervise_record_remark varchar(255) default '' not null comment '监管备案失败原因', constraint uniq_user_id unique (user_id) ) comment '医生信息表' charset = utf8; create index idx_expert_name on doctor_info (expert_name); create index idx_expert_uuid on doctor_info (expert_uuid); create index idx_gmtCreated on doctor_info (gmt_created); create index idx_gmtModified on doctor_info (gmt_modified); create index idx_hospital_uuid_job_number on doctor_info (hospital_uuid, job_number); create index idx_job_number on doctor_info (job_number); create index idx_loginId on doctor_info (login_id); create index idx_mobile_no on doctor_info (mobile_no);
查看它转换后的代码为:
class DoctorInfo(baseModel):
id = BigAutoField()
user_id = BigIntegerField(unique=True)
login_id = CharField(index=True, null=True)
mobile_no = CharField(index=True, null=True)
telephone_no = CharField(null=True)
title_name = CharField(null=True)
dept_uuid = CharField(null=True)
dept_name = CharField(null=True)
hospital_uuid = CharField(null=True)
hospital_name = CharField(null=True)
property = IntegerField(null=True)
expert_uuid = CharField(index=True, null=True)
expert_name = CharField(index=True, null=True)
favorite_dept = CharField(null=True)
job_number = CharField(index=True, null=True)
canal_online = IntegerField(constraints=[SQL("DEFAULT 1")], null=True)
source = BigIntegerField(null=True)
reg_type = IntegerField()
state = IntegerField(constraints=[SQL("DEFAULT 0")])
is_close = IntegerField(constraints=[SQL("DEFAULT 1")])
is_deleted = IntegerField(constraints=[SQL("DEFAULT 0")])
gmt_created = DateTimeField(index=True)
gmt_modified = DateTimeField(index=True)
project_source_id = IntegerField(null=True)
source_group_id = BigIntegerField(null=True)
source_group_name = CharField(null=True)
role_id = CharField(null=True)
grade = IntegerField(constraints=[SQL("DEFAULT 0")], null=True)
is_special = IntegerField(constraints=[SQL("DEFAULT 0")], null=True)
relation_state = IntegerField(null=True)
relation_remark = CharField(null=True)
gmt_relation = DateTimeField(null=True)
is_auto_relation = IntegerField(null=True)
supervise_record_status = IntegerField(constraints=[SQL("DEFAULT 0")])
supervise_record_remark = CharField(constraints=[SQL("DEFAULT ''")])
class meta:
table_name = 'doctor_info'
indexes = (
(('hospital_uuid', 'job_number'), False),
)
我们使用这段代码来完成一次CRUD *** 作
增DoctorInfo(user_id=xx,login_id=xx,....).save()删
DoctorInfo.delete().where(DoctorInfo.expert_name == '张三').execute()改
DoctorInfo.expert_name = '李四' DoctorInfo.save()查
# 方法一 DoctorInfo.get(expert_name='张三') # 方法二 DoctorInfo.select().where(DoctorInfo.expert_name == '张三').get()
更多 *** 作可以参考官方文档
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)