From 6d17020c1a1bd94e107defc698adc445084d01d6 Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Thu, 12 Mar 2009 11:52:55 +0000 Subject: [PATCH] Fixed #10250 -- Added a regression test to ensure that GROUP BY statements are correctly quoted under MySQL. This appears to have been corrected inadvertently since the original report, but the extra regression test will make sure it stays that way. git-svn-id: http://code.djangoproject.com/svn/django/trunk@10043 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- tests/modeltests/aggregation/models.py | 68 +++++++---------- .../aggregation_regress/models.py | 74 +++++++++++-------- 2 files changed, 72 insertions(+), 70 deletions(-) diff --git a/tests/modeltests/aggregation/models.py b/tests/modeltests/aggregation/models.py index 04b05f90f90..ba2ec3a212d 100644 --- a/tests/modeltests/aggregation/models.py +++ b/tests/modeltests/aggregation/models.py @@ -7,54 +7,44 @@ except NameError: from django.utils.itercompat import sorted # For Python 2.3 class Author(models.Model): - name = models.CharField(max_length=100) - age = models.IntegerField() - friends = models.ManyToManyField('self', blank=True) + name = models.CharField(max_length=100) + age = models.IntegerField() + friends = models.ManyToManyField('self', blank=True) - def __unicode__(self): - return self.name + def __unicode__(self): + return self.name class Publisher(models.Model): - name = models.CharField(max_length=300) - num_awards = models.IntegerField() + name = models.CharField(max_length=300) + num_awards = models.IntegerField() - def __unicode__(self): - return self.name + def __unicode__(self): + return self.name class Book(models.Model): - isbn = models.CharField(max_length=9) - name = models.CharField(max_length=300) - pages = models.IntegerField() - rating = models.FloatField() - price = models.DecimalField(decimal_places=2, max_digits=6) - authors = models.ManyToManyField(Author) - contact = models.ForeignKey(Author, related_name='book_contact_set') - publisher = models.ForeignKey(Publisher) - pubdate = models.DateField() + isbn = models.CharField(max_length=9) + name = models.CharField(max_length=300) + pages = models.IntegerField() + rating = models.FloatField() + price = models.DecimalField(decimal_places=2, max_digits=6) + authors = models.ManyToManyField(Author) + contact = models.ForeignKey(Author, related_name='book_contact_set') + publisher = models.ForeignKey(Publisher) + pubdate = models.DateField() - def __unicode__(self): - return self.name + def __unicode__(self): + return self.name class Store(models.Model): - name = models.CharField(max_length=300) - books = models.ManyToManyField(Book) - original_opening = models.DateTimeField() - friday_night_closing = models.TimeField() + name = models.CharField(max_length=300) + books = models.ManyToManyField(Book) + original_opening = models.DateTimeField() + friday_night_closing = models.TimeField() - def __unicode__(self): - return self.name + def __unicode__(self): + return self.name -class Entries(models.Model): - EntryID = models.AutoField(primary_key=True, db_column='Entry ID') - Entry = models.CharField(unique=True, max_length=50) - Exclude = models.BooleanField() - -class Clues(models.Model): - ID = models.AutoField(primary_key=True) - EntryID = models.ForeignKey(Entries, verbose_name='Entry', db_column = 'Entry ID') - Clue = models.CharField(max_length=150) - -# Tests on 'aggergate' +# Tests on 'aggregate' # Different backends and numbers. __test__ = {'API_TESTS': """ >>> from django.core import management @@ -351,10 +341,6 @@ True # Cheating: [a for a in Author.objects.all().annotate(num_coleagues=Count('book__authors__id'), num_books=Count('book__id', distinct=True)) if a.num_coleagues - a.num_books > 0] # F-Syntax is required. Will be fixed after F objects are available -# Tests on fields with non-default table and column names. ->>> Clues.objects.values('EntryID__Entry').annotate(Appearances=Count('EntryID'), Distinct_Clues=Count('Clue', distinct=True)) -[] - # Aggregates also work on dates, times and datetimes >>> Publisher.objects.annotate(earliest_book=Min('book__pubdate')).exclude(earliest_book=None).order_by('earliest_book').values() [{'earliest_book': datetime.date(1991, 10, 15), 'num_awards': 9, 'id': 4, 'name': u'Morgan Kaufmann'}, {'earliest_book': datetime.date(1995, 1, 15), 'num_awards': 7, 'id': 3, 'name': u'Prentice Hall'}, {'earliest_book': datetime.date(2007, 12, 6), 'num_awards': 3, 'id': 1, 'name': u'Apress'}, {'earliest_book': datetime.date(2008, 3, 3), 'num_awards': 1, 'id': 2, 'name': u'Sams'}] diff --git a/tests/regressiontests/aggregation_regress/models.py b/tests/regressiontests/aggregation_regress/models.py index 96f0ef11395..f2763d5c040 100644 --- a/tests/regressiontests/aggregation_regress/models.py +++ b/tests/regressiontests/aggregation_regress/models.py @@ -8,47 +8,56 @@ except NameError: from django.utils.itercompat import sorted # For Python 2.3 class Author(models.Model): - name = models.CharField(max_length=100) - age = models.IntegerField() - friends = models.ManyToManyField('self', blank=True) + name = models.CharField(max_length=100) + age = models.IntegerField() + friends = models.ManyToManyField('self', blank=True) - def __unicode__(self): - return self.name + def __unicode__(self): + return self.name class Publisher(models.Model): - name = models.CharField(max_length=300) - num_awards = models.IntegerField() + name = models.CharField(max_length=300) + num_awards = models.IntegerField() - def __unicode__(self): - return self.name + def __unicode__(self): + return self.name class Book(models.Model): - isbn = models.CharField(max_length=9) - name = models.CharField(max_length=300) - pages = models.IntegerField() - rating = models.FloatField() - price = models.DecimalField(decimal_places=2, max_digits=6) - authors = models.ManyToManyField(Author) - contact = models.ForeignKey(Author, related_name='book_contact_set') - publisher = models.ForeignKey(Publisher) - pubdate = models.DateField() + isbn = models.CharField(max_length=9) + name = models.CharField(max_length=300) + pages = models.IntegerField() + rating = models.FloatField() + price = models.DecimalField(decimal_places=2, max_digits=6) + authors = models.ManyToManyField(Author) + contact = models.ForeignKey(Author, related_name='book_contact_set') + publisher = models.ForeignKey(Publisher) + pubdate = models.DateField() - class Meta: - ordering = ('name',) + class Meta: + ordering = ('name',) - def __unicode__(self): - return self.name + def __unicode__(self): + return self.name class Store(models.Model): - name = models.CharField(max_length=300) - books = models.ManyToManyField(Book) - original_opening = models.DateTimeField() - friday_night_closing = models.TimeField() + name = models.CharField(max_length=300) + books = models.ManyToManyField(Book) + original_opening = models.DateTimeField() + friday_night_closing = models.TimeField() - def __unicode__(self): - return self.name + def __unicode__(self): + return self.name + +class Entries(models.Model): + EntryID = models.AutoField(primary_key=True, db_column='Entry ID') + Entry = models.CharField(unique=True, max_length=50) + Exclude = models.BooleanField() + +class Clues(models.Model): + ID = models.AutoField(primary_key=True) + EntryID = models.ForeignKey(Entries, verbose_name='Entry', db_column = 'Entry ID') + Clue = models.CharField(max_length=150) -#Extra does not play well with values. Modify the tests if/when this is fixed. __test__ = {'API_TESTS': """ >>> from django.core import management >>> from django.db.models import get_app, F @@ -188,6 +197,13 @@ FieldError: Cannot resolve keyword 'foo' into field. Choices are: authors, conta >>> Publisher.objects.annotate(num_books=Count('book')).exclude(num_books__lt=F('num_awards')/2).order_by('name').values('name','num_books','num_awards') [{'num_books': 2, 'name': u'Apress', 'num_awards': 3}, {'num_books': 0, 'name': u"Jonno's House of Books", 'num_awards': 0}, {'num_books': 1, 'name': u'Sams', 'num_awards': 1}] +# Tests on fields with non-default table and column names. +>>> Clues.objects.values('EntryID__Entry').annotate(Appearances=Count('EntryID'), Distinct_Clues=Count('Clue', distinct=True)) +[] + +>>> Entries.objects.annotate(clue_count=Count('clues__ID')) +[] + # Regression for #10089: Check handling of empty result sets with aggregates >>> Book.objects.filter(id__in=[]).count() 0