TestCase Docs: Problematic import in Codesnippet
|Reported by:||kanu@…||Owned by:||nobody|
|Severity:||Keywords:||Test Signal IntegrityError|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
There is a problem when importing models into your test.py as mentioned in http://docs.djangoproject.com/en/dev/topics/testing/#writing-unit-tests.
from myapp.models import Animal
If you register your signals in the models.py as suggested in the docs. They will be registered twice when testing and therefore will be called twice during test runs.
At least this isn't the demanded behavior at worst it throws a an error where no error is.
class Member(models.Model): user = models.ForeignKey(User, unique=True) ... def create_user_profile(sender,instance,created,**kws): if create: m = Member(user = instance ) m.save() signals.post_save.connect(create_user_profile,sender=User)
The second time create_user_profile() is called it throws a IntegrityError: duplicate key value violates.
To fix this you can change the import in test.py to
from myproject.myapp.models import Animal
from models import Animal
the handler are registered only once and everything works fine when running tests.