On Sat, 2009-04-11 at 08:47 +0800, Russell Keith-Magee wrote: > On Sat, Apr 11, 2009 at 5:53 AM, Poromenos <porome...@gmail.com> wrote: > > > > Hello, > > I created a model that has a ForeignKey to ContentType, but now I > > can't use my test fixtures, since the IDs they point to are random > > every time the test database is created. I can't dump the contenttypes > > data because then I get many duplicate inserts, since Django rebuilds > > them every time it sets up the test DB. Is there any way for me to use > > both ContentType and unit tests? > > The IDs won't be completely random, but they certainly will be fragile > and subject to change. > > You have discovered ticket #7052; unfortunately, there isn't any easy > fix at the moment. > > 1) Don't use fixtures - create your test objects in the setUp() method > of your test using normal Python code. > > 2) Use the fixture to define the basic data in your objects, and then > use the setUp() method to modify the contenttypes at runtime, based on > the current values in the database. This means you don't serialize the > contenttype objects themselves in your fixture, and you use and > foreignkey reference to a contenttype as a temporary placeholder for > the real value that will be inserted at runtime. > > Obviously, neither of these are ideal solutions. This bug is something > I want to look at for Django v1.2.
Well, I'd argue (1) is the ideal solution. Creating objects programmatically is robust, self-documenting and encourages testers to think about the minimum requirements needed for a self-contained unittest. It's also fairly easy. Tends to be my normal practice. So opinions clearly differ there. Which doesn't invalidate Russell's opinion, but I wouldn't want people to think it's all horrible when there's already a perfectly standard and supported way of doing this (the setUp() method exists to set things up for unittests). Regards, Malcolm --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---