title: Mocking a python database query
body: <p>I am trying to  test by  mocking  a database query, but receiving an error:</p>
  
  <pre><code>Asssertion error:AssertionError: Expected call: execute()
  Not called
  and create_table() not defined.
  </code></pre>
  
  <p>I want to <code>execute()</code> to be called and use <code>create_table()</code> to return response for asserting against pre-defined values.</p>
  
  <p>app.py </p>
  
  <pre><code>from flask import Flask,g
  
  @app.before_request
  def before_request():
         g.db = mysql.connector.connect(user='root', password='root', database='mysql')
  
  def create_table():
      try:     
          cur = g.db.cursor() #here g is imported form Flask module
          cursor.execute ('CREATE TABLE IF NOT EXISTS Man (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, name VARCHAR(40)')
          data = dict(Table='Man is created')
          resp = jsonify(data)
          cursor.close()
          return resp
  </code></pre>
  
  <p>test.py</p>
  
  <pre><code>  import unittest
    from app import *
    from mock import patch
  
    class Test(unittest.TestCase): 
    def test_create(self):
     with patch("app.g") as mock_g:
       mock_g.db.cursor()
       mock_g.execute.assert_called_with()
       resp = create_table()
       assertEqual(json, '{"Table":"Testmysql is created","Columns": ["id","name","email"]}')
  </code></pre>
  
  <p>What am I doing wrong?Can someone please tell me how to fix it</p>