From 7abe7b2a5f8a1f4db9da8acb1559d044ccd099aa Mon Sep 17 00:00:00 2001 From: "Christopher C. Wells" Date: Wed, 22 Jul 2020 21:56:29 -0700 Subject: [PATCH] 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. --- babybuddy/tests/tests_formats.py | 35 +++++++++++++++++++++++++++++++- core/tests/tests_templatetags.py | 3 ++- gulpfile.js | 15 +++++++++++--- 3 files changed, 48 insertions(+), 5 deletions(-) diff --git a/babybuddy/tests/tests_formats.py b/babybuddy/tests/tests_formats.py index 8089bc59..23fd58e8 100644 --- a/babybuddy/tests/tests_formats.py +++ b/babybuddy/tests/tests_formats.py @@ -3,7 +3,8 @@ import datetime from django.core.exceptions import ValidationError 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): @@ -25,3 +26,35 @@ class FormatsTestCase(TestCase): with self.assertRaises(ValidationError): 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') diff --git a/core/tests/tests_templatetags.py b/core/tests/tests_templatetags.py index f2322a39..a54435fa 100644 --- a/core/tests/tests_templatetags.py +++ b/core/tests/tests_templatetags.py @@ -64,7 +64,8 @@ class TemplateTagsTestCase(TestCase): def test_datetimepicker_format(self): self.assertEqual(datetimepicker.datetimepicker_format(), '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): self.assertEqual(datetimepicker.datetimepicker_format(), 'L HH:mm') diff --git a/gulpfile.js b/gulpfile.js index 0e812043..c0677a80 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -40,7 +40,9 @@ function coverage(cb) { 'coverage', 'run', 'manage.py', - 'test' + 'test', + '--exclude-tag', + 'isolate' ], { stdio: 'inherit' @@ -160,12 +162,19 @@ function styles(cb) { } /** - * Runs all tests. + * Runs all tests _not_ tagged "isolate". * * @param 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)); spawn('pipenv', command, { stdio: 'inherit' }).on('exit', cb); }