Django unit tests for pre/post_save, pre/post_delete signals

1,663 views
Skip to first unread message

George Vilches

unread,
Aug 21, 2007, 9:41:16 AM8/21/07
to django-d...@googlegroups.com
All,

I've looked everywhere in the Django unit tests and can't find anywhere
that's testing these four signals:

pre_save
post_save
pre_delete
post_delete

I found the dispatcher tests, which cover all the generic bits, but
nothing specifically for these model signals.

On the assumption that I'm not blind, I built a unit test for it, which
I've posted below. I've also attached the unit test to #4879 (
http://code.djangoproject.com/ticket/4879 ), because my unit test is
dependent on the post_save signal created keyword. It would be easy
enough to strip it (the tests will pass after a few extra lines of
output were removed, I'll do it if necessary), but I figured adding a
unit test for it would make the ticket pretty painless for submission.

Thoughts?
gav


django/tests/modeltests/signals/models.py:

"""
Testing signals before/after saving and deleting.
"""

from django.db import models
from django.dispatch import dispatcher

class Person(models.Model):
first_name = models.CharField(max_length=20)
last_name = models.CharField(max_length=20)

def __unicode__(self):
return u"%s %s" % (self.first_name, self.last_name)


def pre_save_test(sender, instance, **kwargs):
print 'pre_save signal,', instance

def post_save_test(sender, instance, **kwargs):
print 'post_save signal,', instance
if 'created' in kwargs:
if kwargs['created']:
print 'Is created'
else:
print 'Is updated'

def pre_delete_test(sender, instance, **kwargs):
print 'pre_delete signal,', instance
print 'instance.id is not None: %s' % (instance.id != None)

def post_delete_test(sender, instance, **kwargs):
print 'post_delete signal,', instance
print 'instance.id is None: %s' % (instance.id == None)

__test__ = {'API_TESTS':"""

>>> dispatcher.connect(pre_save_test, signal=models.signals.pre_save)
>>> dispatcher.connect(post_save_test, signal=models.signals.post_save)
>>> dispatcher.connect(pre_delete_test, signal=models.signals.pre_delete)
>>> dispatcher.connect(post_delete_test, signal=models.signals.post_delete)
>>> p1 = Person(first_name='John', last_name='Smith')
>>> p1.save()
pre_save signal, John Smith
post_save signal, John Smith
Is created

>>> p1.first_name = 'Tom'
>>> p1.save()
pre_save signal, Tom Smith
post_save signal, Tom Smith
Is updated

>>> p1.delete()
pre_delete signal, Tom Smith
instance.id is not None: True
post_delete signal, Tom Smith
instance.id is None: True

>>> p2 = Person(first_name='James', last_name='Jones')
>>> p2.id = 99999
>>> p2.save()
pre_save signal, James Jones
post_save signal, James Jones
Is created

>>> p2.id = 99998
>>> p2.save()
pre_save signal, James Jones
post_save signal, James Jones
Is created

>>> p2.delete()
pre_delete signal, James Jones
instance.id is not None: True
post_delete signal, James Jones
instance.id is None: True

>>> Person.objects.all()
[<Person: James Jones>]

>>> dispatcher.disconnect(post_delete_test,
signal=models.signals.post_delete)
>>> dispatcher.disconnect(pre_delete_test,
signal=models.signals.pre_delete)
>>> dispatcher.disconnect(post_save_test, signal=models.signals.post_save)
>>> dispatcher.disconnect(pre_save_test, signal=models.signals.pre_save)
"""}

Russell Keith-Magee

unread,
Aug 21, 2007, 9:47:44 PM8/21/07
to django-d...@googlegroups.com
On 8/21/07, George Vilches <g...@thataddress.com> wrote:
>
> All,
>
> I've looked everywhere in the Django unit tests and can't find anywhere
> that's testing these four signals:
...
> Thoughts?

Only one thought - put this in a ticket so it doesn't get lost or forgotten.

Yours,
Russ Magee %-)

George Vilches

unread,
Aug 21, 2007, 9:52:28 PM8/21/07
to django-d...@googlegroups.com

I've already attached the unit test patch to this ticket:
http://code.djangoproject.com/ticket/4879 . I'll add a note in there
that's a little more specific about the unit tests. The unit tests were
done appropriately then (up to Django snuff)? :)

Thanks,
George

Reply all
Reply to author
Forward
0 new messages