Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a failing test to demonstrate a bug #254

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions polymorphic/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,22 @@ class ModelUnderRelChild(PolymorphicModel):
_private2 = models.CharField(max_length=10)


class Participant(PolymorphicModel):
pass


class UserProfile(Participant):
name = models.CharField(max_length=100)

def __str__(self):
return self.name


class Team(models.Model):
team_name = models.CharField(max_length=100)
user_profiles = models.ManyToManyField(UserProfile, related_name='user_teams')


class MyManagerQuerySet(PolymorphicQuerySet):

def my_queryset_foo(self):
Expand Down Expand Up @@ -1325,6 +1341,44 @@ def func():
# Ensure no queries are made using the default database.
self.assertNumQueries(0, func)

def test_unknown_issue(self):
user_a = UserProfile.objects.create(name='a')
user_b = UserProfile.objects.create(name='b')
user_c = UserProfile.objects.create(name='c')

team1 = Team.objects.create(team_name='team1')
team1.user_profiles.add(user_a, user_b, user_c)
team1.save()

team2 = Team.objects.create(team_name='team2')
team2.user_profiles.add(user_c)
team2.save()

# without prefetch_related, the test passes
my_teams = Team.objects.filter(user_profiles=user_c).prefetch_related('user_profiles').distinct()

print(my_teams[0].user_profiles.all())
print(my_teams[1].user_profiles.all())
self.assertEqual(len(my_teams[0].user_profiles.all()), 3)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't look right; you're not ordering you QuerySet, so the first result returned by the DB might be team2. It'll probably fail, occasionally.

self.assertEqual(len(my_teams[1].user_profiles.all()), 1)

print(my_teams[0].user_profiles.all())
print(my_teams[1].user_profiles.all())
self.assertEqual(len(my_teams[0].user_profiles.all()), 3)
self.assertEqual(len(my_teams[1].user_profiles.all()), 1)

# without this "for" loop, the test passes
for _ in my_teams:
pass

print(my_teams[0].user_profiles.all())
print(my_teams[1].user_profiles.all())
# This time, test fails.
# with sqlite: 4 != 3
# with postgresql: 2 != 3
self.assertEqual(len(my_teams[0].user_profiles.all()), 3)
self.assertEqual(len(my_teams[1].user_profiles.all()), 1)


def qrepr(data):
"""
Expand Down