PageRenderTime 490ms CodeModel.GetById 251ms app.highlight 20ms RepoModel.GetById 216ms app.codeStats 0ms

/tests/regressiontests/admin_util/tests.py

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