Created
October 5, 2010 00:53
-
-
Save wilkes/610755 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def copy_to_temp(file_upload): | |
tmp = tempfile.NamedTemporaryFile(delete=False) | |
while True: | |
data = file_upload.read(8192) | |
tmp.write(data) | |
if not data: | |
break | |
tmp.flush() | |
return tmp | |
# I want to assert that | |
# - NamedTemporaryFile is called with delete=False | |
# - file_upload.read(8192) is called and returns '' | |
# - tmp.write is called with '' | |
# - tmp.flush is called | |
# - tmp is returned | |
=== Result | |
from nose.tools import * | |
from mock import Mock, patch | |
@patch('tempfile.NamedTemporaryFile') | |
def test_copy_to_temp(MockTempFile): | |
tmp = MockTempFile.return_value | |
file_upload = Mock() | |
file_upload.read.return_value = "" | |
result = copy_to_temp(file_upload) | |
MockTempFile.assert_called_with(delete=False) | |
tmp.write.assert_called_with("") | |
assert tmp.flush.called | |
assert tmp == result | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment