title: Mock database connection python
body: <p>I have a file called <code>redis_db.py</code> which has code to connect to redis</p>
  
  <pre><code>import os
  import redis
  import sys
  
  class Database:
         def __init__(self, zset_name):
          redis_host = os.environ.get('REDIS_HOST', '127.0.0.1')
          redis_port = os.environ.get('REDIS_PORT', 6379)
  
          self.db = redis.StrictRedis(host=redis_host, port=redis_port)
          self.zset_name = zset_name
  
      def add(self, key):
          try:
              self.db.zadd(self.zset_name, {key: 0})
          except redis.exceptions.ConnectionError:
              print("Unable to connect to redis host.")
              sys.exit(0)
  </code></pre>
  
  <p>I have another file called app.py which is like this</p>
  
  <pre><code>from flask import Flask
  from redis_db import Database
  
  app = Flask(__name__)
  db = Database('zset')
  
  @app.route('/add_word/word=&lt;word&gt;')
  def add_word(word):
     db.add(word)
     return ("{} added".format(word))
  
  if __name__ == '__main__':
      app.run(host='0.0.0.0', port='8080')
  </code></pre>
  
  <p>Now I am writing unit test for add_word function like this</p>
  
  <pre><code>import unittest
  import sys
  import os
  from unittest import mock
  
  sys.path.append(os.path.dirname(os.path.realpath(__file__)) + "/../api/")
  from api import app  # noqa: E402
  
  
  class Testing(unittest.TestCase):
  
    def test_add_word(self):
        with mock.patch('app.Database') as mockdb:
           mockdb.return_value.add.return_value = ""
        result = app.add_word('shivam')
        self.assertEqual(result, 'shivam word added.')
  
  </code></pre>
  
  <p>Issue I am facing is that even though I am mocking the db method call it is still calling the actual method in the class instead of returning mocked values and during testing I am getting error with message <code>Unable to connect to redis host.</code>. </p>
  
  <p>Can someone please help me in figuring out how can I mock the redis database calls.</p>
  
  <p>I am using unittest module</p>