def test_lookups_using_expression(self):
contains_lookups = {"contains", "contained_by"}
lookups_not_matching = {"gt", "lt", "has_key"}
text_lookups = {
"has_key",
"iexact",
"icontains",
"startswith",
"istartswith",
"endswith",
"iendswith",
"regex",
"iregex",
}
for lookup in JSONField.get_lookups():
with self.subTest(lookup=lookup):
if lookup == "isnull":
continue # not allowed, rhs must be a literal boolean.
if (
lookup in contains_lookups
and not connection.features.supports_json_field_contains
):
continue
if lookup in text_lookups:
rhs = KT("value__bar")
else:
rhs = F("value__bar")
qs = NullableJSONModel.objects.filter(**{f"value__bar__{lookup}": rhs})
if lookup in lookups_not_matching:
self.assertIs(qs.exists(), False)
else:
# might need tweaking
self.assertQuerySetEqual(qs, NullableJSONModel.objects.filter(value__has_key="bar"))