2022-03-23 11:15:36 +00:00
|
|
|
from datetime import date, datetime, timezone
|
2016-04-09 17:17:34 +00:00
|
|
|
|
2020-02-12 13:48:49 +00:00
|
|
|
from django.core.exceptions import ValidationError
|
|
|
|
from django.forms import DateTimeField
|
2016-04-09 17:17:34 +00:00
|
|
|
from django.test import SimpleTestCase
|
2022-03-23 11:15:36 +00:00
|
|
|
from django.utils.timezone import get_fixed_timezone
|
2016-04-09 17:17:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
class DateTimeFieldTest(SimpleTestCase):
|
2019-10-10 08:06:01 +00:00
|
|
|
def test_datetimefield_clean(self):
|
|
|
|
tests = [
|
|
|
|
(date(2006, 10, 25), datetime(2006, 10, 25, 0, 0)),
|
|
|
|
(datetime(2006, 10, 25, 14, 30), datetime(2006, 10, 25, 14, 30)),
|
|
|
|
(datetime(2006, 10, 25, 14, 30, 59), datetime(2006, 10, 25, 14, 30, 59)),
|
|
|
|
(
|
|
|
|
datetime(2006, 10, 25, 14, 30, 59, 200),
|
|
|
|
datetime(2006, 10, 25, 14, 30, 59, 200),
|
|
|
|
),
|
|
|
|
("2006-10-25 14:30:45.000200", datetime(2006, 10, 25, 14, 30, 45, 200)),
|
|
|
|
("2006-10-25 14:30:45.0002", datetime(2006, 10, 25, 14, 30, 45, 200)),
|
|
|
|
("2006-10-25 14:30:45", datetime(2006, 10, 25, 14, 30, 45)),
|
|
|
|
("2006-10-25 14:30:00", datetime(2006, 10, 25, 14, 30)),
|
|
|
|
("2006-10-25 14:30", datetime(2006, 10, 25, 14, 30)),
|
|
|
|
("2006-10-25", datetime(2006, 10, 25, 0, 0)),
|
|
|
|
("10/25/2006 14:30:45.000200", datetime(2006, 10, 25, 14, 30, 45, 200)),
|
|
|
|
("10/25/2006 14:30:45", datetime(2006, 10, 25, 14, 30, 45)),
|
|
|
|
("10/25/2006 14:30:00", datetime(2006, 10, 25, 14, 30)),
|
|
|
|
("10/25/2006 14:30", datetime(2006, 10, 25, 14, 30)),
|
|
|
|
("10/25/2006", datetime(2006, 10, 25, 0, 0)),
|
|
|
|
("10/25/06 14:30:45.000200", datetime(2006, 10, 25, 14, 30, 45, 200)),
|
|
|
|
("10/25/06 14:30:45", datetime(2006, 10, 25, 14, 30, 45)),
|
|
|
|
("10/25/06 14:30:00", datetime(2006, 10, 25, 14, 30)),
|
|
|
|
("10/25/06 14:30", datetime(2006, 10, 25, 14, 30)),
|
|
|
|
("10/25/06", datetime(2006, 10, 25, 0, 0)),
|
2019-10-09 10:08:50 +00:00
|
|
|
# ISO 8601 formats.
|
|
|
|
(
|
|
|
|
"2014-09-23T22:34:41.614804",
|
|
|
|
datetime(2014, 9, 23, 22, 34, 41, 614804),
|
|
|
|
),
|
|
|
|
("2014-09-23T22:34:41", datetime(2014, 9, 23, 22, 34, 41)),
|
|
|
|
("2014-09-23T22:34", datetime(2014, 9, 23, 22, 34)),
|
|
|
|
("2014-09-23", datetime(2014, 9, 23, 0, 0)),
|
2022-03-23 11:15:36 +00:00
|
|
|
("2014-09-23T22:34Z", datetime(2014, 9, 23, 22, 34, tzinfo=timezone.utc)),
|
2019-10-09 10:08:50 +00:00
|
|
|
(
|
|
|
|
"2014-09-23T22:34+07:00",
|
|
|
|
datetime(2014, 9, 23, 22, 34, tzinfo=get_fixed_timezone(420)),
|
|
|
|
),
|
2019-10-10 08:06:01 +00:00
|
|
|
# Whitespace stripping.
|
|
|
|
(" 2006-10-25 14:30:45 ", datetime(2006, 10, 25, 14, 30, 45)),
|
|
|
|
(" 2006-10-25 ", datetime(2006, 10, 25, 0, 0)),
|
|
|
|
(" 10/25/2006 14:30:45 ", datetime(2006, 10, 25, 14, 30, 45)),
|
|
|
|
(" 10/25/2006 14:30 ", datetime(2006, 10, 25, 14, 30)),
|
|
|
|
(" 10/25/2006 ", datetime(2006, 10, 25, 0, 0)),
|
|
|
|
(" 10/25/06 14:30:45 ", datetime(2006, 10, 25, 14, 30, 45)),
|
|
|
|
(" 10/25/06 ", datetime(2006, 10, 25, 0, 0)),
|
2019-10-09 10:08:50 +00:00
|
|
|
(
|
|
|
|
" 2014-09-23T22:34:41.614804 ",
|
|
|
|
datetime(2014, 9, 23, 22, 34, 41, 614804),
|
|
|
|
),
|
2022-03-23 11:15:36 +00:00
|
|
|
(" 2014-09-23T22:34Z ", datetime(2014, 9, 23, 22, 34, tzinfo=timezone.utc)),
|
2019-10-10 08:06:01 +00:00
|
|
|
]
|
2016-04-09 17:17:34 +00:00
|
|
|
f = DateTimeField()
|
2019-10-10 08:06:01 +00:00
|
|
|
for value, expected_datetime in tests:
|
|
|
|
with self.subTest(value=value):
|
|
|
|
self.assertEqual(f.clean(value), expected_datetime)
|
|
|
|
|
|
|
|
def test_datetimefield_clean_invalid(self):
|
|
|
|
f = DateTimeField()
|
|
|
|
msg = "'Enter a valid date/time.'"
|
|
|
|
with self.assertRaisesMessage(ValidationError, msg):
|
2016-04-09 17:17:34 +00:00
|
|
|
f.clean("hello")
|
2019-10-10 08:06:01 +00:00
|
|
|
with self.assertRaisesMessage(ValidationError, msg):
|
2016-04-09 17:17:34 +00:00
|
|
|
f.clean("2006-10-25 4:30 p.m.")
|
2019-10-10 08:06:01 +00:00
|
|
|
with self.assertRaisesMessage(ValidationError, msg):
|
|
|
|
f.clean(" ")
|
2019-10-09 10:08:50 +00:00
|
|
|
with self.assertRaisesMessage(ValidationError, msg):
|
|
|
|
f.clean("2014-09-23T28:23")
|
2016-04-09 17:17:34 +00:00
|
|
|
f = DateTimeField(input_formats=["%Y %m %d %I:%M %p"])
|
2019-10-10 08:06:01 +00:00
|
|
|
with self.assertRaisesMessage(ValidationError, msg):
|
2019-10-09 10:08:50 +00:00
|
|
|
f.clean("2006.10.25 14:30:45")
|
2016-04-09 17:17:34 +00:00
|
|
|
|
2019-10-10 08:06:01 +00:00
|
|
|
def test_datetimefield_clean_input_formats(self):
|
|
|
|
tests = [
|
|
|
|
(
|
|
|
|
"%Y %m %d %I:%M %p",
|
|
|
|
(
|
|
|
|
(date(2006, 10, 25), datetime(2006, 10, 25, 0, 0)),
|
|
|
|
(datetime(2006, 10, 25, 14, 30), datetime(2006, 10, 25, 14, 30)),
|
2022-02-03 19:24:19 +00:00
|
|
|
(
|
2019-10-10 08:06:01 +00:00
|
|
|
datetime(2006, 10, 25, 14, 30, 59),
|
|
|
|
datetime(2006, 10, 25, 14, 30, 59),
|
2022-02-03 19:24:19 +00:00
|
|
|
),
|
|
|
|
(
|
2019-10-10 08:06:01 +00:00
|
|
|
datetime(2006, 10, 25, 14, 30, 59, 200),
|
|
|
|
datetime(2006, 10, 25, 14, 30, 59, 200),
|
2022-02-03 19:24:19 +00:00
|
|
|
),
|
2019-10-10 08:06:01 +00:00
|
|
|
("2006 10 25 2:30 PM", datetime(2006, 10, 25, 14, 30)),
|
2019-10-09 10:08:50 +00:00
|
|
|
# ISO-like formats are always accepted.
|
|
|
|
("2006-10-25 14:30:45", datetime(2006, 10, 25, 14, 30, 45)),
|
2019-10-10 08:06:01 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"%Y.%m.%d %H:%M:%S.%f",
|
|
|
|
(
|
2022-02-03 19:24:19 +00:00
|
|
|
(
|
2019-10-10 08:06:01 +00:00
|
|
|
"2006.10.25 14:30:45.0002",
|
|
|
|
datetime(2006, 10, 25, 14, 30, 45, 200),
|
2022-02-03 19:24:19 +00:00
|
|
|
),
|
2019-10-10 08:06:01 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
]
|
|
|
|
for input_format, values in tests:
|
|
|
|
f = DateTimeField(input_formats=[input_format])
|
|
|
|
for value, expected_datetime in values:
|
|
|
|
with self.subTest(value=value, input_format=input_format):
|
|
|
|
self.assertEqual(f.clean(value), expected_datetime)
|
|
|
|
|
|
|
|
def test_datetimefield_not_required(self):
|
2016-04-09 17:17:34 +00:00
|
|
|
f = DateTimeField(required=False)
|
|
|
|
self.assertIsNone(f.clean(None))
|
|
|
|
self.assertEqual("None", repr(f.clean(None)))
|
|
|
|
self.assertIsNone(f.clean(""))
|
|
|
|
self.assertEqual("None", repr(f.clean("")))
|
|
|
|
|
|
|
|
def test_datetimefield_changed(self):
|
2019-10-10 08:06:01 +00:00
|
|
|
f = DateTimeField(input_formats=["%Y %m %d %I:%M %p"])
|
|
|
|
d = datetime(2006, 9, 17, 14, 30, 0)
|
2016-04-09 17:17:34 +00:00
|
|
|
self.assertFalse(f.has_changed(d, "2006 09 17 2:30 PM"))
|