| 611 | |
| 612 | class LookupCollisionTests(TestCase): |
| 613 | |
| 614 | def setUp(self): |
| 615 | # Here we're using 'gt' as a code number for the year, e.g. 111=>2009. |
| 616 | season_2009 = Season.objects.create(year=2009, gt=111) |
| 617 | season_2009.games.create(home="Houston Astros", away="St. Louis Cardinals") |
| 618 | season_2010 = Season.objects.create(year=2010, gt=222) |
| 619 | season_2010.games.create(home="Houston Astros", away="Chicago Cubs") |
| 620 | season_2010.games.create(home="Houston Astros", away="Milwaukee Brewers") |
| 621 | season_2010.games.create(home="Houston Astros", away="St. Louis Cardinals") |
| 622 | season_2011 = Season.objects.create(year=2011, gt=333) |
| 623 | season_2011.games.create(home="Houston Astros", away="St. Louis Cardinals") |
| 624 | season_2011.games.create(home="Houston Astros", away="Milwaukee Brewers") |
| 625 | hunter_pence = Player.objects.create(name="Hunter Pence") |
| 626 | hunter_pence.games = Game.objects.filter(season__year__in=[2009, 2010]) |
| 627 | pudge = Player.objects.create(name="Ivan Rodriquez") |
| 628 | pudge.games = Game.objects.filter(season__year=2009) |
| 629 | pedro_feliz = Player.objects.create(name="Pedro Feliz") |
| 630 | pedro_feliz.games = Game.objects.filter(season__year__in=[2011]) |
| 631 | johnson = Player.objects.create(name="Johnson") |
| 632 | johnson.games = Game.objects.filter(season__year__in=[2011]) |
| 633 | |
| 634 | def test_lookup_collision(self): |
| 635 | """ |
| 636 | Ensure that genuine field names don't collide with built-in lookup |
| 637 | types ('year', 'gt', 'range', 'in' etc.). |
| 638 | Refs #11670. |
| 639 | """ |
| 640 | # Games in 2010 |
| 641 | self.assertEqual(Game.objects.filter(season__year=2010).count(), 3) |
| 642 | self.assertEqual(Game.objects.filter(season__year__exact=2010).count(), 3) |
| 643 | self.assertEqual(Game.objects.filter(season__gt=222).count(), 3) |
| 644 | self.assertEqual(Game.objects.filter(season__gt__exact=222).count(), 3) |
| 645 | |
| 646 | # Games in 2011 |
| 647 | self.assertEqual(Game.objects.filter(season__year=2011).count(), 2) |
| 648 | self.assertEqual(Game.objects.filter(season__year__exact=2011).count(), 2) |
| 649 | self.assertEqual(Game.objects.filter(season__gt=333).count(), 2) |
| 650 | self.assertEqual(Game.objects.filter(season__gt__exact=333).count(), 2) |
| 651 | self.assertEqual(Game.objects.filter(season__year__gt=2010).count(), 2) |
| 652 | self.assertEqual(Game.objects.filter(season__gt__gt=222).count(), 2) |
| 653 | |
| 654 | # Games played in 2010 and 2011 |
| 655 | self.assertEqual(Game.objects.filter(season__year__in=[2010, 2011]).count(), 5) |
| 656 | self.assertEqual(Game.objects.filter(season__year__gt=2009).count(), 5) |
| 657 | self.assertEqual(Game.objects.filter(season__gt__in=[222, 333]).count(), 5) |
| 658 | self.assertEqual(Game.objects.filter(season__gt__gt=111).count(), 5) |
| 659 | |
| 660 | # Players who played in 2009 |
| 661 | self.assertEqual(Player.objects.filter(games__season__year=2009).distinct().count(), 2) |
| 662 | self.assertEqual(Player.objects.filter(games__season__year__exact=2009).distinct().count(), 2) |
| 663 | self.assertEqual(Player.objects.filter(games__season__gt=111).distinct().count(), 2) |
| 664 | self.assertEqual(Player.objects.filter(games__season__gt__exact=111).distinct().count(), 2) |
| 665 | |
| 666 | # Players who played in 2010 |
| 667 | self.assertEqual(Player.objects.filter(games__season__year=2010).distinct().count(), 1) |
| 668 | self.assertEqual(Player.objects.filter(games__season__year__exact=2010).distinct().count(), 1) |
| 669 | self.assertEqual(Player.objects.filter(games__season__gt=222).distinct().count(), 1) |
| 670 | self.assertEqual(Player.objects.filter(games__season__gt__exact=222).distinct().count(), 1) |
| 671 | |
| 672 | # Players who played in 2011 |
| 673 | self.assertEqual(Player.objects.filter(games__season__year=2011).distinct().count(), 2) |
| 674 | self.assertEqual(Player.objects.filter(games__season__year__exact=2011).distinct().count(), 2) |
| 675 | self.assertEqual(Player.objects.filter(games__season__gt=333).distinct().count(), 2) |
| 676 | self.assertEqual(Player.objects.filter(games__season__year__gt=2010).distinct().count(), 2) |
| 677 | self.assertEqual(Player.objects.filter(games__season__gt__gt=222).distinct().count(), 2) |