Ticket #16187: tests-t16187-2.patch

File tests-t16187-2.patch, 3.5 KB (added by Ulrich Petri, 13 years ago)
  • new file tests/modeltests/lookup_collision/models.py

    diff --git a/tests/modeltests/lookup_collision/__init__.py b/tests/modeltests/lookup_collision/__init__.py
    new file mode 100644
    index 0000000..e69de29
    diff --git a/tests/modeltests/lookup_collision/models.py b/tests/modeltests/lookup_collision/models.py
    new file mode 100644
    index 0000000..e8ed05a
    - +  
     1from django.db import models, DEFAULT_DB_ALIAS, connection
     2from django.conf import settings
     3
     4class RepeatingEvent(models.Model):
     5    name = models.CharField(max_length=100)
     6    week_day = models.IntegerField()
     7    class Meta:
     8        ordering = ('week_day', 'name', )
     9
     10class Appointment(models.Model):
     11    name = models.CharField(max_length=100)
     12    day = models.DateField()
     13    repeating = models.ForeignKey(RepeatingEvent)
     14    class Meta:
     15        ordering = ('day', 'name', )
     16
     17class Calendar(models.Model):
     18    name = models.CharField(max_length=100)
     19    appointments = models.ManyToManyField(Appointment)
     20    class Meta:
     21        ordering = ('name',)
     22
     23    def __unicode__(self):
     24        return self.name
  • new file tests/modeltests/lookup_collision/tests.py

    diff --git a/tests/modeltests/lookup_collision/tests.py b/tests/modeltests/lookup_collision/tests.py
    new file mode 100644
    index 0000000..0a87730
    - +  
     1from datetime import datetime
     2from django.test import TestCase
     3from models import RepeatingEvent, Appointment, Calendar
     4
     5
     6class 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())
Back to Top