Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ answer newbie questions, and generally made Django that much better:
btoll@bestweb.net
C8E
Caio Ariede <caio.ariede@gmail.com>
Caitie Baca <caitlin.baca@yahoo.com>
Calvin Spealman <ironfroggy@gmail.com>
Cameron Curry
Cameron Knight (ckknight)
Expand Down
4 changes: 2 additions & 2 deletions django/contrib/auth/management/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,10 @@ def _rename(self, apps, schema_editor, old_model, new_model):
from django.contrib.auth.models import Permission

db = schema_editor.connection.alias
ctypes = ContentType.objects.filter(
ctypes = ContentType.objects.using(db).filter(
app_label=self.app_label, model__icontains=old_model.lower()
)
for permission in Permission.objects.filter(
for permission in Permission.objects.using(db).filter(
content_type_id__in=ctypes.values("id")
):
prefix = permission.codename.split("_")[0]
Expand Down
20 changes: 15 additions & 5 deletions django/test/testcases.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
from django.forms.fields import CharField
from django.http import QueryDict
from django.http.request import split_domain_port, validate_host
from django.template.base import PartialTemplate
from django.test.client import AsyncClient, Client
from django.test.html import HTMLParseError, parse_html
from django.test.signals import template_rendered
Expand Down Expand Up @@ -138,10 +139,22 @@ def on_template_render(self, sender, signal, template, context, **kwargs):
self.rendered_templates.append(template)
self.context.append(copy(context))

@property
def rendered_template_names(self):
return [
(
f"{t.origin.template_name}#{t.name}"
if isinstance(t, PartialTemplate)
else t.name
)
for t in self.rendered_templates
if t.name is not None
]

def test(self):
self.test_case._assert_template_used(
self.template_name,
[t.name for t in self.rendered_templates if t.name is not None],
self.rendered_template_names,
self.msg_prefix,
self.count,
)
Expand All @@ -159,11 +172,8 @@ def __exit__(self, exc_type, exc_value, traceback):

class _AssertTemplateNotUsedContext(_AssertTemplateUsedContext):
def test(self):
rendered_template_names = [
t.name for t in self.rendered_templates if t.name is not None
]
self.test_case.assertFalse(
self.template_name in rendered_template_names,
self.template_name in self.rendered_template_names,
f"{self.msg_prefix}Template '{self.template_name}' was used "
f"unexpectedly in rendering the response",
)
Expand Down
21 changes: 21 additions & 0 deletions tests/auth_tests/test_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -1546,6 +1546,7 @@ class PermissionRenameOperationsTests(TransactionTestCase):
"django.contrib.auth",
"auth_tests",
]
databases = {"default", "other"}

def setUp(self):
app_config = apps.get_app_config("auth_tests")
Expand Down Expand Up @@ -1605,6 +1606,26 @@ def test_permission_rename(self):
Permission.objects.filter(codename=f"{action}_newmodel").exists()
)

def test_permission_rename_other_db(self):
ct = ContentType.objects.using("default").create(
app_label="auth_tests", model="oldmodel"
)
permission = Permission.objects.using("default").create(
codename="add_oldmodel",
name="Can add old model",
content_type=ct,
)
# RenamePermission respects the database.
call_command("migrate", "auth_tests", verbosity=0, database="other")
permission.refresh_from_db()
self.assertEqual(permission.codename, "add_oldmodel")
self.assertFalse(
Permission.objects.using("other").filter(codename="add_oldmodel").exists()
)
self.assertTrue(
Permission.objects.using("other").filter(codename="add_newmodel").exists()
)

@mock.patch(
"django.db.router.allow_migrate_model",
return_value=False,
Expand Down
3 changes: 3 additions & 0 deletions tests/test_utils/templates/template_used/partials.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{% partialdef hello %}
<p>Hello</p>
{% endpartialdef %}
47 changes: 47 additions & 0 deletions tests/test_utils/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,53 @@ def test_assert_used_on_http_response(self):
self.assertTemplateNotUsed(response, "template.html")


@override_settings(ROOT_URLCONF="test_utils.urls")
class AssertTemplateUsedPartialTests(SimpleTestCase):
def test_template_used_pass(self):
with self.assertTemplateUsed("template_used/partials.html#hello"):
render_to_string("template_used/partials.html#hello")

def test_template_not_used_pass(self):
with self.assertTemplateNotUsed("hello"):
render_to_string("template_used/partials.html#hello")

def test_template_used_fail(self):
msg = "Template 'hello' was not a template used to render the response."
with (
self.assertRaisesMessage(AssertionError, msg),
self.assertTemplateUsed("hello"),
):
render_to_string("template_used/base.html")

def test_template_not_used_fail(self):
msg = (
"Template 'template_used/partials.html#hello' was used "
"unexpectedly in rendering the response"
)
with (
self.assertRaisesMessage(AssertionError, msg),
self.assertTemplateNotUsed("template_used/partials.html#hello"),
):
render_to_string("template_used/partials.html#hello")

def test_template_not_used_pass_non_partial(self):
with self.assertTemplateNotUsed(
"template_used/base.html#template_used/base.html"
):
render_to_string("template_used/base.html")

def test_template_used_fail_non_partial(self):
msg = (
"Template 'template_used/base.html#template_used/base.html' was not a "
"template used to render the response."
)
with (
self.assertRaisesMessage(AssertionError, msg),
self.assertTemplateUsed("template_used/base.html#template_used/base.html"),
):
render_to_string("template_used/base.html")


class HTMLEqualTests(SimpleTestCase):
def test_html_parser(self):
element = parse_html("<div><p>Hello</p></div>")
Expand Down
Loading