title: Python mock multiple queries in a function using pytest_mock
body: <p>I am writing unit test case for a function which has multiple sql queries in it.I am using <code>psycopg2</code> module and trying to mock the <code>cursor</code>.</p>
  
  <p><strong>app.py</strong></p>
  
  <pre class="lang-py prettyprint-override"><code>import psycopg2
  
  def my_function():
      # all connection related code goes here ...
  
      query = "SELECT name,phone FROM customer WHERE name='shanky'"
      cursor.execute(query)
      columns = [i[0] for i in cursor.description]
      customer_response = []
      for row in cursor.fetchall():
          customer_response.append(dict(zip(columns, row)))
  
      query = "SELECT name,id FROM product WHERE name='soap'"
      cursor.execute(query)
      columns = [i[0] for i in cursor.description]
      product_response = []
      for row in cursor.fetchall():
          product_response.append(dict(zip(columns, row)))
  
      return product_response
  </code></pre>
  
  <p><strong>test.py</strong></p>
  
  <pre class="lang-py prettyprint-override"><code>from pytest_mock import mocker
  import psycopg2
  
  def test_my_function(mocker):
      from my_module import app
      mocker.patch('psycopg2.connect')
  
      #first query
      mocked_cursor_one = psycopg2.connect.return_value.cursor.return_value
      mocked_cursor_one.description = [['name'],['phone']]
      mocked_cursor_one.fetchall.return_value = [('shanky', '347539593')]
      mocked_cursor_one.execute.call_args == "SELECT name,phone FROM customer WHERE name='shanky'"
  
      #second query
      mocked_cursor_two = psycopg2.connect.return_value.cursor.return_value
      mocked_cursor_two.description = [['name'],['id']]
      mocked_cursor_two.fetchall.return_value = [('nirma', 12313)]
      mocked_cursor_two.execute.call_args == "SELECT name,id FROM product WHERE name='soap'"
  
      ret = app.my_function()
      assert ret == {'name' : 'nirma', 'id' : 12313}
  </code></pre>
  
  <p>But the mocker always takes the last mock object (the second query).I have already tried multiple hacks, but that didn't work out. How can i mock multiple queries in one function and successfully pass the unit test case? Is it possible to write a unit test case in this fashion or do i need to split the queries in different functions?</p>