封装的 mysql 操作
import pymysql
from config.config import MYSQL_CONFIG_basic_data
class MysqlDb():
#初始数据库配置
def __init__(self, host, port, user, password, db_name):
self.db = pymysql.connect(
host=host,
port=port,
user=user,
passwd=password,
db=db_name
)
self.cur = self.db.cursor(cursor=pymysql.cursors.DictCursor)
def __del__(self): # 对象资源被释放时触发,在对象即将被删除时的最后操作
self.cur.close()
self.db.close()
def select_db(self, sql):
"""查询"""
self.cur.execute(sql)
data = self.cur.fetchall()
return data
def execute_db(self, sql):
"""更新/插入/删除"""
try:
self.cur.execute(sql)
self.db.commit()
except Exception as e:
print("操作出现错误:{}".format(e))
self.db.rollback()
mysql_db = MysqlDb(*MYSQL_CONFIG_basic_data)
数据库配置
MYSQL_CONFIG_basic_data=("xxxxxxx",3306, 'root', '123456', 'system_basic_data')
MYSQL_CONFIG_supply=("xxxxxxx",3306, 'root', '123456', 'traveling_smoothly_supply')
yaml 的用例格式,用例里面会写入 sql 语句,按上面封装的 mysql 就会发现问题,多个数据库时,就不知道用的是哪个了
创建账单:
path: /create_bill
method: post
remark: 创建账单
is_run: True
precondition_sql:
- UPDATE case_test SET title = '标题2', ex = '44' WHERE id = 2
- UPDATE case_test SET title = '标题3', ex = '55' WHERE id = 3
data:
{
"create_month": "6月",
"create_name": "小江",
"sql_test": sql-SELECT title FROM case_test where id=2,
"sql_test1": sql-SELECT title FROM case_test where id=3
}
extract_key:
bill_no: $.bill_no
create_name: $.bill_info.create_name
assert_expression:
['"lj1314"=="$.bill_no"', '"标题2"=="sql-SELECT title FROM case_test where id=2"']