1
0
mirror of https://github.com/django/django.git synced 2025-03-12 10:22:37 +00:00

Increased test coverage for {% get_admin_log %} and {% static %}.

This commit is contained in:
Hasan Ramezani 2018-03-25 17:32:07 +04:30 committed by Tim Graham
parent 3990d74018
commit 76ae1e9a94
2 changed files with 41 additions and 15 deletions

View File

@ -12,7 +12,7 @@ from django.db.models import F
from django.db.models.fields import Field, IntegerField from django.db.models.fields import Field, IntegerField
from django.db.models.functions import Upper from django.db.models.functions import Upper
from django.db.models.lookups import Contains, Exact from django.db.models.lookups import Contains, Exact
from django.template import Context, Template from django.template import Context, Template, TemplateSyntaxError
from django.test import TestCase, override_settings from django.test import TestCase, override_settings
from django.test.client import RequestFactory from django.test.client import RequestFactory
from django.urls import reverse from django.urls import reverse
@ -893,31 +893,24 @@ class ChangeListTests(TestCase):
self.assertNotIn('Add ', response.rendered_content) self.assertNotIn('Add ', response.rendered_content)
class AdminLogNodeTestCase(TestCase): class GetAdminLogTests(TestCase):
def test_get_admin_log_templatetag_custom_user(self): def test_custom_user_pk_not_named_id(self):
""" """
Regression test for ticket #20088: admin log depends on User model {% get_admin_log %} works if the user model's primary key isn't named
having id field as primary key. 'id'.
The old implementation raised an AttributeError when trying to use
the id field.
""" """
context = Context({'user': CustomIdUser()}) context = Context({'user': CustomIdUser()})
template_string = '{% load log %}{% get_admin_log 10 as admin_log for_user user %}' template = Template('{% load log %}{% get_admin_log 10 as admin_log for_user user %}')
template = Template(template_string)
# This template tag just logs. # This template tag just logs.
self.assertEqual(template.render(context), '') self.assertEqual(template.render(context), '')
def test_get_admin_log_templatetag_no_user(self): def test_no_user(self):
""" """{% get_admin_log %} works without specifying a user."""
The {% get_admin_log %} tag should work without specifying a user.
"""
user = User(username='jondoe', password='secret', email='super@example.com') user = User(username='jondoe', password='secret', email='super@example.com')
user.save() user.save()
ct = ContentType.objects.get_for_model(User) ct = ContentType.objects.get_for_model(User)
LogEntry.objects.log_action(user.pk, ct.pk, user.pk, repr(user), 1) LogEntry.objects.log_action(user.pk, ct.pk, user.pk, repr(user), 1)
t = Template( t = Template(
'{% load log %}' '{% load log %}'
'{% get_admin_log 100 as admin_log %}' '{% get_admin_log 100 as admin_log %}'
@ -927,6 +920,26 @@ class AdminLogNodeTestCase(TestCase):
) )
self.assertEqual(t.render(Context({})), 'Added "<User: jondoe>".') self.assertEqual(t.render(Context({})), 'Added "<User: jondoe>".')
def test_missing_args(self):
msg = "'get_admin_log' statements require two arguments"
with self.assertRaisesMessage(TemplateSyntaxError, msg):
Template('{% load log %}{% get_admin_log 10 as %}')
def test_non_integer_limit(self):
msg = "First argument to 'get_admin_log' must be an integer"
with self.assertRaisesMessage(TemplateSyntaxError, msg):
Template('{% load log %}{% get_admin_log "10" as admin_log for_user user %}')
def test_without_as(self):
msg = "Second argument to 'get_admin_log' must be 'as'"
with self.assertRaisesMessage(TemplateSyntaxError, msg):
Template('{% load log %}{% get_admin_log 10 ad admin_log for_user user %}')
def test_without_for_user(self):
msg = "Fourth argument to 'get_admin_log' must be 'for_user'"
with self.assertRaisesMessage(TemplateSyntaxError, msg):
Template('{% load log %}{% get_admin_log 10 as admin_log foruser user %}')
@override_settings(ROOT_URLCONF='admin_changelist.urls') @override_settings(ROOT_URLCONF='admin_changelist.urls')
class SeleniumTests(AdminSeleniumTestCase): class SeleniumTests(AdminSeleniumTestCase):

View File

@ -1,6 +1,7 @@
from urllib.parse import urljoin from urllib.parse import urljoin
from django.conf import settings from django.conf import settings
from django.template import TemplateSyntaxError
from django.test import SimpleTestCase, override_settings from django.test import SimpleTestCase, override_settings
from ..utils import setup from ..utils import setup
@ -32,6 +33,12 @@ class StaticTagTests(SimpleTestCase):
output = self.engine.render_to_string('static-prefixtag04') output = self.engine.render_to_string('static-prefixtag04')
self.assertEqual(output, settings.MEDIA_URL) self.assertEqual(output, settings.MEDIA_URL)
@setup({'t': '{% load static %}{% get_media_prefix ad media_prefix %}{{ media_prefix }}'})
def test_static_prefixtag_without_as(self):
msg = "First argument in 'get_media_prefix' must be 'as'"
with self.assertRaisesMessage(TemplateSyntaxError, msg):
self.engine.render_to_string('t')
@setup({'static-statictag01': '{% load static %}{% static "admin/base.css" %}'}) @setup({'static-statictag01': '{% load static %}{% static "admin/base.css" %}'})
def test_static_statictag01(self): def test_static_statictag01(self):
output = self.engine.render_to_string('static-statictag01') output = self.engine.render_to_string('static-statictag01')
@ -56,3 +63,9 @@ class StaticTagTests(SimpleTestCase):
def test_static_quotes_urls(self): def test_static_quotes_urls(self):
output = self.engine.render_to_string('static-statictag05') output = self.engine.render_to_string('static-statictag05')
self.assertEqual(output, urljoin(settings.STATIC_URL, '/static/special%3Fchars%26quoted.html')) self.assertEqual(output, urljoin(settings.STATIC_URL, '/static/special%3Fchars%26quoted.html'))
@setup({'t': '{% load static %}{% static %}'})
def test_static_statictag_without_path(self):
msg = "'static' takes at least one argument (path to file)"
with self.assertRaisesMessage(TemplateSyntaxError, msg):
self.engine.render_to_string('t')