Thanks for the clarification. I understand now the problem regarding doctests and the comparison of the same objects. The thing I'm still missing is how can the test fail when I'm comparing a list containing an empty list with a list containing an empty list as well. No model object is involved in that case. Basically the view I was trying to test returns a list of list of querysets and, for some combination of parameters, the queryset is empty, that is, it returns a list containing an empty list. Shouldn't the comparison work in that case? The line that fails is the following:
self.assertEquals(perform_search(category_id=1, shop_id=290, tags=['tag1', 'tag2']), [[]]) and the error message is: AssertionError: [[]] != [[]] Is there an explanation for this as well? Maybe I'm doing something wrong? Thanks again Francesco On Sep 20, 2:13 am, "Russell Keith-Magee" <[EMAIL PROTECTED]> wrote: > 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 [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-users?hl=en -~----------~----~----~----~------~----~------~--~---

