text: # -*- coding: utf-8 -*-
  import sys
  sys.path[0:0] = [""]
  
  import unittest
  from datetime import datetime
  
  from mongoengine import *
  
  __all__ = ("ValidatorErrorTest",)
  
  
  class ValidatorErrorTest(unittest.TestCase):
  
      def setUp(self):
          connect(db='mongoenginetest')
  
      def test_to_dict(self):
          """Ensure a ValidationError handles error to_dict correctly.
          """
          error = ValidationError('root')
          self.assertEqual(error.to_dict(), {})
  
          # 1st level error schema
          error.errors = {'1st': ValidationError('bad 1st'), }
          self.assertTrue('1st' in error.to_dict())
          self.assertEqual(error.to_dict()['1st'], 'bad 1st')
  
          # 2nd level error schema
          error.errors = {'1st': ValidationError('bad 1st', errors={
              '2nd': ValidationError('bad 2nd'),
          })}
          self.assertTrue('1st' in error.to_dict())
          self.assertTrue(isinstance(error.to_dict()['1st'], dict))
          self.assertTrue('2nd' in error.to_dict()['1st'])
          self.assertEqual(error.to_dict()['1st']['2nd'], 'bad 2nd')
  
          # moar levels
          error.errors = {'1st': ValidationError('bad 1st', errors={
              '2nd': ValidationError('bad 2nd', errors={
                  '3rd': ValidationError('bad 3rd', errors={
                      '4th': ValidationError('Inception'),
                  }),
              }),
          })}
          self.assertTrue('1st' in error.to_dict())
          self.assertTrue('2nd' in error.to_dict()['1st'])
          self.assertTrue('3rd' in error.to_dict()['1st']['2nd'])
          self.assertTrue('4th' in error.to_dict()['1st']['2nd']['3rd'])
          self.assertEqual(error.to_dict()['1st']['2nd']['3rd']['4th'],
                           'Inception')
  
          self.assertEqual(error.message, "root(2nd.3rd.4th.Inception: ['1st'])")
  
      def test_model_validation(self):
          class User(Document):
              username = StringField(primary_key=True)
              name = StringField(required=True)
  
          User.drop_collection()
  
          try:
              User().validate()
          except ValidationError, e:
              self.assertTrue("User:None" in e.message)
              self.assertEqual(e.to_dict(), {
                  'username': 'Field is required',
                  'name': 'Field is required'})
  
          user = User(username="RossC0", name="Ross").save()
          user.name = None
          try:
              user.save()
          except ValidationError, e:
              self.assertTrue("User:RossC0" in e.message)
              self.assertEqual(e.to_dict(), {
                  'name': 'Field is required'})
  
      def test_fields_rewrite(self):
          class BasePerson(Document):
              name = StringField()
              age = IntField()
              meta = {'abstract': True}
  
          class Person(BasePerson):
              name = StringField(required=True)
  
          p = Person(age=15)
          self.assertRaises(ValidationError, p.validate)
  
      def test_datetime_validation(self):
          class DTDoc(Document):
              date = DateTimeField()
  
          dtd = DTDoc()
          dtd.date = 'whatever'
          self.assertRaises(ValidationError, dtd.save)
  
          # make sure that passing a parsable datetime works
          dtd = DTDoc()
          dtd.date = str(datetime.utcnow())
          dtd.save()
          dtd.reload()
          self.assertTrue(isinstance(dtd.date, datetime))
  
      def test_embedded_document_validation(self):
          """Ensure that embedded documents may be validated.
          """
          class Comment(EmbeddedDocument):
              date = DateTimeField()
              content = StringField(required=True)
  
          comment = Comment()
          self.assertRaises(ValidationError, comment.validate)
  
          comment.content = 'test'
          comment.validate()
  
          comment.date = 4
          self.assertRaises(ValidationError, comment.validate)
  
          comment.date = datetime.now()
          comment.validate()
          self.assertEqual(comment._instance, None)
  
      def test_embedded_db_field_validate(self):
  
          class SubDoc(EmbeddedDocument):
              val = IntField(required=True)
  
          class Doc(Document):
              id = StringField(primary_key=True)
              e = EmbeddedDocumentField(SubDoc, db_field='eb')
  
          try:
              Doc(id="bad").validate()
          except ValidationError, e:
              self.assertTrue("SubDoc:None" in e.message)
              self.assertEqual(e.to_dict(), {
                  "e": {'val': 'OK could not be converted to int'}})
  
          Doc.drop_collection()
  
          Doc(id="test", e=SubDoc(val=15)).save()
  
          doc = Doc.objects.first()
          keys = doc.to_dict().keys()
          self.assertEqual(2, len(keys))
          self.assertTrue('e' in keys)
          self.assertTrue('id' in keys)
  
          with self.assertRaises(ValueError):
              doc.e.val = "OK"
  
  
  if __name__ == '__main__':
      unittest.main()
  