Converted the modeladmin tests to run on python 2 and 3.

This commit is contained in:
Alex Gaynor 2012-08-15 01:46:31 -07:00 committed by Alex Gaynor
parent 54afdbf378
commit cca01c96d1
1 changed files with 29 additions and 29 deletions

View File

@ -5,7 +5,7 @@ from datetime import date
from django import forms from django import forms
from django.conf import settings from django.conf import settings
from django.contrib.admin.options import (ModelAdmin, TabularInline, from django.contrib.admin.options import (ModelAdmin, TabularInline,
InlineModelAdmin, HORIZONTAL, VERTICAL) HORIZONTAL, VERTICAL)
from django.contrib.admin.sites import AdminSite from django.contrib.admin.sites import AdminSite
from django.contrib.admin.validation import validate from django.contrib.admin.validation import validate
from django.contrib.admin.widgets import AdminDateWidget, AdminRadioSelect from django.contrib.admin.widgets import AdminDateWidget, AdminRadioSelect
@ -15,7 +15,7 @@ from django.core.exceptions import ImproperlyConfigured
from django.forms.models import BaseModelFormSet from django.forms.models import BaseModelFormSet
from django.forms.widgets import Select from django.forms.widgets import Select
from django.test import TestCase from django.test import TestCase
from django.test.utils import override_settings, str_prefix from django.test.utils import str_prefix
from django.utils import unittest from django.utils import unittest
from .models import Band, Concert, ValidationTestModel, ValidationTestInlineModel from .models import Band, Concert, ValidationTestModel, ValidationTestInlineModel
@ -24,6 +24,7 @@ from .models import Band, Concert, ValidationTestModel, ValidationTestInlineMode
class MockRequest(object): class MockRequest(object):
pass pass
class MockSuperUser(object): class MockSuperUser(object):
def has_perm(self, perm): def has_perm(self, perm):
return True return True
@ -47,7 +48,7 @@ class ModelAdminTests(TestCase):
def test_default_fields(self): def test_default_fields(self):
ma = ModelAdmin(Band, self.site) ma = ModelAdmin(Band, self.site)
self.assertEqual(ma.get_form(request).base_fields.keys(), self.assertEqual(list(ma.get_form(request).base_fields),
['name', 'bio', 'sign_date']) ['name', 'bio', 'sign_date'])
def test_default_fieldsets(self): def test_default_fieldsets(self):
@ -72,7 +73,7 @@ class ModelAdminTests(TestCase):
ma = BandAdmin(Band, self.site) ma = BandAdmin(Band, self.site)
self.assertEqual( ma.get_fieldsets(request), self.assertEqual(ma.get_fieldsets(request),
[(None, {'fields': ['name']})]) [(None, {'fields': ['name']})])
self.assertEqual(ma.get_fieldsets(request, self.band), self.assertEqual(ma.get_fieldsets(request, self.band),
@ -90,8 +91,8 @@ class ModelAdminTests(TestCase):
fields = ['name'] fields = ['name']
ma = BandAdmin(Band, self.site) ma = BandAdmin(Band, self.site)
self.assertEqual(ma.get_form(request).base_fields.keys(), ['name']) self.assertEqual(list(ma.get_form(request).base_fields), ['name'])
self.assertEqual(ma.get_form(request, self.band).base_fields.keys(), self.assertEqual(list(ma.get_form(request, self.band).base_fields),
['name']) ['name'])
# Using `fieldsets`. # Using `fieldsets`.
@ -99,8 +100,8 @@ class ModelAdminTests(TestCase):
fieldsets = [(None, {'fields': ['name']})] fieldsets = [(None, {'fields': ['name']})]
ma = BandAdmin(Band, self.site) ma = BandAdmin(Band, self.site)
self.assertEqual(ma.get_form(request).base_fields.keys(), ['name']) self.assertEqual(list(ma.get_form(request).base_fields), ['name'])
self.assertEqual(ma.get_form(request, self.band).base_fields.keys(), self.assertEqual(list(ma.get_form(request, self.band).base_fields),
['name']) ['name'])
# Using `exclude`. # Using `exclude`.
@ -108,7 +109,7 @@ class ModelAdminTests(TestCase):
exclude = ['bio'] exclude = ['bio']
ma = BandAdmin(Band, self.site) ma = BandAdmin(Band, self.site)
self.assertEqual(ma.get_form(request).base_fields.keys(), self.assertEqual(list(ma.get_form(request).base_fields),
['name', 'sign_date']) ['name', 'sign_date'])
# You can also pass a tuple to `exclude`. # You can also pass a tuple to `exclude`.
@ -116,7 +117,7 @@ class ModelAdminTests(TestCase):
exclude = ('bio',) exclude = ('bio',)
ma = BandAdmin(Band, self.site) ma = BandAdmin(Band, self.site)
self.assertEqual(ma.get_form(request).base_fields.keys(), self.assertEqual(list(ma.get_form(request).base_fields),
['name', 'sign_date']) ['name', 'sign_date'])
# Using `fields` and `exclude`. # Using `fields` and `exclude`.
@ -125,7 +126,7 @@ class ModelAdminTests(TestCase):
exclude = ['bio'] exclude = ['bio']
ma = BandAdmin(Band, self.site) ma = BandAdmin(Band, self.site)
self.assertEqual(ma.get_form(request).base_fields.keys(), self.assertEqual(list(ma.get_form(request).base_fields),
['name']) ['name'])
def test_custom_form_meta_exclude_with_readonly(self): def test_custom_form_meta_exclude_with_readonly(self):
@ -148,8 +149,8 @@ class ModelAdminTests(TestCase):
form = AdminBandForm form = AdminBandForm
ma = BandAdmin(Band, self.site) ma = BandAdmin(Band, self.site)
self.assertEqual(ma.get_form(request).base_fields.keys(), self.assertEqual(list(ma.get_form(request).base_fields),
['sign_date',]) ['sign_date'])
# Then, with `InlineModelAdmin` ----------------- # Then, with `InlineModelAdmin` -----------------
@ -172,8 +173,8 @@ class ModelAdminTests(TestCase):
ma = BandAdmin(Band, self.site) ma = BandAdmin(Band, self.site)
self.assertEqual( self.assertEqual(
list(ma.get_formsets(request))[0]().forms[0].fields.keys(), list(list(ma.get_formsets(request))[0]().forms[0].fields),
['main_band', 'opening_band', 'id', 'DELETE',]) ['main_band', 'opening_band', 'id', 'DELETE'])
def test_custom_form_meta_exclude(self): def test_custom_form_meta_exclude(self):
""" """
@ -194,8 +195,8 @@ class ModelAdminTests(TestCase):
form = AdminBandForm form = AdminBandForm
ma = BandAdmin(Band, self.site) ma = BandAdmin(Band, self.site)
self.assertEqual(ma.get_form(request).base_fields.keys(), self.assertEqual(list(ma.get_form(request).base_fields),
['bio', 'sign_date',]) ['bio', 'sign_date'])
# Then, with `InlineModelAdmin` ----------------- # Then, with `InlineModelAdmin` -----------------
@ -218,8 +219,8 @@ class ModelAdminTests(TestCase):
ma = BandAdmin(Band, self.site) ma = BandAdmin(Band, self.site)
self.assertEqual( self.assertEqual(
list(ma.get_formsets(request))[0]().forms[0].fields.keys(), list(list(ma.get_formsets(request))[0]().forms[0].fields),
['main_band', 'opening_band', 'day', 'id', 'DELETE',]) ['main_band', 'opening_band', 'day', 'id', 'DELETE'])
def test_custom_form_validation(self): def test_custom_form_validation(self):
# If we specify a form, it should use it allowing custom validation to work # If we specify a form, it should use it allowing custom validation to work
@ -235,7 +236,7 @@ class ModelAdminTests(TestCase):
form = AdminBandForm form = AdminBandForm
ma = BandAdmin(Band, self.site) ma = BandAdmin(Band, self.site)
self.assertEqual(ma.get_form(request).base_fields.keys(), self.assertEqual(list(ma.get_form(request).base_fields),
['name', 'bio', 'sign_date', 'delete']) ['name', 'bio', 'sign_date', 'delete'])
self.assertEqual( self.assertEqual(
@ -255,7 +256,7 @@ class ModelAdminTests(TestCase):
exclude = ['name'] exclude = ['name']
class BandAdmin(ModelAdmin): class BandAdmin(ModelAdmin):
exclude = ['sign_date',] exclude = ['sign_date']
form = AdminBandForm form = AdminBandForm
def get_form(self, request, obj=None, **kwargs): def get_form(self, request, obj=None, **kwargs):
@ -263,9 +264,8 @@ class ModelAdminTests(TestCase):
return super(BandAdmin, self).get_form(request, obj, **kwargs) return super(BandAdmin, self).get_form(request, obj, **kwargs)
ma = BandAdmin(Band, self.site) ma = BandAdmin(Band, self.site)
self.assertEqual(ma.get_form(request).base_fields.keys(), self.assertEqual(list(ma.get_form(request).base_fields),
['name', 'sign_date',]) ['name', 'sign_date'])
def test_formset_exclude_kwarg_override(self): def test_formset_exclude_kwarg_override(self):
""" """
@ -296,8 +296,8 @@ class ModelAdminTests(TestCase):
ma = BandAdmin(Band, self.site) ma = BandAdmin(Band, self.site)
self.assertEqual( self.assertEqual(
list(ma.get_formsets(request))[0]().forms[0].fields.keys(), list(list(ma.get_formsets(request))[0]().forms[0].fields),
['main_band', 'day', 'transport', 'id', 'DELETE',]) ['main_band', 'day', 'transport', 'id', 'DELETE'])
def test_queryset_override(self): def test_queryset_override(self):
# If we need to override the queryset of a ModelChoiceField in our custom form # If we need to override the queryset of a ModelChoiceField in our custom form
@ -461,7 +461,7 @@ class ModelAdminTests(TestCase):
form = AdminConcertForm form = AdminConcertForm
ma = ConcertAdmin(Concert, self.site) ma = ConcertAdmin(Concert, self.site)
self.assertEqual(ma.get_form(request).base_fields.keys(), self.assertEqual(list(ma.get_form(request).base_fields),
['main_band', 'opening_band', 'day']) ['main_band', 'opening_band', 'day'])
class AdminConcertForm(forms.ModelForm): class AdminConcertForm(forms.ModelForm):
@ -475,7 +475,7 @@ class ModelAdminTests(TestCase):
form = AdminConcertForm form = AdminConcertForm
ma = ConcertAdmin(Concert, self.site) ma = ConcertAdmin(Concert, self.site)
self.assertEqual(ma.get_form(request).base_fields.keys(), self.assertEqual(list(ma.get_form(request).base_fields),
['extra', 'transport']) ['extra', 'transport'])
class ConcertInline(TabularInline): class ConcertInline(TabularInline):
@ -491,7 +491,7 @@ class ModelAdminTests(TestCase):
ma = BandAdmin(Band, self.site) ma = BandAdmin(Band, self.site)
self.assertEqual( self.assertEqual(
list(ma.get_formsets(request))[0]().forms[0].fields.keys(), list(list(ma.get_formsets(request))[0]().forms[0].fields),
['extra', 'transport', 'id', 'DELETE', 'main_band']) ['extra', 'transport', 'id', 'DELETE', 'main_band'])