Wednesday, December 4, 2013

Re: Inconsistent Django test results depending upon how the test is called in Django 1.5.1 running on Python 2.7.4


On Mon, Dec 2, 2013 at 4:41 PM, Paul Whipp <paul.whipp@gmail.com> wrote:

Hi Russ,

Thanks for your detailed response, deserving of this detailed investigation:

I eventually found that it is the simple declaration of a model within the test suites that is causing the problem:

django.contrib.contents.tests causes the problem - even if I removed all of the tests and just imported the file:

I copied and pared the file down to this which still causes the error:

from django.db import models      class ConcreteModel(models.Model):      name = models.CharField(max_length=10)      class ProxyModel(ConcreteModel):      class Meta:  	proxy = True  

If I remove the ProxyModel class the error changes and becomes a recursion depth error.

Renaming the classes does not get rid of the error (and the class names are not used in my apps).

The infinite recursion problem feels somehow related. My models do do something unusual: Much of the application revolves around models belonging to a company so, for convenience, all models have a company property that returns the company that owns them - it often does this by referencing objects on it. In the case of a company, the company property returns self. In the case of a note (the model where we're getting this problem), the content_object is a company and the note's company method accesses its content_object company attribute:

class Note(TimeStampedModel): content_type = models.ForeignKey(ContentType) object_id = models.PositiveIntegerField() content_object = generic.GenericForeignKey('
content_type', 'object_id') ... @property def company(self): return self.content_object.company class Company(BaseModel, ModelWithRandomisedToken): ... @property def company(self): return self

I think this is progress. Declaring ANY model class within the test suite causes the infinite recursion issue on the access. So my theory is that something in the django caching somewhere is getting partly updated when the model is declared but not fully sorted so the caches are broken with respect to content types. This has two symptoms - object references being None and infinite recursion, depending upon how the error gets encountered.

I'm guessing that there is some 'tidying up' of content types after all the models are imported that does not get done for a model arbitrarily defined in the test suite.

Does it sound like I am going in the right direction?

Hi Paul,

The short answer is yes -- it sounds like you're on the right track. I'd need to sit down with actual code to confirm anything, butthe behaviour you're describing would be consistent with a content type caching issue. It's certainly an interesting presentation, but that might be partially due to the specifics of your own code.

Regarding tidying up --  keep in mind that there are two ways to skin this particular cat. While it's certainly good (and preferable) practice for every test to clean up after itself, if you've got a test that isn't under your control (e.g., something in Django's test suite), but is still causing problems, one option is to add the cleanup at the *start* of your own test -- i.e., rather than cleaning up after yourself, make sure everything is clean before you start. It's not ideal, but it works.
 
Yours,
Russ Magee %-)

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAJxq84-vBqpGHg7YbEMPtfbofrtOb2ZWHLZD5%3Dh_sOr1Le5FTg%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate