test_timeutils.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. from datetime import timedelta, date
  2. import pytest
  3. from boltons.timeutils import total_seconds, daterange
  4. def test_float_total_seconds():
  5. """Check for floating point precision loss per
  6. http://bugs.python.org/issue8644 and tests in the corresponding
  7. diff, spurred by
  8. https://github.com/mahmoud/boltons/pull/13#issuecomment-93835612 .
  9. The funny thing is that floating point precision loss is
  10. definitely happening. With or without true division, in Python
  11. 2.7's native timedelta.total_seconds() as well as
  12. dateutils.total_seconds. The constants in the tests below are from
  13. manual tests on Python 2.7.6 final. 2.6 does vary slightly, but it
  14. might just be a repr change.
  15. With these tests in mind, I'm not sure why the Python issue got
  16. created in the first place.
  17. """
  18. assert total_seconds(timedelta(microseconds=1)) == 1e-06
  19. assert total_seconds(timedelta(microseconds=-1)) == -1e-06
  20. assert total_seconds(timedelta(microseconds=-2)) == -2e-06
  21. assert total_seconds(timedelta(days=2 ** 29, microseconds=1)) == 46385646796800.0
  22. assert total_seconds(timedelta(seconds=123456.789012)) == 123456.789012
  23. assert total_seconds(timedelta(seconds=-123456.789012)) == -123456.789012
  24. def test_daterange_years():
  25. new_year = date(2017, 1, 1)
  26. bit_rollover = date(2038, 1, 19)
  27. new_years_remaining = daterange(new_year, bit_rollover, step=(1, 0, 0))
  28. assert len(list(new_years_remaining)) == 22
  29. y2025 = date(2025, 1, 1)
  30. bakers_years_til_2025 = list(daterange(new_year, y2025, step=(1, 1, 0)))
  31. assert len(bakers_years_til_2025) == 8
  32. assert bakers_years_til_2025[-1] == date(2024, 8, 1)
  33. assert bakers_years_til_2025[-1] == date(2024, 8, 1)
  34. years_from_2025 = list(daterange(y2025, new_year, step=(-1, 0, 0),
  35. inclusive=True))
  36. assert years_from_2025[0] == date(2025, 1, 1)
  37. assert years_from_2025[-1] == date(2017, 1, 1)
  38. def test_daterange_years_step():
  39. start_day = date(year=2012, month=12, day=25)
  40. end_day = date(year=2016, month=1, day=1)
  41. dates = list(daterange(start_day, end_day, step=(1, 0, 0), inclusive=False))
  42. expected = [date(year=2012, month=12, day=25), date(year=2013, month=12, day=25), date(year=2014, month=12, day=25), date(year=2015, month=12, day=25)]
  43. assert dates == expected
  44. dates = list(daterange(start_day, end_day, step=(0, 13, 0), inclusive=False))
  45. expected = [date(year=2012, month=12, day=25), date(year=2014, month=1, day=25), date(year=2015, month=2, day=25)]
  46. assert dates == expected
  47. def test_daterange_infinite():
  48. today = date.today()
  49. infinite_dates = daterange(today, None)
  50. for i in range(10):
  51. assert next(infinite_dates) == today + timedelta(days=i)
  52. def test_daterange_with_same_start_stop():
  53. today = date.today()
  54. date_range = daterange(today, today)
  55. with pytest.raises(StopIteration):
  56. next(date_range)
  57. date_range_inclusive = daterange(today, today, inclusive=True)
  58. assert next(date_range_inclusive) == today
  59. with pytest.raises(StopIteration):
  60. next(date_range_inclusive)