1
from datetime import date
3
from django.db.models.sql.query import InvalidQuery
4
from django.test import TestCase
6
from models import Author, Book, Coffee, Reviewer, FriendlyAuthor
9
class RawQueryTests(TestCase):
10
fixtures = ['raw_query_books.json']
12
def assertSuccessfulRawQuery(self, model, query, expected_results,
13
expected_annotations=(), params=[], translations=None):
15
Execute the passed query against the passed model and check the output
17
results = list(model.objects.raw(query, params=params, translations=translations))
18
self.assertProcessed(model, results, expected_results, expected_annotations)
19
self.assertAnnotations(results, expected_annotations)
21
def assertProcessed(self, model, results, orig, expected_annotations=()):
23
Compare the results of a raw query against expected results
25
self.assertEqual(len(results), len(orig))
26
for index, item in enumerate(results):
27
orig_item = orig[index]
28
for annotation in expected_annotations:
29
setattr(orig_item, *annotation)
31
for field in model._meta.fields:
32
# Check that all values on the model are equal
33
self.assertEquals(getattr(item,field.attname),
34
getattr(orig_item,field.attname))
35
# This includes checking that they are the same type
36
self.assertEquals(type(getattr(item,field.attname)),
37
type(getattr(orig_item,field.attname)))
39
def assertNoAnnotations(self, results):
41
Check that the results of a raw query contain no annotations
43
self.assertAnnotations(results, ())
45
def assertAnnotations(self, results, expected_annotations):
47
Check that the passed raw query results contain the expected
50
if expected_annotations:
51
for index, result in enumerate(results):
52
annotation, value = expected_annotations[index]
53
self.assertTrue(hasattr(result, annotation))
54
self.assertEqual(getattr(result, annotation), value)
56
def testSimpleRawQuery(self):
58
Basic test of raw query with a simple database query
60
query = "SELECT * FROM raw_query_author"
61
authors = Author.objects.all()
62
self.assertSuccessfulRawQuery(Author, query, authors)
64
def testRawQueryLazy(self):
66
Raw queries are lazy: they aren't actually executed until they're
69
q = Author.objects.raw('SELECT * FROM raw_query_author')
70
self.assert_(q.query.cursor is None)
72
self.assert_(q.query.cursor is not None)
74
def testFkeyRawQuery(self):
76
Test of a simple raw query against a model containing a foreign key
78
query = "SELECT * FROM raw_query_book"
79
books = Book.objects.all()
80
self.assertSuccessfulRawQuery(Book, query, books)
82
def testDBColumnHandler(self):
84
Test of a simple raw query against a model containing a field with
87
query = "SELECT * FROM raw_query_coffee"
88
coffees = Coffee.objects.all()
89
self.assertSuccessfulRawQuery(Coffee, query, coffees)
91
def testOrderHandler(self):
93
Test of raw raw query's tolerance for columns being returned in any
97
('dob, last_name, first_name, id'),
98
('last_name, dob, first_name, id'),
99
('first_name, last_name, dob, id'),
102
for select in selects:
103
query = "SELECT %s FROM raw_query_author" % select
104
authors = Author.objects.all()
105
self.assertSuccessfulRawQuery(Author, query, authors)
107
def testTranslations(self):
109
Test of raw query's optional ability to translate unexpected result
110
column names to specific model fields
112
query = "SELECT first_name AS first, last_name AS last, dob, id FROM raw_query_author"
113
translations = {'first': 'first_name', 'last': 'last_name'}
114
authors = Author.objects.all()
115
self.assertSuccessfulRawQuery(Author, query, authors, translations=translations)
117
def testParams(self):
119
Test passing optional query parameters
121
query = "SELECT * FROM raw_query_author WHERE first_name = %s"
122
author = Author.objects.all()[2]
123
params = [author.first_name]
124
results = list(Author.objects.raw(query, params=params))
125
self.assertProcessed(Author, results, [author])
126
self.assertNoAnnotations(results)
127
self.assertEqual(len(results), 1)
129
def testManyToMany(self):
131
Test of a simple raw query against a model containing a m2m field
133
query = "SELECT * FROM raw_query_reviewer"
134
reviewers = Reviewer.objects.all()
135
self.assertSuccessfulRawQuery(Reviewer, query, reviewers)
137
def testExtraConversions(self):
139
Test to insure that extra translations are ignored.
141
query = "SELECT * FROM raw_query_author"
142
translations = {'something': 'else'}
143
authors = Author.objects.all()
144
self.assertSuccessfulRawQuery(Author, query, authors, translations=translations)
146
def testMissingFields(self):
147
query = "SELECT id, first_name, dob FROM raw_query_author"
148
for author in Author.objects.raw(query):
149
self.assertNotEqual(author.first_name, None)
150
# last_name isn't given, but it will be retrieved on demand
151
self.assertNotEqual(author.last_name, None)
153
def testMissingFieldsWithoutPK(self):
154
query = "SELECT first_name, dob FROM raw_query_author"
156
list(Author.objects.raw(query))
157
self.fail('Query without primary key should fail')
161
def testAnnotations(self):
162
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"
163
expected_annotations = (
169
authors = Author.objects.all()
170
self.assertSuccessfulRawQuery(Author, query, authors, expected_annotations)
172
def testInvalidQuery(self):
173
query = "UPDATE raw_query_author SET first_name='thing' WHERE first_name='Joe'"
174
self.assertRaises(InvalidQuery, Author.objects.raw, query)
176
def testWhiteSpaceQuery(self):
177
query = " SELECT * FROM raw_query_author"
178
authors = Author.objects.all()
179
self.assertSuccessfulRawQuery(Author, query, authors)
181
def testMultipleIterations(self):
182
query = "SELECT * FROM raw_query_author"
183
normal_authors = Author.objects.all()
184
raw_authors = Author.objects.raw(query)
188
for index, raw_author in enumerate(raw_authors):
189
self.assertEqual(normal_authors[index], raw_author)
190
first_iterations += 1
193
second_iterations = 0
194
for index, raw_author in enumerate(raw_authors):
195
self.assertEqual(normal_authors[index], raw_author)
196
second_iterations += 1
198
self.assertEqual(first_iterations, second_iterations)
200
def testGetItem(self):
201
# Indexing on RawQuerySets
202
query = "SELECT * FROM raw_query_author ORDER BY id ASC"
203
third_author = Author.objects.raw(query)[2]
204
self.assertEqual(third_author.first_name, 'Bob')
206
first_two = Author.objects.raw(query)[0:2]
207
self.assertEquals(len(first_two), 2)
209
self.assertRaises(TypeError, lambda: Author.objects.raw(query)['test'])
211
def test_inheritance(self):
212
# date is the end of the Cuban Missile Crisis, I have no idea when
214
f = FriendlyAuthor.objects.create(first_name="Wesley", last_name="Chun",
215
dob=date(1962, 10, 28))
216
query = "SELECT * FROM raw_query_friendlyauthor"
218
[o.pk for o in FriendlyAuthor.objects.raw(query)], [f.pk]