| 1939 | class 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 | |