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