Skip to content

Commit 1351d2c

Browse files
committed
Fix test_missing_fields_without_PK
1 parent e1c680a commit 1351d2c

File tree

1 file changed

+49
-45
lines changed

1 file changed

+49
-45
lines changed

tests/raw_query/tests.py

Lines changed: 49 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
from datetime import date
22
from decimal import Decimal
33

4-
from django.core.exceptions import FieldDoesNotExist
54
from django_mongodb.query import MongoRawQuerySet
5+
6+
from django.core.exceptions import FieldDoesNotExist
67
from django.test import TestCase, skipUnlessDBFeature
78

89
from .models import (
@@ -125,12 +126,8 @@ def assertAnnotations(self, results, expected_annotations):
125126

126127
def test_rawqueryset_repr(self):
127128
queryset = MongoRawQuerySet(raw_query=[])
128-
self.assertEqual(
129-
repr(queryset), "<MongoRawQuerySet: []>"
130-
)
131-
self.assertEqual(
132-
repr(queryset.query), "<MongoRawQuery: []>"
133-
)
129+
self.assertEqual(repr(queryset), "<MongoRawQuerySet: []>")
130+
self.assertEqual(repr(queryset.query), "<MongoRawQuery: []>")
134131

135132
def test_simple_raw_query(self):
136133
"""
@@ -187,7 +184,7 @@ def test_order_handler(self):
187184
)
188185

189186
for select in selects:
190-
cols = [col.strip() for col in select.split(',')]
187+
cols = [col.strip() for col in select.split(",")]
191188
select = {col: 1 for col in cols}
192189
query = [{"$project": select}]
193190
authors = Author.objects.all()
@@ -198,10 +195,21 @@ def test_translations(self):
198195
Test of raw query's optional ability to translate unexpected result
199196
column names to specific model fields
200197
"""
201-
query = [{ "$project": { "first": "$first_name", "last": "$last_name", "dob": 1, "id": 1 }}]
198+
query = [
199+
{
200+
"$project": {
201+
"first": "$first_name",
202+
"last": "$last_name",
203+
"dob": 1,
204+
"id": 1,
205+
}
206+
}
207+
]
202208
translations = {"first": "first_name", "last": "last_name"}
203209
authors = Author.objects.all()
204-
self.assertSuccessfulMongoRawQuery(Author, query, authors, translations=translations)
210+
self.assertSuccessfulMongoRawQuery(
211+
Author, query, authors, translations=translations
212+
)
205213

206214
def test_params(self):
207215
"""
@@ -281,42 +289,42 @@ def test_extra_conversions(self):
281289
query = []
282290
translations = {"something": "else"}
283291
authors = Author.objects.all()
284-
self.assertSuccessfulMongoRawQuery(Author, query, authors, translations=translations)
292+
self.assertSuccessfulMongoRawQuery(
293+
Author, query, authors, translations=translations
294+
)
285295

286296
def test_missing_fields(self):
287-
query = [{"$project": { "id": 1, "first_name": 1, "dob": 1 }}]
297+
query = [{"$project": {"id": 1, "first_name": 1, "dob": 1}}]
288298
for author in Author.objects.raw_mql(query):
289299
self.assertIsNotNone(author.first_name)
290300
# last_name isn't given, but it will be retrieved on demand
291301
self.assertIsNotNone(author.last_name)
292302

293303
def test_missing_fields_without_PK(self):
294-
query = [{"$project": { "first_name": 1, "dob": 1 }}]
304+
query = [{"$project": {"first_name": 1, "dob": 1, "_id": 0}}]
295305
msg = "Raw query must include the primary key"
296306
with self.assertRaisesMessage(FieldDoesNotExist, msg):
297307
list(Author.objects.raw_mql(query))
298308

299309
def test_annotations(self):
300310
query = [
301-
{
302-
"$lookup": {
303-
"from": "raw_query_book",
304-
"localField": "id",
305-
"foreignField": "author_id",
306-
"as": "books"
307-
}
308-
},
309-
{
310-
"$project": {
311-
"first_name": 1,
312-
"last_name": 1,
313-
"dob": 1,
314-
"book_count": { "$size": "$books" }
315-
}
316-
},
317-
{
318-
"$sort": { "id": 1 }
319-
}
311+
{
312+
"$lookup": {
313+
"from": "raw_query_book",
314+
"localField": "id",
315+
"foreignField": "author_id",
316+
"as": "books",
317+
}
318+
},
319+
{
320+
"$project": {
321+
"first_name": 1,
322+
"last_name": 1,
323+
"dob": 1,
324+
"book_count": {"$size": "$books"},
325+
}
326+
},
327+
{"$sort": {"id": 1}},
320328
]
321329
expected_annotations = (
322330
("book_count", 3),
@@ -328,7 +336,7 @@ def test_annotations(self):
328336
self.assertSuccessfulMongoRawQuery(Author, query, authors, expected_annotations)
329337

330338
def test_white_space_query(self):
331-
query = [ ] # noqa: E201
339+
query = [] # noqa: E201
332340
authors = Author.objects.all()
333341
self.assertSuccessfulMongoRawQuery(Author, query, authors)
334342

@@ -353,7 +361,7 @@ def test_multiple_iterations(self):
353361

354362
def test_get_item(self):
355363
# Indexing on MongoRawQuerySets
356-
query = [{"$sort": { "id": 1 }}]
364+
query = [{"$sort": {"id": 1}}]
357365
third_author = Author.objects.raw_mql(query)[2]
358366
self.assertEqual(third_author.first_name, "Bob")
359367

@@ -371,13 +379,13 @@ def test_inheritance(self):
371379
self.assertEqual([o.pk for o in FriendlyAuthor.objects.raw_mql(query)], [f.pk])
372380

373381
def test_query_count(self):
374-
self.assertNumQueries(
375-
1, list, Author.objects.raw_mql([])
376-
)
382+
self.assertNumQueries(1, list, Author.objects.raw_mql([]))
377383

378384
def test_subquery_in_raw_sql(self):
379385
list(
380-
Book.objects.raw_mql([ { "$match": { "paperback": { "$ne": None } } }, { "$project": { "id": 1 } } ])
386+
Book.objects.raw_mql(
387+
[{"$match": {"paperback": {"$ne": None}}}, {"$project": {"id": 1}}]
388+
)
381389
)
382390

383391
def test_db_column_name_is_used_in_raw_query(self):
@@ -392,7 +400,7 @@ def test_db_column_name_is_used_in_raw_query(self):
392400
self.assertEqual(
393401
list(
394402
BookFkAsPk.objects.raw_mql(
395-
[{"$project": { "not_the_default": 1, "_id": 0 }}]
403+
[{"$project": {"not_the_default": 1, "_id": 0}}]
396404
)
397405
),
398406
[b],
@@ -420,12 +428,8 @@ def test_iterator(self):
420428

421429
def test_bool(self):
422430
self.assertIs(bool(Book.objects.raw_mql([])), True)
423-
self.assertIs(
424-
bool(Book.objects.raw_mql([{ "$match": { "id": 0 }}])), False
425-
)
431+
self.assertIs(bool(Book.objects.raw_mql([{"$match": {"id": 0}}])), False)
426432

427433
def test_len(self):
428434
self.assertEqual(len(Book.objects.raw_mql([])), 4)
429-
self.assertEqual(
430-
len(Book.objects.raw_mql([{ "$match": { "id": 0 } }])), 0
431-
)
435+
self.assertEqual(len(Book.objects.raw_mql([{"$match": {"id": 0}}])), 0)

0 commit comments

Comments
 (0)