From 39a884380226e3ae372cdc93b7227ad2c8e7b086 Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Fri, 23 Dec 2016 08:28:18 -0500 Subject: [PATCH] Used assertRaisesMessage() in m2m_through_regress tests. The "needs to have a value for field" messages are incorrect and reference nonexistent fields since the commit in which they were introduced (refs #18823). --- tests/m2m_through_regress/tests.py | 46 ++++++++++++++++++++++++------ 1 file changed, 37 insertions(+), 9 deletions(-) diff --git a/tests/m2m_through_regress/tests.py b/tests/m2m_through_regress/tests.py index 9464c50b93..3ebae1b3d3 100644 --- a/tests/m2m_through_regress/tests.py +++ b/tests/m2m_through_regress/tests.py @@ -67,11 +67,21 @@ class M2MThroughTestCase(TestCase): self.roll.members.set([]) def test_cannot_use_create_on_m2m_with_intermediary_model(self): - with self.assertRaises(AttributeError): + msg = ( + "Cannot use create() on a ManyToManyField which specifies an " + "intermediary model. Use m2m_through_regress.Membership's " + "Manager instead." + ) + with self.assertRaisesMessage(AttributeError, msg): self.rock.members.create(name="Anne") def test_cannot_use_create_on_reverse_m2m_with_intermediary_model(self): - with self.assertRaises(AttributeError): + msg = ( + "Cannot use create() on a ManyToManyField which specifies an " + "intermediary model. Use m2m_through_regress.Membership's " + "Manager instead." + ) + with self.assertRaisesMessage(AttributeError, msg): self.bob.group_set.create(name="Funk") def test_retrieve_reverse_m2m_items_via_custom_id_intermediary(self): @@ -203,14 +213,27 @@ class ToFieldThroughTests(TestCase): ["", ""] ) - def test_add_null(self): - nullcar = Car.objects.create(make=None) - with self.assertRaises(ValueError): - nullcar.drivers._add_items('car', 'driver', self.unused_driver) + def test_m2m_relations_unsable_on_null_to_field(self): + nullcar = Car(make=None) + msg = ( + '"" needs to have a value for field "car" before this ' + 'many-to-many relationship can be used.' + ) + with self.assertRaisesMessage(ValueError, msg): + nullcar.drivers.all() + + def test_m2m_relations_unsable_on_null_pk_obj(self): + msg = ( + "'Car' instance needs to have a primary key value before a " + "many-to-many relationship can be used." + ) + with self.assertRaisesMessage(ValueError, msg): + Car(make='Ford').drivers.all() def test_add_related_null(self): nulldriver = Driver.objects.create(name=None) - with self.assertRaises(ValueError): + msg = 'Cannot add "": the value for field "driver" is None' + with self.assertRaisesMessage(ValueError, msg): self.car.drivers._add_items('car', 'driver', nulldriver) def test_add_reverse(self): @@ -228,12 +251,17 @@ class ToFieldThroughTests(TestCase): def test_add_null_reverse(self): nullcar = Car.objects.create(make=None) - with self.assertRaises(ValueError): + msg = 'Cannot add "": the value for field "car" is None' + with self.assertRaisesMessage(ValueError, msg): self.driver.car_set._add_items('driver', 'car', nullcar) def test_add_null_reverse_related(self): nulldriver = Driver.objects.create(name=None) - with self.assertRaises(ValueError): + msg = ( + '"" needs to have a value for field "driver" before ' + 'this many-to-many relationship can be used.' + ) + with self.assertRaisesMessage(ValueError, msg): nulldriver.car_set._add_items('driver', 'car', self.car) def test_remove(self):