Is there a way to check that two lists of objects are equal in django tests.
lets say I have some model:
class Tag(models.Model):
slug = models.SlugField(max_length=50, unique=True)
def __unicode__(self):
return self.slug
and I run this simple test:
def test_equal_list_fail(self):
tag_list = []
for tag in ['a', 'b', 'c']:
tag_list.append(Tag.objects.create(slug=tag))
tags = Tag.objects.all()
self.assertEqual(tag_list, tags)
this fails with:
======================================================================
FAIL: test_equal_list_fail (userAccount.tests.ProfileTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "path/to/tests.py", line 155, in test_equal_list_fail
self.assertEqual(tag_list, tags)
AssertionError: [<Tag: a>, <Tag: b>, <Tag: c>] != [<Tag: a>, <Tag: b>, <Tag: c>]
----------------------------------------------------------------------
this will work:
def test_equal_list_passes(self):
tag_list = []
for tag in ['a', 'b', 'c']:
tag_list.append(Tag.objects.create(slug=tag))
tags = Tag.objects.all()
for tag_set in zip(tags, tag_list):
self.assertEqual(*tag_set)
However, This fails:
def test_equal_list_fail(self):
tag_list = []
for tag in ['a', 'b', 'c']:
tag_list.append(Tag.objects.create(slug=tag))
tags = Tag.objects.all()
for tag_set in zip(tags, tag_list):
print "\n"
print tag_set[0].slug + "'s pk is %s" % tag_set[0].pk
print tag_set[1].slug + "'s pk is %s" % tag_set[1].pk
print "\n"
self.assertIs(*tag_set)
with:
Creating test database for alias 'default'...
.......
a's pk is 1
a's pk is 1
F.
======================================================================
FAIL: test_equal_list_fail (userAccount.tests.ProfileTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "path/to/tests.py", line 160, in test_equal_list_fail
self.assertIs(*tag_set)
AssertionError: <Tag: a> is not <Tag: a>
Is this expected behavior?
Edit in response to comment
This type of comparison works:
class Obj:
def __init__(self, x):
self.x = x
>>> one = Obj(1)
>>> two = Obj(2)
>>> a = [one, two]
>>> b = [one, two]
>>> a == b
True
Why is the test failing for the other arrays?