I have the following codes for testing model and it's works correctly in
Django 1.3 + Python 2.7 without unittest2::

# in method of TestCase subclass -----------------------------------
user1 = User.objects.create_user(username='user1',
email='us...@test.com', password='password')
user2 = User.objects.create_user(username='user2',
email='us...@test.com', password='password')

self.assertItemsEqual(User.objects.all(), [user1, user2])
#-------------------------------------------------------------------

However the code raise ``AssertionError: Sequences differ: ...`` in
Django 1.3 + Python 2.6 or Django 1.3 + Python 2.7 + unittest2. (well
but I have no idea why the code above works with Django 1.3 + Python 2.7
without unittest2)

This happen because unittest2 or whatever does ``sorted(expected_seq)``
in ``assertItemsEqual`` method but they don't know how to sort the
instances of model class. To solve this problem, I simply add
``__cmp__`` method to Model class like::

# somewhere but called after Django has correctly configured -------
from django.db.models import Model

if not hasattr(Model, '__cmp__'):
    Model.__cmp__ = lambda self, other: cmp(self._get_pk_val(),
other._get_pk_val())
#-------------------------------------------------------------------

But it is too annoying to call this patch in every library which use
``assertItemsEqual`` for testing. So I wander if official django's model
class has ``__cmp__`` method in default.

Thanks.
        

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to django-developers@googlegroups.com.
To unsubscribe from this group, send email to 
django-developers+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en.

Reply via email to