在實際測試中發現,需要插入大量的測試數據或者有依賴關係的數據 來達到測試的目的,這時我們可以使用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()
更多操作可以參考官方文檔