mybuddy/babybuddy/tests/formats/tests_en_gb.py

71 lines
2.6 KiB
Python
Raw Normal View History

2021-11-14 01:20:20 +00:00
# -*- coding: utf-8 -*-
import datetime
from django.core.exceptions import ValidationError
from django.forms.fields import DateTimeField
2021-11-15 16:56:59 +00:00
from django.test import TestCase, override_settings # , tag
2021-11-14 01:20:20 +00:00
from django.utils.formats import date_format, time_format
from babybuddy.middleware import update_en_gb_date_formats
class GbFormatsTestCase(TestCase):
2022-02-10 00:00:30 +00:00
@override_settings(LANGUAGE_CODE="en-GB")
2021-11-14 01:20:20 +00:00
def test_datetime_input_formats(self):
update_en_gb_date_formats()
field = DateTimeField()
supported_custom_examples = [
2022-02-10 00:00:30 +00:00
"20/01/2020",
"20/01/2020 9:30 AM",
"20/01/2020 9:30:03 AM",
"01/10/2020 11:30 PM",
"01/10/2020 11:30:03 AM",
2021-11-14 01:20:20 +00:00
]
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!")
2021-11-14 01:20:20 +00:00
2021-11-15 16:56:59 +00:00
# @tag('isolate')
2022-02-10 00:00:30 +00:00
@override_settings(LANGUAGE_CODE="en-GB", USE_24_HOUR_TIME_FORMAT=True)
2021-11-14 01:20:20 +00:00
def test_use_24_hour_time_format(self):
update_en_gb_date_formats()
field = DateTimeField()
supported_custom_examples = [
2022-02-10 00:00:30 +00:00
"25/10/2006 2:30:59",
"25/10/2006 2:30",
"25/10/2006 14:30:59",
"25/10/2006 14:30",
2021-11-14 01:20:20 +00:00
]
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!")
2021-11-14 01:20:20 +00:00
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"), "4 November 2011 23:05:59")
2021-11-14 01:20:20 +00:00
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"), "04/11/2011 02:05")
2021-11-14 01:20:20 +00:00
t = datetime.time(hour=16, minute=2, second=25)
2022-02-10 00:00:30 +00:00
self.assertEqual(time_format(t), "16:02")
2021-11-14 01:20:20 +00:00
# def test_short_month_day_format(self):
# update_en_gb_date_formats()
# dt = datetime.datetime(year=2021, month=7, day=31, hour=5, minute=5,
# second=5)
# self.assertEqual(date_format(dt, 'SHORT_MONTH_DAY_FORMAT'), '31 Jul')