From a896345971b5da84b57f90d6c381787e57b3b385 Mon Sep 17 00:00:00 2001 From: Doug Hellmann Date: Fri, 18 Jan 2013 16:45:51 -0500 Subject: [PATCH] add tests for file types and fix a python 3 issue with handling files coming from fieldstorage objects --- wsme/tests/test_types.py | 33 +++++++++++++++++++++++++++++++++ wsme/types.py | 2 +- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/wsme/tests/test_types.py b/wsme/tests/test_types.py index f246aa5..2784a7c 100644 --- a/wsme/tests/test_types.py +++ b/wsme/tests/test_types.py @@ -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' diff --git a/wsme/types.py b/wsme/types.py index 8758569..bbd0c91 100644 --- a/wsme/types.py +++ b/wsme/types.py @@ -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