Skip to content

Commit

Permalink
Merge pull request #12 from justjasongreen/date_utils
Browse files Browse the repository at this point in the history
Test date_utils (closes #8)
  • Loading branch information
justjasongreen committed Jul 24, 2016
2 parents f7ef765 + 83d6510 commit faf2364
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
8 changes: 6 additions & 2 deletions predictive_punter/date_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
def dates(date_from, date_to):
"""Iterate over all dates from date_from to date_to (inclusive)"""

time_delta = timedelta(days=1)
if date_from > date_to:
time_delta *= -1

next_date = date_from
while next_date <= date_to:
while (date_from <= date_to and next_date <= date_to) or (date_from > date_to and next_date >= date_to):
yield next_date
next_date += timedelta(days=1)
next_date += time_delta
43 changes: 43 additions & 0 deletions tests/date_utils/dates_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from datetime import datetime

from predictive_punter import date_utils


def test_equal():
"""The dates iterator should generate the expected date when date_from == date_to"""

expected_dates = [
datetime(2016, 2, 2)
]

actual_dates = [date for date in date_utils.dates(datetime(2016, 2, 2), datetime(2016, 2, 2))]

assert actual_dates == expected_dates


def test_forward():
"""The dates iterator should generate the expected dates when date_from < date_to"""

expected_dates = [
datetime(2016, 2, 1),
datetime(2016, 2, 2),
datetime(2016, 2, 3)
]

actual_dates = [date for date in date_utils.dates(datetime(2016, 2, 1), datetime(2016, 2, 3))]

assert actual_dates == expected_dates


def test_reverse():
"""The dates iterator should generate the expected dates when date_from > date_to"""

expected_dates = [
datetime(2016, 2, 3),
datetime(2016, 2, 2),
datetime(2016, 2, 1)
]

actual_dates = [date for date in date_utils.dates(datetime(2016, 2, 3), datetime(2016, 2, 1))]

assert actual_dates == expected_dates

0 comments on commit faf2364

Please sign in to comment.