fix validator of BinaryField. In fact bson.Binary fails if we give it unicode in input #273
This commit is contained in:
		| @@ -1460,10 +1460,10 @@ class BinaryField(BaseField): | |||||||
|         return Binary(value) |         return Binary(value) | ||||||
|  |  | ||||||
|     def validate(self, value): |     def validate(self, value): | ||||||
|         if not isinstance(value, (six.binary_type, six.text_type, Binary)): |         if not isinstance(value, (six.binary_type, Binary)): | ||||||
|             self.error('BinaryField only accepts instances of ' |             self.error('BinaryField only accepts instances of ' | ||||||
|                        '(%s, %s, Binary)' % ( |                        '(%s, %s, Binary)' % ( | ||||||
|                            six.binary_type.__name__, six.text_type.__name__)) |                            six.binary_type.__name__, Binary.__name__)) | ||||||
|  |  | ||||||
|         if self.max_bytes is not None and len(value) > self.max_bytes: |         if self.max_bytes is not None and len(value) > self.max_bytes: | ||||||
|             self.error('Binary value is too long') |             self.error('Binary value is too long') | ||||||
|   | |||||||
| @@ -2967,37 +2967,34 @@ class FieldTest(MongoDBTestCase): | |||||||
|         self.assertEqual(MIME_TYPE, attachment_1.content_type) |         self.assertEqual(MIME_TYPE, attachment_1.content_type) | ||||||
|         self.assertEqual(BLOB, six.binary_type(attachment_1.blob)) |         self.assertEqual(BLOB, six.binary_type(attachment_1.blob)) | ||||||
|  |  | ||||||
|     def test_binary_validation(self): |     def test_binary_validation_succeeds(self): | ||||||
|         """Ensure that invalid values cannot be assigned to binary fields. |         """Ensure that valid values can be assigned to binary fields. | ||||||
|         """ |         """ | ||||||
|         class Attachment(Document): |  | ||||||
|             blob = BinaryField() |  | ||||||
|  |  | ||||||
|         class AttachmentRequired(Document): |         class AttachmentRequired(Document): | ||||||
|             blob = BinaryField(required=True) |             blob = BinaryField(required=True) | ||||||
|  |  | ||||||
|         class AttachmentSizeLimit(Document): |         class AttachmentSizeLimit(Document): | ||||||
|             blob = BinaryField(max_bytes=4) |             blob = BinaryField(max_bytes=4) | ||||||
|  |  | ||||||
|         Attachment.drop_collection() |  | ||||||
|         AttachmentRequired.drop_collection() |  | ||||||
|         AttachmentSizeLimit.drop_collection() |  | ||||||
|  |  | ||||||
|         attachment = Attachment() |  | ||||||
|         attachment.validate() |  | ||||||
|         attachment.blob = 2 |  | ||||||
|         self.assertRaises(ValidationError, attachment.validate) |  | ||||||
|  |  | ||||||
|         attachment_required = AttachmentRequired() |         attachment_required = AttachmentRequired() | ||||||
|         self.assertRaises(ValidationError, attachment_required.validate) |         self.assertRaises(ValidationError, attachment_required.validate) | ||||||
|         attachment_required.blob = Binary(six.b('\xe6\x00\xc4\xff\x07')) |         attachment_required.blob = Binary(six.b('\xe6\x00\xc4\xff\x07')) | ||||||
|         attachment_required.validate() |         attachment_required.validate() | ||||||
|  |  | ||||||
|         attachment_size_limit = AttachmentSizeLimit( |         _5_BYTES = six.b('\xe6\x00\xc4\xff\x07') | ||||||
|             blob=six.b('\xe6\x00\xc4\xff\x07')) |         _4_BYTES = six.b('\xe6\x00\xc4\xff') | ||||||
|         self.assertRaises(ValidationError, attachment_size_limit.validate) |         self.assertRaises(ValidationError, AttachmentSizeLimit(blob=_5_BYTES).validate) | ||||||
|         attachment_size_limit.blob = six.b('\xe6\x00\xc4\xff') |         AttachmentSizeLimit(blob=_4_BYTES).validate() | ||||||
|         attachment_size_limit.validate() |  | ||||||
|  |     def test_binary_validation_fails(self): | ||||||
|  |         """Ensure that invalid values cannot be assigned to binary fields.""" | ||||||
|  |  | ||||||
|  |         class Attachment(Document): | ||||||
|  |             blob = BinaryField() | ||||||
|  |  | ||||||
|  |         for invalid_data in (2, u'Im_a_unicode', ['some_str']): | ||||||
|  |             self.assertRaises(ValidationError, Attachment(blob=invalid_data).validate) | ||||||
|  |  | ||||||
|  |  | ||||||
|     def test_binary_field_primary(self): |     def test_binary_field_primary(self): | ||||||
|         class Attachment(Document): |         class Attachment(Document): | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user