Re: Django 1.0.2 Test Suite - Modeltests-m2m_through

0 views
Skip to first unread message
Message has been deleted

Russell Keith-Magee

unread,
May 26, 2009, 8:23:33 PM5/26/09
to django-d...@googlegroups.com
On Wed, May 27, 2009 at 2:21 AM, Tarun Pasrija <tarun....@gmail.com> wrote:
>
> Hi
>
> In my effort to contribute rectification of Django test suite, I ran
> it using the following command to select the model as m2m_through
>
> $ python runtests.py --settings=settings --verbosity=2 m2m_through
>
> The output Failure result is as follows:-
>
> Failed example:
>    Person.objects.filter(membership__date_joined__gt=datetime(2004,
> 1, 1)).distinct()
> Expected:
>    [<Person: Jane>, <Person: Jim>]
> Got:
>    [<Person: Jane>, <Person: Jim>, <Person: Jim>]

I'm intrigued as to how you are getting this failure. The m2m_through
tests have been passing for me for some time (and are passing at the
moment). There haven't been any recent modifications that should be
affecting this test.

Further - looking at the test itself, the logic looks sound - the
distinct() condition is appropriate, and should be sufficient to
eliminate the second "Jim" from the returned data.

So - you _shouldn't_ be getting a failure. Can you elaborate on how
you have generate this failure (i.e., the database you are using,
version numbers for every component in your stack, operating systems,
etc)?

> Code that generates this failure is:-
>
> # QuerySet's distinct() method can correct this problem.
>>>> Person.objects.filter(membership__date_joined__gt=datetime(2004, 1, 1)).distinct()
> [<Person: Jane>, <Person: Jim>]
>
> The reason for this failure is the sql that gets generated for this
> which I printed using as_sql function as:-
>
> ('SELECT DISTINCT "M2M_THROUGH_PERSON"."ID",
> "M2M_THROUGH_PERSON"."NAME" FROM "M2M_THROUGH_PERSON" INNER JOIN
> "M2M_THROUGH_MEMBERSHIP" ON ("M2M_THROUGH_PERSON"."ID" =
> "M2M_THROUGH_MEMBERSHIP"."PERSON_ID") WHERE
> "M2M_THROUGH_MEMBERSHIP"."DATE_JOINED" > %s  ORDER BY
> "M2M_THROUGH_PERSON"."NAME" ASC', (u'2004-01-01 00:00:00',))
>
> Since the Person.ID is always distinct, we would always get 3 columns
> as output. To rectify this, the following document potrays a solution
> to use the values from Queryset

Incorrect. You are returning a list of Person objects. The ID is the
ID of the person, which is distinct to the person instance. The
results you are getting in this case should be:

1, Jane
2, Jim
2, Jim

The third row is then eliminated by the distinct call.

Yours,
Russ Magee %-)

Reply all
Reply to author
Forward
0 new messages