add tests for file types and fix a python 3 issue with handling files coming from fieldstorage objects

This commit is contained in:
Doug Hellmann 2013-01-18 16:45:51 -05:00
parent 003fed76a3
commit a896345971
2 changed files with 34 additions and 1 deletions

View File

@ -427,3 +427,36 @@ class TestTypes(unittest.TestCase):
a.datatype = [weakref.ref(int)]
assert isinstance(a.datatype, list)
assert a.datatype[0] is int
def test_file_get_content_by_reading(self):
class buffer:
def read(self):
return 'abcdef'
f = types.File(file=buffer())
assert f.content == 'abcdef'
def test_file_content_overrides_file(self):
class buffer:
def read(self):
return 'from-file'
f = types.File(content='from-content', file=buffer())
assert f.content == 'from-content'
def test_file_setting_content_discards_file(self):
class buffer:
def read(self):
return 'from-file'
f = types.File(file=buffer())
f.content = 'from-content'
assert f.content == 'from-content'
def test_file_field_storage(self):
class buffer:
def read(self):
return 'from-file'
class fieldstorage:
filename = 'static.json'
file = buffer()
type = 'application/json'
f = types.File(fieldstorage=fieldstorage)
assert f.content == 'from-file'

View File

@ -583,7 +583,7 @@ class File(Base):
if fieldstorage.file:
self._file = fieldstorage.file
self.filename = fieldstorage.filename
self.contenttype = unicode(fieldstorage.type)
self.contenttype = text(fieldstorage.type)
else:
self._content = fieldstorage.value