summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorJohan Schiff <johan@radkompaniet.se>2020-06-09 11:23:31 +0200
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2021-03-06 20:40:29 +0100
commitd01709aae21de9cd2565b9c52f32732ea28a2d98 (patch)
tree83326f30469fb3dda431362dea3adfd6e058f85f /tests
parent286fb73b6962d197ed0cf041755fb724cfe08600 (diff)
Fixed #24141 -- Added QuerySet.contains().
Diffstat (limited to 'tests')
-rw-r--r--tests/basic/tests.py1
-rw-r--r--tests/queries/test_contains.py62
-rw-r--r--tests/queries/test_qs_combinators.py6
3 files changed, 69 insertions, 0 deletions
diff --git a/tests/basic/tests.py b/tests/basic/tests.py
index c99fc7e723..8b40f9c33c 100644
--- a/tests/basic/tests.py
+++ b/tests/basic/tests.py
@@ -602,6 +602,7 @@ class ManagerTest(SimpleTestCase):
'only',
'using',
'exists',
+ 'contains',
'explain',
'_insert',
'_update',
diff --git a/tests/queries/test_contains.py b/tests/queries/test_contains.py
new file mode 100644
index 0000000000..a58dbe180f
--- /dev/null
+++ b/tests/queries/test_contains.py
@@ -0,0 +1,62 @@
+from django.test import TestCase
+
+from .models import DumbCategory, NamedCategory, ProxyCategory
+
+
+class ContainsTests(TestCase):
+ @classmethod
+ def setUpTestData(cls):
+ cls.category = DumbCategory.objects.create()
+ cls.proxy_category = ProxyCategory.objects.create()
+
+ def test_unsaved_obj(self):
+ msg = 'QuerySet.contains() cannot be used on unsaved objects.'
+ with self.assertRaisesMessage(ValueError, msg):
+ DumbCategory.objects.contains(DumbCategory())
+
+ def test_obj_type(self):
+ msg = "'obj' must be a model instance."
+ with self.assertRaisesMessage(TypeError, msg):
+ DumbCategory.objects.contains(object())
+
+ def test_values(self):
+ msg = 'Cannot call QuerySet.contains() after .values() or .values_list().'
+ with self.assertRaisesMessage(TypeError, msg):
+ DumbCategory.objects.values_list('pk').contains(self.category)
+ with self.assertRaisesMessage(TypeError, msg):
+ DumbCategory.objects.values('pk').contains(self.category)
+
+ def test_basic(self):
+ with self.assertNumQueries(1):
+ self.assertIs(DumbCategory.objects.contains(self.category), True)
+ # QuerySet.contains() doesn't evaluate a queryset.
+ with self.assertNumQueries(1):
+ self.assertIs(DumbCategory.objects.contains(self.category), True)
+
+ def test_evaluated_queryset(self):
+ qs = DumbCategory.objects.all()
+ proxy_qs = ProxyCategory.objects.all()
+ # Evaluate querysets.
+ list(qs)
+ list(proxy_qs)
+ with self.assertNumQueries(0):
+ self.assertIs(qs.contains(self.category), True)
+ self.assertIs(qs.contains(self.proxy_category), True)
+ self.assertIs(proxy_qs.contains(self.category), True)
+ self.assertIs(proxy_qs.contains(self.proxy_category), True)
+
+ def test_proxy_model(self):
+ with self.assertNumQueries(1):
+ self.assertIs(DumbCategory.objects.contains(self.proxy_category), True)
+ with self.assertNumQueries(1):
+ self.assertIs(ProxyCategory.objects.contains(self.category), True)
+
+ def test_wrong_model(self):
+ qs = DumbCategory.objects.all()
+ named_category = NamedCategory(name='category')
+ with self.assertNumQueries(0):
+ self.assertIs(qs.contains(named_category), False)
+ # Evaluate the queryset.
+ list(qs)
+ with self.assertNumQueries(0):
+ self.assertIs(qs.contains(named_category), False)
diff --git a/tests/queries/test_qs_combinators.py b/tests/queries/test_qs_combinators.py
index 81c7b2e3a3..3b8204645d 100644
--- a/tests/queries/test_qs_combinators.py
+++ b/tests/queries/test_qs_combinators.py
@@ -404,6 +404,12 @@ class QuerySetSetOperationTests(TestCase):
msg % (operation, combinator),
):
getattr(getattr(qs, combinator)(qs), operation)()
+ with self.assertRaisesMessage(
+ NotSupportedError,
+ msg % ('contains', combinator),
+ ):
+ obj = Number.objects.first()
+ getattr(qs, combinator)(qs).contains(obj)
def test_get_with_filters_unsupported_on_combined_qs(self):
qs = Number.objects.all()