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).
This commit is contained in:
Tim Graham 2016-12-23 08:28:18 -05:00
parent bcae045de0
commit 39a8843802
1 changed files with 37 additions and 9 deletions

View File

@ -67,11 +67,21 @@ class M2MThroughTestCase(TestCase):
self.roll.members.set([]) self.roll.members.set([])
def test_cannot_use_create_on_m2m_with_intermediary_model(self): 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") self.rock.members.create(name="Anne")
def test_cannot_use_create_on_reverse_m2m_with_intermediary_model(self): 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") self.bob.group_set.create(name="Funk")
def test_retrieve_reverse_m2m_items_via_custom_id_intermediary(self): def test_retrieve_reverse_m2m_items_via_custom_id_intermediary(self):
@ -203,14 +213,27 @@ class ToFieldThroughTests(TestCase):
["<Driver: Barney Gumble>", "<Driver: Ryan Briscoe>"] ["<Driver: Barney Gumble>", "<Driver: Ryan Briscoe>"]
) )
def test_add_null(self): def test_m2m_relations_unsable_on_null_to_field(self):
nullcar = Car.objects.create(make=None) nullcar = Car(make=None)
with self.assertRaises(ValueError): msg = (
nullcar.drivers._add_items('car', 'driver', self.unused_driver) '"<Car: None>" 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): def test_add_related_null(self):
nulldriver = Driver.objects.create(name=None) nulldriver = Driver.objects.create(name=None)
with self.assertRaises(ValueError): msg = 'Cannot add "<Driver: None>": the value for field "driver" is None'
with self.assertRaisesMessage(ValueError, msg):
self.car.drivers._add_items('car', 'driver', nulldriver) self.car.drivers._add_items('car', 'driver', nulldriver)
def test_add_reverse(self): def test_add_reverse(self):
@ -228,12 +251,17 @@ class ToFieldThroughTests(TestCase):
def test_add_null_reverse(self): def test_add_null_reverse(self):
nullcar = Car.objects.create(make=None) nullcar = Car.objects.create(make=None)
with self.assertRaises(ValueError): msg = 'Cannot add "<Car: None>": the value for field "car" is None'
with self.assertRaisesMessage(ValueError, msg):
self.driver.car_set._add_items('driver', 'car', nullcar) self.driver.car_set._add_items('driver', 'car', nullcar)
def test_add_null_reverse_related(self): def test_add_null_reverse_related(self):
nulldriver = Driver.objects.create(name=None) nulldriver = Driver.objects.create(name=None)
with self.assertRaises(ValueError): msg = (
'"<Driver: None>" 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) nulldriver.car_set._add_items('driver', 'car', self.car)
def test_remove(self): def test_remove(self):