I'm currently facing a weird problem with the testing framework after
updating to the latest trunk version of the newforms-admin branch. I
have written a small example and a unittest to explain the issue and
to show that there seems to be some sort of problem after the qsrf
merge. I'm not quite sure where the problem is - unittest or qsrf -
that's why i post.
Below are two simple models Vocabulary and Term. A vocabulary contains
terms. When a new vocabulary is created (saved the first time), it
automatically creates a root term for the vocabulary.
Both models overload the save() method.
Running the unittest given below with django 0.97-newforms-admin-
SVN-7233 all tests pass fine.
When running the unittest given below with django 0.97-newforms-admin-
SVN-7599 the tests give the following error:
======================================================================
FAIL: test_qsrf (nmy.qsrftest.tests.QsrfTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/peter/src/nmy/site-packages/nmy/qsrftest/tests.py", line
27, in test_qsrf
self.failUnlessEqual(v.root, v.root.vocabulary.root)
AssertionError: <Term: Term object> != None
----------------------------------------------------------------------
I tried to track down the problem, checked my unittest, checked my
models - but the only thing I could find is weird: As soon as I
reference the ``vocabulary`` field in a term's save() method the test
fails. If I remove all references to the ``vocabulary`` field from the
save() method the tests pass fine.
I found that a simple read-only operation like print or even a simple
noop reference of the ``vocabulary`` field changes the behaviour of
the unittest.
--- qsrftest/models.py
from django.db import models
class Vocabulary(models.Model):
root = models.ForeignKey('Term', null=True, blank=True,
related_name='root_of')
def save(self):
"""When a vocabulary is saved and it has no root term, a new
root
term is created. This happens only the first time a vocabulary
is
saved.
"""
super(Vocabulary, self).save()
if not self.root:
self.root = Term.objects.create(vocabulary=self)
super(Vocabulary, self).save()
class Term(models.Model):
vocabulary = models.ForeignKey(Vocabulary)
def save(self):
super(Term, self).save()
# As soon as self.vocabulary is referenced here, the unittest
fails.
# It does not matter if the self.vocabulary is printed, used
or changed.
# Any of the following will make the unittest fail with
r7599!
#print "term.vocabulary:", self.vocabulary
self.vocabulary
---
--- qsrftest/tests.py
import unittest
from qsrftest.models import Vocabulary, Term
class QsrfTestCase(unittest.TestCase):
def test_qsrf(self):
v = Vocabulary.objects.create()
# Check if the new vocabulary has a root term
self.failUnless(v.root != None,
'A newly created vocabulary must have a root
term.')
# Check that the root item is associated to the vocabulary.
self.failUnlessEqual(v, v.root.vocabulary,
'The vocabulary of the implicity created root
term must'\
'be set. %s != %s'\
% (v, v.root.vocabulary))
# Check cyclic correctness. This test succeeds with r7433 and
fails
# with r7599 when the ``vocabulary`` field is referenced in
the
# term's save() method.
self.failUnlessEqual(v.root, v.root.vocabulary.root)
---
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---