How to mock psycopg2 cursor object?
<p>I have this code segment in Python2:</p>

<pre><code>def super_cool_method():
    con = psycopg2.connect(**connection_stuff)
    cur = con.cursor(cursor_factory=DictCursor)
    cur.execute("Super duper SQL query")
    rows = cur.fetchall()

    for row in rows:
        # do some data manipulation on row
    return rows
</code></pre>

<p>that I'd like to write some unittests for. I'm wondering how to use <code>mock.patch</code> in order to patch out the cursor and connection variables so that they return a fake set of data? I've tried the following segment of code for my unittests but to no avail:</p>

<pre><code>@mock.patch("psycopg2.connect")
@mock.patch("psycopg2.extensions.cursor.fetchall")
def test_super_awesome_stuff(self, a, b):
    testing = super_cool_method()
</code></pre>

<p>But I seem to get the following error:</p>

<pre><code>TypeError: can't set attributes of built-in/extension type 'psycopg2.extensions.cursor'
</code></pre>