| 1 | from datetime import datetime |
| 2 | from django.test import TestCase |
| 3 | from models import RepeatingEvent, Appointment, Calendar |
| 4 | |
| 5 | |
| 6 | class LookupCollisionTests(TestCase): |
| 7 | """Test for collisions between lookup type names and field names""" |
| 8 | |
| 9 | def setUp(self): |
| 10 | # Create a RepeatingEvent |
| 11 | self.rep1 = RepeatingEvent(name="Every Thursday", week_day=5) |
| 12 | self.rep1.save() |
| 13 | # Create an Appointment |
| 14 | self.ap1 = Appointment(name="Today and every thursday", day=datetime(2011, 6, 9), repeating=self.rep1) |
| 15 | self.ap1.save() |
| 16 | # Create a Calendar |
| 17 | self.cal = Calendar(name="My Calendar") |
| 18 | self.cal.save() |
| 19 | self.cal.appointments = [self.ap1, ] |
| 20 | self.cal.save() |
| 21 | |
| 22 | def test_query_simple(self): |
| 23 | """Test simple queries with colliding names without traversing relations""" |
| 24 | self.assertTrue(RepeatingEvent.objects.filter(week_day=5).exists()) |
| 25 | self.assertTrue(Appointment.objects.filter(day=datetime(2011, 6, 9)).exists()) |
| 26 | |
| 27 | def test_query_related(self): |
| 28 | """Test queries through related fields with colliding field names""" |
| 29 | self.assertTrue(Appointment.objects.filter(repeating__week_day=5).exists()) |
| 30 | |
| 31 | self.assertTrue(Calendar.objects.filter(appointments__day=datetime(2011, 6, 9)).exists()) |
| 32 | self.assertTrue(Calendar.objects.filter(appointments__repeating__week_day=5).exists()) |
| 33 | |
| 34 | def test_query_related_with_lookup(self): |
| 35 | """Test queries through related fields with colliding names with explicit lookup types""" |
| 36 | self.assertTrue(RepeatingEvent.objects.filter(week_day__exact=5).exists()) |
| 37 | |
| 38 | self.assertTrue(Appointment.objects.filter(day__exact=datetime(2011, 6, 9)).exists()) |
| 39 | self.assertTrue(Appointment.objects.filter(day__week_day=5).exists()) |
| 40 | self.assertTrue(Appointment.objects.filter(repeating__week_day__exact=5).exists()) |
| 41 | |
| 42 | self.assertTrue(Calendar.objects.filter(appointments__day__exact=datetime(2011, 6, 9)).exists()) |
| 43 | self.assertTrue(Calendar.objects.filter(appointments__day__week_day=5).exists()) |
| 44 | self.assertTrue(Calendar.objects.filter(appointments__repeating__week_day__exact=5).exists()) |