From 025026ec178eab352bcb8ca2f659da177bc1b57d Mon Sep 17 00:00:00 2001 From: "Jeffrey A. Clark" Date: Mon, 11 Nov 2024 21:02:02 -0500 Subject: [PATCH] Fix test_missing_fields_without_PK --- tests/raw_query/tests.py | 94 +++++++++++++++++++++------------------- 1 file changed, 49 insertions(+), 45 deletions(-) diff --git a/tests/raw_query/tests.py b/tests/raw_query/tests.py index 79712f7bea..5a77eba0bc 100644 --- a/tests/raw_query/tests.py +++ b/tests/raw_query/tests.py @@ -1,8 +1,9 @@ from datetime import date from decimal import Decimal -from django.core.exceptions import FieldDoesNotExist from django_mongodb.query import MongoRawQuerySet + +from django.core.exceptions import FieldDoesNotExist from django.test import TestCase, skipUnlessDBFeature from .models import ( @@ -125,12 +126,8 @@ def assertAnnotations(self, results, expected_annotations): def test_rawqueryset_repr(self): queryset = MongoRawQuerySet(raw_query=[]) - self.assertEqual( - repr(queryset), "" - ) - self.assertEqual( - repr(queryset.query), "" - ) + self.assertEqual(repr(queryset), "") + self.assertEqual(repr(queryset.query), "") def test_simple_raw_query(self): """ @@ -187,7 +184,7 @@ def test_order_handler(self): ) for select in selects: - cols = [col.strip() for col in select.split(',')] + cols = [col.strip() for col in select.split(",")] select = {col: 1 for col in cols} query = [{"$project": select}] authors = Author.objects.all() @@ -198,10 +195,21 @@ def test_translations(self): Test of raw query's optional ability to translate unexpected result column names to specific model fields """ - query = [{ "$project": { "first": "$first_name", "last": "$last_name", "dob": 1, "id": 1 }}] + query = [ + { + "$project": { + "first": "$first_name", + "last": "$last_name", + "dob": 1, + "id": 1, + } + } + ] translations = {"first": "first_name", "last": "last_name"} authors = Author.objects.all() - self.assertSuccessfulMongoRawQuery(Author, query, authors, translations=translations) + self.assertSuccessfulMongoRawQuery( + Author, query, authors, translations=translations + ) def test_params(self): """ @@ -281,42 +289,42 @@ def test_extra_conversions(self): query = [] translations = {"something": "else"} authors = Author.objects.all() - self.assertSuccessfulMongoRawQuery(Author, query, authors, translations=translations) + self.assertSuccessfulMongoRawQuery( + Author, query, authors, translations=translations + ) def test_missing_fields(self): - query = [{"$project": { "id": 1, "first_name": 1, "dob": 1 }}] + query = [{"$project": {"id": 1, "first_name": 1, "dob": 1}}] for author in Author.objects.raw_mql(query): self.assertIsNotNone(author.first_name) # last_name isn't given, but it will be retrieved on demand self.assertIsNotNone(author.last_name) def test_missing_fields_without_PK(self): - query = [{"$project": { "first_name": 1, "dob": 1 }}] + query = [{"$project": {"first_name": 1, "dob": 1, "_id": 0}}] msg = "Raw query must include the primary key" with self.assertRaisesMessage(FieldDoesNotExist, msg): list(Author.objects.raw_mql(query)) def test_annotations(self): query = [ - { - "$lookup": { - "from": "raw_query_book", - "localField": "id", - "foreignField": "author_id", - "as": "books" - } - }, - { - "$project": { - "first_name": 1, - "last_name": 1, - "dob": 1, - "book_count": { "$size": "$books" } - } - }, - { - "$sort": { "id": 1 } - } + { + "$lookup": { + "from": "raw_query_book", + "localField": "id", + "foreignField": "author_id", + "as": "books", + } + }, + { + "$project": { + "first_name": 1, + "last_name": 1, + "dob": 1, + "book_count": {"$size": "$books"}, + } + }, + {"$sort": {"id": 1}}, ] expected_annotations = ( ("book_count", 3), @@ -328,7 +336,7 @@ def test_annotations(self): self.assertSuccessfulMongoRawQuery(Author, query, authors, expected_annotations) def test_white_space_query(self): - query = [ ] # noqa: E201 + query = [] # noqa: E201 authors = Author.objects.all() self.assertSuccessfulMongoRawQuery(Author, query, authors) @@ -353,7 +361,7 @@ def test_multiple_iterations(self): def test_get_item(self): # Indexing on MongoRawQuerySets - query = [{"$sort": { "id": 1 }}] + query = [{"$sort": {"id": 1}}] third_author = Author.objects.raw_mql(query)[2] self.assertEqual(third_author.first_name, "Bob") @@ -371,13 +379,13 @@ def test_inheritance(self): self.assertEqual([o.pk for o in FriendlyAuthor.objects.raw_mql(query)], [f.pk]) def test_query_count(self): - self.assertNumQueries( - 1, list, Author.objects.raw_mql([]) - ) + self.assertNumQueries(1, list, Author.objects.raw_mql([])) def test_subquery_in_raw_sql(self): list( - Book.objects.raw_mql([ { "$match": { "paperback": { "$ne": None } } }, { "$project": { "id": 1 } } ]) + Book.objects.raw_mql( + [{"$match": {"paperback": {"$ne": None}}}, {"$project": {"id": 1}}] + ) ) 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): self.assertEqual( list( BookFkAsPk.objects.raw_mql( - [{"$project": { "not_the_default": 1, "_id": 0 }}] + [{"$project": {"not_the_default": 1, "_id": 0}}] ) ), [b], @@ -420,12 +428,8 @@ def test_iterator(self): def test_bool(self): self.assertIs(bool(Book.objects.raw_mql([])), True) - self.assertIs( - bool(Book.objects.raw_mql([{ "$match": { "id": 0 }}])), False - ) + self.assertIs(bool(Book.objects.raw_mql([{"$match": {"id": 0}}])), False) def test_len(self): self.assertEqual(len(Book.objects.raw_mql([])), 4) - self.assertEqual( - len(Book.objects.raw_mql([{ "$match": { "id": 0 } }])), 0 - ) + self.assertEqual(len(Book.objects.raw_mql([{"$match": {"id": 0}}])), 0)