I have the following code setup, and I have a test case to test the Db.__init__
already, how do I test the __del__
? Can you provide an example?
import psycopg2
class Db(object):
def __init__(self):
import app
conn_string = "host='{}' port='{}' dbname='{}' user='{}' password='{}'".format(app.app.config['DB_HOST'], \
app.app.config['DB_PORT'], app.app.config['DB_NAME'], app.app.config['DB_USER'], \
app.app.config['DB_PASSWORD'])
self.conn = psycopg2.connect(conn_string)
def __del__(self):
self.conn.close()
Test Case
@ddt
class TestDB(unittest.TestCase):
@patch('psycopg2.connect')
def test_db_constructor(self, mock_psycopg2_connect):
mock_psycopg2_connect.returned_value = True
db = Db()
self.assertTrue(db.conn)