Hi,

i am cross-posting this from Django users because it might be more an
internal issue than a user problem. The original post is here:

http://groups.google.com/group/django-users/browse_thread/thread/652f35c90ef14f23

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 also tested this with  0.97-newforms-admin-SVN-7609 (which has some
bugfixes regarding queries) but the issue remains.

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.
The problem is arises when I use a relation from a Vocabulary to a
Term instance. The term instance has a field which relates back to
the Vocabulary - when using the relation to the vocabulary and
then again want to reference the term, the reference is empty.

In other words:

v.root != v.root.vocabulary.root (where v==vocabulary)

This is wrong, because it should be v.root== v.root.vocabulary.root

I apologize when this description is somehow confusing, but the
easiest way is to use the models and unittest below to demonstrate
the issue. The unitttest should fail (which is incorrect behaviour).
After
commenting the line in Term.save() the test runs fine (the commented
line does nothing/is a read-only operation).

--- 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 r7233 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 developers" 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-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to