title: Python mock psycopg2 from base class
body: <p>Hi I am having troubles with mocking cursor object from inherited class.
  Here is some part of my code:</p>
  
  <pre><code># classa.py
  class ClassA(ClassB):
      def __init__(self, params):
          ClassB.__init__(params)
          # other params
  
      def get_current_attr(self):
          sql = "SELECT max(attr) FROM {};".format(self.table_name)
          self.rs_cursor.execute(sql)
          current_attr = self.rs_cursor.fetchone()[0]
          return current_attr
  
  # classb.py
  import psycopg2
  
  class ClassB:
      def __init__(self, params):
          self.redshift_conn = self.open_redshift_connection()
          self.redshift_conn.set_isolation_level(
              psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
          self.rs_cursor = self.redshift_conn.cursor()
  </code></pre>
  
  <p>Now I want to mock psycopg2 cursor in get_current_attr() function from ClassA. I tried to do it like this:</p>
  
  <pre><code>@mock.patch("mypackage.classa.classb.psycopg2.connect")
  def test_get_current_attr(self, mock_connect):
      mock_con = mock_connect.return_value
      mock_cur = mock_con.cursor.return_value
      mock_cur.fetch_one.return_value = 1
      result = self.gap_load_instance.get_current_batch_id()
      mock_cur.execute.assert_called_with(
          "SELECT max(attr) FROM tablename"
      )
  </code></pre>
  
  <p>And I am getting:</p>
  
  <blockquote>
    <p>raise AssertionError('Expected call: %s\nNot called' % (expected,))</p>
  </blockquote>
  
  <p>Can anyone help me with this?</p>