tests.py 8.3 KB

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