'DB Ref usage in dict_fields.'

def test_db_ref_usage(self):

    class User(Document, ):
        name = StringField()
    class Book(Document, ):
        name = StringField()
        author = ReferenceField(User)
        extra = DictField()
        meta = {'ordering': ['+name']}
        def __unicode__(self):
            return self.name
        def __str__(self):
            return self.name
    User.drop_collection()
    Book.drop_collection()
    bob = User.objects.create(name='Bob')
    jon = User.objects.create(name='Jon')
    karl = User.objects.create(name='Karl')
    susan = User.objects.create(name='Susan')
    peter = User.objects.create(name='Peter')
    Book.objects.create(name='1', author=bob, extra={'a': bob.to_dbref(), 'b': [karl.to_dbref(), susan.to_dbref()]})
    Book.objects.create(name='2', author=bob, extra={'a': bob.to_dbref(), 'b': karl.to_dbref()})
    Book.objects.create(name='3', author=bob, extra={'a': bob.to_dbref(), 'c': [jon.to_dbref(), peter.to_dbref()]})
    Book.objects.create(name='4', author=bob)
    Book.objects.create(name='5', author=jon)
    Book.objects.create(name='6', author=peter)
    Book.objects.create(name='7', author=jon)
    Book.objects.create(name='8', author=jon)
    Book.objects.create(name='9', author=jon, extra={'a': peter.to_dbref()})
    self.assertEqual(','.join([str(b) for b in Book.objects.all()]), '1,2,3,4,5,6,7,8,9')
    self.assertEqual(','.join([str(b) for b in Book.objects.filter(((Q(extra__a=bob) | Q(author=bob)) | Q(extra__b=bob)))]), '1,2,3,4')
    self.assertEqual(','.join([str(b) for b in Book.objects.filter(((Q(extra__a__all=[karl, susan]) | Q(author__all=[karl, susan])) | Q(extra__b__all=[karl.to_dbref(), susan.to_dbref()])))]), '1')
    self.assertEqual(u','.join([str(b) for b in Book.objects.filter(__raw__={'$where': "\n                                                                                                                                                                                function(){\n                                                                                                                                                                                                return    this.name    ==    '1'    ||\n                                                                                                                                                                                                                            this.name    ==    '2';}"})]), '1,2')
