Adding a test to ensure exception handling is correct.

This commit is contained in:
Gareth J. Greenaway 2018-12-14 09:16:09 -08:00
parent 858cfac113
commit b85c5bf6d9
No known key found for this signature in database
GPG key ID: 10B62F8A7CAD7A41

View file

@ -282,6 +282,23 @@ class MySQLTestCase(TestCase, LoaderModuleMockMixin):
def test_query(self):
self._test_call(mysql.query, 'SELECT * FROM testdb', 'testdb', 'SELECT * FROM testdb')
def test_query_error(self):
def mock_execute(cur, query):
'''
Mock of info method
'''
raise MySQLdb.OperationalError(9999, 'Something Went Wrong')
connect_mock = MagicMock()
with patch.object(mysql, '_connect', connect_mock):
with patch.dict(mysql.__salt__, {'config.option': MagicMock()}):
with patch.object(mysql, '_execute') as execute_mock:
execute_mock.side_effect = mock_execute
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_call(self, function, expected_sql, *args, **kwargs):
connect_mock = MagicMock()
with patch.object(mysql, '_connect', connect_mock):