title: Unit testing in python - Can we mock an SQL query result in python?
body: <p>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.</p>
  <p><strong>I would like to know, is there a way I can mock the result retrieved from the SQL query?</strong></p>
  <p>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.</p>
  <p>Please share your insights.
  <a href="https://i.stack.imgur.com/PKgmu.png" rel="noreferrer">My method is something like :</a></p>
  <pre><code>    try:
          db_connect = sqlite3.connect(&quot;address_zip.db&quot;)
          db_cur = db_connect.cursor()
          db_cur.execute(&quot;&quot;&quot; Select STD_ADDRESS,STD_CITY,STD_STATE from address&quot;&quot;&quot;)
          sql_result=pd.read_sql_query(&quot;&quot;&quot; Select STD_ADDRESS,STD_CITY,STD_STATE from address&quot;&quot;&quot;,db_connect)
          df = pd.DataFrame(sql_result, columns=['STD_ADDRESS', 'STD_CITY', 'STD_STATE'])
          print(f&quot;The data from database is :\n {df}&quot;)
          db_cur.close()
          db_connect.close()
      except Exception as e:
          print(f&quot;DB exception: {e}&quot;)
  
          return df
  </code></pre>
  <p>There are several actions done on the variable &quot;df&quot; in the original code, my agenda is to mock this object <strong>df</strong>.</p>
  <p>But I am unable to understand how?
  Sorry this may be very basic question, I am new to python.</p>