title: Python Mocking the postgres database
body: <p>MagicMock name='con.cursor.fetchall' id='a121213312'
  I want to have value when I call read function</p>
  
  <p>db.py</p>
  
  <pre><code>try:
      con = psycopg2.connect(
                      host="yhvh",
                      database="python_db",
                      user="postgres",
                      password="ichoose",
                      )
  except:
      print("Unable to connect database")
  
  # Open a cursor to perform database operation
  cur = con.cursor()
  
  def read(con):
      """
      Read data in Database
      """
      print("Read")
      cur = con.cursor()
  
      # execute the query
      data ="SELECT id, name FROM employees"
      cur.execute(
          data
      )
      # fetchall - returns all entries
      rows = cur.fetchall()
  
      for r in rows:
          print(f"id {r[0]} name {r[1]}")
  
      return rows
  
  </code></pre>
  
  <p>test_db.py</p>
  
  <pre><code>class TestDb(unittest.TestCase):
      """
      Study
          - Mock and Unittes
      """
      def test_read(self):
          expected = (9, 'jibreel')
  
          with patch("db.con") as mock_connect:
              mock_con = mock_connect.return_value
              mock_cur = mock_con.cursor.return_value
              mock_cur.fetchall.return_value = expected
  
              result = db.read(mock_connect)
              print(result)
              self.assertEqual(result, expected)
  
  
  </code></pre>
  
  <p>The error when I test it</p>
  
  <p>AssertionError: MagicMock name='con.cursor.fetchall' id='a121213312' != (9, 'jibreel')</p>