requests_practice

import requests
import random
import sqlite3

#建立数据库连接游标
def create_sqlite(path='D:\djangotest\db.sqlite3'):
    conn=sqlite3.connect(path)
    cur=conn.cursor()
    return conn,cur

#执行sql语句
def execute_other(conn,cur,sql):
    try:
        cur.execute(sql)
    except Exception as e:
        print(e)
        conn.rollback()
    else:
        conn.commit()

def execute_query(cur,sql):
    cur.execute(sql)
    res = cur.fetchall()
    return res

#关闭连接游标
def close_conn_and_cur(conn,cur):
    cur.close()
    conn.close()

#实例化session对象
def session_open():
    s=requests.Session()
    return s

#添加发布会
def test_add_fbh(s,dic_fbh):
    r1=s.post('http://127.0.0.1:8000/api/add_event/',data=dic_fbh)
    eid=dic_fbh['eid']
    assert_code(r1,200,10000)
    sql1=f"select * from hell_event where id={eid}"
    res=execute_query(cur,sql1)
    for k in dic_fbh.keys():
        assert_values(dic_fbh[k],res[0])  #[(11,'发布会11',...)]
    return r1.json()

#查询发布会
def test_select_fbh(s,eid):
    r2=s.get(f'http://127.0.0.1:8000/api/get_event_list/?eid={eid}')
    assert_code(r2,200,10000)
    sql1=f"select * from hell_event where id={eid}"
    res=execute_query(cur,sql1)
    assert_values(r2.json()['data']['name'],res[0])
    return r2.json()

#添加嘉宾
def test_add_jb(s,dic_jb):
    r3=s.post('http://127.0.0.1:8000/api/add_guest/',data=dic_jb)
    assert_code(r3,200,10000)
    eid=dic_jb['eid']
    phone=dic_jb['phone']
    sql1=f"select * from hell_guest where event_id={eid} and phone={phone}"
    res=execute_query(cur,sql1)
    for k in dic_jb:
        assert_values(dic_jb[k],res[0])
    return r3.json()

#查询嘉宾
def test_select_jb(s,eid,phone):
    r4=s.post(f'http://127.0.0.1:8000/api/get_guest_list/?eid={eid}&phone={phone}')
    assert_code(r4,200,10000)
    sql1=f"select * from hell_guest where event_id={eid} and phone={phone}"
    res=execute_query(cur,sql1)
    assert_values(r4.json()['data']['phone'],res[0])
    return r4.json()

#发布会签到
def test_fbh_user(s,eid,phone):
    r5=s.post('http://127.0.0.1:8000/api/user_sign/',data={'eid':eid,'phone':phone})
    assert_code(r5,200,200)
    sql1=f"select * from hell_guest where event_id={eid} and phone={phone}"
    res=execute_query(cur,sql1)
    assert_values(res[0][4],[1])
    return r5.json()

#查询发布会(鉴权)
def test_sec_select_fbh(s,eid):
    r6=s.post(f'http://127.0.0.1:8000/api/sec_get_event_list/?eid={eid}',auth=('admin1','3233781m!@#'))
    assert_code(r6,200,10000)
    sql1=f"select * from hell_event where id={eid}"
    res=execute_query(cur,sql1)
    assert_values(r6.json()['data']['name'],res[0])
    return r6.json()

#关闭实例化
def session_end(s):
    s.close()

#创建发布会数据
def random_fbh():
    eid=random.randint(200,9999)
    name='发布会'+str(eid)
    limit=1000
    address='陕西'
    start_time='2024-11-07 00:00:00'
    dic_fbh={'eid':eid,'name':name,'limit':limit,'address':address,'start_time':start_time}
    return eid,dic_fbh

#创建嘉宾数据
def random_jb(eid):
    realname='张'+str(random.randint(0,9999))
    phone=str(random.randint(13000000000,19999999999))
    dic_jb={'eid':eid,'realname':realname,'phone':phone,'email':str(phone)+'@qq.com'}
    return phone,dic_jb

#断言状态响应码和业务响应码
def assert_code(r,code1,code2):
    assert r.status_code==code1
    assert r.json()['status']==code2

#断言返回信息
def assert_values(v1,v2):
    assert v1 in v2

# 清理环境
def delete_data(num):
    conn, cur = create_sqlite('D:\djangotest\djangotest\db.sqlite3')
    execute_other(conn,cur,f'delete from hell_guest where event_id>={num};')
    execute_other(conn,cur,f'delete from hell_event where id>={num};')
    close_conn_and_cur(conn,cur)

def create_conn_and_cur(path="D:\djangotest\djangotest\db.sqlite3"):
    conn = sqlite3.connect(path)
    cur = conn.cursor()
    return conn,cur
if __name__ == '__main__':
    # 清理环境
    delete_data(200)
    conn,cur=create_sqlite('D:\djangotest\djangotest\db.sqlite3')
    s=session_open()
    #创建发布会数据
    eid,dic_fbh=random_fbh()
    print(dic_fbh)
    #添加、查询发布会
    test_add_fbh(s,dic_fbh)
    test_select_fbh(s,eid)
    #创建嘉宾数据
    phone,dic_jb=random_jb(eid)
    print(dic_jb)
    #添加查询嘉宾
    test_add_jb(s,dic_jb)
    test_select_jb(s,eid,phone)
    #发布会签到
    test_fbh_user(s,eid,phone)
    # #查询发布会(鉴权)
    test_sec_select_fbh(s,eid)
    # 清理环境
    delete_data(200)
    # #关闭实例化
    session_end(s)
    #关闭游标和数据库连接
    close_conn_and_cur(conn,cur)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值