| | 1 | # Quick tests for the humanize templatefilters (django.contrib.humanize) |
|---|
| | 2 | |
|---|
| | 3 | from django.template import Template, Context, add_to_builtins |
|---|
| | 4 | from django.conf import settings |
|---|
| | 5 | from django.utils.dateformat import DateFormat |
|---|
| | 6 | from django.utils.translation import gettext as _ |
|---|
| | 7 | from datetime import timedelta, date |
|---|
| | 8 | import unittest |
|---|
| | 9 | |
|---|
| | 10 | add_to_builtins('django.contrib.humanize.templatetags.humanize') |
|---|
| | 11 | |
|---|
| | 12 | class TemplateTest(unittest.TestCase): |
|---|
| | 13 | def test_naturalday(self): |
|---|
| | 14 | |
|---|
| | 15 | today = date.today() |
|---|
| | 16 | yesterday = today - timedelta(days=1) |
|---|
| | 17 | tomorrow = today + timedelta(days=1) |
|---|
| | 18 | someday = today - timedelta(days=10) |
|---|
| | 19 | notdate = "I'm not a date value" |
|---|
| | 20 | |
|---|
| | 21 | t_today = Template('{{ today|naturalday }}') |
|---|
| | 22 | t_tomorrow = Template('{{ tomorrow|naturalday }}') |
|---|
| | 23 | t_yesterday = Template('{{ yesterday|naturalday }}') |
|---|
| | 24 | t_someday = Template('{{ someday|naturalday }}') |
|---|
| | 25 | t_someday_custom = Template('{{ someday|naturalday:"d.m.Y" }}') |
|---|
| | 26 | t_notdate = Template('{{ notdate|naturalday }}') |
|---|
| | 27 | |
|---|
| | 28 | rendered_today = t_today.render(Context(locals())).strip() |
|---|
| | 29 | self.assertEqual(rendered_today, _('today')) |
|---|
| | 30 | |
|---|
| | 31 | rendered_yesterday = t_yesterday.render(Context(locals())).strip() |
|---|
| | 32 | self.assertEqual(rendered_yesterday, _('yesterday')) |
|---|
| | 33 | |
|---|
| | 34 | rendered_tomorrow = t_tomorrow.render(Context(locals())).strip() |
|---|
| | 35 | self.assertEqual(rendered_tomorrow, _('tomorrow')) |
|---|
| | 36 | |
|---|
| | 37 | rendered_someday = t_someday.render(Context(locals())).strip() |
|---|
| | 38 | self.assertEqual(rendered_someday, DateFormat(someday).format(settings.DATE_FORMAT)) |
|---|
| | 39 | |
|---|
| | 40 | rendered_someday_custom = t_someday_custom.render(Context(locals())).strip() |
|---|
| | 41 | self.assertEqual(rendered_someday_custom, DateFormat(someday).format('d.m.Y')) |
|---|
| | 42 | |
|---|
| | 43 | rendered_notdate = t_notdate.render(Context(locals())).strip() |
|---|
| | 44 | self.assertEqual(rendered_notdate, "I'm not a date value") |
|---|
| | 45 | |