On 9/20/07, cesco <[EMAIL PROTECTED]> wrote: > > Hi, > > I'd like to test a query and the data returned by that query is a list > containing a list of objects. For example: > [[<MyModel: object_1>, <MyModel: object_2>, ..., <MyModel: object_n>]] > > If I try testing using unittests I get the following error: > AssertionError: [[]] != [[]] > though to me the expected output and the one I get look identical.
Without the exact test, it's difficult to know exactly what is going wrong; however, there is one possible generic cause of difficulty. Despite all appearances, <MyModel: object_1> != <MyModel: object_1>. By extension, lists of <MyModel: object_1> won't be equal, either. <MyModel: object_1> is the output representation of a Django model object. The thing is, two model objects with that point to the same database object (i.e., they have the same model and have the same primary key) _are not equal_. This is because the _wrapper_ objects are different. [1] Generally, if you want to check if two object instances are equal, compare their primary keys; if you want to compare two lists, compare lists of primary keys: i.e., [obj.id for obj in query.all()] = [1,2,3] > If I try with doc test then I get the problem when the output span > multiple lines: even though the objects returned are the right ones, > the expected string and the one I get are different because of > newlines and this make the test fail. This is an inherent difficulty with doctests, and it's one of the reasons you might want to favour using unittest over doctest. [1] Before you ask, no, we can't just override __eq__ to check for PK equality - search the archives if you want to know why. Yours, Russ Magee %-) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com 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 -~----------~----~----~----~------~----~------~--~---