tests.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. from __future__ import absolute_import
  2. from datetime import date
  3. from django.db.models.query_utils import InvalidQuery
  4. from django.test import TestCase, skipUnlessDBFeature
  5. from .models import Author, Book, Coffee, Reviewer, FriendlyAuthor
  6. class RawQueryTests(TestCase):
  7. fixtures = ['raw_query_books.json']
  8. def assertSuccessfulRawQuery(self, model, query, expected_results,
  9. expected_annotations=(), params=[], translations=None):
  10. """
  11. Execute the passed query against the passed model and check the output
  12. """
  13. results = list(model.objects.raw(query, params=params, translations=translations))
  14. self.assertProcessed(model, results, expected_results, expected_annotations)
  15. self.assertAnnotations(results, expected_annotations)
  16. def assertProcessed(self, model, results, orig, expected_annotations=()):
  17. """
  18. Compare the results of a raw query against expected results
  19. """
  20. self.assertEqual(len(results), len(orig))
  21. for index, item in enumerate(results):
  22. orig_item = orig[index]
  23. for annotation in expected_annotations:
  24. setattr(orig_item, *annotation)
  25. for field in model._meta.fields:
  26. # Check that all values on the model are equal
  27. self.assertEqual(getattr(item,field.attname),
  28. getattr(orig_item,field.attname))
  29. # This includes checking that they are the same type
  30. self.assertEqual(type(getattr(item,field.attname)),
  31. type(getattr(orig_item,field.attname)))
  32. def assertNoAnnotations(self, results):
  33. """
  34. Check that the results of a raw query contain no annotations
  35. """
  36. self.assertAnnotations(results, ())
  37. def assertAnnotations(self, results, expected_annotations):
  38. """
  39. Check that the passed raw query results contain the expected
  40. annotations
  41. """
  42. if expected_annotations:
  43. for index, result in enumerate(results):
  44. annotation, value = expected_annotations[index]
  45. self.assertTrue(hasattr(result, annotation))
  46. self.assertEqual(getattr(result, annotation), value)
  47. def testSimpleRawQuery(self):
  48. """
  49. Basic test of raw query with a simple database query
  50. """
  51. query = "SELECT * FROM raw_query_author"
  52. authors = Author.objects.all()
  53. self.assertSuccessfulRawQuery(Author, query, authors)
  54. def testRawQueryLazy(self):
  55. """
  56. Raw queries are lazy: they aren't actually executed until they're
  57. iterated over.
  58. """
  59. q = Author.objects.raw('SELECT * FROM raw_query_author')
  60. self.assertTrue(q.query.cursor is None)
  61. list(q)
  62. self.assertTrue(q.query.cursor is not None)
  63. def testFkeyRawQuery(self):
  64. """
  65. Test of a simple raw query against a model containing a foreign key
  66. """
  67. query = "SELECT * FROM raw_query_book"
  68. books = Book.objects.all()
  69. self.assertSuccessfulRawQuery(Book, query, books)
  70. def testDBColumnHandler(self):
  71. """
  72. Test of a simple raw query against a model containing a field with
  73. db_column defined.
  74. """
  75. query = "SELECT * FROM raw_query_coffee"
  76. coffees = Coffee.objects.all()
  77. self.assertSuccessfulRawQuery(Coffee, query, coffees)
  78. def testOrderHandler(self):
  79. """
  80. Test of raw raw query's tolerance for columns being returned in any
  81. order
  82. """
  83. selects = (
  84. ('dob, last_name, first_name, id'),
  85. ('last_name, dob, first_name, id'),
  86. ('first_name, last_name, dob, id'),
  87. )
  88. for select in selects:
  89. query = "SELECT %s FROM raw_query_author" % select
  90. authors = Author.objects.all()
  91. self.assertSuccessfulRawQuery(Author, query, authors)
  92. def testTranslations(self):
  93. """
  94. Test of raw query's optional ability to translate unexpected result
  95. column names to specific model fields
  96. """
  97. query = "SELECT first_name AS first, last_name AS last, dob, id FROM raw_query_author"
  98. translations = {'first': 'first_name', 'last': 'last_name'}
  99. authors = Author.objects.all()
  100. self.assertSuccessfulRawQuery(Author, query, authors, translations=translations)
  101. def testParams(self):
  102. """
  103. Test passing optional query parameters
  104. """
  105. query = "SELECT * FROM raw_query_author WHERE first_name = %s"
  106. author = Author.objects.all()[2]
  107. params = [author.first_name]
  108. qset = Author.objects.raw(query, params=params)
  109. results = list(qset)
  110. self.assertProcessed(Author, results, [author])
  111. self.assertNoAnnotations(results)
  112. self.assertEqual(len(results), 1)
  113. self.assertIsInstance(repr(qset), str)
  114. @skipUnlessDBFeature('supports_paramstyle_pyformat')
  115. def testPyformatParams(self):
  116. """
  117. Test passing optional query parameters
  118. """
  119. query = "SELECT * FROM raw_query_author WHERE first_name = %(first)s"
  120. author = Author.objects.all()[2]
  121. params = {'first': author.first_name}
  122. qset = Author.objects.raw(query, params=params)
  123. results = list(qset)
  124. self.assertProcessed(Author, results, [author])
  125. self.assertNoAnnotations(results)
  126. self.assertEqual(len(results), 1)
  127. self.assertIsInstance(repr(qset), str)
  128. def testManyToMany(self):
  129. """
  130. Test of a simple raw query against a model containing a m2m field
  131. """
  132. query = "SELECT * FROM raw_query_reviewer"
  133. reviewers = Reviewer.objects.all()
  134. self.assertSuccessfulRawQuery(Reviewer, query, reviewers)
  135. def testExtraConversions(self):
  136. """
  137. Test to insure that extra translations are ignored.
  138. """
  139. query = "SELECT * FROM raw_query_author"
  140. translations = {'something': 'else'}
  141. authors = Author.objects.all()
  142. self.assertSuccessfulRawQuery(Author, query, authors, translations=translations)
  143. def testMissingFields(self):
  144. query = "SELECT id, first_name, dob FROM raw_query_author"
  145. for author in Author.objects.raw(query):
  146. self.assertNotEqual(author.first_name, None)
  147. # last_name isn't given, but it will be retrieved on demand
  148. self.assertNotEqual(author.last_name, None)
  149. def testMissingFieldsWithoutPK(self):
  150. query = "SELECT first_name, dob FROM raw_query_author"
  151. try:
  152. list(Author.objects.raw(query))
  153. self.fail('Query without primary key should fail')
  154. except InvalidQuery:
  155. pass
  156. def testAnnotations(self):
  157. query = "SELECT a.*, count(b.id) as book_count FROM raw_query_author a LEFT JOIN raw_query_book b ON a.id = b.author_id GROUP BY a.id, a.first_name, a.last_name, a.dob ORDER BY a.id"
  158. expected_annotations = (
  159. ('book_count', 3),
  160. ('book_count', 0),
  161. ('book_count', 1),
  162. ('book_count', 0),
  163. )
  164. authors = Author.objects.all()
  165. self.assertSuccessfulRawQuery(Author, query, authors, expected_annotations)
  166. def testWhiteSpaceQuery(self):
  167. query = " SELECT * FROM raw_query_author"
  168. authors = Author.objects.all()
  169. self.assertSuccessfulRawQuery(Author, query, authors)
  170. def testMultipleIterations(self):
  171. query = "SELECT * FROM raw_query_author"
  172. normal_authors = Author.objects.all()
  173. raw_authors = Author.objects.raw(query)
  174. # First Iteration
  175. first_iterations = 0
  176. for index, raw_author in enumerate(raw_authors):
  177. self.assertEqual(normal_authors[index], raw_author)
  178. first_iterations += 1
  179. # Second Iteration
  180. second_iterations = 0
  181. for index, raw_author in enumerate(raw_authors):
  182. self.assertEqual(normal_authors[index], raw_author)
  183. second_iterations += 1
  184. self.assertEqual(first_iterations, second_iterations)
  185. def testGetItem(self):
  186. # Indexing on RawQuerySets
  187. query = "SELECT * FROM raw_query_author ORDER BY id ASC"
  188. third_author = Author.objects.raw(query)[2]
  189. self.assertEqual(third_author.first_name, 'Bob')
  190. first_two = Author.objects.raw(query)[0:2]
  191. self.assertEqual(len(first_two), 2)
  192. self.assertRaises(TypeError, lambda: Author.objects.raw(query)['test'])
  193. def test_inheritance(self):
  194. # date is the end of the Cuban Missile Crisis, I have no idea when
  195. # Wesley was bron
  196. f = FriendlyAuthor.objects.create(first_name="Wesley", last_name="Chun",
  197. dob=date(1962, 10, 28))
  198. query = "SELECT * FROM raw_query_friendlyauthor"
  199. self.assertEqual(
  200. [o.pk for o in FriendlyAuthor.objects.raw(query)], [f.pk]
  201. )
  202. def test_query_count(self):
  203. self.assertNumQueries(1,
  204. list, Author.objects.raw("SELECT * FROM raw_query_author")
  205. )