def test__connect_mysqldb(): """ Test the _connect function in the MySQL module """ mysqldb_connect_mock = MagicMock(autospec=True, return_value=MockMySQLConnect()) with patch.dict(mysql.__salt__, {"config.option": MagicMock()}): with patch("MySQLdb.connect", mysqldb_connect_mock): mysql._connect() assert "mysql.error" not in mysql.__context__
def test__connect_mysqldb(self): """ Test the _connect function in the MySQL module """ with patch.dict(mysql.__salt__, {"config.option": MagicMock()}): with patch("MySQLdb.connect", return_value=MockMySQLConnect()): ret = mysql._connect() self.assertNotIn("mysql.error", mysql.__context__)
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'")
def test__connect_mysqldb(): """ Test the _connect function in the MySQL module """ mysqldb_connect_mock = MagicMock(autospec=True, return_value=MockMySQLConnect()) with patch.dict(mysql.__salt__, {"config.option": MagicMock()}): with patch("MySQLdb.connect", mysqldb_connect_mock): ret = mysql._connect() assert "mysql.error" not in mysql.__context__ # If 'use_unicode' is activated here, it would retrieve utf8 strings # as unicode() objects in salt and we do not want that. So it needs # to be False _, connargs = mysqldb_connect_mock.call_args assert connargs["use_unicode"] is False
def test__connect_pymysql_exception(self): """ Test the _connect function in the MySQL module """ with patch.dict(mysql.__salt__, {"config.option": MagicMock()}): with patch( "MySQLdb.connect", side_effect=pymysql.err.InternalError( 1698, "Access denied for user 'root'@'localhost'"), ): ret = mysql._connect() self.assertIn("mysql.error", mysql.__context__) self.assertEqual( mysql.__context__["mysql.error"], "MySQL Error 1698: Access denied for user 'root'@'localhost'", )