| 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_notdate = Template('{{ notdate|naturalday }}') |
| 26 | |
| 27 | rendered_today = t_today.render(Context(locals())).strip() |
| 28 | self.assertEqual(rendered_today, _('today')) |
| 29 | |
| 30 | rendered_yesterday = t_yesterday.render(Context(locals())).strip() |
| 31 | self.assertEqual(rendered_yesterday, _('yesterday')) |
| 32 | |
| 33 | rendered_tomorrow = t_tomorrow.render(Context(locals())).strip() |
| 34 | self.assertEqual(rendered_tomorrow, _('tomorrow')) |
| 35 | |
| 36 | rendered_someday = t_someday.render(Context(locals())).strip() |
| 37 | self.assertEqual(rendered_someday, DateFormat(someday).format(settings.DATE_FORMAT)) |
| 38 | |
| 39 | rendered_notdate = t_notdate.render(Context(locals())).strip() |
| 40 | self.assertEqual(rendered_notdate, "I'm not a date value") |
| 41 | |