|
1 | 1 | import datetime |
2 | 2 | import unittest |
3 | 3 |
|
| 4 | +from django import template |
4 | 5 | from django.contrib.admin import ModelAdmin |
| 6 | +from django.contrib.admin.templatetags.admin_filters import truncated_unordered_list |
5 | 7 | from django.contrib.admin.templatetags.admin_list import date_hierarchy |
6 | 8 | from django.contrib.admin.templatetags.admin_modify import submit_row |
7 | 9 | from django.contrib.admin.templatetags.base import InclusionAdminNode |
8 | 10 | from django.contrib.auth import get_permission_codename |
9 | 11 | from django.contrib.auth.admin import UserAdmin |
10 | 12 | from django.contrib.auth.models import User |
11 | 13 | from django.template.base import Token, TokenType |
12 | | -from django.test import RequestFactory, TestCase |
| 14 | +from django.test import RequestFactory, SimpleTestCase, TestCase |
13 | 15 | from django.urls import reverse |
14 | 16 | from django.utils.version import PY314 |
15 | 17 |
|
|
18 | 20 | from .tests import AdminViewBasicTestCase, get_perm |
19 | 21 |
|
20 | 22 |
|
| 23 | +class TruncatedUnorderedListTests(SimpleTestCase): |
| 24 | + def test_no_max_items(self): |
| 25 | + result = truncated_unordered_list(["item 1", "item 2"], None) |
| 26 | + self.assertEqual(result, ("\t<li>item 1</li>\n" "\t<li>item 2</li>")) |
| 27 | + self.assertNotIn("more object", result) |
| 28 | + |
| 29 | + def test_max_items_zero(self): |
| 30 | + result = truncated_unordered_list(["a", "b", "c"], 0) |
| 31 | + self.assertEqual(result, "") |
| 32 | + self.assertNotIn("more object", result) |
| 33 | + |
| 34 | + def test_flat_list_truncated(self): |
| 35 | + self.assertEqual( |
| 36 | + truncated_unordered_list(["a", "b", "c", "d", "e"], 3), |
| 37 | + ( |
| 38 | + "\t<li>a</li>\n" |
| 39 | + "\t<li>b</li>\n" |
| 40 | + "\t<li>c</li>\n" |
| 41 | + "\t<li>…and 2 more objects.</li>" |
| 42 | + ), |
| 43 | + ) |
| 44 | + |
| 45 | + def test_nested_two_levels_truncated(self): |
| 46 | + self.assertEqual( |
| 47 | + truncated_unordered_list(["a", ["a1", "a2"], "b", "c", "d", "e"], 3), |
| 48 | + ( |
| 49 | + "\t<li>a\n" |
| 50 | + "\t<ul>\n" |
| 51 | + "\t\t<li>a1</li>\n" |
| 52 | + "\t\t<li>a2</li>\n" |
| 53 | + "\t</ul>\n" |
| 54 | + "\t</li>\n" |
| 55 | + "\t<li>…and 4 more objects.</li>" |
| 56 | + ), |
| 57 | + ) |
| 58 | + |
| 59 | + def test_nested_and_top_level_truncated(self): |
| 60 | + self.assertEqual( |
| 61 | + truncated_unordered_list( |
| 62 | + ["a", ["n1", "n2", "n3", "n4", "n5"], "b", "c", "d", "e"], |
| 63 | + 3, |
| 64 | + ), |
| 65 | + ( |
| 66 | + "\t<li>a\n" |
| 67 | + "\t<ul>\n" |
| 68 | + "\t\t<li>n1</li>\n" |
| 69 | + "\t\t<li>n2</li>\n" |
| 70 | + "\t</ul>\n" |
| 71 | + "\t</li>\n" |
| 72 | + "\t<li>…and 7 more objects.</li>" |
| 73 | + ), |
| 74 | + ) |
| 75 | + |
| 76 | + def test_nested_three_levels_truncated(self): |
| 77 | + self.assertEqual( |
| 78 | + truncated_unordered_list(["a", ["a1", ["a1x"]], "b", "c", "d", "e"], 3), |
| 79 | + ( |
| 80 | + "\t<li>a\n" |
| 81 | + "\t<ul>\n" |
| 82 | + "\t\t<li>a1\n" |
| 83 | + "\t\t<ul>\n" |
| 84 | + "\t\t\t<li>a1x</li>\n" |
| 85 | + "\t\t</ul>\n" |
| 86 | + "\t\t</li>\n" |
| 87 | + "\t</ul>\n" |
| 88 | + "\t</li>\n" |
| 89 | + "\t<li>…and 4 more objects.</li>" |
| 90 | + ), |
| 91 | + ) |
| 92 | + |
| 93 | + def test_max_items_equal_to_length(self): |
| 94 | + self.assertEqual( |
| 95 | + truncated_unordered_list(["a", "b", "c"], 3), |
| 96 | + ("\t<li>a</li>\n" "\t<li>b</li>\n" "\t<li>c</li>"), |
| 97 | + ) |
| 98 | + |
| 99 | + def test_max_items_greater_than_length(self): |
| 100 | + self.assertEqual( |
| 101 | + truncated_unordered_list(["a", "b"], 10), |
| 102 | + ("\t<li>a</li>\n" "\t<li>b</li>"), |
| 103 | + ) |
| 104 | + |
| 105 | + def test_truncated_single_remaining(self): |
| 106 | + self.assertEqual( |
| 107 | + truncated_unordered_list(["a", "b", "c"], 2), |
| 108 | + ("\t<li>a</li>\n" "\t<li>b</li>\n" "\t<li>…and 1 more object.</li>"), |
| 109 | + ) |
| 110 | + |
| 111 | + def test_autoescape(self): |
| 112 | + self.assertEqual( |
| 113 | + truncated_unordered_list(["<a>item</a>", "safe"], 1), |
| 114 | + ("\t<li><a>item</a></li>\n" "\t<li>…and 1 more object.</li>"), |
| 115 | + ) |
| 116 | + |
| 117 | + def test_autoescape_off(self): |
| 118 | + self.assertEqual( |
| 119 | + truncated_unordered_list(["<a>item</a>", "safe"], 1, autoescape=False), |
| 120 | + ("\t<li><a>item</a></li>\n" "\t<li>…and 1 more object.</li>"), |
| 121 | + ) |
| 122 | + |
| 123 | + def test_empty_list(self): |
| 124 | + self.assertEqual(truncated_unordered_list([], 5), "") |
| 125 | + |
| 126 | + def test_template_rendering(self): |
| 127 | + t = template.Template( |
| 128 | + "{% load admin_filters %}{{ items|truncated_unordered_list:2 }}" |
| 129 | + ) |
| 130 | + result = t.render(template.Context({"items": ["a", "b", "c"]})) |
| 131 | + self.assertIn("<li>a</li>", result) |
| 132 | + self.assertIn("<li>b</li>", result) |
| 133 | + self.assertIn("<li>…and 1 more object.</li>", result) |
| 134 | + self.assertNotIn("<li>c</li>", result) |
| 135 | + |
| 136 | + |
21 | 137 | class AdminTemplateTagsTest(AdminViewBasicTestCase): |
22 | 138 | request_factory = RequestFactory() |
23 | 139 |
|
|
0 commit comments