python使用faker生成随机测试数据

65 阅读2分钟

python使用faker生成随机测试数据

import csv
from sys import prefix

from faker import Faker
import random
import pymysql.cursors
import datetime
import string

def insert_into_deliverypersonnel(data):
# python使用faker生成随机测试数据
        connection = pymysql.connect(host='localhost',
                                     user='root',
                                     password='',
                                     database='djangoz04j9t0n',
                                     cursorclass=pymysql.cursors.DictCursor)

        try:
            with connection.cursor() as cursor:
                # 执行SQL查询
                sql = "insert into deliverypersonnel(employeeid,name,gender,age,workage,workarea,email,phone,address,deliveryrecord,workefficiency)values(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
                cursor.execute(sql, data)
                connection.commit()
        except:
            print('hello')
        finally:
            # 关闭连接
            connection.close()

# python使用faker生成随机测试数据

def insert_into_order_data(workarea):

    # 生成订单名称
    prefix = '订单'
    length = 6
    current_date = datetime.datetime.now().strftime('%Y%m%d%H%M%S')  # 精确到秒
    random_str = ''.join(random.choices(string.ascii_uppercase + string.digits, k=length))
    ordername = f"{prefix}_{current_date}_{random_str}"
    print(ordername)

    # 生成订单类型
    order_type_list = ['生鲜','餐饮美食','甜点饮品','商超日用品','鲜花绿植','医药','跑腿服务','垂直消费品类']
    ordertype=order_type_list[random.randint(0,7)]

    order_quantity = random.randint(5, 100)
    orderdistribution= workarea

    quarter_list = ['第1季度', '第2季度', '第3季度', '第4季度']
    quarter = quarter_list[random.randint(0,3)]

    amountofmoney = random.randint(50, 1000)

    connection = pymysql.connect(host='localhost',
                                 user='root',
                                 password='',
                                 database='djangoz04j9t0n',
                                 cursorclass=pymysql.cursors.DictCursor)

    try:
        with connection.cursor() as cursor:
            # 执行SQL查询
            sql = "insert into orderdata(ordername, ordertype, orderquantity, orderdistribution, quarter, amountofmoney)values(%s,%s,%s,%s,%s,%s)"
            cursor.execute(sql, (
            ordername, ordertype, order_quantity, orderdistribution, quarter, amountofmoney))
            connection.commit()
    except:
        print('hello')
    finally:
        # 关闭连接
        connection.close()

# python使用faker生成随机测试数据

def insert_into_schedulingsystem():
    sql = "insert into schedulingsystem(ordername, ordertype, employeeid, name, phone, deliveryrecord) select ordername, ordertype, employeeid, name, phone, deliveryrecord from orderdata,deliverypersonnel where orderdata.id = deliverypersonnel.id "

def insert_into_customersatisfaction():
    sql = 'insert into customersatisfaction(ordername, ordertype)select ordername, ordertype from orderdata'

    for i in range(1, 14007):
        print(i)
        name = fake.name()
        print(name)
        satisfaction_list = ['非常满意', '满意', '不满意', '非常不满意']
        satisfaction = satisfaction_list[random.randint(0, 3)]
        print(satisfaction)

        connection = pymysql.connect(host='localhost',
                                     user='root',
                                     password='',
                                     database='djangoz04j9t0n',
                                     cursorclass=pymysql.cursors.DictCursor)

        try:
            with connection.cursor() as cursor:
                # 执行SQL查询
                sql = "update customersatisfaction set customername = %s, satisfaction=%s, evaluationcontent=%s where id=%s"
                cursor.execute(sql, (name, satisfaction, satisfaction, str(i)))
                connection.commit()
                print('执行完毕')
        except:
            print('hello')
        finally:
            # 关闭连接
            connection.close()

# python使用faker生成随机测试数据
fake = Faker(["zh_CN"])
Faker.seed(random.randint(0, 100000))
insert_into_customersatisfaction()

# python使用faker生成随机测试数据
# python使用faker生成随机测试数据
# python使用faker生成随机测试数据
# python使用faker生成随机测试数据
# python使用faker生成随机测试数据
# python使用faker生成随机测试数据
# python使用faker生成随机测试数据
# python使用faker生成随机测试数据
# python使用faker生成随机测试数据
# python使用faker生成随机测试数据
# python使用faker生成随机测试数据
# python使用faker生成随机测试数据
# python使用faker生成随机测试数据
# python使用faker生成随机测试数据
# python使用faker生成随机测试数据
# python使用faker生成随机测试数据
# python使用faker生成随机测试数据
# python使用faker生成随机测试数据
# python使用faker生成随机测试数据
# python使用faker生成随机测试数据
# python使用faker生成随机测试数据
# python使用faker生成随机测试数据
# python使用faker生成随机测试数据
# python使用faker生成随机测试数据

        # insert_into_deliverypersonnel((employeeid,name,gender,age,workage,workarea,email,phone,address,deliveryrecord,workefficiency))
        # insert_into_order_data(workarea)

原文链接: www.cnblogs.com/zqxLonely/p…