Ticket #11830: save_model_validation_error_tests_only.2.diff

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

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

  • new file tests/regressiontests/admin_views/fixtures/admin-views-save-model.xml

    diff -r 6f697e5f2375 tests/regressiontests/admin_views/fixtures/admin-views-save-model.xml
    - +  
     1<?xml version="1.0" encoding="utf-8"?>
     2<django-objects version="1.0">
     3    <object pk="100" model="auth.user">
     4        <field type="CharField" name="username">super</field>
     5        <field type="CharField" name="first_name">Super</field>
     6        <field type="CharField" name="last_name">User</field>
     7        <field type="CharField" name="email">super@example.com</field>
     8        <field type="CharField" name="password">sha1$995a3$6011485ea3834267d719b4c801409b8b1ddd0158</field>
     9        <field type="BooleanField" name="is_staff">True</field>
     10        <field type="BooleanField" name="is_active">True</field>
     11        <field type="BooleanField" name="is_superuser">True</field>
     12        <field type="DateTimeField" name="last_login">2007-05-30 13:20:10</field>
     13        <field type="DateTimeField" name="date_joined">2007-05-30 13:20:10</field>
     14        <field to="auth.group" name="groups" rel="ManyToManyRel"></field>
     15        <field to="auth.permission" name="user_permissions" rel="ManyToManyRel"></field>
     16    </object>
     17    <object pk="1" model="admin_views.organization">
     18        <field type="CharField" name="name">Django foundation</field>
     19    </object>
     20    <object pk="2" model="admin_views.organization">
     21        <field type="CharField" name="name">Python foundation</field>
     22    </object>
     23    <object pk="3" model="admin_views.organization">
     24        <field type="CharField" name="name">Zope foundation</field>
     25    </object>
     26    <object pk="4" model="admin_views.organization">
     27        <field type="CharField" name="name">Parrot foundation</field>
     28    </object>
     29    <object pk="1" model="admin_views.dilbert">
     30        <field type="CharField" name="name">Dilbert</field>
     31    </object>
     32    <object pk="1" model="admin_views.role">
     33        <field type="CharField" name="name">Programmer</field>
     34        <field to="admin_views.organization" name="organization" rel="ManyToOneRel">1</field>
     35        <field to="admin_views.dilbert" name="dilbert" rel="ManyToOneRel">1</field>
     36    </object>
     37    <object pk="2" model="admin_views.role">
     38        <field type="CharField" name="name">Coffee drinker</field>
     39        <field to="admin_views.organization" name="organization" rel="ManyToOneRel">1</field>
     40        <field to="admin_views.dilbert" name="dilbert" rel="ManyToOneRel">1</field>
     41    </object>
     42    <object pk="3" model="admin_views.role">
     43        <field type="CharField" name="name">Programmer</field>
     44        <field to="admin_views.organization" name="organization" rel="ManyToOneRel">2</field>
     45        <field to="admin_views.dilbert" name="dilbert" rel="ManyToOneRel">1</field>
     46    </object>
     47    <object pk="4" model="admin_views.role">
     48        <field type="CharField" name="name">Coffee drinker</field>
     49        <field to="admin_views.organization" name="organization" rel="ManyToOneRel">2</field>
     50        <field to="admin_views.dilbert" name="dilbert" rel="ManyToOneRel">1</field>
     51    </object>
     52    <object pk="5" model="admin_views.role">
     53        <field type="CharField" name="name">Coffee drinker</field>
     54        <field to="admin_views.organization" name="organization" rel="ManyToOneRel">3</field>
     55        <field to="admin_views.dilbert" name="dilbert" rel="ManyToOneRel">1</field>
     56    </object>
     57    <object pk="6" model="admin_views.role">
     58        <field type="CharField" name="name">Programmer</field>
     59        <field to="admin_views.organization" name="organization" rel="ManyToOneRel">3</field>
     60        <field to="admin_views.dilbert" name="dilbert" rel="ManyToOneRel">1</field>
     61    </object>
     62    <object pk="7" model="admin_views.role">
     63        <field type="CharField" name="name">Coffee drinker</field>
     64        <field to="admin_views.organization" name="organization" rel="ManyToOneRel">4</field>
     65        <field to="admin_views.dilbert" name="dilbert" rel="ManyToOneRel">1</field>
     66    </object>
     67    <object pk="8" model="admin_views.role">
     68        <field type="CharField" name="name">Programmer</field>
     69        <field to="admin_views.organization" name="organization" rel="ManyToOneRel">4</field>
     70        <field to="admin_views.dilbert" name="dilbert" rel="ManyToOneRel">1</field>
     71    </object>
     72</django-objects>
  • 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