Re: testing unique_together IntegrityError

2012-11-10 Thread Mike Dewhirst

On 11/11/2012 4:45pm, Mike Dewhirst wrote:

Can anyone help with some unit test guidance please?

How can I trap an IntegrityError in a test to prove unique_together is
working?

I'm modelling a company and multiple divisions. A solo division doesn't
need a division.name so I'm allowing it to be blank. It's __unicode__()
method returns self.name or self.company.name

All fine so far. But I insist two divisions cannot have the same name.

In Division's Meta class we have unique_together = (company, name) and
this works as advertised

In a unit test I don't seem to be able to swallow the IntegrityError!!


E..
=
...
IntegrityError: duplicate key value violates unique constraint
"company_division_company_id_name_key"
DETAIL:  Key (company_id, name)=(7, ) already exists.

Here is the unit test ...

 def test_twodivswithnoname(self):
 tstc = Company.objects.get(name='Company Inc')
 tstdiv, c = Division.objects.get_or_create(company=tstc)
 tstdiv.name = ''
 tstdiv.save()
 expected = ''
 result = tstdiv.name
 # prove nothing up my sleeve
 self.assertEqual(result, expected)
 # if name is None, __unicode__() returns the owning company name
 expected = 'Company Inc'
 result = '%s' % tstdiv
 self.assertEqual(result, expected)


This didn't swallow it either ...

try:
tstdivb = Division.objects.create(company=tstc, name='')
except IntegrityError as e:
pass



 tstdivb = Division.objects.create(company=tstc)
 tstdivb.name = ''
 try:
 # company and name are unique together
 tstdivb.save()
 except IntegrityError as e:
 pass
 tstdivb.delete()

I've also tried self.assertRaises(IntegrityError) instead of the
try/except block. Same result.

Thanks

Mike



--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



testing unique_together IntegrityError

2012-11-10 Thread Mike Dewhirst

Can anyone help with some unit test guidance please?

How can I trap an IntegrityError in a test to prove unique_together is 
working?


I'm modelling a company and multiple divisions. A solo division doesn't 
need a division.name so I'm allowing it to be blank. It's __unicode__() 
method returns self.name or self.company.name


All fine so far. But I insist two divisions cannot have the same name.

In Division's Meta class we have unique_together = (company, name) and 
this works as advertised


In a unit test I don't seem to be able to swallow the IntegrityError!!


E..
=
...
IntegrityError: duplicate key value violates unique constraint 
"company_division_company_id_name_key"

DETAIL:  Key (company_id, name)=(7, ) already exists.

Here is the unit test ...

def test_twodivswithnoname(self):
tstc = Company.objects.get(name='Company Inc')
tstdiv, c = Division.objects.get_or_create(company=tstc)
tstdiv.name = ''
tstdiv.save()
expected = ''
result = tstdiv.name
# prove nothing up my sleeve
self.assertEqual(result, expected)
# if name is None, __unicode__() returns the owning company name
expected = 'Company Inc'
result = '%s' % tstdiv
self.assertEqual(result, expected)
tstdivb = Division.objects.create(company=tstc)
tstdivb.name = ''
try:
# company and name are unique together
tstdivb.save()
except IntegrityError as e:
pass
tstdivb.delete()

I've also tried self.assertRaises(IntegrityError) instead of the 
try/except block. Same result.


Thanks

Mike

--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.