| | 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 | |
| | 280 | |
| | 281 | |
| | 282 | class 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 | |
| | 299 | class 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 | |
| | 346 | class 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 | ) |