I want to write pytest cases for mysql python function by using mocker databases for below code can you help me to write pytest

  database, flask-mysql, pytest, pytest-mock, python
I want to write pytest cases for mysql python function  by using mocker databases for below 

enter code here code can you help me to write pytest
enter code here "
def check_for_db():
db_name = os.environ.get(‘db_name’)
mysql_hostname = os.environ.get(‘mysql_hostname’)
mysql_username = os.environ.get(‘mysql_username’)
mysql_pw =os.environ.get(‘mysql_pw’)
conn = mysql.connector.connect(host=mysql_hostname,user=mysql_username,password=mysql_pw)
cur = conn.cursor(buffered=True)
sql_query = ‘SHOW DATABASES;’
cur.execute(sql_query)
db_exitis = False
for databases in cur :
# print(databases)
if (databases[0]) == db_name:
db_exitis = True
conn = mysql.connector.connect(host=mysql_hostname,user=mysql_username,password=mysql_pw,
database=(os.environ.get(‘db_name’)))
cur = conn.cursor(buffered=True)

        sql_query = 'SHOW TABLES;'
        table = "devices"
        cur.execute(sql_query)
        tdl_exists = False
        for tables in cur:
            if (tables[0]) == table:
                tdl_exists = True
        if tdl_exists == True:
            sql_query = 'SHOW COLUMNS FROM DEVICES;'
            column_Name = "location"
            cur.execute(sql_query)
            col_exists = False
            for column in cur:
                if (column[0] == column_Name:
                    col_exists = True
        if col_exists == False:
           sql_alter_command = 'AlTER TABLE devices ADD loaction VARCHAR(256) AFTER type;'
           cur.execute(sql_alter_command)
           conn.commit()

    if db_exitis == False:
        print('DB doesnot exists')
        create_db(db_name)
        conn = mysql.connector.connect(host=mysql_hostname,user=mysql_username,password=mysql_pw, 
                                       database=(os.environ.get('db_name')))
        cur = conn.cursor(buffered=True)
    # verify the table already exists

        sql_setup_command = 'CREATE TABLE devices (id_INTEGER(16) NOT NULL AUTO_INCREMENT, name VARCHAR(48), ip_address VARCHAR(48) UNIQUE, community VARCHAR(48),type VARCHAR(2546), location VARCHAR(256), PRIMARY KEY(id);'
     
        cur.execute(sql_setup_command)
        conn.commit() "

Source: Python Questions

LEAVE A COMMENT