Refs #27804 -- Used subTest() in filesizeformat tests and HumanizeTests.

This commit is contained in:
Jon Dufresne 2019-06-09 16:07:19 -07:00 committed by Mariusz Felisiak
parent dcb8f00d06
commit e065b29387
2 changed files with 57 additions and 37 deletions

View File

@ -31,10 +31,14 @@ class HumanizeTests(SimpleTestCase):
def humanize_tester(self, test_list, result_list, method, normalize_result_func=escape): def humanize_tester(self, test_list, result_list, method, normalize_result_func=escape):
for test_content, result in zip(test_list, result_list): for test_content, result in zip(test_list, result_list):
t = Template('{%% load humanize %%}{{ test_content|%s }}' % method) with self.subTest(test_content):
rendered = t.render(Context(locals())).strip() t = Template('{%% load humanize %%}{{ test_content|%s }}' % method)
self.assertEqual(rendered, normalize_result_func(result), rendered = t.render(Context(locals())).strip()
msg="%s test failed, produced '%s', should've produced '%s'" % (method, rendered, result)) self.assertEqual(
rendered,
normalize_result_func(result),
msg="%s test failed, produced '%s', should've produced '%s'" % (method, rendered, result)
)
def test_ordinal(self): def test_ordinal(self):
test_list = ('1', '2', '3', '4', '11', '12', test_list = ('1', '2', '3', '4', '11', '12',
@ -289,9 +293,10 @@ class HumanizeTests(SimpleTestCase):
humanize.datetime = DocumentedMockDateTime humanize.datetime = DocumentedMockDateTime
try: try:
for test_time_string, expected_natural_time in test_data: for test_time_string, expected_natural_time in test_data:
test_time = datetime.datetime.strptime(test_time_string, time_format) with self.subTest(test_time_string):
natural_time = humanize.naturaltime(test_time).replace('\xa0', ' ') test_time = datetime.datetime.strptime(test_time_string, time_format)
self.assertEqual(expected_natural_time, natural_time) natural_time = humanize.naturaltime(test_time).replace('\xa0', ' ')
self.assertEqual(expected_natural_time, natural_time)
finally: finally:
humanize.datetime = orig_humanize_datetime humanize.datetime = orig_humanize_datetime

View File

@ -6,38 +6,53 @@ from django.utils import translation
class FunctionTests(SimpleTestCase): class FunctionTests(SimpleTestCase):
def test_formats(self): def test_formats(self):
self.assertEqual(filesizeformat(1023), '1023\xa0bytes') tests = [
self.assertEqual(filesizeformat(1024), '1.0\xa0KB') (1023, '1023\xa0bytes'),
self.assertEqual(filesizeformat(10 * 1024), '10.0\xa0KB') (1024, '1.0\xa0KB'),
self.assertEqual(filesizeformat(1024 * 1024 - 1), '1024.0\xa0KB') (10 * 1024, '10.0\xa0KB'),
self.assertEqual(filesizeformat(1024 * 1024), '1.0\xa0MB') (1024 * 1024 - 1, '1024.0\xa0KB'),
self.assertEqual(filesizeformat(1024 * 1024 * 50), '50.0\xa0MB') (1024 * 1024, '1.0\xa0MB'),
self.assertEqual(filesizeformat(1024 * 1024 * 1024 - 1), '1024.0\xa0MB') (1024 * 1024 * 50, '50.0\xa0MB'),
self.assertEqual(filesizeformat(1024 * 1024 * 1024), '1.0\xa0GB') (1024 * 1024 * 1024 - 1, '1024.0\xa0MB'),
self.assertEqual(filesizeformat(1024 * 1024 * 1024 * 1024), '1.0\xa0TB') (1024 * 1024 * 1024, '1.0\xa0GB'),
self.assertEqual(filesizeformat(1024 * 1024 * 1024 * 1024 * 1024), '1.0\xa0PB') (1024 * 1024 * 1024 * 1024, '1.0\xa0TB'),
self.assertEqual(filesizeformat(1024 * 1024 * 1024 * 1024 * 1024 * 2000), '2000.0\xa0PB') (1024 * 1024 * 1024 * 1024 * 1024, '1.0\xa0PB'),
self.assertEqual(filesizeformat(complex(1, -1)), '0\xa0bytes') (1024 * 1024 * 1024 * 1024 * 1024 * 2000, '2000.0\xa0PB'),
self.assertEqual(filesizeformat(""), '0\xa0bytes') (complex(1, -1), '0\xa0bytes'),
self.assertEqual(filesizeformat("\N{GREEK SMALL LETTER ALPHA}"), '0\xa0bytes') ('', '0\xa0bytes'),
('\N{GREEK SMALL LETTER ALPHA}', '0\xa0bytes'),
]
for value, expected in tests:
with self.subTest(value=value):
self.assertEqual(filesizeformat(value), expected)
def test_localized_formats(self): def test_localized_formats(self):
tests = [
(1023, '1023\xa0Bytes'),
(1024, '1,0\xa0KB'),
(10 * 1024, '10,0\xa0KB'),
(1024 * 1024 - 1, '1024,0\xa0KB'),
(1024 * 1024, '1,0\xa0MB'),
(1024 * 1024 * 50, '50,0\xa0MB'),
(1024 * 1024 * 1024 - 1, '1024,0\xa0MB'),
(1024 * 1024 * 1024, '1,0\xa0GB'),
(1024 * 1024 * 1024 * 1024, '1,0\xa0TB'),
(1024 * 1024 * 1024 * 1024 * 1024, '1,0\xa0PB'),
(1024 * 1024 * 1024 * 1024 * 1024 * 2000, '2000,0\xa0PB'),
(complex(1, -1), '0\xa0Bytes'),
('', '0\xa0Bytes'),
('\N{GREEK SMALL LETTER ALPHA}', '0\xa0Bytes'),
]
with self.settings(USE_L10N=True), translation.override('de'): with self.settings(USE_L10N=True), translation.override('de'):
self.assertEqual(filesizeformat(1023), '1023\xa0Bytes') for value, expected in tests:
self.assertEqual(filesizeformat(1024), '1,0\xa0KB') with self.subTest(value=value):
self.assertEqual(filesizeformat(10 * 1024), '10,0\xa0KB') self.assertEqual(filesizeformat(value), expected)
self.assertEqual(filesizeformat(1024 * 1024 - 1), '1024,0\xa0KB')
self.assertEqual(filesizeformat(1024 * 1024), '1,0\xa0MB')
self.assertEqual(filesizeformat(1024 * 1024 * 50), '50,0\xa0MB')
self.assertEqual(filesizeformat(1024 * 1024 * 1024 - 1), '1024,0\xa0MB')
self.assertEqual(filesizeformat(1024 * 1024 * 1024), '1,0\xa0GB')
self.assertEqual(filesizeformat(1024 * 1024 * 1024 * 1024), '1,0\xa0TB')
self.assertEqual(filesizeformat(1024 * 1024 * 1024 * 1024 * 1024), '1,0\xa0PB')
self.assertEqual(filesizeformat(1024 * 1024 * 1024 * 1024 * 1024 * 2000), '2000,0\xa0PB')
self.assertEqual(filesizeformat(complex(1, -1)), '0\xa0Bytes')
self.assertEqual(filesizeformat(""), '0\xa0Bytes')
self.assertEqual(filesizeformat("\N{GREEK SMALL LETTER ALPHA}"), '0\xa0Bytes')
def test_negative_numbers(self): def test_negative_numbers(self):
self.assertEqual(filesizeformat(-100), '-100\xa0bytes') tests = [
self.assertEqual(filesizeformat(-1024 * 1024 * 50), '-50.0\xa0MB') (-100, '-100\xa0bytes'),
(-1024 * 1024 * 50, '-50.0\xa0MB'),
]
for value, expected in tests:
with self.subTest(value=value):
self.assertEqual(filesizeformat(value), expected)