Add test for USE_24_HOUR_TIME_FORMAT (#148)

Note: this new test has been tagged "isolate" and excluded from regular tests
because the locale behavior being tested involves Django internals that must
be handled at initialization and cannot be changed during runtime (i.e. this
new test with PASS when run alone, but FAIL when run with other tests).

A future commit or set of commits should come up with a way to execute all
tests tagged "isolate" in isolated test environments as part of the full test
suite. For now, this test simply does not run until it is run manually.
This commit is contained in:
Christopher C. Wells 2020-07-22 21:56:29 -07:00
parent 23ac4ade91
commit 7abe7b2a5f
3 changed files with 48 additions and 5 deletions

View File

@ -3,7 +3,8 @@ import datetime
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.forms.fields import DateTimeField from django.forms.fields import DateTimeField
from django.test import TestCase from django.test import TestCase, override_settings, tag
from django.utils.formats import date_format, time_format
class FormatsTestCase(TestCase): class FormatsTestCase(TestCase):
@ -25,3 +26,35 @@ class FormatsTestCase(TestCase):
with self.assertRaises(ValidationError): with self.assertRaises(ValidationError):
field.to_python('invalid date string!') field.to_python('invalid date string!')
@tag('isolate')
@override_settings(LANGUAGE_CODE='en', USE_24_HOUR_TIME_FORMAT=True)
def test_use_24_hour_time_format_en(self):
field = DateTimeField()
supported_custom_examples = [
'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):
field.to_python('invalid date string!')
dt = datetime.datetime.fromisoformat('2011-11-04 23:05:59')
self.assertEqual(
date_format(dt, 'DATETIME_FORMAT'), 'Nov. 4, 2011, 23:05:59')
dt = datetime.datetime.fromisoformat('2011-11-04 02:05:59')
self.assertEqual(
date_format(dt, 'SHORT_DATETIME_FORMAT'), '11/04/2011 2:05:59')
t = datetime.time.fromisoformat('16:02:25')
self.assertEqual(time_format(t), '16:02:25')

View File

@ -64,7 +64,8 @@ class TemplateTagsTestCase(TestCase):
def test_datetimepicker_format(self): def test_datetimepicker_format(self):
self.assertEqual(datetimepicker.datetimepicker_format(), 'L LT') self.assertEqual(datetimepicker.datetimepicker_format(), 'L LT')
self.assertEqual(datetimepicker.datetimepicker_format('L LT'), 'L LT') self.assertEqual(datetimepicker.datetimepicker_format('L LT'), 'L LT')
self.assertEqual(datetimepicker.datetimepicker_format('L LTS'), 'L LTS') self.assertEqual(
datetimepicker.datetimepicker_format('L LTS'), 'L LTS')
with self.settings(USE_24_HOUR_TIME_FORMAT=True): with self.settings(USE_24_HOUR_TIME_FORMAT=True):
self.assertEqual(datetimepicker.datetimepicker_format(), 'L HH:mm') self.assertEqual(datetimepicker.datetimepicker_format(), 'L HH:mm')

View File

@ -40,7 +40,9 @@ function coverage(cb) {
'coverage', 'coverage',
'run', 'run',
'manage.py', 'manage.py',
'test' 'test',
'--exclude-tag',
'isolate'
], ],
{ {
stdio: 'inherit' stdio: 'inherit'
@ -160,12 +162,19 @@ function styles(cb) {
} }
/** /**
* Runs all tests. * Runs all tests _not_ tagged "isolate".
* *
* @param cb * @param cb
*/ */
function test(cb) { function test(cb) {
var command = ['run', 'python', 'manage.py', 'test']; var command = [
'run',
'python',
'manage.py',
'test',
'--exclude-tag',
'isolate'
];
command = command.concat(process.argv.splice(3)); command = command.concat(process.argv.splice(3));
spawn('pipenv', command, { stdio: 'inherit' }).on('exit', cb); spawn('pipenv', command, { stdio: 'inherit' }).on('exit', cb);
} }