Skip to content

Commit 19502fc

Browse files
committed
add test_contained_by_subquery (broken)
1 parent aff20ae commit 19502fc

File tree

2 files changed

+81
-1
lines changed

2 files changed

+81
-1
lines changed

django_mongodb_backend/fields/array.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,13 @@ class ArrayContainedBy(ArrayRHSMixin, FieldGetDbPrepValueMixin, Lookup):
260260
def as_mql(self, compiler, connection):
261261
lhs_mql = process_lhs(self, compiler, connection)
262262
value = process_rhs(self, compiler, connection)
263-
return {"$and": [{"$ne": [lhs_mql, None]}, {"$setIsSubset": [lhs_mql, value]}]}
263+
return {
264+
"$and": [
265+
{"$ne": [lhs_mql, None]},
266+
{"$ne": [value, None]},
267+
{"$setIsSubset": [lhs_mql, value]},
268+
]
269+
}
264270

265271

266272
@ArrayField.register_lookup

tests/model_fields_/test_arrayfield.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,80 @@ def test_contains_subquery(self):
369369
self.objs[1:3],
370370
)
371371

372+
def test_contained_by_subquery(self):
373+
IntegerArrayModel.objects.create(field=[2, 3])
374+
inner_qs = IntegerArrayModel.objects.values_list("field", flat=True)
375+
self.assertSequenceEqual(
376+
NullableIntegerArrayModel.objects.filter(field__contained_by=inner_qs[:1]),
377+
self.objs[1:3],
378+
)
379+
IntegerArrayModel.objects.create(field=[2])
380+
inner_qs = IntegerArrayModel.objects.filter(field__contained_by=OuterRef("field"))
381+
# fails with "both operands of $setIsSubset must be arrays. Second
382+
# argument is of type: null"
383+
self.assertSequenceEqual(
384+
NullableIntegerArrayModel.objects.filter(Exists(inner_qs)),
385+
self.objs[1:3],
386+
)
387+
388+
# [
389+
# {
390+
# "$lookup": {
391+
# "as": "__subquery0",
392+
# "from": "model_fields__integerarraymodel",
393+
# "let": {"parent__field__0": "$field"},
394+
# "pipeline": [
395+
# {
396+
# "$match": {
397+
# "$expr": {
398+
# "$and": [
399+
# {"$ne": ["$field", None]},
400+
# {"$ne": ["$$parent__field__0", None]},
401+
# {"$setIsSubset": ["$field", "$$parent__field__0"]},
402+
# ]
403+
# }
404+
# }
405+
# },
406+
# {"$project": {"a": {"$literal": 1}}},
407+
# {"$limit": 1},
408+
# ],
409+
# }
410+
# },
411+
# {
412+
# "$set": {
413+
# "__subquery0": {
414+
# "$cond": {
415+
# "if": {
416+
# "$or": [
417+
# {"$eq": [{"$type": "$__subquery0"}, "missing"]},
418+
# {"$eq": [{"$size": "$__subquery0"}, 0]},
419+
# ]
420+
# },
421+
# "then": {},
422+
# "else": {"$arrayElemAt": ["$__subquery0", 0]},
423+
# }
424+
# }
425+
# }
426+
# },
427+
# {
428+
# "$match": {
429+
# "$expr": {
430+
# "$eq": [
431+
# {
432+
# "$not": {
433+
# "$or": [
434+
# {"$eq": [{"$type": "$__subquery0.a"}, "missing"]},
435+
# {"$eq": ["$__subquery0.a", None]},
436+
# ]
437+
# }
438+
# },
439+
# True,
440+
# ]
441+
# }
442+
# }
443+
# },
444+
# ]
445+
372446
def test_contains_including_expression(self):
373447
self.assertSequenceEqual(
374448
NullableIntegerArrayModel.objects.filter(

0 commit comments

Comments
 (0)