fromsouth.testsimportMonkeypatcherfromsouth.modelsinspectorimport*fromfakeapp.modelsimportHorribleModel,get_sentinel_objectfromdjango.utils.functionalimportwrapson_delete_is_available=hasattr(models,"PROTECT")# models here is django.db.modelstry:# skipUnless added in Python 2.7;fromunittestimportskipUnlessskipUnlessOnDeleteAvailable=skipUnless(on_delete_is_available,"not testing on_delete -- not available on Django<1.3")exceptImportError:try:# django.utils.unittest added in Django 1.3;fromdjango.utils.unittestimportskipUnlessskipUnlessOnDeleteAvailable=skipUnless(on_delete_is_available,"not testing on_delete -- not available on Django<1.3")exceptImportError:defskipUnlessOnDeleteAvailable(testfunc):@wraps(testfunc)defwrapper(self):ifon_delete_is_available:# Apply methodgetattr(self,testfunc.__name__)()else:# The skip exceptions are not available either...print"Skipping",testfunc.__name__,"--","not testing on_delete -- not available on Django<1.3"returnwrapperclassTestModelInspector(Monkeypatcher):""" Tests if the various parts of the modelinspector work. """deftest_get_value(self):# Let's start nicely.name=HorribleModel._meta.get_field_by_name("name")[0]slug=HorribleModel._meta.get_field_by_name("slug")[0]user=HorribleModel._meta.get_field_by_name("user")[0]# Simple int retrievalself.assertEqual(get_value(name,["max_length",{}]),"255",)# Bool retrievalself.assertEqual(get_value(slug,["unique",{}]),"True",)# String retrievalself.assertEqual(get_value(user,["rel.related_name",{}]),"'horribles'",)# Default triggeringself.assertEqual(get_value(slug,["unique",{"default":False}]),"True",)self.assertRaises(IsDefault,get_value,slug,["unique",{"default":True}],)@skipUnlessOnDeleteAvailabledeftest_get_value_on_delete(self):# First validate the FK fields with on_delete optionso_set_null_on_delete=HorribleModel._meta.get_field_by_name("o_set_null_on_delete")[0]o_cascade_delete=HorribleModel._meta.get_field_by_name("o_cascade_delete")[0]o_protect=HorribleModel._meta.get_field_by_name("o_protect")[0]o_default_on_delete=HorribleModel._meta.get_field_by_name("o_default_on_delete")[0]o_set_on_delete_function=HorribleModel._meta.get_field_by_name("o_set_on_delete_function")[0]o_set_on_delete_value=HorribleModel._meta.get_field_by_name("o_set_on_delete_value")[0]o_no_action_on_delete=HorribleModel._meta.get_field_by_name("o_no_action_on_delete")[0]# TODO this is repeated from the introspection_details in modelsinspector:# better to refactor that so we can reference these settings, in case they# must change at some point.on_delete=["rel.on_delete",{"default":models.CASCADE,"is_django_function":True,"converter":convert_on_delete_handler,}]# Foreign Key cascade update/deleteself.assertRaises(IsDefault,get_value,o_cascade_delete,on_delete,)self.assertEqual(get_value(o_protect,on_delete),"models.PROTECT",)self.assertEqual(get_value(o_no_action_on_delete,on_delete),"models.DO_NOTHING",)self.assertEqual(get_value(o_set_null_on_delete,on_delete),"models.SET_NULL",)self.assertEqual(get_value(o_default_on_delete,on_delete),"models.SET_DEFAULT",)# For now o_set_on_delete raises, see modelsinspector.py#self.assertEqual(# get_value(o_set_on_delete_function, on_delete),# "models.SET(get_sentinel_object)",#)self.assertRaises(ValueError,get_value,o_set_on_delete_function,on_delete,)self.assertEqual(get_value(o_set_on_delete_value,on_delete),"models.SET(%s)"%value_clean(get_sentinel_object()),)