diff --git a/.gitignore b/.gitignore index eacf8123..2b92ab27 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ *.sw[po] .coverage .eggs +.testmondata .tox build dist diff --git a/tests/test_filesize.py b/tests/test_filesize.py index 74a05aea..cf50a95e 100644 --- a/tests/test_filesize.py +++ b/tests/test_filesize.py @@ -2,55 +2,34 @@ """Tests for filesize humanizing.""" +import pytest from humanize import filesize -from .base import HumanizeTestCase - -class FilesizeTestCase(HumanizeTestCase): - def test_naturalsize(self): - tests = ( - 300, - 3000, - 3000000, - 3000000000, - 3000000000000, - (300, True), - (3000, True), - (3000000, True), - (300, False, True), - (3000, False, True), - (3000000, False, True), - (1024, False, True), - (10 ** 26 * 30, False, True), - (10 ** 26 * 30, True), - 10 ** 26 * 30, - (1, False, False), - (3141592, False, False, "%.2f"), - (3000, False, True, "%.3f"), - (3000000000, False, True, "%.0f"), - (10 ** 26 * 30, True, False, "%.3f"), - ) - results = ( - "300 Bytes", - "3.0 kB", - "3.0 MB", - "3.0 GB", - "3.0 TB", - "300 Bytes", - "2.9 KiB", - "2.9 MiB", - "300B", - "2.9K", - "2.9M", - "1.0K", - "2481.5Y", - "2481.5 YiB", - "3000.0 YB", - "1 Byte", - "3.14 MB", - "2.930K", - "3G", - "2481.542 YiB", - ) - self.assertManyResults(filesize.naturalsize, tests, results) +@pytest.mark.parametrize( + "test_args, expected", + [ + ([300], "300 Bytes"), + ([3000], "3.0 kB"), + ([3000000], "3.0 MB"), + ([3000000000], "3.0 GB"), + ([3000000000000], "3.0 TB"), + ([300, True], "300 Bytes"), + ([3000, True], "2.9 KiB"), + ([3000000, True], "2.9 MiB"), + ([300, False, True], "300B"), + ([3000, False, True], "2.9K"), + ([3000000, False, True], "2.9M"), + ([1024, False, True], "1.0K"), + ([10 ** 26 * 30, False, True], "2481.5Y"), + ([10 ** 26 * 30, True], "2481.5 YiB"), + ([10 ** 26 * 30], "3000.0 YB"), + ([1, False, False], "1 Byte"), + ([3141592, False, False, "%.2f"], "3.14 MB"), + ([3000, False, True, "%.3f"], "2.930K"), + ([3000000000, False, True, "%.0f"], "3G"), + ([10 ** 26 * 30, True, False, "%.3f"], "2481.542 YiB"), + ], +) +def test_naturaltime_minimum_unit_default(test_args, expected): + assert filesize.naturalsize(*test_args) == expected diff --git a/tests/test_number.py b/tests/test_number.py index 66db1469..886bf192 100644 --- a/tests/test_number.py +++ b/tests/test_number.py @@ -2,120 +2,110 @@ """Number tests.""" +import pytest from humanize import number -from .base import HumanizeTestCase +@pytest.mark.parametrize( + "test_input, expected", + [ + ("1", "1st"), + ("2", "2nd"), + ("3", "3rd"), + ("4", "4th"), + ("11", "11th"), + ("12", "12th"), + ("13", "13th"), + ("101", "101st"), + ("102", "102nd"), + ("103", "103rd"), + ("111", "111th"), + ("something else", "something else"), + (None, None), + ], +) +def test_ordinal(test_input, expected): + assert number.ordinal(test_input) == expected -class NumberTestCase(HumanizeTestCase): - def test_ordinal(self): - test_list = ( - "1", - "2", - "3", - "4", - "11", - "12", - "13", - "101", - "102", - "103", - "111", - "something else", - None, - ) - result_list = ( - "1st", - "2nd", - "3rd", - "4th", - "11th", - "12th", - "13th", - "101st", - "102nd", - "103rd", - "111th", - "something else", - None, - ) - self.assertManyResults(number.ordinal, test_list, result_list) - def test_intcomma(self): - test_list = ( - 100, - 1000, - 10123, - 10311, - 1000000, - 1234567.25, - "100", - "1000", - "10123", - "10311", - "1000000", - "1234567.1234567", - None, - ) - result_list = ( - "100", - "1,000", - "10,123", - "10,311", - "1,000,000", - "1,234,567.25", - "100", - "1,000", - "10,123", - "10,311", - "1,000,000", - "1,234,567.1234567", - None, - ) - self.assertManyResults(number.intcomma, test_list, result_list) +@pytest.mark.parametrize( + "test_input, expected", + [ + (100, "100"), + (1000, "1,000"), + (10123, "10,123"), + (10311, "10,311"), + (1000000, "1,000,000"), + (1234567.25, "1,234,567.25"), + ("100", "100"), + ("1000", "1,000"), + ("10123", "10,123"), + ("10311", "10,311"), + ("1000000", "1,000,000"), + ("1234567.1234567", "1,234,567.1234567"), + (None, None), + ], +) +def test_intcomma(test_input, expected): + assert number.intcomma(test_input) == expected - def test_intword(self): - # make sure that powers & human_powers have the same number of items - self.assertEqual(len(number.powers), len(number.human_powers)) - # test the result of intword - test_list = ( - "100", - "1000000", - "1200000", - "1290000", - "1000000000", - "2000000000", - "6000000000000", - "1300000000000000", - "3500000000000000000000", - "8100000000000000000000000000000000", - None, - ("1230000", "%0.2f"), - 10 ** 101, - ) - result_list = ( - "100", - "1.0 million", - "1.2 million", - "1.3 million", - "1.0 billion", - "2.0 billion", - "6.0 trillion", - "1.3 quadrillion", - "3.5 sextillion", - "8.1 decillion", - None, - "1.23 million", - "1" + "0" * 101, - ) - self.assertManyResults(number.intword, test_list, result_list) - def test_apnumber(self): - test_list = (1, 2, 4, 5, 9, 10, "7", None) - result_list = ("one", "two", "four", "five", "nine", "10", "seven", None) - self.assertManyResults(number.apnumber, test_list, result_list) +def test_intword_powers(): + # make sure that powers & human_powers have the same number of items + assert len(number.powers) == len(number.human_powers) - def test_fractional(self): - test_list = (1, 2.0, (4.0 / 3.0), (5.0 / 6.0), "7", "8.9", "ten", None) - result_list = ("1", "2", "1 1/3", "5/6", "7", "8 9/10", "ten", None) - self.assertManyResults(number.fractional, test_list, result_list) + +@pytest.mark.parametrize( + "test_args, expected", + [ + (["100"], "100"), + (["1000000"], "1.0 million"), + (["1200000"], "1.2 million"), + (["1290000"], "1.3 million"), + (["1000000000"], "1.0 billion"), + (["2000000000"], "2.0 billion"), + (["6000000000000"], "6.0 trillion"), + (["1300000000000000"], "1.3 quadrillion"), + (["3500000000000000000000"], "3.5 sextillion"), + (["8100000000000000000000000000000000"], "8.1 decillion"), + ([None], None), + (["1230000", "%0.2f"], "1.23 million"), + ([10 ** 101], "1" + "0" * 101), + ], +) +def test_intword(test_args, expected): + assert number.intword(*test_args) == expected + + +@pytest.mark.parametrize( + "test_input, expected", + [ + (1, "one"), + (2, "two"), + (4, "four"), + (5, "five"), + (9, "nine"), + (10, "10"), + ("7", "seven"), + (None, None), + ], +) +def test_apnumber(test_input, expected): + assert number.apnumber(test_input) == expected + + +@pytest.mark.parametrize( + "test_input, expected", + [ + (1, "1"), + (2.0, "2"), + (4.0 / 3.0, "1 1/3"), + (5.0 / 6.0, "5/6"), + ("7", "7"), + ("8.9", "8 9/10"), + ("ten", "ten"), + (None, None), + ], +) +def test_fractional(test_input, expected): + assert number.fractional(test_input) == expected diff --git a/tests/test_time.py b/tests/test_time.py index 3ce02bea..7217c640 100644 --- a/tests/test_time.py +++ b/tests/test_time.py @@ -14,13 +14,18 @@ ONE_DAY_DELTA = dt.timedelta(days=1) # In seconds -ONE_YEAR = 31556952 -ONE_DAY = 86400 -ONE_HOUR = 3600 -FOUR_MILLISECONDS = 4 / 1000 -ONE_MILLISECOND = 1 / 1000 -FOUR_MICROSECONDS = 4 / 1000000 ONE_MICROSECOND = 1 / 1000000 +FOUR_MICROSECONDS = 4 / 1000000 +ONE_MILLISECOND = 1 / 1000 +FOUR_MILLISECONDS = 4 / 1000 +ONE_HOUR = 3600 +ONE_DAY = 24 * ONE_HOUR +ONE_YEAR = 365.25 * ONE_DAY + +with freeze_time("2020-02-02"): + TODAY = dt.date.today() + TOMORROW = TODAY + ONE_DAY_DELTA + YESTERDAY = TODAY - ONE_DAY_DELTA class FakeDate: @@ -274,82 +279,69 @@ def nt_nomonths(d): self.assertManyResults(nt_nomonths, test_list, result_list) - @freeze_time("2020-02-02") - def test_naturalday(self): - # Arrange - today = dt.date.today() - tomorrow = today + ONE_DAY_DELTA - yesterday = today - ONE_DAY_DELTA - - someday = dt.date(today.year, 3, 5) - someday_result = "Mar 05" - - valerrtest = FakeDate(290149024, 2, 2) - overflowtest = FakeDate(120390192341, 2, 2) - - test_list = ( - today, - tomorrow, - yesterday, - someday, - "02/26/1984", - (dt.date(1982, 6, 27), "%Y.%m.%d"), - None, - "Not a date at all.", - valerrtest, - overflowtest, - ) - result_list = ( - "today", - "tomorrow", - "yesterday", - someday_result, - "02/26/1984", - "1982.06.27", - None, - "Not a date at all.", - valerrtest, - overflowtest, - ) - - # Act / Assert - self.assertManyResults(time.naturalday, test_list, result_list) - - @freeze_time("2020-02-02") - def test_naturaldate(self): - # Arrange - today = dt.date.today() - tomorrow = today + ONE_DAY_DELTA - yesterday = today - ONE_DAY_DELTA - - someday = dt.date(today.year, 3, 5) - someday_result = "Mar 05" - - test_list = ( - today, - tomorrow, - yesterday, - someday, - dt.date(1982, 6, 27), - None, - "Not a date at all.", - VALUE_ERROR_TEST, - OVERFLOW_ERROR_TEST, - ) - result_list = ( - "today", - "tomorrow", - "yesterday", - someday_result, - "Jun 27 1982", - None, - "Not a date at all.", - VALUE_ERROR_TEST, - OVERFLOW_ERROR_TEST, - ) - - # Act / Assert - self.assertManyResults(time.naturaldate, test_list, result_list) + +@freeze_time("2020-02-02") +@pytest.mark.parametrize( + "test_args, expected", + [ + ([TODAY], "today"), + ([TOMORROW], "tomorrow"), + ([YESTERDAY], "yesterday"), + ([dt.date(TODAY.year, 3, 5)], "Mar 05"), + (["02/26/1984"], "02/26/1984"), + ([dt.date(1982, 6, 27), "%Y.%m.%d"], "1982.06.27"), + ([None], None), + (["Not a date at all."], "Not a date at all."), + ([VALUE_ERROR_TEST], VALUE_ERROR_TEST), + ([OVERFLOW_ERROR_TEST], OVERFLOW_ERROR_TEST), + ], +) +def test_naturalday(test_args, expected): + assert time.naturalday(*test_args) == expected + + +@freeze_time("2020-02-02") +@pytest.mark.parametrize( + "test_input, expected", + [ + (TODAY, "today"), + (TOMORROW, "tomorrow"), + (YESTERDAY, "yesterday"), + (dt.date(TODAY.year, 3, 5), "Mar 05"), + (dt.date(1982, 6, 27), "Jun 27 1982"), + (None, None), + ("Not a date at all.", "Not a date at all."), + (VALUE_ERROR_TEST, VALUE_ERROR_TEST), + (OVERFLOW_ERROR_TEST, OVERFLOW_ERROR_TEST), + (dt.date(2019, 2, 2), "Feb 02 2019"), + (dt.date(2019, 3, 2), "Mar 02"), + (dt.date(2019, 4, 2), "Apr 02"), + (dt.date(2019, 5, 2), "May 02"), + (dt.date(2019, 6, 2), "Jun 02"), + (dt.date(2019, 7, 2), "Jul 02"), + (dt.date(2019, 8, 2), "Aug 02"), + (dt.date(2019, 9, 2), "Sep 02"), + (dt.date(2019, 10, 2), "Oct 02"), + (dt.date(2019, 11, 2), "Nov 02"), + (dt.date(2019, 12, 2), "Dec 02"), + (dt.date(2020, 1, 2), "Jan 02"), + (dt.date(2020, 2, 2), "today"), + (dt.date(2020, 3, 2), "Mar 02"), + (dt.date(2020, 4, 2), "Apr 02"), + (dt.date(2020, 5, 2), "May 02"), + (dt.date(2020, 6, 2), "Jun 02"), + (dt.date(2020, 7, 2), "Jul 02"), + (dt.date(2020, 8, 2), "Aug 02"), + (dt.date(2020, 9, 2), "Sep 02"), + (dt.date(2020, 10, 2), "Oct 02"), + (dt.date(2020, 11, 2), "Nov 02"), + (dt.date(2020, 12, 2), "Dec 02"), + (dt.date(2021, 1, 2), "Jan 02"), + (dt.date(2021, 2, 2), "Feb 02 2021"), + ], +) +def test_naturaldate(test_input, expected): + assert time.naturaldate(test_input) == expected @pytest.mark.parametrize(