

def setUp(self):
    connect(db='mongoenginetest')

    class Author(Document):
        id = SequenceField(primary_key=True)
        name = StringField()

        def __unicode__(self):
            return self.name

        @classmethod
        def pre_init(cls, sender, document, *args, **kwargs):
            signal_output.append(('pre_init signal, %s' % cls.__name__))
            signal_output.append(kwargs['values'])

        @classmethod
        def post_init(cls, sender, document, **kwargs):
            signal_output.append(('post_init signal, %s, document._created = %s' % (document, document._created)))

        @classmethod
        def pre_save(cls, sender, document, **kwargs):
            signal_output.append(('pre_save signal, %s' % document))
            signal_output.append(kwargs)

        @classmethod
        def pre_save_post_validation(cls, sender, document, **kwargs):
            signal_output.append(('pre_save_post_validation signal, %s' % document))
            if kwargs.pop('created', False):
                signal_output.append('Is created')
            else:
                signal_output.append('Is updated')
            signal_output.append(kwargs)

        @classmethod
        def post_save(cls, sender, document, **kwargs):
            dirty_keys = (document._delta()[0].keys() + document._delta()[1].keys())
            signal_output.append(('post_save signal, %s' % document))
            signal_output.append(('post_save dirty keys, %s' % dirty_keys))
            if kwargs.pop('created', False):
                signal_output.append('Is created')
            else:
                signal_output.append('Is updated')
            signal_output.append(kwargs)

        @classmethod
        def pre_delete(cls, sender, document, **kwargs):
            signal_output.append(('pre_delete signal, %s' % document))
            signal_output.append(kwargs)

        @classmethod
        def post_delete(cls, sender, document, **kwargs):
            signal_output.append(('post_delete signal, %s' % document))
            signal_output.append(kwargs)

        @classmethod
        def pre_bulk_insert(cls, sender, documents, **kwargs):
            signal_output.append(('pre_bulk_insert signal, %s' % documents))
            signal_output.append(kwargs)

        @classmethod
        def post_bulk_insert(cls, sender, documents, **kwargs):
            signal_output.append(('post_bulk_insert signal, %s' % documents))
            if kwargs.pop('loaded', False):
                signal_output.append('Is loaded')
            else:
                signal_output.append('Not loaded')
            signal_output.append(kwargs)
    self.Author = Author
    Author.drop_collection()
    Author.id.set_next_value(0)

    class Another(Document):
        name = StringField()

        def __unicode__(self):
            return self.name

        @classmethod
        def pre_delete(cls, sender, document, **kwargs):
            signal_output.append(('pre_delete signal, %s' % document))
            signal_output.append(kwargs)

        @classmethod
        def post_delete(cls, sender, document, **kwargs):
            signal_output.append(('post_delete signal, %s' % document))
            signal_output.append(kwargs)
    self.Another = Another
    Another.drop_collection()

    class ExplicitId(Document):
        id = IntField(primary_key=True)

        @classmethod
        def post_save(cls, sender, document, **kwargs):
            if ('created' in kwargs):
                if kwargs['created']:
                    signal_output.append('Is created')
                else:
                    signal_output.append('Is updated')
    self.ExplicitId = ExplicitId
    ExplicitId.drop_collection()

    class Post(Document):
        title = StringField()
        content = StringField()
        active = BooleanField(default=False)

        def __unicode__(self):
            return self.title

        @classmethod
        def pre_bulk_insert(cls, sender, documents, **kwargs):
            signal_output.append(('pre_bulk_insert signal, %s' % [(doc, {
                'active': documents[n].active,
            }) for (n, doc) in enumerate(documents)]))
            for document in documents:
                if (not document.active):
                    document.active = True
            signal_output.append(kwargs)

        @classmethod
        def post_bulk_insert(cls, sender, documents, **kwargs):
            signal_output.append(('post_bulk_insert signal, %s' % [(doc, {
                'active': documents[n].active,
            }) for (n, doc) in enumerate(documents)]))
            if kwargs.pop('loaded', False):
                signal_output.append('Is loaded')
            else:
                signal_output.append('Not loaded')
            signal_output.append(kwargs)
    self.Post = Post
    Post.drop_collection()
    self.pre_signals = (len(signals.pre_init.receivers), len(signals.post_init.receivers), len(signals.pre_save.receivers), len(signals.pre_save_post_validation.receivers), len(signals.post_save.receivers), len(signals.pre_delete.receivers), len(signals.post_delete.receivers), len(signals.pre_bulk_insert.receivers), len(signals.post_bulk_insert.receivers))
    signals.pre_init.connect(Author.pre_init, sender=Author)
    signals.post_init.connect(Author.post_init, sender=Author)
    signals.pre_save.connect(Author.pre_save, sender=Author)
    signals.pre_save_post_validation.connect(Author.pre_save_post_validation, sender=Author)
    signals.post_save.connect(Author.post_save, sender=Author)
    signals.pre_delete.connect(Author.pre_delete, sender=Author)
    signals.post_delete.connect(Author.post_delete, sender=Author)
    signals.pre_bulk_insert.connect(Author.pre_bulk_insert, sender=Author)
    signals.post_bulk_insert.connect(Author.post_bulk_insert, sender=Author)
    signals.pre_delete.connect(Another.pre_delete, sender=Another)
    signals.post_delete.connect(Another.post_delete, sender=Another)
    signals.post_save.connect(ExplicitId.post_save, sender=ExplicitId)
    signals.pre_bulk_insert.connect(Post.pre_bulk_insert, sender=Post)
    signals.post_bulk_insert.connect(Post.post_bulk_insert, sender=Post)

dataset/ETHPy150Open MongoEngine/mongoengine/tests/test_signals.py SignalTests.setUp/original