python unit test: assertEqual on same objects throwing AssertionError
Asked Answered
I

3

7

I have a class as

class PlaylistManager(models.Manager):
    def add_playlist(self, name):
        playlist = Playlist(name=name)
        playlist.save()
        return playlist

    def get_playlist_with_id(self, id):
        return super(PlaylistManager, self).get_query_set().filter(pk=id)

class Playlist(models.Model):
    name = models.CharField(max_length=30)
    date_created = models.DateTimeField(auto_now_add=True)
    date_modified = models.DateTimeField(auto_now=True)
    deleted = models.BooleanField(default=False)
    objects = PlaylistManager() # is a customer manager

    def __repr__(self):
        return '<Playlist name:%s, date_created:%s, date_modified:%s, deleted:%s>' % \
                (self.name, self.date_created, self.date_modified, self.deleted)

    class Meta:
        db_table = 'playlists'

and i test as

def test_get_playlist(self):
    playlist = Utility.add_playlist()
    self.assertEqual(Playlist.objects.get_playlist_with_id(playlist.id), playlist)

class Utility():
    @staticmethod
    def add_playlist(playlist_name=PLAYLIST):
        return Playlist.objects.add_playlist(playlist_name)

When I run the test, I see error as

AssertionError: [<Playlist name:playlist, date_created:2012-07-18 19:54:12.265909+00:00, date_modified:2012-07-18 19:54:12.265955+00:00, deleted:False>] != <Playlist name:playlist, date_created:2012-07-18 19:54:12.265909+00:00, date_modified:2012-07-18 19:54:12.265955+00:00, deleted:False>

even when the two objects are same.

Is there anything I am missing here?

Intelligibility answered 18/7, 2012 at 20:1 Comment(2)
It must not be the same object. Try comparing id(Playlist.objects.get_playlist_with_id(playlist.id)) and id(playlist). Just because the information contained in them is the same does not mean they are the same object (that is, that obj1 == obj2)Zygotene
Even though they're phrased differently, I think this is essentially a duplicate of #2513842Gardner
C
4

Compare model id or pk and it will be ok.

Condensate answered 18/7, 2012 at 20:6 Comment(0)
T
17

assertEqual() uses the == operator to compare the classes. The default == operator of user-defined classes compares instances by object identity. This means two instances are only considered equal when they are the same instance.

Turbinal answered 18/7, 2012 at 20:4 Comment(5)
This answer could be improved by mentioning the __eq__ operator. In fact, lambda self, other: self.pk == other.pk might not be a terrible implementation.Gardner
@kojiro: I don't think it is a good idea to define __eq__() when the only place where it is used is in the unit tests, so I won't update my answer.Turbinal
Why is it throwing an error, though; and not asserting to False?Crux
@Crux It's the whole point of an assertion that it throws and AssertionError if the assertion fails. Not sure what "asserting to False" means.Turbinal
@SvenMarnach you're right, my bad. Sorry and thanks for pointing it out!Crux
C
4

Compare model id or pk and it will be ok.

Condensate answered 18/7, 2012 at 20:6 Comment(0)
E
3

The AssertionError is correct. Your get_playlist_with_id returns a Queryset not a Playlist instance. If you want get_playlist_with_id to return the single instance then you should use get not filter.

Evasive answered 18/7, 2012 at 20:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.