def test_insert_violations_with_error(self):
"""Test insert_violations handles errors during insert.
Setup:
* Create mocks:
* self.dao.conn
* self.dao.get_latest_snapshot_timestamp
* self.dao.create_snapshot_table
* Create side effect for one violation to raise an error.
Expect:
* Log MySQLError when table insert error occurs and return list
of errors.
* Return a tuple of (num_violations-1, [violation])
"""
resource_name = 'policy_violations'
self.dao.get_latest_snapshot_timestamp = mock.MagicMock(
return_value=self.fake_snapshot_timestamp)
self.dao.create_snapshot_table = mock.MagicMock(
return_value=self.fake_table_name)
violation_dao.LOGGER = mock.MagicMock()
def insert_violation_side_effect(*args, **kwargs):
if args[2] == self.expected_fake_violations[1]:
raise MySQLdb.DataError(
self.resource_name, mock.MagicMock())
else:
return mock.DEFAULT
self.dao.execute_sql_with_commit = mock.MagicMock(
side_effect=insert_violation_side_effect)
actual = self.dao.insert_violations(
self.fake_flattened_violations,
self.resource_name)
expected = (2, [self.expected_fake_violations[1]])
self.assertEqual(expected, actual)
self.assertEquals(1, violation_dao.LOGGER.error.call_count)
violation_dao_test.py 文件源码
python
阅读 17
收藏 0
点赞 0
评论 0
评论列表
文章目录