Add GridFSProxy.__nonzero__
For documents that do not have a value set for a given field, most field types return None (or [] in the case of ListField). This makes it easy to test whether a field has been set using "if doc.field". FileFields, on the other hand, always return a GridFSProxy. Adding GridFSProxy.__nonzero__ which simply checks for a grid_id allows the same boolean-test pattern for FileFields, as well.
This commit is contained in:
@@ -1300,6 +1300,21 @@ class FieldTest(unittest.TestCase):
|
||||
|
||||
TestFile.drop_collection()
|
||||
|
||||
def test_file_boolean(self):
|
||||
"""Ensure that a boolean test of a FileField indicates its presence
|
||||
"""
|
||||
class TestFile(Document):
|
||||
file = FileField()
|
||||
|
||||
testfile = TestFile()
|
||||
self.assertFalse(bool(testfile.file))
|
||||
testfile.file = 'Hello, World!'
|
||||
testfile.file.content_type = 'text/plain'
|
||||
testfile.save()
|
||||
self.assertTrue(bool(testfile.file))
|
||||
|
||||
TestFile.drop_collection()
|
||||
|
||||
def test_geo_indexes(self):
|
||||
"""Ensure that indexes are created automatically for GeoPointFields.
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user