mybuddy/babybuddy/tests/formats/tests_en_us.py

68 lines
2.5 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
import datetime
from django.core.exceptions import ValidationError
from django.forms.fields import DateTimeField
from django.test import TestCase, override_settings, tag
from django.utils.formats import date_format, time_format
from babybuddy.middleware import update_en_us_date_formats
class FormatsTestCase(TestCase):
def test_datetime_input_formats(self):
update_en_us_date_formats()
field = DateTimeField()
supported_custom_examples = [
2022-02-10 00:00:30 +00:00
"01/20/2020 9:30 AM",
"01/20/2020 9:30:03 AM",
"10/01/2020 11:30 PM",
"10/01/2020 11:30:03 AM",
]
for example in supported_custom_examples:
try:
result = field.to_python(example)
self.assertIsInstance(result, datetime.datetime)
except ValidationError:
self.fail('Format of "{}" not recognized!'.format(example))
with self.assertRaises(ValidationError):
2022-02-10 00:00:30 +00:00
field.to_python("invalid date string!")
2022-02-10 00:00:30 +00:00
@tag("isolate")
@override_settings(LANGUAGE_CODE="en-US", USE_24_HOUR_TIME_FORMAT=True)
def test_use_24_hour_time_format(self):
update_en_us_date_formats()
field = DateTimeField()
supported_custom_examples = [
2022-02-10 00:00:30 +00:00
"10/25/2006 2:30:59",
"10/25/2006 2:30",
"10/25/2006 14:30:59",
"10/25/2006 14:30",
]
for example in supported_custom_examples:
try:
result = field.to_python(example)
self.assertIsInstance(result, datetime.datetime)
except ValidationError:
self.fail('Format of "{}" not recognized!'.format(example))
with self.assertRaises(ValidationError):
2022-02-10 00:00:30 +00:00
field.to_python("invalid date string!")
2022-02-10 00:00:30 +00:00
dt = datetime.datetime(year=2011, month=11, day=4, hour=23, minute=5, second=59)
self.assertEqual(date_format(dt, "DATETIME_FORMAT"), "Nov. 4, 2011, 23:05:59")
2022-02-10 00:00:30 +00:00
dt = datetime.datetime(year=2011, month=11, day=4, hour=2, minute=5, second=59)
self.assertEqual(date_format(dt, "SHORT_DATETIME_FORMAT"), "11/04/2011 2:05:59")
t = datetime.time(hour=16, minute=2, second=25)
2022-02-10 00:00:30 +00:00
self.assertEqual(time_format(t), "16:02:25")
2021-08-01 03:09:14 +00:00
def test_short_month_day_format(self):
update_en_us_date_formats()
2022-02-10 00:00:30 +00:00
dt = datetime.datetime(year=2021, month=7, day=31, hour=5, minute=5, second=5)
self.assertEqual(date_format(dt, "SHORT_MONTH_DAY_FORMAT"), "Jul 31")