Ticket #4879: django_unit_test_model_signals.diff

File django_unit_test_model_signals.diff, 2.6 KB (added by George Vilches <gav@…>, 17 years ago)

Unit test for all model signals, including testing the post_save created keyword

  • django_live/tests/modeltests/signals/models.py

     
     1"""
     2Testing signals before/after saving and deleting.
     3"""
     4
     5from django.db import models
     6from django.dispatch import dispatcher
     7
     8class Person(models.Model):
     9    first_name = models.CharField(max_length=20)
     10    last_name = models.CharField(max_length=20)
     11
     12    def __unicode__(self):
     13        return u"%s %s" % (self.first_name, self.last_name)
     14
     15
     16def pre_save_test(sender, instance, **kwargs):
     17    print 'pre_save signal,', instance
     18
     19def post_save_test(sender, instance, **kwargs):
     20    print 'post_save signal,', instance
     21    if 'created' in kwargs:
     22        if kwargs['created']:
     23            print 'Is created'
     24        else:
     25            print 'Is updated'
     26
     27def pre_delete_test(sender, instance, **kwargs):
     28    print 'pre_delete signal,', instance
     29    print 'instance.id is not None: %s' % (instance.id != None) 
     30
     31def post_delete_test(sender, instance, **kwargs):
     32    print 'post_delete signal,', instance
     33    print 'instance.id is None: %s' % (instance.id == None) 
     34
     35__test__ = {'API_TESTS':"""
     36
     37>>> dispatcher.connect(pre_save_test, signal=models.signals.pre_save)
     38>>> dispatcher.connect(post_save_test, signal=models.signals.post_save)
     39>>> dispatcher.connect(pre_delete_test, signal=models.signals.pre_delete)
     40>>> dispatcher.connect(post_delete_test, signal=models.signals.post_delete)
     41>>> p1 = Person(first_name='John', last_name='Smith')
     42>>> p1.save()
     43pre_save signal, John Smith
     44post_save signal, John Smith
     45Is created
     46
     47>>> p1.first_name = 'Tom'
     48>>> p1.save()
     49pre_save signal, Tom Smith
     50post_save signal, Tom Smith
     51Is updated
     52
     53>>> p1.delete()
     54pre_delete signal, Tom Smith
     55instance.id is not None: True
     56post_delete signal, Tom Smith
     57instance.id is None: True
     58
     59>>> p2 = Person(first_name='James', last_name='Jones')
     60>>> p2.id = 99999
     61>>> p2.save()
     62pre_save signal, James Jones
     63post_save signal, James Jones
     64Is created
     65
     66>>> p2.id = 99998
     67>>> p2.save()
     68pre_save signal, James Jones
     69post_save signal, James Jones
     70Is created
     71
     72>>> p2.delete()
     73pre_delete signal, James Jones
     74instance.id is not None: True
     75post_delete signal, James Jones
     76instance.id is None: True
     77
     78>>> Person.objects.all()
     79[<Person: James Jones>]
     80
     81>>> dispatcher.disconnect(post_delete_test, signal=models.signals.post_delete)
     82>>> dispatcher.disconnect(pre_delete_test, signal=models.signals.pre_delete)
     83>>> dispatcher.disconnect(post_save_test, signal=models.signals.post_save)
     84>>> dispatcher.disconnect(pre_save_test, signal=models.signals.pre_save)
     85"""}
Back to Top