Ticket #12654: test.diff

File test.diff, 2.8 KB (added by Karataev Pavel, 15 years ago)

Better tests

  • tests/regressiontests/admin_util/tests.py

     
    33from django.db import models
    44
    55from django.contrib import admin
    6 from django.contrib.admin.util import display_for_field, label_for_field
     6from django.contrib.admin.util import display_for_field, label_for_field, lookup_field
    77from django.contrib.admin.views.main import EMPTY_CHANGELIST_VALUE
    88
    99from models import Article
     
    1111
    1212
    1313class UtilTests(unittest.TestCase):
     14    def test_values_from_lookup_field(self):
     15        """
     16        Regression test for #12654: lookup_field
     17        """
     18        from django.contrib.sites.models import Site
     19        from django.utils.formats import localize
     20        from datetime import datetime
    1421
     22        SITE_NAME = 'example.com'
     23        TITLE_TEXT = 'Some title'
     24        CREATED_DATE = datetime.min
     25        ADMIN_METHOD = 'admin method'
     26        SIMPLE_FUNCTION = 'function'
     27        INSTANCE_ATTRIBUTE = 'attr'
     28
     29        class FakeModelAdmin(object):
     30            def get_admin_value(self, obj):
     31                return ADMIN_METHOD
     32
     33        simple_function = lambda obj: SIMPLE_FUNCTION
     34
     35        article = Article(
     36            site=Site(domain=SITE_NAME),
     37            title=TITLE_TEXT,
     38            created=CREATED_DATE,
     39        )
     40        article.non_field = INSTANCE_ATTRIBUTE
     41
     42        verifications = (
     43            ('site', SITE_NAME),
     44            ('created', localize(CREATED_DATE)),
     45            ('title', TITLE_TEXT),
     46            ('get_admin_value', ADMIN_METHOD),
     47            (simple_function, SIMPLE_FUNCTION),
     48            ('test_from_model', article.test_from_model()),
     49            ('non_field', INSTANCE_ATTRIBUTE)
     50        )
     51
     52        for name, value in verifications:
     53            field, attr, resolved_value = lookup_field(
     54                name,
     55                article,
     56                FakeModelAdmin()
     57            )
     58
     59            if field is not None:
     60                resolved_value = display_for_field(resolved_value, field)
     61
     62            self.assertEqual(value, resolved_value)
     63
    1564    def test_null_display_for_field(self):
    1665        """
    1766        Regression test for #12550: display_for_field should handle None
  • tests/regressiontests/admin_util/models.py

     
    66    """
    77    A simple Article model for testing
    88    """
     9    site = models.ForeignKey('sites.Site')
    910    title = models.CharField(max_length=100)
    1011    title2 = models.CharField(max_length=100, verbose_name="another name")
    1112    created = models.DateTimeField()
Back to Top