Summary : I have a method which i would like to test. This methods involves connecting the database,extracting the result (from sql query) and saving it into a data frame.
I would like to know, is there a way I can mock the result retrieved from the SQL query?
I tried unittest library, created a dataframe like the one received from the SQl query. But I am unable to figure out how can I pass this variable to the method while testing.
Please share your insights. My method is something like :
try:
db_connect = sqlite3.connect("address_zip.db")
db_cur = db_connect.cursor()
db_cur.execute(""" Select STD_ADDRESS,STD_CITY,STD_STATE from address""")
sql_result=pd.read_sql_query(""" Select STD_ADDRESS,STD_CITY,STD_STATE from address""",db_connect)
df = pd.DataFrame(sql_result, columns=['STD_ADDRESS', 'STD_CITY', 'STD_STATE'])
print(f"The data from database is :\n {df}")
db_cur.close()
db_connect.close()
except Exception as e:
print(f"DB exception: {e}")
return df
There are several actions done on the variable "df" in the original code, my agenda is to mock this object df.
But I am unable to understand how? Sorry this may be very basic question, I am new to python.