Skip to content

Commit

Permalink
Fix test_missing_fields_without_PK
Browse files Browse the repository at this point in the history
  • Loading branch information
aclark4life committed Nov 12, 2024
1 parent 6b019ca commit 025026e
Showing 1 changed file with 49 additions and 45 deletions.
94 changes: 49 additions & 45 deletions tests/raw_query/tests.py
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down Expand Up @@ -125,12 +126,8 @@ def assertAnnotations(self, results, expected_annotations):

def test_rawqueryset_repr(self):
queryset = MongoRawQuerySet(raw_query=[])
self.assertEqual(
repr(queryset), "<MongoRawQuerySet: []>"
)
self.assertEqual(
repr(queryset.query), "<MongoRawQuery: []>"
)
self.assertEqual(repr(queryset), "<MongoRawQuerySet: []>")
self.assertEqual(repr(queryset.query), "<MongoRawQuery: []>")

def test_simple_raw_query(self):
"""
Expand Down Expand Up @@ -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()
Expand 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):
"""
Expand Down Expand Up @@ -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),
Expand All @@ -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)

Expand All @@ -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")

Expand All @@ -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):
Expand All @@ -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],
Expand Down Expand Up @@ -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)

0 comments on commit 025026e

Please sign in to comment.