Ticket #16937: 16937_additional_tests.diff

File 16937_additional_tests.diff, 9.1 KB (added by Anssi Kääriäinen, 13 years ago)

Additional tests

  • tests/modeltests/prefetch_related/models.py

    diff --git a/tests/modeltests/prefetch_related/models.py b/tests/modeltests/prefetch_related/models.py
    index ba998c0..dee8842 100644
    a b from django.db import models  
    55## Basic tests
    66
    77class Author(models.Model):
    8     name = models.CharField(max_length=50)
     8    name = models.CharField(max_length=50, unique=True)
    99    first_book = models.ForeignKey('Book', related_name='first_time_authors')
     10    favorite_authors = models.ManyToManyField(
     11        'self', through='FavoriteAuthors', symmetrical=False, related_name='favors_me')
    1012
    1113    def __unicode__(self):
    1214        return self.name
    class Author(models.Model):  
    1517        ordering = ['id']
    1618
    1719
     20class AuthorWithAge(Author):
     21    author = models.OneToOneField(Author, parent_link=True)
     22    age = models.IntegerField()
     23
     24
     25class FavoriteAuthors(models.Model):
     26    author = models.ForeignKey(Author, to_field='name', related_name='i_like')
     27    likes_author = models.ForeignKey(Author, to_field='name', related_name='likes_me')
     28
     29    class Meta:
     30         ordering = ['id']
     31
     32
     33class AuthorAddress(models.Model):
     34    author = models.ForeignKey(Author, to_field='name', related_name='addresses')
     35    address = models.TextField()   
     36
     37    class Meta:
     38        ordering = ['id']
     39
     40    def __unicode__(self):
     41        return self.address
     42
     43
    1844class Book(models.Model):
    1945    title = models.CharField(max_length=255)
    2046    authors = models.ManyToManyField(Author, related_name='books')
    class Book(models.Model):  
    2551    class Meta:
    2652        ordering = ['id']
    2753
     54class BookWithYear(Book):
     55    book = models.OneToOneField(Book, parent_link=True)
     56    published_year = models.IntegerField()
     57    aged_authors = models.ManyToManyField(
     58        AuthorWithAge, related_name='books_with_year')
     59
    2860
    2961class Reader(models.Model):
    3062    name = models.CharField(max_length=50)
  • tests/modeltests/prefetch_related/tests.py

    diff --git a/tests/modeltests/prefetch_related/tests.py b/tests/modeltests/prefetch_related/tests.py
    index 1ef76ce..86d8175 100644
    a b  
    11from django.contrib.contenttypes.models import ContentType
    22from django.test import TestCase
     3from django.utils import unittest
    34
    4 from models import Author, Book, Reader, Qualification, Teacher, Department, TaggedItem, Bookmark
    5 
     5from models import (Author, Book, Reader, Qualification, Teacher, Department,
     6                    TaggedItem, Bookmark, AuthorAddress, FavoriteAuthors,
     7                    AuthorWithAge, BookWithYear)
    68
    79class PrefetchRelatedTests(TestCase):
    810
    class PrefetchRelatedTests(TestCase):  
    100102                [[u"Amy", u"Belinda"]],    # Jane - Sense and Sense
    101103            ])
    102104
     105    def test_overriding_prefetch(self):
     106        with self.assertNumQueries(3):
     107            qs = Author.objects.prefetch_related('books', 'books__read_by')
     108            lists = [[[unicode(r) for r in b.read_by.all()]
     109                      for b in a.books.all()]
     110                     for a in qs]
     111            self.assertEqual(lists,
     112            [
     113                [[u"Amy"], [u"Belinda"]],  # Charlotte - Poems, Jane Eyre
     114                [[u"Amy"]],                # Anne - Poems
     115                [[u"Amy"], []],            # Emily - Poems, Wuthering Heights
     116                [[u"Amy", u"Belinda"]],    # Jane - Sense and Sense
     117            ])
     118        with self.assertNumQueries(3):
     119            qs = Author.objects.prefetch_related('books__read_by', 'books')
     120            lists = [[[unicode(r) for r in b.read_by.all()]
     121                      for b in a.books.all()]
     122                     for a in qs]
     123            self.assertEqual(lists,
     124            [
     125                [[u"Amy"], [u"Belinda"]],  # Charlotte - Poems, Jane Eyre
     126                [[u"Amy"]],                # Anne - Poems
     127                [[u"Amy"], []],            # Emily - Poems, Wuthering Heights
     128                [[u"Amy", u"Belinda"]],    # Jane - Sense and Sense
     129            ])
     130
    103131    def test_get(self):
    104132        """
    105133        Test that objects retrieved with .get() get the prefetch behaviour
    class GenericRelationTests(TestCase):  
    249277            tags = [t.tag for b in Bookmark.objects.prefetch_related('tags')
    250278                    for t in b.tags.all()]
    251279            self.assertEqual(sorted(tags), ["django", "python"])
     280
     281
     282class TooManySQLParamsTest(TestCase):
     283    def setUp(self):
     284        # SQLite has a limit of 1000 parameters per query
     285        NUM_OBJS = 1000
     286        objs = [Book(title=str(i)) for i in range(0, NUM_OBJS)]
     287        # For the limit reason we need to run bulk_create in
     288        # multiple batches.
     289        for i in range(0, NUM_OBJS / 100):
     290            Book.objects.bulk_create(objs[i*100:(i+1)*100])
     291
     292    @unittest.expectedFailure
     293    def test_prefetch(self):
     294        # The below query should not raise any exception - we do not
     295        # test anything else here.
     296        list(Book.objects.prefetch_related('first_time_authors'))
     297 
     298
     299class MultiTableInheritanceTest(TestCase):
     300    def setUp(self):
     301        self.book1 = BookWithYear.objects.create(
     302            title="Poems", published_year=2010)
     303        self.book2 = BookWithYear.objects.create(
     304            title="More poems", published_year=2011)
     305        self.author1 = AuthorWithAge.objects.create(
     306            name='Jane', first_book=self.book1, age=50)
     307        self.author2 = AuthorWithAge.objects.create(
     308            name='Tom', first_book=self.book1, age=49)
     309        self.author3 = AuthorWithAge.objects.create(
     310            name='Robert', first_book=self.book2, age=48)
     311        self.authorAddress = AuthorAddress.objects.create(
     312            author=self.author1, address='SomeStreet 1')
     313        self.book2.aged_authors.add(self.author2, self.author3)
     314
     315    def test_foreignkey(self):
     316        with self.assertNumQueries(2):
     317            qs = AuthorWithAge.objects.prefetch_related('addresses')
     318            addresses = [[unicode(address) for address in obj.addresses.all()]
     319                         for obj in qs]
     320        self.assertEquals(addresses, [[unicode(self.authorAddress)], [], []])
     321
     322    def test_m2m_to_inheriting_model(self):
     323        qs = AuthorWithAge.objects.prefetch_related('books_with_year')
     324        with self.assertNumQueries(2):
     325            lst = [[unicode(book) for book in author.books_with_year.all()]
     326                   for author in qs]
     327        qs = AuthorWithAge.objects.all()
     328        lst2 = [[unicode(book) for book in author.books_with_year.all()]
     329                for author in qs]
     330        self.assertEquals(lst, lst2)
     331
     332        qs = BookWithYear.objects.prefetch_related('aged_authors')
     333        with self.assertNumQueries(2):
     334            lst = [[unicode(author) for author in book.aged_authors.all()]
     335                   for book in qs]
     336        qs = BookWithYear.objects.all()
     337        lst2 = [[unicode(author) for author in book.aged_authors.all()]
     338               for book in qs]
     339        self.assertEquals(lst, lst2)
     340
     341    def test_parent_link_prefetch(self):
     342        with self.assertRaises(ValueError) as cm:
     343            qs = list(AuthorWithAge.objects.prefetch_related('author'))
     344        self.assertTrue('prefetch_related' in cm.exception.message)
     345
     346class ForeignKeyToFieldTest(TestCase):
     347    def setUp(self):
     348        self.book = Book.objects.create(title="Poems")
     349        self.author1 = Author.objects.create(name='Jane', first_book=self.book)
     350        self.author2 = Author.objects.create(name='Tom', first_book=self.book)
     351        self.author3 = Author.objects.create(name='Robert', first_book=self.book)
     352        self.authorAddress = AuthorAddress.objects.create(
     353            author=self.author1, address='SomeStreet 1'
     354        )
     355        FavoriteAuthors.objects.create(author=self.author1,
     356                                       likes_author=self.author2)
     357        FavoriteAuthors.objects.create(author=self.author2,
     358                                       likes_author=self.author3)
     359        FavoriteAuthors.objects.create(author=self.author3,
     360                                       likes_author=self.author1)
     361   
     362    def test_foreignkey(self):
     363        with self.assertNumQueries(2):
     364            qs = Author.objects.prefetch_related('addresses')
     365            addresses = [[unicode(address) for address in obj.addresses.all()]
     366                         for obj in qs]
     367        self.assertEquals(addresses, [[unicode(self.authorAddress)], [], []])
     368 
     369    @unittest.expectedFailure
     370    def test_m2m(self):
     371        with self.assertNumQueries(3):
     372            qs = Author.objects.prefetch_related('favorite_authors', 'favors_me')
     373            favorites = [(
     374                 [unicode(i_like) for i_like in author.favorite_authors.all()],
     375                 [unicode(likes_me) for likes_me in author.favors_me.all()]
     376                ) for author in qs]
     377        self.assertEquals(
     378            favorites,
     379            [
     380                ([unicode(self.author2)],[unicode(self.author3)]),
     381                ([unicode(self.author3)],[unicode(self.author1)]),
     382                ([unicode(self.author1)],[unicode(self.author2)])
     383            ]
     384        )
Back to Top