'Ensure that invalid values cannot be assigned to binary fields.'

def test_binary_validation(self):

    class Attachment(Document, ):
        blob = BinaryField()
    class AttachmentRequired(Document, ):
        blob = BinaryField(required=True)
    class AttachmentSizeLimit(Document, ):
        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()
    self.assertRaises(ValidationError, attachment_required.validate)
    attachment_required.blob = Binary(six.b('\xe6\x00\xc4\xff\x07'))
    attachment_required.validate()
    attachment_size_limit = AttachmentSizeLimit(blob=six.b('\xe6\x00\xc4\xff\x07'))
    self.assertRaises(ValidationError, attachment_size_limit.validate)
    attachment_size_limit.blob = six.b('\xe6\x00\xc4\xff')
    attachment_size_limit.validate()
