desc
stringlengths
3
26.7k
decl
stringlengths
11
7.89k
bodies
stringlengths
8
553k
'Sequence names are correct when resetting generic relations (Ref #13941)'
def test_generic_relation(self):
models.Post.objects.create(id=10, name='1st post', text='hello world') cursor = connection.cursor() commands = connections[DEFAULT_DB_ALIAS].ops.sequence_reset_sql(no_style(), [models.Post]) for sql in commands: cursor.execute(sql) obj = models.Post.objects.create(name='New post', t...
'Try to create a model instance that violates a FK constraint. If it fails it should fail with IntegrityError.'
def test_integrity_checks_on_creation(self):
a = models.Article(headline='This is a test', pub_date=datetime.datetime(2005, 7, 27), reporter_id=30) try: a.save() except IntegrityError: pass
'Try to update a model instance introducing a FK constraint violation. If it fails it should fail with IntegrityError.'
def test_integrity_checks_on_update(self):
models.Article.objects.create(headline='Test article', pub_date=datetime.datetime(2010, 9, 4), reporter=self.r) a = models.Article.objects.get(headline='Test article') a.reporter_id = 30 try: a.save() except IntegrityError: pass
'Regression test for the use of None as a query value. None is interpreted as an SQL NULL, but only in __exact queries. Set up some initial polls and choices'
def test_none_as_null(self):
p1 = Poll(question='Why?') p1.save() c1 = Choice(poll=p1, choice='Because.') c1.save() c2 = Choice(poll=p1, choice='Why Not?') c2.save() self.assertQuerysetEqual(Choice.objects.filter(choice__exact=None), []) self.assertQuerysetEqual(Choice.objects.exclude(choice=None).order_by('id'),...
'Querying across reverse relations and then another relation should insert outer joins correctly so as not to exclude results.'
def test_reverse_relations(self):
obj = OuterA.objects.create() self.assertQuerysetEqual(OuterA.objects.filter(inner__second=None), ['<OuterA: OuterA object>']) self.assertQuerysetEqual(OuterA.objects.filter(inner__second__data=None), ['<OuterA: OuterA object>']) inner_obj = Inner.objects.create(first=obj) self.assertQue...
'Tests that URLs that look like absolute file paths after the settings.ADMIN_MEDIA_PREFIX don\'t turn into absolute file paths.'
def test_media_urls(self):
data = ((('%scss/base.css' % settings.ADMIN_MEDIA_PREFIX), ('css', 'base.css')),) bad_data = () if (os.sep == '/'): data += ((('%s\\css/base.css' % settings.ADMIN_MEDIA_PREFIX), ('\\css', 'base.css')),) bad_data += (('%s/css/base.css' % settings.ADMIN_MEDIA_PREFIX), ('%s///css/base.css' % se...
'Test that extra_context works'
def changelist_view(self, request):
return super(ArticleAdmin, self).changelist_view(request, extra_context={'extra_var': 'Hello!'})
'Only allow changing objects with even id number'
def has_change_permission(self, request, obj=None):
return (request.user.is_staff and (obj is not None) and ((obj.id % 2) == 0))
'Test that extra_context works'
def changelist_view(self, request):
return super(CustomArticleAdmin, self).changelist_view(request, extra_context={'extra_var': 'Hello!'})
'If you leave off the trailing slash, app should redirect and add it.'
def testTrailingSlashRequired(self):
request = self.client.get(('/test_admin/%s/admin_views/article/add' % self.urlbit)) self.assertRedirects(request, ('/test_admin/%s/admin_views/article/add/' % self.urlbit), status_code=301)
'A smoke test to ensure GET on the add_view works.'
def testBasicAddGet(self):
response = self.client.get(('/test_admin/%s/admin_views/section/add/' % self.urlbit)) self.assertEqual(response.status_code, 200)
'A smoke test to ensure GET on the change_view works.'
def testBasicEditGet(self):
response = self.client.get(('/test_admin/%s/admin_views/section/1/' % self.urlbit)) self.assertEqual(response.status_code, 200)
'A smoke test to ensure GET on the change_view works (returns an HTTP 404 error, see #11191) when passing a string as the PK argument for a model with an integer PK field.'
def testBasicEditGetStringPK(self):
response = self.client.get(('/test_admin/%s/admin_views/section/abc/' % self.urlbit)) self.assertEqual(response.status_code, 404)
'A smoke test to ensure POST on add_view works.'
def testBasicAddPost(self):
post_data = {'name': u'Another Section', 'article_set-TOTAL_FORMS': u'3', 'article_set-INITIAL_FORMS': u'0', 'article_set-MAX_NUM_FORMS': u'0'} response = self.client.post(('/test_admin/%s/admin_views/section/add/' % self.urlbit), post_data) self.assertEqual(response.status_code, 302)
'Ensure http response from a popup is properly escaped.'
def testPopupAddPost(self):
post_data = {'_popup': u'1', 'title': u'title with a new\nline', 'content': u'some content', 'date_0': u'2010-09-10', 'date_1': u'14:55:39'} response = self.client.post(('/test_admin/%s/admin_views/article/add/' % self.urlbit), post_data) self.failUnlessEqual(response.status_code, 200) self....
'A smoke test to ensure POST on edit_view works.'
def testBasicEditPost(self):
response = self.client.post(('/test_admin/%s/admin_views/section/1/' % self.urlbit), self.inline_post_data) self.assertEqual(response.status_code, 302)
'Test "save as".'
def testEditSaveAs(self):
post_data = self.inline_post_data.copy() post_data.update({'_saveasnew': u'Save+as+new', 'article_set-1-section': u'1', 'article_set-2-section': u'1', 'article_set-3-section': u'1', 'article_set-4-section': u'1', 'article_set-5-section': u'1'}) response = self.client.post(('/test_admin/%s/admin_views/sectio...
'Ensure we can sort on a list_display field that is a callable (column 2 is callable_year in ArticleAdmin)'
def testChangeListSortingCallable(self):
response = self.client.get(('/test_admin/%s/admin_views/article/' % self.urlbit), {'ot': 'asc', 'o': 2}) self.assertEqual(response.status_code, 200) self.assertTrue(((response.content.index('Oldest content') < response.content.index('Middle content')) and (response.content.index('Middle content') <...
'Ensure we can sort on a list_display field that is a Model method (colunn 3 is \'model_year\' in ArticleAdmin)'
def testChangeListSortingModel(self):
response = self.client.get(('/test_admin/%s/admin_views/article/' % self.urlbit), {'ot': 'dsc', 'o': 3}) self.assertEqual(response.status_code, 200) self.assertTrue(((response.content.index('Newest content') < response.content.index('Middle content')) and (response.content.index('Middle content') <...
'Ensure we can sort on a list_display field that is a ModelAdmin method (colunn 4 is \'modeladmin_year\' in ArticleAdmin)'
def testChangeListSortingModelAdmin(self):
response = self.client.get(('/test_admin/%s/admin_views/article/' % self.urlbit), {'ot': 'asc', 'o': 4}) self.assertEqual(response.status_code, 200) self.assertTrue(((response.content.index('Oldest content') < response.content.index('Middle content')) and (response.content.index('Middle content') <...
'Ensure admin changelist filters do not contain objects excluded via limit_choices_to. This also tests relation-spanning filters (e.g. \'color__value\').'
def testLimitedFilter(self):
response = self.client.get(('/test_admin/%s/admin_views/thing/' % self.urlbit)) self.assertEqual(response.status_code, 200) self.assertTrue(('<div id="changelist-filter">' in response.content), 'Expected filter not found in changelist view.') self.assertFalse(('<a href="?color__i...
'Ensure incorrect lookup parameters are handled gracefully.'
def testIncorrectLookupParameters(self):
response = self.client.get(('/test_admin/%s/admin_views/thing/' % self.urlbit), {'notarealfield': '5'}) self.assertRedirects(response, ('/test_admin/%s/admin_views/thing/?e=1' % self.urlbit)) response = self.client.get(('/test_admin/%s/admin_views/thing/' % self.urlbit), {'color__id__exact': 'StringNotInteg...
'Ensure is_null is handled correctly.'
def testIsNullLookups(self):
Article.objects.create(title='I Could Go Anywhere', content='Versatile', date=datetime.datetime.now()) response = self.client.get(('/test_admin/%s/admin_views/article/' % self.urlbit)) self.assertTrue(('4 articles' in response.content), '"4 articles" missing from response') respo...
'Ensures the admin changelist shows correct values in the relevant column for rows corresponding to instances of a model in which a named group has been used in the choices option of a field.'
def testNamedGroupFieldChoicesChangeList(self):
response = self.client.get(('/test_admin/%s/admin_views/fabric/' % self.urlbit)) self.assertEqual(response.status_code, 200) self.assertTrue((('<a href="1/">Horizontal</a>' in response.content) and ('<a href="2/">Vertical</a>' in response.content)), "Changelist table isn't showing the r...
'Ensures the filter UI shows correctly when at least one named group has been used in the choices option of a model field.'
def testNamedGroupFieldChoicesFilter(self):
response = self.client.get(('/test_admin/%s/admin_views/fabric/' % self.urlbit)) self.assertEqual(response.status_code, 200) self.assertTrue(('<div id="changelist-filter">' in response.content), 'Expected filter not found in changelist view.') self.assertTrue((('<a href="?surface...
'Check if the Javascript i18n view returns an empty language catalog if the default language is non-English but the selected language is English. See #13388 and #3594 for more details.'
def testI18NLanguageNonEnglishDefault(self):
try: settings.LANGUAGE_CODE = 'fr' activate('en-us') response = self.client.get('/test_admin/admin/jsi18n/') self.assertNotContains(response, 'Choisir une heure') finally: deactivate()
'Makes sure that the fallback language is still working properly in cases where the selected language cannot be found.'
def testI18NLanguageNonEnglishFallback(self):
try: settings.LANGUAGE_CODE = 'fr' activate('none') response = self.client.get('/test_admin/admin/jsi18n/') self.assertContains(response, 'Choisir une heure') finally: deactivate()
'Check if L10N is deactivated, the Javascript i18n view doesn\'t return localized date/time formats. Refs #14824.'
def testL10NDeactivated(self):
try: settings.LANGUAGE_CODE = 'ru' settings.USE_L10N = False activate('ru') response = self.client.get('/test_admin/admin/jsi18n/') self.assertNotContains(response, '%d.%m.%Y %H:%M:%S') self.assertContains(response, '%Y-%m-%d %H:%M:%S') finally: deac...
'Regressions test for ticket 15103 - filtering on fields defined in a ForeignKey \'limit_choices_to\' should be allowed, otherwise raw_id_fields can break.'
def test_allowed_filtering_15103(self):
try: self.client.get('/test_admin/admin/admin_views/inquisition/?leader__name=Palin&leader__age=27') except SuspiciousOperation: self.fail('Filters should be allowed if they are defined on a ForeignKey pointing to this model')
'JavaScript-assisted auto-focus on first field.'
def testSingleWidgetFirsFieldFocus(self):
response = self.client.get(('/test_admin/%s/admin_views/picture/add/' % self.urlbit)) self.assertContains(response, '<script type="text/javascript">document.getElementById("id_name").focus();</script>')
'JavaScript-assisted auto-focus should work if a model/ModelAdmin setup is such that the first form field has a MultiWidget.'
def testMultiWidgetFirsFieldFocus(self):
response = self.client.get(('/test_admin/%s/admin_views/reservation/add/' % self.urlbit)) self.assertContains(response, '<script type="text/javascript">document.getElementById("id_start_date_0").focus();</script>')
'Ensure save as actually creates a new person'
def test_save_as_duplication(self):
post_data = {'_saveasnew': '', 'name': 'John M', 'gender': 1, 'age': 42} response = self.client.post('/test_admin/admin/admin_views/person/1/', post_data) self.assertEqual(len(Person.objects.filter(name='John M')), 1) self.assertEqual(len(Person.objects.filter(id=1)), 1)
'Ensure that \'save as\' is displayed when activated and after submitting invalid data aside save_as_new will not show us a form to overwrite the initial model.'
def test_save_as_display(self):
response = self.client.get('/test_admin/admin/admin_views/person/1/') self.assertTrue(response.context['save_as']) post_data = {'_saveasnew': '', 'name': 'John M', 'gender': 3, 'alive': 'checked'} response = self.client.post('/test_admin/admin/admin_views/person/1/', post_data) self.assertEqual(r...
'Test setup.'
def setUp(self):
opts = Article._meta add_user = User.objects.get(username='adduser') add_user.user_permissions.add(get_perm(Article, opts.get_add_permission())) change_user = User.objects.get(username='changeuser') change_user.user_permissions.add(get_perm(Article, opts.get_change_permission())) delete_user = U...
'Make sure only staff members can log in. Successful posts to the login page will redirect to the orignal url. Unsuccessfull attempts will continue to render the login page with a 200 status code.'
def testLogin(self):
request = self.client.get('/test_admin/admin/') self.assertEqual(request.status_code, 200) login = self.client.post('/test_admin/admin/', self.super_login) self.assertRedirects(login, '/test_admin/admin/') self.assertFalse(login.context) self.client.get('/test_admin/admin/logout/') request =...
'Test add view restricts access and actually adds items.'
def testAddView(self):
add_dict = {'title': 'D\xc3\xb8m ikke', 'content': '<p>great article</p>', 'date_0': '2008-03-18', 'date_1': '10:54:39', 'section': 1} self.client.get('/test_admin/admin/') self.client.post('/test_admin/admin/', self.changeuser_login) self.assertEqual(self.client.session.test_cookie_worked(), Fals...
'Change view should restrict access and allow users to edit items.'
def testChangeView(self):
change_dict = {'title': 'Ikke ford\xc3\xb8mt', 'content': '<p>edited article</p>', 'date_0': '2008-03-18', 'date_1': '10:54:39', 'section': 1} self.client.get('/test_admin/admin/') self.client.post('/test_admin/admin/', self.adduser_login) request = self.client.get('/test_admin/admin/admin_views/a...
'The foreign key widget should only show the "add related" button if the user has permission to add that related item.'
def testConditionallyShowAddSectionLink(self):
url = '/test_admin/admin/admin_views/article/add/' add_link_text = ' class="add-another"' self.client.get('/test_admin/admin/') self.client.post('/test_admin/admin/', self.adduser_login) response = self.client.get(url) self.assertNotContains(response, add_link_text) add_user = User.object...
'Delete view should restrict access and actually delete items.'
def testDeleteView(self):
delete_dict = {'post': 'yes'} self.client.get('/test_admin/admin/') self.client.post('/test_admin/admin/', self.adduser_login) request = self.client.get('/test_admin/admin/admin_views/article/1/delete/') self.assertEqual(request.status_code, 403) post = self.client.post('/test_admin/admin/admin_...
'Objects should be nested to display the relationships that cause them to be scheduled for deletion.'
def test_nesting(self):
pattern = re.compile('<li>Plot: <a href=".+/admin_views/plot/1/">World Domination</a>\\s*<ul>\\s*<li>Plot details: <a href=".+/admin_views/plotdetails/1/">almost finished</a>') response = self.client.get(('/test_admin/admin/admin_views/villain/%s/delete/' % quote(1))) self.assertTrue(pa...
'Cyclic relationships should still cause each object to only be listed once.'
def test_cyclic(self):
one = '<li>Cyclic one: <a href="/test_admin/admin/admin_views/cyclicone/1/">I am recursive</a>' two = '<li>Cyclic two: <a href="/test_admin/admin/admin_views/cyclictwo/1/">I am recursive too</a>' response = self.client.get(('/test_admin/admin/admin_views/cyclicone/%s/delete/...
'If a deleted object has two relationships from another model, both of those should be followed in looking for related objects to delete.'
def test_multiple_fkeys_to_same_model(self):
should_contain = '<li>Plot: <a href="/test_admin/admin/admin_views/plot/1/">World Domination</a>' response = self.client.get(('/test_admin/admin/admin_views/villain/%s/delete/' % quote(1))) self.assertContains(response, should_contain) response = self.client.get(('/test_admin/admin/admin_views/...
'If a deleted object has two relationships pointing to it from another object, the other object should still only be listed once.'
def test_multiple_fkeys_to_same_instance(self):
should_contain = '<li>Plot: <a href="/test_admin/admin/admin_views/plot/2/">World Peace</a></li>' response = self.client.get(('/test_admin/admin/admin_views/villain/%s/delete/' % quote(2))) self.assertContains(response, should_contain, 1)
'In the case of an inherited model, if either the child or parent-model instance is deleted, both instances are listed for deletion, as well as any relationships they have.'
def test_inheritance(self):
should_contain = ['<li>Villain: <a href="/test_admin/admin/admin_views/villain/3/">Bob</a>', '<li>Super villain: <a href="/test_admin/admin/admin_views/supervillain/3/">Bob</a>', '<li>Secret hideout: floating castle', '<li>Super secret hideout: super floating castle!'] res...
'If a deleted object has GenericForeignKeys pointing to it, those objects should be listed for deletion.'
def test_generic_relations(self):
plot = Plot.objects.get(pk=3) tag = FunkyTag.objects.create(content_object=plot, name='hott') should_contain = '<li>Funky tag: hott' response = self.client.get(('/test_admin/admin/admin_views/plot/%s/delete/' % quote(3))) self.assertContains(response, should_contain)
'Retrieving the history for the object using urlencoded form of primary key should work'
def test_get_history_view(self):
response = self.client.get(('/test_admin/admin/admin_views/modelwithstringprimarykey/%s/history/' % quote(self.pk))) self.assertContains(response, escape(self.pk)) self.assertEqual(response.status_code, 200)
'Retrieving the object using urlencoded form of primary key should work'
def test_get_change_view(self):
response = self.client.get(('/test_admin/admin/admin_views/modelwithstringprimarykey/%s/' % quote(self.pk))) self.assertContains(response, escape(self.pk)) self.assertEqual(response.status_code, 200)
'The link from the changelist referring to the changeform of the object should be quoted'
def test_changelist_to_changeform_link(self):
response = self.client.get('/test_admin/admin/admin_views/modelwithstringprimarykey/') should_contain = ('<th><a href="%s/">%s</a></th></tr>' % (quote(self.pk), escape(self.pk))) self.assertContains(response, should_contain)
'The link from the recent actions list referring to the changeform of the object should be quoted'
def test_recentactions_link(self):
response = self.client.get('/test_admin/admin/') should_contain = ('<a href="admin_views/modelwithstringprimarykey/%s/">%s</a>' % (quote(self.pk), escape(self.pk))) self.assertContains(response, should_contain)
'If a LogEntry is missing content_type it will not display it in span tag under the hyperlink.'
def test_recentactions_without_content_type(self):
response = self.client.get('/test_admin/admin/') should_contain = ('<a href="admin_views/modelwithstringprimarykey/%s/">%s</a>' % (quote(self.pk), escape(self.pk))) self.assertContains(response, should_contain) should_contain = 'Model with string primary key' self.assertContains(respo...
'The link from the delete confirmation page referring back to the changeform of the object should be quoted'
def test_deleteconfirmation_link(self):
response = self.client.get(('/test_admin/admin/admin_views/modelwithstringprimarykey/%s/delete/' % quote(self.pk))) should_contain = ('/%s/">%s</a>' % (iri_to_uri(quote(self.pk)), escape(self.pk))) self.assertContains(response, should_contain)
'A model with a primary key that ends with add should be visible'
def test_url_conflicts_with_add(self):
add_model = ModelWithStringPrimaryKey(id='i have something to add') add_model.save() response = self.client.get(('/test_admin/admin/admin_views/modelwithstringprimarykey/%s/' % quote(add_model.pk))) should_contain = '<h1>Change model with string primary key</h1>' self.asse...
'A model with a primary key that ends with delete should be visible'
def test_url_conflicts_with_delete(self):
delete_model = ModelWithStringPrimaryKey(id='delete') delete_model.save() response = self.client.get(('/test_admin/admin/admin_views/modelwithstringprimarykey/%s/' % quote(delete_model.pk))) should_contain = '<h1>Change model with string primary key</h1>' self.assertContains(response,...
'A model with a primary key that ends with history should be visible'
def test_url_conflicts_with_history(self):
history_model = ModelWithStringPrimaryKey(id='history') history_model.save() response = self.client.get(('/test_admin/admin/admin_views/modelwithstringprimarykey/%s/' % quote(history_model.pk))) should_contain = '<h1>Change model with string primary key</h1>' self.assertContains(respo...
'Ensure that we see the login form'
def test_secure_view_shows_login_if_not_logged_in(self):
response = self.client.get('/test_admin/admin/secure-view/') self.assertTemplateUsed(response, 'admin/login.html')
'Make sure only staff members can log in. Successful posts to the login page will redirect to the orignal url. Unsuccessfull attempts will continue to render the login page with a 200 status code.'
def test_staff_member_required_decorator_works_as_per_admin_login(self):
request = self.client.get('/test_admin/admin/secure-view/') self.assertEqual(request.status_code, 200) login = self.client.post('/test_admin/admin/secure-view/', self.super_login) self.assertRedirects(login, '/test_admin/admin/secure-view/') self.assertFalse(login.context) self.client.get('/test...
'Only admin users should be able to use the admin shortcut view.'
def test_shortcut_view_only_available_to_staff(self):
user_ctype = ContentType.objects.get_for_model(User) user = User.objects.get(username='super') shortcut_url = ('/test_admin/admin/r/%s/%s/' % (user_ctype.pk, user.pk)) response = self.client.get(shortcut_url, follow=False) self.assertTemplateUsed(response, 'admin/login.html') self.client.login(u...
'A test to ensure that POST on edit_view handles non-ascii characters.'
def testUnicodeEdit(self):
post_data = {'name': u'Test l\xe6rdommer', 'chapter_set-TOTAL_FORMS': u'6', 'chapter_set-INITIAL_FORMS': u'3', 'chapter_set-MAX_NUM_FORMS': u'0', 'chapter_set-0-id': u'1', 'chapter_set-0-title': u'Norske bostaver \xe6\xf8\xe5 skaper problemer', 'chapter_set-0-content': u'&lt;p&gt;Sv\xe6rt frustrer...
'Ensure that the delete_view handles non-ascii characters'
def testUnicodeDelete(self):
delete_dict = {'post': 'yes'} response = self.client.get('/test_admin/admin/admin_views/book/1/delete/') self.assertEqual(response.status_code, 200) response = self.client.post('/test_admin/admin/admin_views/book/1/delete/', delete_dict) self.assertRedirects(response, '/test_admin/admin/admin_views/...
'Ensure that non field errors are displayed for each of the forms in the changelist\'s formset. Refs #13126.'
def test_non_field_errors(self):
fd1 = FoodDelivery.objects.create(reference='123', driver='bill', restaurant='thai') fd2 = FoodDelivery.objects.create(reference='456', driver='bill', restaurant='india') fd3 = FoodDelivery.objects.create(reference='789', driver='bill', restaurant='pizza') data = {'form-TOTAL_FORMS': '3', 'form-INITIAL_...
'Fields should not be list-editable in popups.'
def test_list_editable_popup(self):
response = self.client.get('/test_admin/admin/admin_views/person/') self.assertNotEqual(response.context['cl'].list_editable, ()) response = self.client.get(('/test_admin/admin/admin_views/person/?%s' % IS_POPUP_VAR)) self.assertEqual(response.context['cl'].list_editable, ())
'Ensure that hidden pk fields aren\'t displayed in the table body and that their corresponding human-readable value is displayed instead. Note that the hidden pk fields are in fact be displayed but separately (not in the table), and only once. Refs #12475.'
def test_pk_hidden_fields(self):
story1 = Story.objects.create(title='The adventures of Guido', content='Once upon a time in Djangoland...') story2 = Story.objects.create(title='Crouching Tiger, Hidden Python', content='The Python was sneaking into...') response = self.client.get('/test_admin/ad...
'Similarly as test_pk_hidden_fields, but when the hidden pk fields are referenced in list_display_links. Refs #12475.'
def test_pk_hidden_fields_with_list_display_links(self):
story1 = OtherStory.objects.create(title='The adventures of Guido', content='Once upon a time in Djangoland...') story2 = OtherStory.objects.create(title='Crouching Tiger, Hidden Python', content='The Python was sneaking into...') response = self.client.get('/tes...
'Check that a search that mentions sibling models'
def test_search_on_sibling_models(self):
response = self.client.get('/test_admin/admin/admin_views/recommendation/?q=bar') self.assertContains(response, '\n1 recommendation\n')
'Ensure that the to_field GET parameter is preserved when a search is performed. Refs #10918.'
def test_with_fk_to_field(self):
from django.contrib.admin.views.main import TO_FIELD_VAR response = self.client.get(('/test_admin/admin/auth/user/?q=joe&%s=username' % TO_FIELD_VAR)) self.assertContains(response, '\n1 user\n') self.assertContains(response, '<input type="hidden" name="t" value="username"/>')
'Ensure that inline models which inherit from a common parent are correctly handled by admin.'
def testInline(self):
foo_user = u'foo username' bar_user = u'bar username' name_re = re.compile('name="(.*?)"') response = self.client.get('/test_admin/admin/admin_views/persona/add/') names = name_re.findall(response.content) self.assertEqual(len(names), len(set(names))) post_data = {'name': u'Test Nam...
'Tests a custom action defined in a ModelAdmin method'
def test_model_admin_custom_action(self):
action_data = {ACTION_CHECKBOX_NAME: [1], 'action': 'mail_admin', 'index': 0} response = self.client.post('/test_admin/admin/admin_views/subscriber/', action_data) self.assertEqual(len(mail.outbox), 1) self.assertEqual(mail.outbox[0].subject, 'Greetings from a ModelAdmin action')
'Tests the default delete action defined as a ModelAdmin method'
def test_model_admin_default_delete_action(self):
action_data = {ACTION_CHECKBOX_NAME: [1, 2], 'action': 'delete_selected', 'index': 0} delete_confirmation_data = {ACTION_CHECKBOX_NAME: [1, 2], 'action': 'delete_selected', 'post': 'yes'} confirmation = self.client.post('/test_admin/admin/admin_views/subscriber/', action_data) self.assertContains(confir...
'If USE_THOUSAND_SEPARATOR is set, make sure that the ids for the objects selected for deletion are rendered without separators. Refs #14895.'
def test_non_localized_pk(self):
self.old_USE_THOUSAND_SEPARATOR = settings.USE_THOUSAND_SEPARATOR self.old_USE_L10N = settings.USE_L10N settings.USE_THOUSAND_SEPARATOR = True settings.USE_L10N = True subscriber = Subscriber.objects.get(id=1) subscriber.id = 9999 subscriber.save() action_data = {ACTION_CHECKBOX_NAME: [9...
'Tests the default delete action defined as a ModelAdmin method in the case where some related objects are protected from deletion.'
def test_model_admin_default_delete_action_protected(self):
q1 = Question.objects.create(question='Why?') a1 = Answer.objects.create(question=q1, answer='Because.') a2 = Answer.objects.create(question=q1, answer='Yes.') q2 = Question.objects.create(question='Wherefore?') action_data = {ACTION_CHECKBOX_NAME: [q1.pk, q2.pk], 'action': 'delete_selected', 'index...
'Tests a custom action defined in a function'
def test_custom_function_mail_action(self):
action_data = {ACTION_CHECKBOX_NAME: [1], 'action': 'external_mail', 'index': 0} response = self.client.post('/test_admin/admin/admin_views/externalsubscriber/', action_data) self.assertEqual(len(mail.outbox), 1) self.assertEqual(mail.outbox[0].subject, 'Greetings from a function action')
'Tests a custom action defined in a function'
def test_custom_function_action_with_redirect(self):
action_data = {ACTION_CHECKBOX_NAME: [1], 'action': 'redirect_to', 'index': 0} response = self.client.post('/test_admin/admin/admin_views/externalsubscriber/', action_data) self.assertEqual(response.status_code, 302)
'Test that actions which don\'t return an HttpResponse are redirected to the same page, retaining the querystring (which may contain changelist information).'
def test_default_redirect(self):
action_data = {ACTION_CHECKBOX_NAME: [1], 'action': 'external_mail', 'index': 0} url = '/test_admin/admin/admin_views/externalsubscriber/?ot=asc&o=1' response = self.client.post(url, action_data) self.assertRedirects(response, url)
'Tests a ModelAdmin without any action'
def test_model_without_action(self):
response = self.client.get('/test_admin/admin/admin_views/oldsubscriber/') self.assertEqual(response.context['action_form'], None) self.assertTrue(('<input type="checkbox" class="action-select"' not in response.content), 'Found an unexpected action toggle checkboxbox in response')...
'Tests that a ModelAdmin without any actions still gets jQuery included in page'
def test_model_without_action_still_has_jquery(self):
response = self.client.get('/test_admin/admin/admin_views/oldsubscriber/') self.assertEqual(response.context['action_form'], None) self.assertTrue(('jquery.min.js' in response.content), 'jQuery missing from admin pages for model with no admin actions')
'Tests that the checkbox column class is present in the response'
def test_action_column_class(self):
response = self.client.get('/test_admin/admin/admin_views/subscriber/') self.assertNotEqual(response.context['action_form'], None) self.assertTrue(('action-checkbox-column' in response.content), 'Expected an action-checkbox-column in response')
'Test that actions come from the form whose submit button was pressed (#10618).'
def test_multiple_actions_form(self):
action_data = {ACTION_CHECKBOX_NAME: [1], 'action': ['external_mail', 'delete_selected'], 'index': 0} response = self.client.post('/test_admin/admin/admin_views/externalsubscriber/', action_data) self.assertEqual(len(mail.outbox), 1) self.assertEqual(mail.outbox[0].subject, 'Greetings from a fu...
'User should see a warning when \'Go\' is pressed and no items are selected.'
def test_user_message_on_none_selected(self):
action_data = {ACTION_CHECKBOX_NAME: [], 'action': 'delete_selected', 'index': 0} response = self.client.post('/test_admin/admin/admin_views/subscriber/', action_data) msg = 'Items must be selected in order to perform actions on them. No items have been changed.'...
'User should see a warning when \'Go\' is pressed and no action is selected.'
def test_user_message_on_no_action(self):
action_data = {ACTION_CHECKBOX_NAME: [1, 2], 'action': '', 'index': 0} response = self.client.post('/test_admin/admin/admin_views/subscriber/', action_data) msg = 'No action selected.' self.assertContains(response, msg) self.assertEqual(Subscriber.objects.count(), 2)
'Check if the selection counter is there.'
def test_selection_counter(self):
response = self.client.get('/test_admin/admin/admin_views/subscriber/') self.assertContains(response, '0 of 2 selected')
'Actions should not be shown in popups.'
def test_popup_actions(self):
response = self.client.get('/test_admin/admin/admin_views/subscriber/') self.assertNotEquals(response.context['action_form'], None) response = self.client.get(('/test_admin/admin/admin_views/subscriber/?%s' % IS_POPUP_VAR)) self.assertEqual(response.context['action_form'], None)
'Validate that a custom ChangeList class can be used (#9749)'
def test_custom_changelist(self):
post_data = {'name': u'First Gadget'} response = self.client.post(('/test_admin/%s/admin_views/gadget/add/' % self.urlbit), post_data) self.assertEqual(response.status_code, 302) response = self.client.get(('/test_admin/%s/admin_views/gadget/' % self.urlbit)) response = self.client.get(('/test_ad...
'InlineModelAdmin broken?'
def test(self):
response = self.client.get('/test_admin/admin/admin_views/parent/add/') self.assertEqual(response.status_code, 200)
'Test that inline file uploads correctly display prior data (#10002).'
def test_inline_file_upload_edit_validation_error_post(self):
post_data = {'name': u'Test Gallery', 'pictures-TOTAL_FORMS': u'2', 'pictures-INITIAL_FORMS': u'1', 'pictures-MAX_NUM_FORMS': u'0', 'pictures-0-id': unicode(self.picture.id), 'pictures-0-gallery': unicode(self.gallery.id), 'pictures-0-name': 'Test Picture', 'pictures-0-image': '', 'pictures-1-id': '', 'pictur...
'A simple model can be saved as inlines'
def test_simple_inline(self):
self.post_data['widget_set-0-name'] = 'Widget 1' collector_url = ('/test_admin/admin/admin_views/collector/%d/' % self.collector.pk) response = self.client.post(collector_url, self.post_data) self.assertEqual(response.status_code, 302) self.assertEqual(Widget.objects.count(), 1) self.assertEq...
'A model with an explicit autofield primary key can be saved as inlines. Regression for #8093'
def test_explicit_autofield_inline(self):
self.post_data['grommet_set-0-name'] = 'Grommet 1' collector_url = ('/test_admin/admin/admin_views/collector/%d/' % self.collector.pk) response = self.client.post(collector_url, self.post_data) self.assertEqual(response.status_code, 302) self.assertEqual(Grommet.objects.count(), 1) self.asser...
'A model with a character PK can be saved as inlines. Regression for #10992'
def test_char_pk_inline(self):
self.post_data['doohickey_set-0-code'] = 'DH1' self.post_data['doohickey_set-0-name'] = 'Doohickey 1' collector_url = ('/test_admin/admin/admin_views/collector/%d/' % self.collector.pk) response = self.client.post(collector_url, self.post_data) self.assertEqual(response.status_code, 302) self...
'A model with an integer PK can be saved as inlines. Regression for #10992'
def test_integer_pk_inline(self):
self.post_data['whatsit_set-0-index'] = '42' self.post_data['whatsit_set-0-name'] = 'Whatsit 1' response = self.client.post('/test_admin/admin/admin_views/collector/1/', self.post_data) self.assertEqual(response.status_code, 302) self.assertEqual(Whatsit.objects.count(), 1) self.assertEqual(W...
'An inherited model can be saved as inlines. Regression for #11042'
def test_inherited_inline(self):
self.post_data['fancydoodad_set-0-name'] = 'Fancy Doodad 1' collector_url = ('/test_admin/admin/admin_views/collector/%d/' % self.collector.pk) response = self.client.post(collector_url, self.post_data) self.assertEqual(response.status_code, 302) self.assertEqual(FancyDoodad.objects.count(), 1...
'Check that an inline with an editable ordering fields is updated correctly. Regression for #10922'
def test_ordered_inline(self):
Category.objects.create(id=1, order=1, collector=self.collector) Category.objects.create(id=2, order=2, collector=self.collector) Category.objects.create(id=3, order=0, collector=self.collector) Category.objects.create(id=4, order=0, collector=self.collector) self.post_data.update({'name': 'Frederic...
'Check the never-cache status of the main index'
def testAdminIndex(self):
response = self.client.get('/test_admin/admin/') self.assertEqual(get_max_age(response), 0)
'Check the never-cache status of an application index'
def testAppIndex(self):
response = self.client.get('/test_admin/admin/admin_views/') self.assertEqual(get_max_age(response), 0)
'Check the never-cache status of a model index'
def testModelIndex(self):
response = self.client.get('/test_admin/admin/admin_views/fabric/') self.assertEqual(get_max_age(response), 0)
'Check the never-cache status of a model add page'
def testModelAdd(self):
response = self.client.get('/test_admin/admin/admin_views/fabric/add/') self.assertEqual(get_max_age(response), 0)
'Check the never-cache status of a model edit page'
def testModelView(self):
response = self.client.get('/test_admin/admin/admin_views/section/1/') self.assertEqual(get_max_age(response), 0)
'Check the never-cache status of a model history page'
def testModelHistory(self):
response = self.client.get('/test_admin/admin/admin_views/section/1/history/') self.assertEqual(get_max_age(response), 0)
'Check the never-cache status of a model delete page'
def testModelDelete(self):
response = self.client.get('/test_admin/admin/admin_views/section/1/delete/') self.assertEqual(get_max_age(response), 0)
'Check the never-cache status of login views'
def testLogin(self):
self.client.logout() response = self.client.get('/test_admin/admin/') self.assertEqual(get_max_age(response), 0)
'Check the never-cache status of logout view'
def testLogout(self):
response = self.client.get('/test_admin/admin/logout/') self.assertEqual(get_max_age(response), 0)
'Check the never-cache status of the password change view'
def testPasswordChange(self):
self.client.logout() response = self.client.get('/test_admin/password_change/') self.assertEqual(get_max_age(response), None)