Sunday, December 1, 2013

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

I have test cases that pass when tested individually, pass when the full app is tested but fail when the tests for the entire project are run:

(lsapi)~ $ django test  Creating test database for alias 'default'...  .................................................................................................................................................s.....................................................................................................................................x..........................................................................................................................................................................................................................................................Exception RuntimeError: 'maximum recursion depth exceeded' in <function remove at 0x13447d0> ignored  Exception RuntimeError: 'maximum recursion depth exceeded' in <function remove at 0x13447d0> ignored  Exception RuntimeError: 'maximum recursion depth exceeded' in <function remove at 0x13447d0> ignored  ...ss........E.....................................................................................................................E.E.Setting content_object to liquid app one  .EEE.EEEE...............................................................................................................  ======================================================================  ERROR: test_get_direct_notes (lsapi.tests.DirectGetTestCase)  ----------------------------------------------------------------------  Traceback (most recent call last):    File "ls-api/lsapi/tests.py", line 869, in _method  ....    File "ls-core/lscore/model/note.py", line 37, in company      return self.content_object.company  AttributeError: 'NoneType' object has no attribute 'company'  ...      (lsapi)~ $ django test lsapi.NotesTestCase.test_wrong_user_cannot_put  Creating test database for alias 'default'...  .  ----------------------------------------------------------------------  Ran 1 test in 0.241s    OK  Destroying test database for alias 'default'...      (lsapi)~ $ django test lsapi  Creating test database for alias 'default'...  ...................................................ss..................................................................................................................................Setting content_object to liquid app one  ...................Exception RuntimeError: 'maximum recursion depth exceeded while calling a Python object' in <_ctypes.DictRemover object at 0x46dac70> ignored  .....................................................................................................  ----------------------------------------------------------------------  Ran 303 tests in 71.469s    OK (skipped=2)  Destroying test database for alias 'default'...  

The 'django' command is an alias for django-admin.py. So the tests pass if run as a single case or as a test suite when testing the entire app but fail with errors if run when running the tests for all of the apps in the project.

Investigating the error in the full suite test: It is the result of an attribute being None when it 'should' have a value. The attribute is a content_object set up in a fixture.

I added a debugging setattr on the class and nothing is setting it to None so it seems that the fixture code is not being properly executed to set up the object leading to the errors. The fixture is using content types because it is for 'notes' that can be associated with various model classes:

notes.py:

...  class Note(TimeStampedModel):      content_type = models.ForeignKey(ContentType)      object_id = models.PositiveIntegerField()      content_object = generic.GenericForeignKey('content_type', 'object_id')  ...      def __setattr__(self, key, value):  	if key == 'content_object':  	    if value is None:  		import pdb; pdb.set_trace()  	    print('Setting content_object to {0}'.format(value))  	return super(Note, self).__setattr__(key, value)  

local_test.json:

...   {"pk": 1, "model": "lscore.note",    "fields": {"content_type": 16,  	     "object_id": 2,  	     "created_by": 4,  	     "text": "This is the note text"}},  ...  

Thinking on this further, GenericForeignKey is likely to work by accessing the content_type/object_id fields and the fixtures code will be responsible for putting these directly into the database. On this basis, I trapped the error and investigated the database at the point when it occurred in the full suite test. Sure enough, the content object has not been set in the test database when the error occurs. The above fixture works fine in the other test invocations and all of the fixtures are in a single file which is the only fixtures file specified and loaded for the test case.

Looking at the recursion errors which only occur on the full suite testing and which might conceivably have something to do with this, I added sys.setrecursionlimit(3000) to the relevant tests.py file. This eliminated the recursion errors but had no effect on the test errors described above.

So at this stage I still have a test suite that works fine when tested individually or as an app (e.g. 'django-admin.py lsapi') but which consistently fails if tested when all of the project's apps are tested together (e.g. 'django-admin.py test').

Can anyone shed any light on this or let me know how I might address the problem?

--
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/26f4a2bf-7ac3-457f-b93d-3d4e189dfbdb%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment