According to a previous (http://groups.google.com/group/django-users/ browse_thread/thread/64a9558d5c02e39/c0c28daed410898d?lnk=gst&q=catch +save+exception#c0c28daed410898d) post it's bad practice to wrap a model's .save() in a try/except. Adrian suggests using manipulators instead.
I'm writing a test case for a very basic Tag model. I've flagged the .name field as unique and want to test this in my test case. Here's the model: from django.db import models from django.db import IntegrityError import re class Tag(models.Model): date_added = models.DateTimeField(auto_now_add=True) name = models.CharField(maxlength=100, unique=True, primary_key=True) condensed = models.CharField(maxlength=100, unique=True, blank=True) def condense_name(self, name): condensing = name # remove all punctuation and non-word characters condensing = re.sub(r'[\W ]', "_", condensing) # remove multiple underscores condensing = re.sub(r'__+', '_', condensing) # trim the beginning and end of the string of underscores condensing = re.sub(r'(^_)|(_$)', '', condensing) return condensing def save(self): self.condensed = self.condense_name(self.name) try: exists = Tag.objects.get(condensed__exact=self.condensed) except: raise IntegrityError('Condensed name is not unique, already saved to the database.') super(Tag, self).save() def __str__(self): return self.name Here's the test case: from django.test import TestCase from django.db import IntegrityError from tags.models import Tag class TestTagModel(TestCase): def test_init(self): self.assert_(Tag()) def test_condensed(self): data = [ {'data':'spaced tag', 'expected':'spaced_tag'}, {'data':'&tag, with punctuation!', 'expected':'tag_with_punctuation'}, {'data':u'tag wîth aççénts', 'expected':'tag_w_th_a_nts'}, ] for item in data: toTest = Tag(name=item["data"]) self.assertEqual(toTest.condense_name(toTest.name), item["expected"]) def test_unique(self): unique_data = [ 'test 1', 'test 2', 'test 3', ] duplicate_data = [ 'test_1', 'test 2', 'test_3', ] for item in unique_data: toTest = Tag(name=item) toTest.save() self.assert_(toTest) for item in duplicate_data: toTest = Tag(name=item) try: try: toTest.save() except IntegrityError, e: self.assert_(True) finally: self.assertEquals(len(unique_data), len(Tag.objects.all())) The test case failes by raising an IntegrityError even though I'm trapping it and asserting success if this Exception is thrown. I had re-written this sequence a few times with the same results. 1) Why can't I trap the IntegrityError in my test case? 2) What is the best practice for writing test cases to validate that only unique data is being saved to the database? Bonus question: 1) Does self.failUnlessRaises() work with Python 2.4? Replacing the bottom section of the test case with self.failUnlessRaises() does not work on my Ubuntu 6.06 server w/Python 2.4 and the latest Django SVN release. Thank you very much in advance for _any_ light you can shed on this. Mark --~--~---------~--~----~------------~-------~--~----~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---