the question is pretty straightforward: I can't use self.assertRaises in a Django 1.5 unit test.
I'd really like to verify that when you try to delete a provider from a database who doesn't exist, a LookupError is raised.
As you can see, I have a ProviderDAO class, where I define how providers are to be deleted. Then I simply call this method, delete_provider(), in the unit test class ProviderManagerTests. I've outlined all the possibilities in the code, and at this point I've come here for answers (I've read the suggestions in related questions but nothing is doing the trick) I'm using python 2.7.5 Thanks!
class ProviderManager(models.Manager):
def delete_provider(self, provider_id):
matching_providers = Provider.objects.filter(providerId=provider_id)
print 'delete provider method matching providers %s ' % matching_providers
print 'delete provider method matching providers %s ' % matching_providers.count()
if matching_providers.count() > 0:
matching_providers.delete()
elif matching_providers.count() == 0:
raise LookupError('No deletion; no providers matching this id')
class ProviderManagerTests(TestCase):
def test_delete_nonexistent_provider(self):
provider_manager = ProviderManager()
someRoles = Role(name='boss')
someRoles.save()
someSpecialties = Specialty(name='senior care')
someSpecialties.save()
provider_manager.delete_provider('398236503462063')
#self.assertIsNone(provider,'provider should be none')
#self.assertTrue(Provider.objects.filter(providerId='123456') == 0)
###self.assertRaises(LookupError,provider_manager.delete_provider, providerId='398236503462063')
#self.assertRaises(LookupError,provider_manager.delete_provider, provider_id='398236503462063')
#self.assertRaises(LookupError,lambda: provider_manager.delete_provider,provider_id='234234234')
#self.assertRaises(LookupError,lambda: provider_manager.delete_provider(provider_id='234234234'))
# with self.assertRaises(LookupError):
# provider_manager.delete_provider(provider_id='398236503462063')
with self.assertRaises(LookupError):
provider_manager.delete_provider('398236503462063')
and the stack track:
======================================================================
ERROR: test_delete_nonexistent_provider (edu.tests.ProviderTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/tests.py", line 68, in test_delete_nonexistent_provider
provider_manager.delete_provider('398236503462063')
File "/Users/models.py", line 92, in delete_provider
raise LookupError('No deletion; no providers matching this id')
LookupError: No deletion; no providers matching this id
----------------------------------------------------------------------
Ran 14 tests in 0.306s
FAILED (errors=1)
By the way, you could write this way:
self.assertRaises(LookupError, provider_manager.delete_provider, '398236503462063')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With