import sqlite3
# 创建连接和游标
def create_conn_and_cur(path="D:\djangotest\db.sqlite3"):
conn = sqlite3.connect(path)
cur = conn.cursor()
return conn,cur
def execute_query(cur,sql):
cur.execute(sql)
res = cur.fetchall()
return res
def execute_other(conn,cur,sql):
try:
res = cur.execute(sql)
except Exception as e:
print(e)
conn.rollback()
else:
return res
def close_conn_and_cur(conn,cur):
cur.close()
conn.close()
import requests,random
# 引入Session
def pre_test():
se = requests.Session()
return se
# 创建发布会
def create_news_conference_data():
eid = random.randint(20,9999)
name = '发布会'+str(eid)
limit = random.randint(100,200)
status = True
address = random.choice(['北京鸟巢','北京水立方','西安省体育场','北京人民大会堂'])
start_time = '2024-01-01 12:00:00'
data = {'eid': eid, 'name': name, 'limit': limit, 'status': status,'address': address,'start_time':start_time}
return data
# 添加发布会
def add_news_conference(se,data):
r1 = se.post('http://127.0.0.1:8000/api/add_event/',data=data)
# 断言响应状态码和业务状态码
assert_status_code_and_business_code(r1,200,200)
eid = data['eid']
return eid
# 查询发布会
def query_news_conference(se,eid):
r2 = se.get(f'http://127.0.0.1:8000/api/get_event_list/?eid={eid}')
# 断言响应状态码和业务状态码
assert_status_code_and_business_code(r2,200,200)
expected_name = r2.json()['data']['name']
conn, cur = create_conn_and_cur()
res = execute_query(cur, f'select * from hell_event where id = "{eid}";')
close_conn_and_cur(conn, cur)
# 断言业务数据: name
assert_business_data(expected_name,res[0])
def create_guest(eid):
realname = '张'+str(random.randint(100,999))
phone = random.randint(13411110000,13999999999)
email = str(phone)+'@qq.com'
data2 = {'eid':eid,'realname':realname,'phone':phone,'email':email}
return data2
def add_guest(data2):
r3 =se.post('http://127.0.0.1:8000/api/add_guest/',data=data2)
# 断言响应状态码和业务状态码
assert_status_code_and_business_code(r3,200,10000)
phone = data2['phone']
return phone
# 查询嘉宾
def query_guest(eid,phone):
r4 = se.get(f'http://127.0.0.1:8000/api/get_guest_list/?eid={eid}&phone={phone}')
# 断言响应状态码和业务状态码
assert_status_code_and_business_code(r4, 200, 10000)
expected_phone = r4.json()['data']['phone']
conn, cur = create_conn_and_cur()
res = execute_query(cur, f'select * from hell_guest where event_id = "{eid}";')
close_conn_and_cur(conn, cur)
# 断言业务数据: phone
assert_business_data(expected_phone, res[0])
# 发布会签到
def conference_sign_in(eid,phone):
r5 = se.post('http://127.0.0.1:8000/api/user_sign/',data={'eid':eid,'phone':phone})
# 断言响应状态码和业务状态码
assert_status_code_and_business_code(r5, 200, 200)
# 查询发布会(授权)
def query_conference_auth(eid):
r6 = se.get(f'http://127.0.0.1:8000/api/sec_get_event_list/?eid={eid}',headers={'Authorization':'Basic YWRtaW4xOjMyMzM3ODFtIUAj'})
# r6 = se.get(f'http://127.0.0.1:8000/api/sec_get_event_list/?eid={eid}',auth={'admin1','3233781m!@#'})
# 断言响应状态码和业务状态码
assert_status_code_and_business_code(r6, 200, 200)
# 帮助方法:断言响应状态码和业务状态码
def assert_status_code_and_business_code(r,code1,code2):
# 断言响应状态为指定的code1
assert r.status_code==code1
# 断言业务状态码为指定的code2
assert r.json()['status']==code2
# 帮助方法:断言数据值
def assert_business_data(v1,v2):
assert v1 in v2
def end_test(se):
se.close()
if __name__ == '__main__':
se = pre_test()
dt = create_news_conference_data()
id = add_news_conference(se,dt)
query_news_conference(se,id)
dt2 = create_guest(id)
phone1 = add_guest(dt2)
query_guest(id,phone1)
conference_sign_in(id,phone1)
query_conference_auth(id)
end_test(se)
conn,cur = create_conn_and_cur()
res = execute_query(cur,'select * from hell_event where id = 13;')
print(res)
close_conn_and_cur(conn,cur)
basic2
于 2024-02-01 21:50:41 首次发布