Python mock - วิธีที่ถูกต้องในการเยาะเย้ยและจัดการเส้นทางคงที่ที่กำหนดไว้และจัดการการเชื่อมต่อ pymysql

#try block is not in a function and is defined on top in the file

try:
    ssl = {'ca': '/opt/bundle.pem'}
    auth_token = rds_client.generate_db_auth_token(os.environ['DB_HOST'], 3306, os.environ['DB_USER'])
    db_conn = pymysql.connect(host=rds_endpoint, user=db_username, passwd=auth_token, db=db_name, ssl=ssl, connect_timeout=15)
    logger.info(f'Connected successfully to {db_name}')
except Exception as error:
    logger.error(error)
    raise error

...
...
def lambda_handler(event, context):
    try:
        table_name = 'events'

        return write_events(event, table_name, db_conn)
    except Exception as error:
        logger.error(error)
        raise

@pytest.fixture
def init():
    from swu_email_events_db import app
    yield app
...
...
def test_create_rds_conn(rds, init):
    with patch('events_db.app.pymysql.connect') as mock_connect:
        mock_connect.return_value = MagicMock(name='db_conn')
        response = init.lambda_handler({}, {})