From d12ed398ee83e550f78b684c30ceb5d591b4bc0f Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Mon, 28 Oct 2024 08:24:59 -0400 Subject: [PATCH 1/2] add a couple of captured queries tests --- tests/queries_/__init__.py | 0 tests/queries_/models.py | 16 ++++++++++++++++ tests/queries_/test_mql.py | 26 ++++++++++++++++++++++++++ 3 files changed, 42 insertions(+) create mode 100644 tests/queries_/__init__.py create mode 100644 tests/queries_/models.py create mode 100644 tests/queries_/test_mql.py diff --git a/tests/queries_/__init__.py b/tests/queries_/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/queries_/models.py b/tests/queries_/models.py new file mode 100644 index 000000000..61b93890b --- /dev/null +++ b/tests/queries_/models.py @@ -0,0 +1,16 @@ +from django.db import models + + +class Author(models.Model): + name = models.CharField(max_length=10) + + def __str__(self): + return self.name + + +class Book(models.Model): + title = models.CharField(max_length=10) + author = models.ForeignKey(Author, models.CASCADE) + + def __str__(self): + return self.title diff --git a/tests/queries_/test_mql.py b/tests/queries_/test_mql.py new file mode 100644 index 000000000..788354c77 --- /dev/null +++ b/tests/queries_/test_mql.py @@ -0,0 +1,26 @@ +from django.test import TestCase + +from .models import Author, Book + + +class MQLTests(TestCase): + def test_all(self): + with self.assertNumQueries(1) as ctx: + list(Author.objects.all()) + query = ctx.captured_queries[0]["sql"] + self.assertEqual(query, "db.queries__author.aggregate([{'$match': {'$expr': {}}}])") + + def test_join(self): + with self.assertNumQueries(1) as ctx: + list(Book.objects.filter(author__name="Bob")) + query = ctx.captured_queries[0]["sql"] + self.assertEqual( + query, + "db.queries__book.aggregate([" + "{'$lookup': {'from': 'queries__author', " + "'let': {'parent__field__0': {'$convert': {'input': '$author_id', 'to': 'string'}}}, " + "'pipeline': [{'$match': {'$expr': {'$and': [{'$eq': ['$$parent__field__0', " + "{'$convert': {'input': '$_id', 'to': 'string'}}]}]}}}], 'as': 'queries__author'}}, " + "{'$unwind': '$queries__author'}, " + "{'$match': {'$expr': {'$eq': ['$queries__author.name', 'Bob']}}}])", + ) From 221a2826312210928d1adcc5417ac87391b98910 Mon Sep 17 00:00:00 2001 From: Emanuel Lupi Date: Sat, 26 Oct 2024 17:07:13 -0300 Subject: [PATCH 2/2] remove unnecessary $convert in ObjectIdAutoField $lookup queries --- django_mongodb/fields/auto.py | 3 +++ tests/queries_/test_mql.py | 6 +++--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/django_mongodb/fields/auto.py b/django_mongodb/fields/auto.py index 34dc3a449..9240c759b 100644 --- a/django_mongodb/fields/auto.py +++ b/django_mongodb/fields/auto.py @@ -34,6 +34,9 @@ def get_prep_value(self, value): def db_type(self, connection): return "objectId" + def rel_db_type(self, connection): + return "objectId" + def to_python(self, value): if value is None or isinstance(value, int): return value diff --git a/tests/queries_/test_mql.py b/tests/queries_/test_mql.py index 788354c77..d61e5839d 100644 --- a/tests/queries_/test_mql.py +++ b/tests/queries_/test_mql.py @@ -18,9 +18,9 @@ def test_join(self): query, "db.queries__book.aggregate([" "{'$lookup': {'from': 'queries__author', " - "'let': {'parent__field__0': {'$convert': {'input': '$author_id', 'to': 'string'}}}, " - "'pipeline': [{'$match': {'$expr': {'$and': [{'$eq': ['$$parent__field__0', " - "{'$convert': {'input': '$_id', 'to': 'string'}}]}]}}}], 'as': 'queries__author'}}, " + "'let': {'parent__field__0': '$author_id'}, " + "'pipeline': [{'$match': {'$expr': " + "{'$and': [{'$eq': ['$$parent__field__0', '$_id']}]}}}], 'as': 'queries__author'}}, " "{'$unwind': '$queries__author'}, " "{'$match': {'$expr': {'$eq': ['$queries__author.name', 'Bob']}}}])", )