|
1 | 1 | from bson import SON |
| 2 | +from django.db.models import Sum |
2 | 3 | from django.test import TestCase |
3 | 4 |
|
4 | 5 | from django_mongodb_backend.test import MongoTestCaseMixin |
@@ -73,3 +74,112 @@ def test_eq_and_in(self): |
73 | 74 | "lookup__book", |
74 | 75 | [{"$match": {"$and": [{"isbn": {"$in": ("12345", "56789")}}, {"title": "Moby Dick"}]}}], |
75 | 76 | ) |
| 77 | + |
| 78 | + def test_gt(self): |
| 79 | + with self.assertNumQueries(1) as ctx: |
| 80 | + list(Number.objects.filter(num__gt=2)) |
| 81 | + self.assertAggregateQuery( |
| 82 | + ctx.captured_queries[0]["sql"], |
| 83 | + "lookup__number", |
| 84 | + [ |
| 85 | + {"$match": {"num": {"$gt": 2}}}, |
| 86 | + {"$addFields": {"num": "$num"}}, |
| 87 | + {"$sort": SON([("num", 1)])}, |
| 88 | + ], |
| 89 | + ) |
| 90 | + |
| 91 | + def test_gte(self): |
| 92 | + with self.assertNumQueries(1) as ctx: |
| 93 | + list(Number.objects.filter(num__gte=2)) |
| 94 | + self.assertAggregateQuery( |
| 95 | + ctx.captured_queries[0]["sql"], |
| 96 | + "lookup__number", |
| 97 | + [ |
| 98 | + {"$match": {"num": {"$gte": 2}}}, |
| 99 | + {"$addFields": {"num": "$num"}}, |
| 100 | + {"$sort": SON([("num", 1)])}, |
| 101 | + ], |
| 102 | + ) |
| 103 | + |
| 104 | + def test_group_by_with_having(self): |
| 105 | + with self.assertNumQueries(1) as ctx: |
| 106 | + list(Number.objects.values("num").annotate(total=Sum("num")).filter(total=1)) |
| 107 | + self.assertAggregateQuery( |
| 108 | + ctx.captured_queries[0]["sql"], |
| 109 | + "lookup__number", |
| 110 | + [ |
| 111 | + { |
| 112 | + "$group": { |
| 113 | + "__aggregation1": {"$sum": "$num"}, |
| 114 | + "_id": {"num": "$num"}, |
| 115 | + "total": {"$sum": "$num"}, |
| 116 | + } |
| 117 | + }, |
| 118 | + {"$addFields": {"num": "$_id.num"}}, |
| 119 | + {"$unset": "_id"}, |
| 120 | + {"$match": {"__aggregation1": 1}}, |
| 121 | + {"$project": {"num": 1, "total": "$__aggregation1"}}, |
| 122 | + {"$sort": SON([("num", 1)])}, |
| 123 | + ], |
| 124 | + ) |
| 125 | + |
| 126 | + def test_subquery_filter_constant(self): |
| 127 | + with self.assertNumQueries(1) as ctx: |
| 128 | + list(Number.objects.filter(num__in=Number.objects.filter(num__gt=2).values("num"))) |
| 129 | + self.assertAggregateQuery( |
| 130 | + ctx.captured_queries[0]["sql"], |
| 131 | + "lookup__number", |
| 132 | + [ |
| 133 | + { |
| 134 | + "$lookup": { |
| 135 | + "as": "__subquery0", |
| 136 | + "from": "lookup__number", |
| 137 | + "let": {}, |
| 138 | + "pipeline": [ |
| 139 | + {"$match": {"num": {"$gt": 2}}}, |
| 140 | + { |
| 141 | + "$facet": { |
| 142 | + "group": [ |
| 143 | + {"$group": {"_id": None, "tmp_name": {"$addToSet": "$num"}}} |
| 144 | + ] |
| 145 | + } |
| 146 | + }, |
| 147 | + { |
| 148 | + "$project": { |
| 149 | + "num": { |
| 150 | + "$ifNull": [ |
| 151 | + { |
| 152 | + "$getField": { |
| 153 | + "input": {"$arrayElemAt": ["$group", 0]}, |
| 154 | + "field": "tmp_name", |
| 155 | + } |
| 156 | + }, |
| 157 | + [], |
| 158 | + ] |
| 159 | + } |
| 160 | + } |
| 161 | + }, |
| 162 | + ], |
| 163 | + } |
| 164 | + }, |
| 165 | + { |
| 166 | + "$set": { |
| 167 | + "__subquery0": { |
| 168 | + "$cond": { |
| 169 | + "if": { |
| 170 | + "$or": [ |
| 171 | + {"$eq": [{"$type": "$__subquery0"}, "missing"]}, |
| 172 | + {"$eq": [{"$size": "$__subquery0"}, 0]}, |
| 173 | + ] |
| 174 | + }, |
| 175 | + "then": {}, |
| 176 | + "else": {"$arrayElemAt": ["$__subquery0", 0]}, |
| 177 | + } |
| 178 | + } |
| 179 | + } |
| 180 | + }, |
| 181 | + {"$match": {"$expr": {"$in": ["$num", "$__subquery0.num"]}}}, |
| 182 | + {"$addFields": {"num": "$num"}}, |
| 183 | + {"$sort": SON([("num", 1)])}, |
| 184 | + ], |
| 185 | + ) |
0 commit comments