tests.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. from __future__ import with_statement
  2. from django.contrib.contenttypes.models import ContentType
  3. from django.test import TestCase
  4. from django.utils import unittest
  5. from models import (Author, Book, Reader, Qualification, Teacher, Department,
  6. TaggedItem, Bookmark, AuthorAddress, FavoriteAuthors,
  7. AuthorWithAge, BookWithYear, Person, House, Room,
  8. Employee)
  9. class PrefetchRelatedTests(TestCase):
  10. def setUp(self):
  11. self.book1 = Book.objects.create(title="Poems")
  12. self.book2 = Book.objects.create(title="Jane Eyre")
  13. self.book3 = Book.objects.create(title="Wuthering Heights")
  14. self.book4 = Book.objects.create(title="Sense and Sensibility")
  15. self.author1 = Author.objects.create(name="Charlotte",
  16. first_book=self.book1)
  17. self.author2 = Author.objects.create(name="Anne",
  18. first_book=self.book1)
  19. self.author3 = Author.objects.create(name="Emily",
  20. first_book=self.book1)
  21. self.author4 = Author.objects.create(name="Jane",
  22. first_book=self.book4)
  23. self.book1.authors.add(self.author1, self.author2, self.author3)
  24. self.book2.authors.add(self.author1)
  25. self.book3.authors.add(self.author3)
  26. self.book4.authors.add(self.author4)
  27. self.reader1 = Reader.objects.create(name="Amy")
  28. self.reader2 = Reader.objects.create(name="Belinda")
  29. self.reader1.books_read.add(self.book1, self.book4)
  30. self.reader2.books_read.add(self.book2, self.book4)
  31. def test_m2m_forward(self):
  32. with self.assertNumQueries(2):
  33. lists = [list(b.authors.all()) for b in Book.objects.prefetch_related('authors')]
  34. normal_lists = [list(b.authors.all()) for b in Book.objects.all()]
  35. self.assertEqual(lists, normal_lists)
  36. def test_m2m_reverse(self):
  37. with self.assertNumQueries(2):
  38. lists = [list(a.books.all()) for a in Author.objects.prefetch_related('books')]
  39. normal_lists = [list(a.books.all()) for a in Author.objects.all()]
  40. self.assertEqual(lists, normal_lists)
  41. def test_foreignkey_reverse(self):
  42. with self.assertNumQueries(2):
  43. lists = [list(b.first_time_authors.all())
  44. for b in Book.objects.prefetch_related('first_time_authors')]
  45. self.assertQuerysetEqual(self.book2.authors.all(), [u"<Author: Charlotte>"])
  46. def test_survives_clone(self):
  47. with self.assertNumQueries(2):
  48. lists = [list(b.first_time_authors.all())
  49. for b in Book.objects.prefetch_related('first_time_authors').exclude(id=1000)]
  50. def test_len(self):
  51. with self.assertNumQueries(2):
  52. qs = Book.objects.prefetch_related('first_time_authors')
  53. length = len(qs)
  54. lists = [list(b.first_time_authors.all())
  55. for b in qs]
  56. def test_bool(self):
  57. with self.assertNumQueries(2):
  58. qs = Book.objects.prefetch_related('first_time_authors')
  59. x = bool(qs)
  60. lists = [list(b.first_time_authors.all())
  61. for b in qs]
  62. def test_count(self):
  63. with self.assertNumQueries(2):
  64. qs = Book.objects.prefetch_related('first_time_authors')
  65. [b.first_time_authors.count() for b in qs]
  66. def test_exists(self):
  67. with self.assertNumQueries(2):
  68. qs = Book.objects.prefetch_related('first_time_authors')
  69. [b.first_time_authors.exists() for b in qs]
  70. def test_clear(self):
  71. """
  72. Test that we can clear the behavior by calling prefetch_related()
  73. """
  74. with self.assertNumQueries(5):
  75. with_prefetch = Author.objects.prefetch_related('books')
  76. without_prefetch = with_prefetch.prefetch_related(None)
  77. lists = [list(a.books.all()) for a in without_prefetch]
  78. def test_m2m_then_m2m(self):
  79. """
  80. Test we can follow a m2m and another m2m
  81. """
  82. with self.assertNumQueries(3):
  83. qs = Author.objects.prefetch_related('books__read_by')
  84. lists = [[[unicode(r) for r in b.read_by.all()]
  85. for b in a.books.all()]
  86. for a in qs]
  87. self.assertEqual(lists,
  88. [
  89. [[u"Amy"], [u"Belinda"]], # Charlotte - Poems, Jane Eyre
  90. [[u"Amy"]], # Anne - Poems
  91. [[u"Amy"], []], # Emily - Poems, Wuthering Heights
  92. [[u"Amy", u"Belinda"]], # Jane - Sense and Sense
  93. ])
  94. def test_overriding_prefetch(self):
  95. with self.assertNumQueries(3):
  96. qs = Author.objects.prefetch_related('books', 'books__read_by')
  97. lists = [[[unicode(r) for r in b.read_by.all()]
  98. for b in a.books.all()]
  99. for a in qs]
  100. self.assertEqual(lists,
  101. [
  102. [[u"Amy"], [u"Belinda"]], # Charlotte - Poems, Jane Eyre
  103. [[u"Amy"]], # Anne - Poems
  104. [[u"Amy"], []], # Emily - Poems, Wuthering Heights
  105. [[u"Amy", u"Belinda"]], # Jane - Sense and Sense
  106. ])
  107. with self.assertNumQueries(3):
  108. qs = Author.objects.prefetch_related('books__read_by', 'books')
  109. lists = [[[unicode(r) for r in b.read_by.all()]
  110. for b in a.books.all()]
  111. for a in qs]
  112. self.assertEqual(lists,
  113. [
  114. [[u"Amy"], [u"Belinda"]], # Charlotte - Poems, Jane Eyre
  115. [[u"Amy"]], # Anne - Poems
  116. [[u"Amy"], []], # Emily - Poems, Wuthering Heights
  117. [[u"Amy", u"Belinda"]], # Jane - Sense and Sense
  118. ])
  119. def test_get(self):
  120. """
  121. Test that objects retrieved with .get() get the prefetch behaviour
  122. """
  123. # Need a double
  124. with self.assertNumQueries(3):
  125. author = Author.objects.prefetch_related('books__read_by').get(name="Charlotte")
  126. lists = [[unicode(r) for r in b.read_by.all()]
  127. for b in author.books.all()]
  128. self.assertEqual(lists, [[u"Amy"], [u"Belinda"]]) # Poems, Jane Eyre
  129. def test_foreign_key_then_m2m(self):
  130. """
  131. Test we can follow an m2m relation after a relation like ForeignKey
  132. that doesn't have many objects
  133. """
  134. with self.assertNumQueries(2):
  135. qs = Author.objects.select_related('first_book').prefetch_related('first_book__read_by')
  136. lists = [[unicode(r) for r in a.first_book.read_by.all()]
  137. for a in qs]
  138. self.assertEqual(lists, [[u"Amy"],
  139. [u"Amy"],
  140. [u"Amy"],
  141. [u"Amy", "Belinda"]])
  142. def test_attribute_error(self):
  143. qs = Reader.objects.all().prefetch_related('books_read__xyz')
  144. with self.assertRaises(AttributeError) as cm:
  145. list(qs)
  146. self.assertTrue('prefetch_related' in str(cm.exception))
  147. def test_invalid_final_lookup(self):
  148. qs = Book.objects.prefetch_related('authors__first_book')
  149. with self.assertRaises(ValueError) as cm:
  150. list(qs)
  151. self.assertTrue('prefetch_related' in str(cm.exception))
  152. self.assertTrue("first_book" in str(cm.exception))
  153. class DefaultManagerTests(TestCase):
  154. def setUp(self):
  155. self.qual1 = Qualification.objects.create(name="BA")
  156. self.qual2 = Qualification.objects.create(name="BSci")
  157. self.qual3 = Qualification.objects.create(name="MA")
  158. self.qual4 = Qualification.objects.create(name="PhD")
  159. self.teacher1 = Teacher.objects.create(name="Mr Cleese")
  160. self.teacher2 = Teacher.objects.create(name="Mr Idle")
  161. self.teacher3 = Teacher.objects.create(name="Mr Chapman")
  162. self.teacher1.qualifications.add(self.qual1, self.qual2, self.qual3, self.qual4)
  163. self.teacher2.qualifications.add(self.qual1)
  164. self.teacher3.qualifications.add(self.qual2)
  165. self.dept1 = Department.objects.create(name="English")
  166. self.dept2 = Department.objects.create(name="Physics")
  167. self.dept1.teachers.add(self.teacher1, self.teacher2)
  168. self.dept2.teachers.add(self.teacher1, self.teacher3)
  169. def test_m2m_then_m2m(self):
  170. with self.assertNumQueries(3):
  171. # When we prefetch the teachers, and force the query, we don't want
  172. # the default manager on teachers to immediately get all the related
  173. # qualifications, since this will do one query per teacher.
  174. qs = Department.objects.prefetch_related('teachers')
  175. depts = "".join(["%s department: %s\n" %
  176. (dept.name, ", ".join(unicode(t) for t in dept.teachers.all()))
  177. for dept in qs])
  178. self.assertEqual(depts,
  179. "English department: Mr Cleese (BA, BSci, MA, PhD), Mr Idle (BA)\n"
  180. "Physics department: Mr Cleese (BA, BSci, MA, PhD), Mr Chapman (BSci)\n")
  181. class GenericRelationTests(TestCase):
  182. def test_traverse_GFK(self):
  183. """
  184. Test that we can traverse a 'content_object' with prefetch_related()
  185. """
  186. # In fact, there is no special support for this in prefetch_related code
  187. # - we can traverse any object that will lead us to objects that have
  188. # related managers.
  189. book1 = Book.objects.create(title="Winnie the Pooh")
  190. book2 = Book.objects.create(title="Do you like green eggs and spam?")
  191. reader1 = Reader.objects.create(name="me")
  192. reader2 = Reader.objects.create(name="you")
  193. book1.read_by.add(reader1)
  194. book2.read_by.add(reader2)
  195. TaggedItem.objects.create(tag="awesome", content_object=book1)
  196. TaggedItem.objects.create(tag="awesome", content_object=book2)
  197. ct = ContentType.objects.get_for_model(Book)
  198. # We get 4 queries - 1 for main query, 2 for each access to
  199. # 'content_object' because these can't be handled by select_related, and
  200. # 1 for the 'read_by' relation.
  201. with self.assertNumQueries(4):
  202. # If we limit to books, we know that they will have 'read_by'
  203. # attributes, so the following makes sense:
  204. qs = TaggedItem.objects.select_related('content_type').prefetch_related('content_object__read_by').filter(tag='awesome').filter(content_type=ct, tag='awesome')
  205. readers_of_awesome_books = [r.name for tag in qs
  206. for r in tag.content_object.read_by.all()]
  207. self.assertEqual(readers_of_awesome_books, ["me", "you"])
  208. def test_generic_relation(self):
  209. b = Bookmark.objects.create(url='http://www.djangoproject.com/')
  210. t1 = TaggedItem.objects.create(content_object=b, tag='django')
  211. t2 = TaggedItem.objects.create(content_object=b, tag='python')
  212. with self.assertNumQueries(2):
  213. tags = [t.tag for b in Bookmark.objects.prefetch_related('tags')
  214. for t in b.tags.all()]
  215. self.assertEqual(sorted(tags), ["django", "python"])
  216. class MultiTableInheritanceTest(TestCase):
  217. def setUp(self):
  218. self.book1 = BookWithYear.objects.create(
  219. title="Poems", published_year=2010)
  220. self.book2 = BookWithYear.objects.create(
  221. title="More poems", published_year=2011)
  222. self.author1 = AuthorWithAge.objects.create(
  223. name='Jane', first_book=self.book1, age=50)
  224. self.author2 = AuthorWithAge.objects.create(
  225. name='Tom', first_book=self.book1, age=49)
  226. self.author3 = AuthorWithAge.objects.create(
  227. name='Robert', first_book=self.book2, age=48)
  228. self.authorAddress = AuthorAddress.objects.create(
  229. author=self.author1, address='SomeStreet 1')
  230. self.book2.aged_authors.add(self.author2, self.author3)
  231. def test_foreignkey(self):
  232. with self.assertNumQueries(2):
  233. qs = AuthorWithAge.objects.prefetch_related('addresses')
  234. addresses = [[unicode(address) for address in obj.addresses.all()]
  235. for obj in qs]
  236. self.assertEquals(addresses, [[unicode(self.authorAddress)], [], []])
  237. def test_m2m_to_inheriting_model(self):
  238. qs = AuthorWithAge.objects.prefetch_related('books_with_year')
  239. with self.assertNumQueries(2):
  240. lst = [[unicode(book) for book in author.books_with_year.all()]
  241. for author in qs]
  242. qs = AuthorWithAge.objects.all()
  243. lst2 = [[unicode(book) for book in author.books_with_year.all()]
  244. for author in qs]
  245. self.assertEquals(lst, lst2)
  246. qs = BookWithYear.objects.prefetch_related('aged_authors')
  247. with self.assertNumQueries(2):
  248. lst = [[unicode(author) for author in book.aged_authors.all()]
  249. for book in qs]
  250. qs = BookWithYear.objects.all()
  251. lst2 = [[unicode(author) for author in book.aged_authors.all()]
  252. for book in qs]
  253. self.assertEquals(lst, lst2)
  254. def test_parent_link_prefetch(self):
  255. with self.assertRaises(ValueError) as cm:
  256. qs = list(AuthorWithAge.objects.prefetch_related('author'))
  257. self.assertTrue('prefetch_related' in str(cm.exception))
  258. class ForeignKeyToFieldTest(TestCase):
  259. def setUp(self):
  260. self.book = Book.objects.create(title="Poems")
  261. self.author1 = Author.objects.create(name='Jane', first_book=self.book)
  262. self.author2 = Author.objects.create(name='Tom', first_book=self.book)
  263. self.author3 = Author.objects.create(name='Robert', first_book=self.book)
  264. self.authorAddress = AuthorAddress.objects.create(
  265. author=self.author1, address='SomeStreet 1'
  266. )
  267. FavoriteAuthors.objects.create(author=self.author1,
  268. likes_author=self.author2)
  269. FavoriteAuthors.objects.create(author=self.author2,
  270. likes_author=self.author3)
  271. FavoriteAuthors.objects.create(author=self.author3,
  272. likes_author=self.author1)
  273. def test_foreignkey(self):
  274. with self.assertNumQueries(2):
  275. qs = Author.objects.prefetch_related('addresses')
  276. addresses = [[unicode(address) for address in obj.addresses.all()]
  277. for obj in qs]
  278. self.assertEquals(addresses, [[unicode(self.authorAddress)], [], []])
  279. def test_m2m(self):
  280. with self.assertNumQueries(3):
  281. qs = Author.objects.all().prefetch_related('favorite_authors', 'favors_me')
  282. favorites = [(
  283. [unicode(i_like) for i_like in author.favorite_authors.all()],
  284. [unicode(likes_me) for likes_me in author.favors_me.all()]
  285. ) for author in qs]
  286. self.assertEquals(
  287. favorites,
  288. [
  289. ([unicode(self.author2)],[unicode(self.author3)]),
  290. ([unicode(self.author3)],[unicode(self.author1)]),
  291. ([unicode(self.author1)],[unicode(self.author2)])
  292. ]
  293. )
  294. class LookupOrderingTest(TestCase):
  295. """
  296. Test cases that demonstrate that ordering of lookups is important, and
  297. ensure it is preserved.
  298. """
  299. def setUp(self):
  300. self.person1 = Person.objects.create(name="Joe")
  301. self.person2 = Person.objects.create(name="Mary")
  302. self.house1 = House.objects.create(address="123 Main St")
  303. self.house2 = House.objects.create(address="45 Side St")
  304. self.house3 = House.objects.create(address="6 Downing St")
  305. self.house4 = House.objects.create(address="7 Regents St")
  306. self.room1_1 = Room.objects.create(name="Dining room", house=self.house1)
  307. self.room1_2 = Room.objects.create(name="Lounge", house=self.house1)
  308. self.room1_3 = Room.objects.create(name="Kitchen", house=self.house1)
  309. self.room2_1 = Room.objects.create(name="Dining room", house=self.house2)
  310. self.room2_2 = Room.objects.create(name="Lounge", house=self.house2)
  311. self.room3_1 = Room.objects.create(name="Dining room", house=self.house3)
  312. self.room3_2 = Room.objects.create(name="Lounge", house=self.house3)
  313. self.room3_3 = Room.objects.create(name="Kitchen", house=self.house3)
  314. self.room4_1 = Room.objects.create(name="Dining room", house=self.house4)
  315. self.room4_2 = Room.objects.create(name="Lounge", house=self.house4)
  316. self.person1.houses.add(self.house1, self.house2)
  317. self.person2.houses.add(self.house3, self.house4)
  318. def test_order(self):
  319. with self.assertNumQueries(4):
  320. # The following two queries must be done in the same order as written,
  321. # otherwise 'primary_house' will cause non-prefetched lookups
  322. qs = Person.objects.prefetch_related('houses__rooms',
  323. 'primary_house__occupants')
  324. [list(p.primary_house.occupants.all()) for p in qs]
  325. class NullableTest(TestCase):
  326. def setUp(self):
  327. boss = Employee.objects.create(name="Peter")
  328. worker1 = Employee.objects.create(name="Joe", boss=boss)
  329. worker2 = Employee.objects.create(name="Angela", boss=boss)
  330. def test_traverse_nullable(self):
  331. with self.assertNumQueries(2):
  332. qs = Employee.objects.select_related('boss').prefetch_related('boss__serfs')
  333. co_serfs = [list(e.boss.serfs.all()) if e.boss is not None else []
  334. for e in qs]
  335. qs2 = Employee.objects.select_related('boss')
  336. co_serfs2 = [list(e.boss.serfs.all()) if e.boss is not None else []
  337. for e in qs2]
  338. self.assertEqual(co_serfs, co_serfs2)