diff options
author | Hasan Ramezani <hasan.r67@gmail.com> | 2020-10-18 23:01:56 +0200 |
---|---|---|
committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2020-10-19 12:43:32 +0200 |
commit | 3b1746d5194778fab15c15dd14c212e0912ec8f3 (patch) | |
tree | 39d79227fc60923f55d3b7284ec6f943fae41284 /tests/delete | |
parent | 0eee5c1b9c2e306aa2c2807daf146ee88676bc97 (diff) | |
download | django-3b1746d5194778fab15c15dd14c212e0912ec8f3.tar.gz |
Fixed #32107 -- Fixed ProtectedError.protected_objects and RestrictedError.restricted_objects.
Regression in 4ca5c565f4dc9e97845036e86416abc5cfde766c and
ab3cbd8b9a315911248227208630a020cedca08f.
Thanks Vitaliy Yelnik for the report.
Diffstat (limited to 'tests/delete')
-rw-r--r-- | tests/delete/tests.py | 35 |
1 files changed, 23 insertions, 12 deletions
diff --git a/tests/delete/tests.py b/tests/delete/tests.py index 485ae1aaf5..3dce135dd5 100644 --- a/tests/delete/tests.py +++ b/tests/delete/tests.py @@ -75,19 +75,21 @@ class OnDeleteTests(TestCase): "Cannot delete some instances of model 'R' because they are " "referenced through protected foreign keys: 'A.protect'." ) - with self.assertRaisesMessage(ProtectedError, msg): + with self.assertRaisesMessage(ProtectedError, msg) as cm: a.protect.delete() + self.assertEqual(cm.exception.protected_objects, {a}) def test_protect_multiple(self): a = create_a('protect') - B.objects.create(protect=a.protect) + b = B.objects.create(protect=a.protect) msg = ( "Cannot delete some instances of model 'R' because they are " "referenced through protected foreign keys: 'A.protect', " "'B.protect'." ) - with self.assertRaisesMessage(ProtectedError, msg): + with self.assertRaisesMessage(ProtectedError, msg) as cm: a.protect.delete() + self.assertEqual(cm.exception.protected_objects, {a, b}) def test_protect_path(self): a = create_a('protect') @@ -97,8 +99,9 @@ class OnDeleteTests(TestCase): "Cannot delete some instances of model 'P' because they are " "referenced through protected foreign keys: 'R.p'." ) - with self.assertRaisesMessage(ProtectedError, msg): + with self.assertRaisesMessage(ProtectedError, msg) as cm: a.protect.p.delete() + self.assertEqual(cm.exception.protected_objects, {a}) def test_do_nothing(self): # Testing DO_NOTHING is a bit harder: It would raise IntegrityError for a normal model, @@ -176,19 +179,21 @@ class OnDeleteTests(TestCase): "Cannot delete some instances of model 'R' because they are " "referenced through restricted foreign keys: 'A.restrict'." ) - with self.assertRaisesMessage(RestrictedError, msg): + with self.assertRaisesMessage(RestrictedError, msg) as cm: a.restrict.delete() + self.assertEqual(cm.exception.restricted_objects, {a}) def test_restrict_multiple(self): a = create_a('restrict') - B3.objects.create(restrict=a.restrict) + b3 = B3.objects.create(restrict=a.restrict) msg = ( "Cannot delete some instances of model 'R' because they are " "referenced through restricted foreign keys: 'A.restrict', " "'B3.restrict'." ) - with self.assertRaisesMessage(RestrictedError, msg): + with self.assertRaisesMessage(RestrictedError, msg) as cm: a.restrict.delete() + self.assertEqual(cm.exception.restricted_objects, {a, b3}) def test_restrict_path_cascade_indirect(self): a = create_a('restrict') @@ -198,8 +203,9 @@ class OnDeleteTests(TestCase): "Cannot delete some instances of model 'P' because they are " "referenced through restricted foreign keys: 'A.restrict'." ) - with self.assertRaisesMessage(RestrictedError, msg): + with self.assertRaisesMessage(RestrictedError, msg) as cm: a.restrict.p.delete() + self.assertEqual(cm.exception.restricted_objects, {a}) # Object referenced also with CASCADE relationship can be deleted. a.cascade.p = a.restrict.p a.cascade.save() @@ -221,13 +227,14 @@ class OnDeleteTests(TestCase): delete_top = DeleteTop.objects.create() b1 = B1.objects.create(delete_top=delete_top) b2 = B2.objects.create(delete_top=delete_top) - DeleteBottom.objects.create(b1=b1, b2=b2) + delete_bottom = DeleteBottom.objects.create(b1=b1, b2=b2) msg = ( "Cannot delete some instances of model 'B1' because they are " "referenced through restricted foreign keys: 'DeleteBottom.b1'." ) - with self.assertRaisesMessage(RestrictedError, msg): + with self.assertRaisesMessage(RestrictedError, msg) as cm: b1.delete() + self.assertEqual(cm.exception.restricted_objects, {delete_bottom}) self.assertTrue(DeleteTop.objects.exists()) self.assertTrue(B1.objects.exists()) self.assertTrue(B2.objects.exists()) @@ -243,14 +250,18 @@ class OnDeleteTests(TestCase): delete_top = DeleteTop.objects.create() generic_b1 = GenericB1.objects.create(generic_delete_top=delete_top) generic_b2 = GenericB2.objects.create(generic_delete_top=delete_top) - GenericDeleteBottom.objects.create(generic_b1=generic_b1, generic_b2=generic_b2) + generic_delete_bottom = GenericDeleteBottom.objects.create( + generic_b1=generic_b1, + generic_b2=generic_b2, + ) msg = ( "Cannot delete some instances of model 'GenericB1' because they " "are referenced through restricted foreign keys: " "'GenericDeleteBottom.generic_b1'." ) - with self.assertRaisesMessage(RestrictedError, msg): + with self.assertRaisesMessage(RestrictedError, msg) as cm: generic_b1.delete() + self.assertEqual(cm.exception.restricted_objects, {generic_delete_bottom}) self.assertTrue(DeleteTop.objects.exists()) self.assertTrue(GenericB1.objects.exists()) self.assertTrue(GenericB2.objects.exists()) |