def test_write_zip(self):
with patch('%s.zipfile.ZipFile' % pbm, autospec=True) as mock_zf:
with patch('%s.logger' % pbm, autospec=True) as mock_logger:
self.cls._write_zip('myfsrc', 'mypath.zip')
# the only way I can find to capture attributes being set on the ZipInfo
# is to not mock it, but use a real ZipInfo object. Unfortunately, that
# makes assertin on calls a bit more difficult...
assert len(mock_zf.mock_calls) == 4
assert mock_zf.mock_calls[0] == call('mypath.zip', 'w')
assert mock_zf.mock_calls[1] == call().__enter__()
assert mock_zf.mock_calls[3] == call().__exit__(None, None, None)
# ok, now handle the second call, which should have the ZipInfo
# as its first argument...
# test that it's the right chained method call
assert mock_zf.mock_calls[2][0] == '().__enter__().writestr'
# test its arguments
arg_tup = mock_zf.mock_calls[2][1]
assert isinstance(arg_tup[0], ZipInfo)
assert arg_tup[0].filename == 'webhook2lambda2sqs_func.py'
assert arg_tup[0].date_time == (2016, 7, 1, 2, 3, 4)
assert arg_tup[0].external_attr == 0x0755 << 16
assert arg_tup[1] == 'myfsrc'
assert mock_logger.mock_calls == [
call.debug('setting zipinfo date to: %s', (2016, 7, 1, 2, 3, 4)),
call.debug('setting zipinfo file mode to: %s', (0x0755 << 16)),
call.debug('writing zip file at: %s', 'mypath.zip')
]
评论列表
文章目录