PageRenderTime 67ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/Django-1.4.3/tests/regressiontests/admin_util/tests.py

https://bitbucket.org/ducopdep/tiny_blog
Python | 286 lines | 214 code | 45 blank | 27 comment | 4 complexity | b3212a7e505f2ce4987a06bfed432989 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. from __future__ import absolute_import
  2. from datetime import datetime
  3. from django.conf import settings
  4. from django.contrib import admin
  5. from django.contrib.admin import helpers
  6. from django.contrib.admin.util import (display_for_field, label_for_field,
  7. lookup_field, NestedObjects)
  8. from django.contrib.admin.views.main import EMPTY_CHANGELIST_VALUE
  9. from django.contrib.sites.models import Site
  10. from django.db import models, DEFAULT_DB_ALIAS
  11. from django import forms
  12. from django.test import TestCase
  13. from django.utils import unittest
  14. from django.utils.formats import localize
  15. from django.utils.safestring import mark_safe
  16. from .models import Article, Count, Event, Location
  17. class NestedObjectsTests(TestCase):
  18. """
  19. Tests for ``NestedObject`` utility collection.
  20. """
  21. def setUp(self):
  22. self.n = NestedObjects(using=DEFAULT_DB_ALIAS)
  23. self.objs = [Count.objects.create(num=i) for i in range(5)]
  24. def _check(self, target):
  25. self.assertEqual(self.n.nested(lambda obj: obj.num), target)
  26. def _connect(self, i, j):
  27. self.objs[i].parent = self.objs[j]
  28. self.objs[i].save()
  29. def _collect(self, *indices):
  30. self.n.collect([self.objs[i] for i in indices])
  31. def test_unrelated_roots(self):
  32. self._connect(2, 1)
  33. self._collect(0)
  34. self._collect(1)
  35. self._check([0, 1, [2]])
  36. def test_siblings(self):
  37. self._connect(1, 0)
  38. self._connect(2, 0)
  39. self._collect(0)
  40. self._check([0, [1, 2]])
  41. def test_non_added_parent(self):
  42. self._connect(0, 1)
  43. self._collect(0)
  44. self._check([0])
  45. def test_cyclic(self):
  46. self._connect(0, 2)
  47. self._connect(1, 0)
  48. self._connect(2, 1)
  49. self._collect(0)
  50. self._check([0, [1, [2]]])
  51. def test_queries(self):
  52. self._connect(1, 0)
  53. self._connect(2, 0)
  54. # 1 query to fetch all children of 0 (1 and 2)
  55. # 1 query to fetch all children of 1 and 2 (none)
  56. # Should not require additional queries to populate the nested graph.
  57. self.assertNumQueries(2, self._collect, 0)
  58. class UtilTests(unittest.TestCase):
  59. def test_values_from_lookup_field(self):
  60. """
  61. Regression test for #12654: lookup_field
  62. """
  63. SITE_NAME = 'example.com'
  64. TITLE_TEXT = 'Some title'
  65. CREATED_DATE = datetime.min
  66. ADMIN_METHOD = 'admin method'
  67. SIMPLE_FUNCTION = 'function'
  68. INSTANCE_ATTRIBUTE = 'attr'
  69. class MockModelAdmin(object):
  70. def get_admin_value(self, obj):
  71. return ADMIN_METHOD
  72. simple_function = lambda obj: SIMPLE_FUNCTION
  73. article = Article(
  74. site=Site(domain=SITE_NAME),
  75. title=TITLE_TEXT,
  76. created=CREATED_DATE,
  77. )
  78. article.non_field = INSTANCE_ATTRIBUTE
  79. verifications = (
  80. ('site', SITE_NAME),
  81. ('created', localize(CREATED_DATE)),
  82. ('title', TITLE_TEXT),
  83. ('get_admin_value', ADMIN_METHOD),
  84. (simple_function, SIMPLE_FUNCTION),
  85. ('test_from_model', article.test_from_model()),
  86. ('non_field', INSTANCE_ATTRIBUTE)
  87. )
  88. mock_admin = MockModelAdmin()
  89. for name, value in verifications:
  90. field, attr, resolved_value = lookup_field(name, article, mock_admin)
  91. if field is not None:
  92. resolved_value = display_for_field(resolved_value, field)
  93. self.assertEqual(value, resolved_value)
  94. def test_null_display_for_field(self):
  95. """
  96. Regression test for #12550: display_for_field should handle None
  97. value.
  98. """
  99. display_value = display_for_field(None, models.CharField())
  100. self.assertEqual(display_value, EMPTY_CHANGELIST_VALUE)
  101. display_value = display_for_field(None, models.CharField(
  102. choices=(
  103. (None, "test_none"),
  104. )
  105. ))
  106. self.assertEqual(display_value, "test_none")
  107. display_value = display_for_field(None, models.DateField())
  108. self.assertEqual(display_value, EMPTY_CHANGELIST_VALUE)
  109. display_value = display_for_field(None, models.TimeField())
  110. self.assertEqual(display_value, EMPTY_CHANGELIST_VALUE)
  111. # Regression test for #13071: NullBooleanField has special
  112. # handling.
  113. display_value = display_for_field(None, models.NullBooleanField())
  114. expected = u'<img src="%sadmin/img/icon-unknown.gif" alt="None" />' % settings.STATIC_URL
  115. self.assertEqual(display_value, expected)
  116. display_value = display_for_field(None, models.DecimalField())
  117. self.assertEqual(display_value, EMPTY_CHANGELIST_VALUE)
  118. display_value = display_for_field(None, models.FloatField())
  119. self.assertEqual(display_value, EMPTY_CHANGELIST_VALUE)
  120. def test_label_for_field(self):
  121. """
  122. Tests for label_for_field
  123. """
  124. self.assertEqual(
  125. label_for_field("title", Article),
  126. "title"
  127. )
  128. self.assertEqual(
  129. label_for_field("title2", Article),
  130. "another name"
  131. )
  132. self.assertEqual(
  133. label_for_field("title2", Article, return_attr=True),
  134. ("another name", None)
  135. )
  136. self.assertEqual(
  137. label_for_field("__unicode__", Article),
  138. "article"
  139. )
  140. self.assertEqual(
  141. label_for_field("__str__", Article),
  142. "article"
  143. )
  144. self.assertRaises(
  145. AttributeError,
  146. lambda: label_for_field("unknown", Article)
  147. )
  148. def test_callable(obj):
  149. return "nothing"
  150. self.assertEqual(
  151. label_for_field(test_callable, Article),
  152. "Test callable"
  153. )
  154. self.assertEqual(
  155. label_for_field(test_callable, Article, return_attr=True),
  156. ("Test callable", test_callable)
  157. )
  158. self.assertEqual(
  159. label_for_field("test_from_model", Article),
  160. "Test from model"
  161. )
  162. self.assertEqual(
  163. label_for_field("test_from_model", Article, return_attr=True),
  164. ("Test from model", Article.test_from_model)
  165. )
  166. self.assertEqual(
  167. label_for_field("test_from_model_with_override", Article),
  168. "not What you Expect"
  169. )
  170. self.assertEqual(
  171. label_for_field(lambda x: "nothing", Article),
  172. "--"
  173. )
  174. class MockModelAdmin(object):
  175. def test_from_model(self, obj):
  176. return "nothing"
  177. test_from_model.short_description = "not Really the Model"
  178. self.assertEqual(
  179. label_for_field("test_from_model", Article, model_admin=MockModelAdmin),
  180. "not Really the Model"
  181. )
  182. self.assertEqual(
  183. label_for_field("test_from_model", Article,
  184. model_admin = MockModelAdmin,
  185. return_attr = True
  186. ),
  187. ("not Really the Model", MockModelAdmin.test_from_model)
  188. )
  189. def test_related_name(self):
  190. """
  191. Regression test for #13963
  192. """
  193. self.assertEqual(
  194. label_for_field('location', Event, return_attr=True),
  195. ('location', None),
  196. )
  197. self.assertEqual(
  198. label_for_field('event', Location, return_attr=True),
  199. ('awesome event', None),
  200. )
  201. self.assertEqual(
  202. label_for_field('guest', Event, return_attr=True),
  203. ('awesome guest', None),
  204. )
  205. def test_logentry_unicode(self):
  206. """
  207. Regression test for #15661
  208. """
  209. log_entry = admin.models.LogEntry()
  210. log_entry.action_flag = admin.models.ADDITION
  211. self.assertTrue(
  212. unicode(log_entry).startswith('Added ')
  213. )
  214. log_entry.action_flag = admin.models.CHANGE
  215. self.assertTrue(
  216. unicode(log_entry).startswith('Changed ')
  217. )
  218. log_entry.action_flag = admin.models.DELETION
  219. self.assertTrue(
  220. unicode(log_entry).startswith('Deleted ')
  221. )
  222. def test_safestring_in_field_label(self):
  223. # safestring should not be escaped
  224. class MyForm(forms.Form):
  225. text = forms.CharField(label=mark_safe('<i>text</i>'))
  226. cb = forms.BooleanField(label=mark_safe('<i>cb</i>'))
  227. form = MyForm()
  228. self.assertEqual(helpers.AdminField(form, 'text', is_first=False).label_tag(),
  229. '<label for="id_text" class="required inline"><i>text</i>:</label>')
  230. self.assertEqual(helpers.AdminField(form, 'cb', is_first=False).label_tag(),
  231. '<label for="id_cb" class="vCheckboxLabel required inline"><i>cb</i></label>')
  232. # normal strings needs to be escaped
  233. class MyForm(forms.Form):
  234. text = forms.CharField(label='&text')
  235. cb = forms.BooleanField(label='&cb')
  236. form = MyForm()
  237. self.assertEqual(helpers.AdminField(form, 'text', is_first=False).label_tag(),
  238. '<label for="id_text" class="required inline">&amp;text:</label>')
  239. self.assertEqual(helpers.AdminField(form, 'cb', is_first=False).label_tag(),
  240. '<label for="id_cb" class="vCheckboxLabel required inline">&amp;cb</label>')