title: How to patch a class in a Python unittest, that is used in an inner method?
body: <p>Given a scenario where I have a Flask application that has a route to get resources from a database, there are three files:</p>
  <ol>
  <li>The Flask &quot;main&quot; app, with the endpoints (api.py)</li>
  <li>A database class, to handle connections (database.py)</li>
  <li>A domain class for specific resources (resource.py)</li>
  </ol>
  <p>The api file looks like this:</p>
  <pre class="lang-py prettyprint-override"><code>from src.objects.resource import Resource
  
  @api.route('/api/resource/&lt;id&gt;', methods=['GET'])
  def get_resource(id):
      records = Resource().query(id)
      return records
  </code></pre>
  <p>The <code>Resource</code> object has, in its constructor, an instance of the database class being constructed:</p>
  <pre class="lang-py prettyprint-override"><code>class Resource(Database):
      def __init__(self):
          Database.__init__(self, 'resource')
  </code></pre>
  <p>And the <code>Database</code> class connects to the database on <code>__init__</code>:</p>
  <pre class="lang-py prettyprint-override"><code>class Database:
      def __init__(self, entity):
          self.conn = psycopg2.connect(os.getenv('DB_URI', 'localhost'))
  </code></pre>
  <hr />
  <p>What I really want right now is to create a unit test to mock what happens when the API method <code>get_resource</code> is hit (not what happens with an HTTP call in, but what happens with both valid and invalid ids being provided as arguments).</p>
  <p>Since the Resource object is acting mostly as an interface to database retrieval right now, I would like to mock it in my test, so when the method constructs an instance of <code>Resource</code> it gets the mock during the test, not the actual Resource instance.</p>
  <p>However, what I'm seeing is that the real constructors are being called. The test code is as follows:</p>
  <pre class="lang-py prettyprint-override"><code>import unittest
  from unittest.mock import patch
  import json
  import src.api
  from flask import Flask
  
  
  class TestApi(unittest.TestCase):
  
      @patch('src.objects.resource.Resource')
      def test_get_resource(self, mock_obj):
  
          # given
          instance = mock_obj.return_value
          instance.query.return_value = {
              'id': 1,
              'name': 'Resource\'s Name'
          }
  
          # when
          with Flask(__name__).app_context():
              # then
              result = json.loads(src.api.get_resource(1).get_data())
  
              self.assertEqual(True, 'data' in result)
              self.assertEqual(True, 'error' in result)
              self.assertEqual(False, result['error'])
  </code></pre>
  <p>And I am getting a <code>ProgrammingError</code> from the <code>psycopg2</code> module (which I don't want to be called):</p>
  <pre><code>E psycopg2.ProgrammingError: invalid dsn: missing &quot;=&quot; after &quot;localhost&quot; in connection info string
  </code></pre>
  <hr />
  <p>Should I mock the <code>__init__</code> method of the Resource object? If so, how?</p>