def test_query_error(self): connect_mock = MagicMock() with patch.object(mysql, '_connect', connect_mock): with patch.dict(mysql.__salt__, {'config.option': MagicMock()}): # Use the OperationalError from the salt mysql module because that # exception can come from either MySQLdb or pymysql side_effect = mysql.OperationalError(9999, 'Something Went Wrong') with patch.object(mysql, '_execute', MagicMock(side_effect=side_effect)): mysql.query('testdb', 'SELECT * FROM testdb') self.assertIn('mysql.error', mysql.__context__) expected = 'MySQL Error 9999: Something Went Wrong' self.assertEqual(mysql.__context__['mysql.error'], expected)
def test_query_error(): connect_mock = MagicMock() with patch.object(mysql, "_connect", connect_mock): with patch.dict(mysql.__salt__, {"config.option": MagicMock()}): # Use the OperationalError from the salt mysql module because that # exception can come from either MySQLdb or pymysql side_effect = mysql.OperationalError(9999, "Something Went Wrong") with patch.object(mysql, "_execute", MagicMock(side_effect=side_effect)): mysql.query("testdb", "SELECT * FROM testdb") assert "mysql.error" in mysql.__context__ expected = "MySQL Error 9999: Something Went Wrong" assert mysql.__context__["mysql.error"] == expected
def test__connect_mysqldb_exception(): """ Test the _connect function in the MySQL module """ with patch.dict(mysql.__salt__, {"config.option": MagicMock()}): with patch( "MySQLdb.connect", side_effect=mysql.OperationalError( 1698, "Access denied for user 'root'@'localhost'"), ): ret = mysql._connect() assert "mysql.error" in mysql.__context__ assert ( mysql.__context__["mysql.error"] == "MySQL Error 1698: Access denied for user 'root'@'localhost'")