Ticket #11830: save_model_validation_error_tests_only.diff

File save_model_validation_error_tests_only.diff, 7.2 KB (added by Tobias McNulty, 15 years ago)

updated patch (with tests only) to apply to current trunk

  • tests/regressiontests/admin_views/models.py

    diff -r 6f697e5f2375 tests/regressiontests/admin_views/models.py
    a b  
    565565    def __unicode__(self):
    566566        return self.name
    567567
     568
     569class Organization(models.Model):
     570    """
     571    Test the save method
     572    """
     573   
     574    name = models.CharField(max_length=255)
     575    dilberts = models.ManyToManyField(
     576        'Dilbert', through='Role', blank=True
     577    )
     578
     579    def save(self, force_insert=False, force_update=False):
     580        if self.pk == 1: # For test_model_save_method
     581            self.dilberts.clear()
     582        super(Organization, self).save(force_insert, force_update)
     583
     584
     585class Role(models.Model):
     586    name = models.CharField(max_length=255)
     587    organization = models.ForeignKey('Organization')
     588    dilbert = models.ForeignKey('Dilbert')
     589       
     590
     591class Dilbert(models.Model):
     592    name = models.CharField(max_length=255)
     593
     594
     595class RoleInline(admin.TabularInline):
     596    model = Role
     597
     598
     599class OrganizationAdmin(admin.ModelAdmin):
     600    """
     601    Test the save_model method.
     602    """
     603    inlines = (RoleInline,)
     604
     605    def save_model(self, request, obj, form, change):
     606        obj.save()
     607        if obj.pk == 2: # For test_admin_save_model_method
     608            role = Role.objects.get(pk=3)
     609            role.delete()
     610        if obj.pk == 4: # For test_change_data
     611            role = Role.objects.get(pk=8)
     612            role.name = 'Water drinker'
     613
    568614admin.site.register(Article, ArticleAdmin)
    569615admin.site.register(CustomArticle, CustomArticleAdmin)
    570616admin.site.register(Section, save_as=True, inlines=[ArticleInline])
     
    588634admin.site.register(Recommender)
    589635admin.site.register(Collector, CollectorAdmin)
    590636admin.site.register(Category, CategoryAdmin)
     637admin.site.register(Organization, OrganizationAdmin)
    591638admin.site.register(Post, PostAdmin)
    592639admin.site.register(Gadget, GadgetAdmin)
    593640admin.site.register(Villain)
  • tests/regressiontests/admin_views/tests.py

    diff -r 6f697e5f2375 tests/regressiontests/admin_views/tests.py
    a b  
    2323    ExternalSubscriber, FooAccount, Gallery, ModelWithStringPrimaryKey, \
    2424    Person, Persona, Picture, Podcast, Section, Subscriber, Vodcast, \
    2525    Language, Collector, Widget, Grommet, DooHickey, FancyDoodad, Whatsit, \
    26     Category, Post, Plot, FunkyTag
     26    Category, Organization, Role, Post, Plot, FunkyTag
    2727
    2828
    2929class AdminViewBasicTest(TestCase):
     
    19361936        self.failUnlessEqual(get_max_age(response), None)
    19371937
    19381938
     1939class SaveModelTest(TestCase):
     1940    fixtures = ['admin-views-save-model.xml']
     1941
     1942    def setUp(self):
     1943        self.client.login(username='super', password='secret')
     1944        self.post_data = {
     1945            "name": u"Django foundation",
     1946            # inline data
     1947            "role_set-TOTAL_FORMS": "6",
     1948            "role_set-INITIAL_FORMS": "2",
     1949            "role_set-MAX_NUM_FORMS": "0",
     1950            "role_set-0-id": "1",
     1951            "role_set-0-name": u"Programmer",
     1952            "role_set-0-organization": "1",
     1953            "role_set-0-dilbert": "1",
     1954            "role_set-1-id": "2",
     1955            "role_set-1-name": u"Coffee drinker",
     1956            "role_set-1-organization": "1",
     1957            "role_set-1-dilbert": "1",
     1958            "role_set-2-id": "",
     1959            "role_set-2-name": "",
     1960            "role_set-2-organization": "",
     1961            "role_set-2-dilbert": "",
     1962            "role_set-3-id": "",
     1963            "role_set-3-name": "",
     1964            "role_set-3-organization": "",
     1965            "role_set-3-dilbert": "",
     1966            "role_set-4-id": "",
     1967            "role_set-4-name": "",
     1968            "role_set-4-organization": "",
     1969            "role_set-4-dilbert": "",
     1970            "role_set-5-id": "",
     1971            "role_set-5-name": "",
     1972            "role_set-5-organization": "",
     1973            "role_set-5-dilbert": "",
     1974        }
     1975
     1976    def tearDown(self):
     1977        self.client.logout()
     1978
     1979    def test_model_save_method(self):
     1980        """
     1981        Test removing of related objects in the save method on the model.
     1982        """
     1983        org = Organization.objects.get(pk=1)
     1984        self.assertEqual(org.dilberts.count(), 2)
     1985       
     1986        response = self.client.post(
     1987            '/test_admin/admin/admin_views/organization/1/', self.post_data
     1988        )
     1989
     1990        self.assertEqual(org.dilberts.count(), 0)
     1991        self.assertEqual(response.status_code, 302) # redirect somewhere
     1992
     1993    def test_admin_save_model_method(self):
     1994        """
     1995        Test removing of related objects in the model_save method
     1996        in the ModelAdmin.
     1997        """
     1998        self.post_data['role_set-0-id'] = '3'
     1999        self.post_data['role_set-0-organization'] = '2'
     2000        self.post_data['role_set-1-id'] = '4'
     2001        self.post_data['role_set-1-organization'] = '2'
     2002        self.post_data['role_set-1-DELETE'] = 'on'
     2003
     2004        org = Organization.objects.get(pk=2)
     2005        self.assertEqual(org.dilberts.count(), 2)
     2006
     2007        response = self.client.post(
     2008            '/test_admin/admin/admin_views/organization/2/', self.post_data
     2009        )
     2010
     2011        self.assertEqual(org.dilberts.count(), 0)
     2012        self.assertEqual(response.status_code, 302) # redirect somewhere
     2013
     2014    def test_invalid_related_field_choice(self):
     2015        """
     2016        A test to ensure that the model is not changed when an
     2017        invalid role_set-x-id is posted.
     2018        """
     2019        self.post_data['role_set-0-id'] = '100' #Should not exist
     2020        self.post_data['role_set-0-organization'] = '3'
     2021        self.post_data['role_set-1-id'] = '6'
     2022        self.post_data['role_set-1-organization'] = '3'
     2023        self.post_data['role_set-1-DELETE'] = 'on'
     2024
     2025        org = Organization.objects.get(pk=3)
     2026        self.assertEqual(org.dilberts.count(), 2)
     2027
     2028        response = self.client.post(
     2029            '/test_admin/admin/admin_views/organization/3/', self.post_data
     2030        )
     2031
     2032        self.assertEqual(org.dilberts.count(), 2)
     2033        self.assertEqual(response.status_code, 200)
     2034
     2035    def test_change_data(self):
     2036        """
     2037        A test to ensure that the model is not changed when an
     2038        invalid role_set-x-id is posted.
     2039        """
     2040        self.post_data['role_set-0-id'] = '7' #Should not exist
     2041        self.post_data['role_set-0-organization'] = '4'
     2042        self.post_data['role_set-1-id'] = '8'
     2043        self.post_data['role_set-1-name'] = 'Tea drinker'
     2044        self.post_data['role_set-1-organization'] = '4'
     2045
     2046
     2047        org = Organization.objects.get(pk=4)
     2048        self.assertEqual(org.dilberts.count(), 2)
     2049
     2050        response = self.client.post(
     2051            '/test_admin/admin/admin_views/organization/4/', self.post_data
     2052        )
     2053
     2054        role = Role.objects.get(pk=8)
     2055        # The form is the master
     2056        self.assertEqual(role.name, 'Tea drinker')
     2057
     2058        # save_model or save is the master
     2059        #self.assertEqual(role.name, 'Water drinker')
     2060
     2061        self.assertEqual(org.dilberts.count(), 2)
     2062        self.assertEqual(response.status_code, 302) # redirect somewhere
     2063
     2064
    19392065class ReadonlyTest(TestCase):
    19402066    fixtures = ['admin-views-users.xml']
    19412067
Back to Top