summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorMariusz Felisiak <felisiak.mariusz@gmail.com>2021-03-10 09:16:28 +0100
committerGitHub <noreply@github.com>2021-03-10 09:16:28 +0100
commitba9a2b754452b542d3f472f0acce6f940911aced (patch)
tree3ef805503af0ff58dedba310ae18a0b1f14c8edd /tests
parent6f5dbe9dbe45b23b3befe4f1cd2ea13b6049ab96 (diff)
Refs #32508 -- Raised TypeError instead of using "assert" on unsupported operations for sliced querysets.
Diffstat (limited to 'tests')
-rw-r--r--tests/delete/tests.py4
-rw-r--r--tests/distinct_on_fields/tests.py5
-rw-r--r--tests/get_earliest_or_latest/tests.py10
-rw-r--r--tests/lookup/tests.py5
-rw-r--r--tests/queries/tests.py15
-rw-r--r--tests/update/tests.py2
6 files changed, 35 insertions, 6 deletions
diff --git a/tests/delete/tests.py b/tests/delete/tests.py
index 3dce135dd5..bf6543d735 100644
--- a/tests/delete/tests.py
+++ b/tests/delete/tests.py
@@ -275,6 +275,10 @@ class OnDeleteTests(TestCase):
class DeletionTests(TestCase):
+ def test_sliced_queryset(self):
+ msg = "Cannot use 'limit' or 'offset' with delete()."
+ with self.assertRaisesMessage(TypeError, msg):
+ M.objects.all()[0:5].delete()
def test_m2m(self):
m = M.objects.create()
diff --git a/tests/distinct_on_fields/tests.py b/tests/distinct_on_fields/tests.py
index 34d4509cec..f87a3affb3 100644
--- a/tests/distinct_on_fields/tests.py
+++ b/tests/distinct_on_fields/tests.py
@@ -98,6 +98,11 @@ class DistinctOnTests(TestCase):
c2 = c1.distinct('pk')
self.assertNotIn('OUTER JOIN', str(c2.query))
+ def test_sliced_queryset(self):
+ msg = 'Cannot create distinct fields once a slice has been taken.'
+ with self.assertRaisesMessage(TypeError, msg):
+ Staff.objects.all()[0:5].distinct('name')
+
def test_transform(self):
new_name = self.t1.name.upper()
self.assertNotEqual(self.t1.name, new_name)
diff --git a/tests/get_earliest_or_latest/tests.py b/tests/get_earliest_or_latest/tests.py
index 96ebe19f32..0234deb195 100644
--- a/tests/get_earliest_or_latest/tests.py
+++ b/tests/get_earliest_or_latest/tests.py
@@ -81,6 +81,11 @@ class EarliestOrLatestTests(TestCase):
Article.objects.model._meta.get_latest_by = ('pub_date', 'expire_date')
self.assertEqual(Article.objects.filter(pub_date=datetime(2005, 7, 28)).earliest(), a4)
+ def test_earliest_sliced_queryset(self):
+ msg = 'Cannot change a query once a slice has been taken.'
+ with self.assertRaisesMessage(TypeError, msg):
+ Article.objects.all()[0:5].earliest()
+
def test_latest(self):
# Because no Articles exist yet, latest() raises ArticleDoesNotExist.
with self.assertRaises(Article.DoesNotExist):
@@ -143,6 +148,11 @@ class EarliestOrLatestTests(TestCase):
Article.objects.model._meta.get_latest_by = ('pub_date', 'expire_date')
self.assertEqual(Article.objects.filter(pub_date=datetime(2005, 7, 27)).latest(), a3)
+ def test_latest_sliced_queryset(self):
+ msg = 'Cannot change a query once a slice has been taken.'
+ with self.assertRaisesMessage(TypeError, msg):
+ Article.objects.all()[0:5].latest()
+
def test_latest_manual(self):
# You can still use latest() with a model that doesn't have
# "get_latest_by" set -- just pass in the field name manually.
diff --git a/tests/lookup/tests.py b/tests/lookup/tests.py
index 548f470523..28ddc13aa0 100644
--- a/tests/lookup/tests.py
+++ b/tests/lookup/tests.py
@@ -244,6 +244,11 @@ class LookupTests(TestCase):
with self.assertRaisesMessage(ValueError, msg % field_name):
Model.objects.in_bulk(field_name=field_name)
+ def test_in_bulk_sliced_queryset(self):
+ msg = "Cannot use 'limit' or 'offset' with in_bulk()."
+ with self.assertRaisesMessage(TypeError, msg):
+ Article.objects.all()[0:5].in_bulk([self.a1.id, self.a2.id])
+
def test_values(self):
# values() returns a list of dictionaries instead of object instances --
# and you can specify which fields you want to retrieve.
diff --git a/tests/queries/tests.py b/tests/queries/tests.py
index 1cbe005fa8..b9b0a72686 100644
--- a/tests/queries/tests.py
+++ b/tests/queries/tests.py
@@ -700,7 +700,8 @@ class Queries1Tests(TestCase):
)
self.assertQuerysetEqual(q.reverse(), [])
q.query.low_mark = 1
- with self.assertRaisesMessage(AssertionError, 'Cannot change a query once a slice has been taken'):
+ msg = 'Cannot change a query once a slice has been taken.'
+ with self.assertRaisesMessage(TypeError, msg):
q.extra(select={'foo': "1"})
self.assertQuerysetEqual(q.defer('meal'), [])
self.assertQuerysetEqual(q.only('meal'), [])
@@ -2359,15 +2360,18 @@ class QuerySetSupportsPythonIdioms(TestCase):
)
def test_slicing_cannot_filter_queryset_once_sliced(self):
- with self.assertRaisesMessage(AssertionError, "Cannot filter a query once a slice has been taken."):
+ msg = 'Cannot filter a query once a slice has been taken.'
+ with self.assertRaisesMessage(TypeError, msg):
Article.objects.all()[0:5].filter(id=1)
def test_slicing_cannot_reorder_queryset_once_sliced(self):
- with self.assertRaisesMessage(AssertionError, "Cannot reorder a query once a slice has been taken."):
+ msg = 'Cannot reorder a query once a slice has been taken.'
+ with self.assertRaisesMessage(TypeError, msg):
Article.objects.all()[0:5].order_by('id')
def test_slicing_cannot_combine_queries_once_sliced(self):
- with self.assertRaisesMessage(AssertionError, "Cannot combine queries once a slice has been taken."):
+ msg = 'Cannot combine queries once a slice has been taken.'
+ with self.assertRaisesMessage(TypeError, msg):
Article.objects.all()[0:1] & Article.objects.all()[4:5]
def test_slicing_negative_indexing_not_supported_for_single_element(self):
@@ -2417,7 +2421,8 @@ class WeirdQuerysetSlicingTests(TestCase):
self.assertQuerysetEqual(Article.objects.all()[0:0], [])
self.assertQuerysetEqual(Article.objects.all()[0:0][:10], [])
self.assertEqual(Article.objects.all()[:0].count(), 0)
- with self.assertRaisesMessage(TypeError, 'Cannot reverse a query once a slice has been taken.'):
+ msg = 'Cannot change a query once a slice has been taken.'
+ with self.assertRaisesMessage(TypeError, msg):
Article.objects.all()[:0].latest('created')
def test_empty_resultset_sql(self):
diff --git a/tests/update/tests.py b/tests/update/tests.py
index 588db40de6..0ba6fe43c3 100644
--- a/tests/update/tests.py
+++ b/tests/update/tests.py
@@ -130,7 +130,7 @@ class AdvancedTests(TestCase):
"""
method = DataPoint.objects.all()[:2].update
msg = 'Cannot update a query once a slice has been taken.'
- with self.assertRaisesMessage(AssertionError, msg):
+ with self.assertRaisesMessage(TypeError, msg):
method(another_value='another thing')
def test_update_respects_to_field(self):