From a6c6e2811fbf162d8e5cf62a3489b60220f5b2c4 Mon Sep 17 00:00:00 2001 From: Hugo Date: Fri, 14 Feb 2020 17:48:53 +0200 Subject: [PATCH] Convert remaining tests to use pytest --- tests/base.py | 24 --- tests/test_time.py | 407 ++++++++++++++++++--------------------------- 2 files changed, 163 insertions(+), 268 deletions(-) delete mode 100644 tests/base.py diff --git a/tests/base.py b/tests/base.py deleted file mode 100644 index 80a7f70f..00000000 --- a/tests/base.py +++ /dev/null @@ -1,24 +0,0 @@ -#!/usr/bin/env python - -"""Tests base classes.""" - -from unittest import TestCase - - -class HumanizeTestCase(TestCase): - def assertManyResults(self, function, args, results): - """Goes through a list of arguments and makes sure that function called - upon them lists a similarly ordered list of results. If more than one - argument is required, each position in args may be a tuple.""" - for arg, result in zip(args, results): - if isinstance(arg, tuple): - self.assertEqual(function(*arg), result) - else: - self.assertEqual(function(arg), result) - - def assertEqualDatetime(self, dt1, dt2): - self.assertEqual((dt1 - dt2).seconds, 0) - - def assertEqualTimedelta(self, td1, td2): - self.assertEqual(td1.days, td2.days) - self.assertEqual(td1.seconds, td2.seconds) diff --git a/tests/test_time.py b/tests/test_time.py index 7217c640..1a9ae9ce 100644 --- a/tests/test_time.py +++ b/tests/test_time.py @@ -3,14 +3,11 @@ """Tests for time humanizing.""" import datetime as dt -from unittest.mock import patch import pytest from freezegun import freeze_time from humanize import time -from .base import HumanizeTestCase - ONE_DAY_DELTA = dt.timedelta(days=1) # In seconds @@ -23,6 +20,7 @@ ONE_YEAR = 365.25 * ONE_DAY with freeze_time("2020-02-02"): + NOW = dt.datetime.now() TODAY = dt.date.today() TOMORROW = TODAY + ONE_DAY_DELTA YESTERDAY = TODAY - ONE_DAY_DELTA @@ -37,247 +35,168 @@ def __init__(self, year, month, day): OVERFLOW_ERROR_TEST = FakeDate(120390192341, 2, 2) -class TimeUtilitiesTestCase(HumanizeTestCase): - """These are not considered "public" interfaces, but require tests anyway.""" - - def test_date_and_delta(self): - now = dt.datetime.now() - td = dt.timedelta - int_tests = (3, 29, 86399, 86400, 86401 * 30) - date_tests = [now - td(seconds=x) for x in int_tests] - td_tests = [td(seconds=x) for x in int_tests] - results = [(now - td(seconds=x), td(seconds=x)) for x in int_tests] - for t in (int_tests, date_tests, td_tests): - for arg, result in zip(t, results): - date, d = time.date_and_delta(arg) - self.assertEqualDatetime(date, result[0]) - self.assertEqualTimedelta(d, result[1]) - self.assertEqual(time.date_and_delta("NaN"), (None, "NaN")) - - -class TimeTestCase(HumanizeTestCase): - """Tests for the public interface of humanize.time""" - - def test_naturaldelta_nomonths(self): - now = dt.datetime.now() - test_list = [ - dt.timedelta(days=7), - dt.timedelta(days=31), - dt.timedelta(days=230), - dt.timedelta(days=400), - ] - result_list = [ - "7 days", - "31 days", - "230 days", - "1 year, 35 days", - ] - with patch("humanize.time._now") as mocked: - mocked.return_value = now - - def nd_nomonths(d): - return time.naturaldelta(d, months=False) - - self.assertManyResults(nd_nomonths, test_list, result_list) - - def test_naturaldelta(self): - now = dt.datetime.now() - test_list = [ - 0, - 1, - 30, - dt.timedelta(minutes=1, seconds=30), - dt.timedelta(minutes=2), - dt.timedelta(hours=1, minutes=30, seconds=30), - dt.timedelta(hours=23, minutes=50, seconds=50), - dt.timedelta(days=1), - dt.timedelta(days=500), - dt.timedelta(days=365 * 2 + 35), - dt.timedelta(seconds=1), - dt.timedelta(seconds=30), - dt.timedelta(minutes=1, seconds=30), - dt.timedelta(minutes=2), - dt.timedelta(hours=1, minutes=30, seconds=30), - dt.timedelta(hours=23, minutes=50, seconds=50), - dt.timedelta(days=1), - dt.timedelta(days=500), - dt.timedelta(days=365 * 2 + 35), - # regression tests for bugs in post-release humanize - dt.timedelta(days=10000), - dt.timedelta(days=365 + 35), - 30, - dt.timedelta(days=365 * 2 + 65), - dt.timedelta(days=365 + 4), - dt.timedelta(days=35), - dt.timedelta(days=65), - dt.timedelta(days=9), - dt.timedelta(days=365), - "NaN", - ] - result_list = [ - "a moment", - "a second", - "30 seconds", - "a minute", - "2 minutes", - "an hour", - "23 hours", - "a day", - "1 year, 4 months", - "2 years", - "a second", - "30 seconds", - "a minute", - "2 minutes", - "an hour", - "23 hours", - "a day", - "1 year, 4 months", - "2 years", - "27 years", - "1 year, 1 month", - "30 seconds", - "2 years", - "1 year, 4 days", - "a month", - "2 months", - "9 days", - "a year", - "NaN", - ] - with patch("humanize.time._now") as mocked: - mocked.return_value = now - self.assertManyResults(time.naturaldelta, test_list, result_list) - - def test_naturaltime(self): - now = dt.datetime.now() - test_list = [ - now, - now - dt.timedelta(seconds=1), - now - dt.timedelta(seconds=30), - now - dt.timedelta(minutes=1, seconds=30), - now - dt.timedelta(minutes=2), - now - dt.timedelta(hours=1, minutes=30, seconds=30), - now - dt.timedelta(hours=23, minutes=50, seconds=50), - now - dt.timedelta(days=1), - now - dt.timedelta(days=500), - now - dt.timedelta(days=365 * 2 + 35), - now + dt.timedelta(seconds=1), - now + dt.timedelta(seconds=30), - now + dt.timedelta(minutes=1, seconds=30), - now + dt.timedelta(minutes=2), - now + dt.timedelta(hours=1, minutes=30, seconds=30), - now + dt.timedelta(hours=23, minutes=50, seconds=50), - now + dt.timedelta(days=1), - now + dt.timedelta(days=500), - now + dt.timedelta(days=365 * 2 + 35), - # regression tests for bugs in post-release humanize - now + dt.timedelta(days=10000), - now - dt.timedelta(days=365 + 35), - 30, - now - dt.timedelta(days=365 * 2 + 65), - now - dt.timedelta(days=365 + 4), - "NaN", - ] - result_list = [ - "now", - "a second ago", - "30 seconds ago", - "a minute ago", - "2 minutes ago", - "an hour ago", - "23 hours ago", - "a day ago", - "1 year, 4 months ago", - "2 years ago", - "a second from now", - "30 seconds from now", - "a minute from now", - "2 minutes from now", - "an hour from now", - "23 hours from now", - "a day from now", - "1 year, 4 months from now", - "2 years from now", - "27 years from now", - "1 year, 1 month ago", - "30 seconds ago", - "2 years ago", - "1 year, 4 days ago", - "NaN", - ] - with patch("humanize.time._now") as mocked: - mocked.return_value = now - self.assertManyResults(time.naturaltime, test_list, result_list) - - def test_naturaltime_nomonths(self): - now = dt.datetime.now() - test_list = [ - now, - now - dt.timedelta(seconds=1), - now - dt.timedelta(seconds=30), - now - dt.timedelta(minutes=1, seconds=30), - now - dt.timedelta(minutes=2), - now - dt.timedelta(hours=1, minutes=30, seconds=30), - now - dt.timedelta(hours=23, minutes=50, seconds=50), - now - dt.timedelta(days=1), - now - dt.timedelta(days=17), - now - dt.timedelta(days=47), - now - dt.timedelta(days=500), - now - dt.timedelta(days=365 * 2 + 35), - now + dt.timedelta(seconds=1), - now + dt.timedelta(seconds=30), - now + dt.timedelta(minutes=1, seconds=30), - now + dt.timedelta(minutes=2), - now + dt.timedelta(hours=1, minutes=30, seconds=30), - now + dt.timedelta(hours=23, minutes=50, seconds=50), - now + dt.timedelta(days=1), - now + dt.timedelta(days=500), - now + dt.timedelta(days=365 * 2 + 35), - # regression tests for bugs in post-release humanize - now + dt.timedelta(days=10000), - now - dt.timedelta(days=365 + 35), - 30, - now - dt.timedelta(days=365 * 2 + 65), - now - dt.timedelta(days=365 + 4), - "NaN", - ] - result_list = [ - "now", - "a second ago", - "30 seconds ago", - "a minute ago", - "2 minutes ago", - "an hour ago", - "23 hours ago", - "a day ago", - "17 days ago", - "47 days ago", - "1 year, 135 days ago", - "2 years ago", - "a second from now", - "30 seconds from now", - "a minute from now", - "2 minutes from now", - "an hour from now", - "23 hours from now", - "a day from now", - "1 year, 135 days from now", - "2 years from now", - "27 years from now", - "1 year, 35 days ago", - "30 seconds ago", - "2 years ago", - "1 year, 4 days ago", - "NaN", - ] - with patch("humanize.time._now") as mocked: - mocked.return_value = now - - def nt_nomonths(d): - return time.naturaltime(d, months=False) - - self.assertManyResults(nt_nomonths, test_list, result_list) +def assertEqualDatetime(dt1, dt2): + assert (dt1 - dt2).seconds == 0 + + +def assertEqualTimedelta(td1, td2): + assert td1.days == td2.days + assert td1.seconds == td2.seconds + + +# These are not considered "public" interfaces, but require tests anyway. + + +def test_date_and_delta(): + now = dt.datetime.now() + td = dt.timedelta + int_tests = (3, 29, 86399, 86400, 86401 * 30) + date_tests = [now - td(seconds=x) for x in int_tests] + td_tests = [td(seconds=x) for x in int_tests] + results = [(now - td(seconds=x), td(seconds=x)) for x in int_tests] + for t in (int_tests, date_tests, td_tests): + for arg, result in zip(t, results): + date, d = time.date_and_delta(arg) + assertEqualDatetime(date, result[0]) + assertEqualTimedelta(d, result[1]) + assert time.date_and_delta("NaN") == (None, "NaN") + + +# Tests for the public interface of humanize.time + + +def nd_nomonths(d): + return time.naturaldelta(d, months=False) + + +@pytest.mark.parametrize( + "test_input, expected", + [ + (dt.timedelta(days=7), "7 days"), + (dt.timedelta(days=31), "31 days"), + (dt.timedelta(days=230), "230 days"), + (dt.timedelta(days=400), "1 year, 35 days"), + ], +) +def test_naturaldelta_nomonths(test_input, expected): + assert nd_nomonths(test_input) == expected + + +@pytest.mark.parametrize( + "test_input, expected", + [ + (0, "a moment"), + (1, "a second"), + (30, "30 seconds"), + (dt.timedelta(minutes=1, seconds=30), "a minute"), + (dt.timedelta(minutes=2), "2 minutes"), + (dt.timedelta(hours=1, minutes=30, seconds=30), "an hour"), + (dt.timedelta(hours=23, minutes=50, seconds=50), "23 hours"), + (dt.timedelta(days=1), "a day"), + (dt.timedelta(days=500), "1 year, 4 months"), + (dt.timedelta(days=365 * 2 + 35), "2 years"), + (dt.timedelta(seconds=1), "a second"), + (dt.timedelta(seconds=30), "30 seconds"), + (dt.timedelta(minutes=1, seconds=30), "a minute"), + (dt.timedelta(minutes=2), "2 minutes"), + (dt.timedelta(hours=1, minutes=30, seconds=30), "an hour"), + (dt.timedelta(hours=23, minutes=50, seconds=50), "23 hours"), + (dt.timedelta(days=1), "a day"), + (dt.timedelta(days=500), "1 year, 4 months"), + (dt.timedelta(days=365 * 2 + 35), "2 years"), + # regression tests for bugs in post-release humanize + (dt.timedelta(days=10000), "27 years"), + (dt.timedelta(days=365 + 35), "1 year, 1 month"), + (30, "30 seconds"), + (dt.timedelta(days=365 * 2 + 65), "2 years"), + (dt.timedelta(days=365 + 4), "1 year, 4 days"), + (dt.timedelta(days=35), "a month"), + (dt.timedelta(days=65), "2 months"), + (dt.timedelta(days=9), "9 days"), + (dt.timedelta(days=365), "a year"), + ("NaN", "NaN"), + ], +) +def test_naturaldelta(test_input, expected): + assert time.naturaldelta(test_input) == expected + + +@freeze_time("2020-02-02") +@pytest.mark.parametrize( + "test_input, expected", + [ + (NOW, "now"), + (NOW - dt.timedelta(seconds=1), "a second ago"), + (NOW - dt.timedelta(seconds=30), "30 seconds ago"), + (NOW - dt.timedelta(minutes=1, seconds=30), "a minute ago"), + (NOW - dt.timedelta(minutes=2), "2 minutes ago"), + (NOW - dt.timedelta(hours=1, minutes=30, seconds=30), "an hour ago"), + (NOW - dt.timedelta(hours=23, minutes=50, seconds=50), "23 hours ago"), + (NOW - dt.timedelta(days=1), "a day ago"), + (NOW - dt.timedelta(days=500), "1 year, 4 months ago"), + (NOW - dt.timedelta(days=365 * 2 + 35), "2 years ago"), + (NOW + dt.timedelta(seconds=1), "a second from now"), + (NOW + dt.timedelta(seconds=30), "30 seconds from now"), + (NOW + dt.timedelta(minutes=1, seconds=30), "a minute from now"), + (NOW + dt.timedelta(minutes=2), "2 minutes from now"), + (NOW + dt.timedelta(hours=1, minutes=30, seconds=30), "an hour from now"), + (NOW + dt.timedelta(hours=23, minutes=50, seconds=50), "23 hours from now"), + (NOW + dt.timedelta(days=1), "a day from now"), + (NOW + dt.timedelta(days=500), "1 year, 4 months from now"), + (NOW + dt.timedelta(days=365 * 2 + 35), "2 years from now"), + # regression tests for bugs in post-release humanize + (NOW + dt.timedelta(days=10000), "27 years from now"), + (NOW - dt.timedelta(days=365 + 35), "1 year, 1 month ago"), + (30, "30 seconds ago"), + (NOW - dt.timedelta(days=365 * 2 + 65), "2 years ago"), + (NOW - dt.timedelta(days=365 + 4), "1 year, 4 days ago"), + ("NaN", "NaN"), + ], +) +def test_naturaltime(test_input, expected): + assert time.naturaltime(test_input) == expected + + +def nt_nomonths(d): + return time.naturaltime(d, months=False) + + +@freeze_time("2020-02-02") +@pytest.mark.parametrize( + "test_input, expected", + [ + (NOW, "now"), + (NOW - dt.timedelta(seconds=1), "a second ago"), + (NOW - dt.timedelta(seconds=30), "30 seconds ago"), + (NOW - dt.timedelta(minutes=1, seconds=30), "a minute ago"), + (NOW - dt.timedelta(minutes=2), "2 minutes ago"), + (NOW - dt.timedelta(hours=1, minutes=30, seconds=30), "an hour ago"), + (NOW - dt.timedelta(hours=23, minutes=50, seconds=50), "23 hours ago"), + (NOW - dt.timedelta(days=1), "a day ago"), + (NOW - dt.timedelta(days=17), "17 days ago"), + (NOW - dt.timedelta(days=47), "47 days ago"), + (NOW - dt.timedelta(days=500), "1 year, 135 days ago"), + (NOW - dt.timedelta(days=365 * 2 + 35), "2 years ago"), + (NOW + dt.timedelta(seconds=1), "a second from now"), + (NOW + dt.timedelta(seconds=30), "30 seconds from now"), + (NOW + dt.timedelta(minutes=1, seconds=30), "a minute from now"), + (NOW + dt.timedelta(minutes=2), "2 minutes from now"), + (NOW + dt.timedelta(hours=1, minutes=30, seconds=30), "an hour from now"), + (NOW + dt.timedelta(hours=23, minutes=50, seconds=50), "23 hours from now"), + (NOW + dt.timedelta(days=1), "a day from now"), + (NOW + dt.timedelta(days=500), "1 year, 135 days from now"), + (NOW + dt.timedelta(days=365 * 2 + 35), "2 years from now"), + # regression tests for bugs in post-release humanize + (NOW + dt.timedelta(days=10000), "27 years from now"), + (NOW - dt.timedelta(days=365 + 35), "1 year, 35 days ago"), + (30, "30 seconds ago"), + (NOW - dt.timedelta(days=365 * 2 + 65), "2 years ago"), + (NOW - dt.timedelta(days=365 + 4), "1 year, 4 days ago"), + ("NaN", "NaN"), + ], +) +def test_naturaltime_nomonths(test_input, expected): + assert nt_nomonths(test_input) == expected @freeze_time("2020-02-02")