"""Unit tests for datetime utility functions.""" import pytest from analytics.consts import models as model_consts from analytics.utils import date def test_get_date(): """Test transformation of a date string into a datetime obj.""" dates = [date.get('2014-10-01'), date.get('10-01-2014', format='%m-%d-%Y')] for current_date in dates: assert current_date.year == 2014 assert current_date.month == 10 assert current_date.day == 1 def test_get_wrong_date(): """ Test get_wrong_date. Dates that do not match the format returns empty. """ assert not date.get('2015-20-01') def test_create_empty_date_ranges(): """Test creating empty date ranges.""" ranges = date.create_empty_date_ranges('2015-10-01', '2015-11-01') assert len(ranges) == 32 # includes 2015-11-01. def test_month_difference(): """Test difference between two dates in months.""" m = date.month_difference(date.get('2014-10-01'), date.get('2015-02-01')) assert m == 5 def test_generator_month_between_dates(): """The generator return one month at a time between two dates.""" dates = list(date.generator_month_between_dates( date.get('2014-10-01'), date.get('2015-02-01'))) assert len(dates) == 5 assert dates[0] == date.get('2014-10-01') assert dates[1] == date.get('2014-11-01') assert dates[2] == date.get('2014-12-01') assert dates[3] == date.get('2015-01-01') assert dates[4] == date.get('2015-02-01') def test_generator_month_chunk_same_date(): """Test the generator that returns a month range.""" start_date = date.get('2014-10-01') end_date = date.get('2014-10-01') dates = list(date.month_chunk(start_date, end_date))[0] assert dates[0] == dates[1] def test_generator_month_chunk_same_month(): """Test the generator that returns a month range.""" start_date = date.get('2014-10-01') end_date = date.get('2014-10-20') dates = list(date.month_chunk(start_date, end_date))[0] assert dates[0].day == 1 assert dates[1].day == 20 assert dates[0].month == dates[1].month assert dates[0].year == dates[1].year def test_generator_month_chunk(): """Test the generator that returns a month range.""" start_date = date.get('2014-10-08') end_date = date.get('2015-07-20') ranges = list(date.month_chunk(start_date, end_date)) # oct, nov, dec, jan, feb, march, april, may, june, july assert len(ranges) == 10 # Check it goes through all the dates assert ranges[0][0] == start_date for i in range(2): assert ranges[9][i].year == end_date.year assert ranges[9][i].month == end_date.month assert ranges[9][1] == end_date def test_generator_month_chunk_with_size(): """Test the generator that returns a month range with a different size.""" start_date = date.get('2014-10-08') end_date = date.get('2015-07-20') ranges = list(date.month_chunk(start_date, end_date, size=2)) # (oct, nov), (dec, jan), (feb, march), (april, may), (june, july) assert len(ranges) == 5 # Check it goes through all the dates assert ranges[0][0] == start_date assert ranges[4][1] == end_date ranges = list(date.month_chunk(start_date, end_date, size=3)) # (oct, nov, dec), (jan, feb, march), (april, may, june), (july) assert len(ranges) == 4 # Check it goes through all the dates assert ranges[0][0] == start_date for i in range(2): assert ranges[3][i].year == end_date.year assert ranges[3][i].month == end_date.month assert ranges[3][1] == end_date @pytest.mark.parametrize( 'start_date, end_date, period', [ ('2017-01-01', '2017-01-31', model_consts.MONTH_PERIOD), ('2017-01-01', '2017-01-07', model_consts.WEEK_PERIOD), ('2016-01-01', '2017-01-01', model_consts.YEAR_PERIOD), ('2017-01-01', '2018-01-01', model_consts.YEAR_PERIOD), ('2017-01-01', '2018-01-08', model_consts.YEAR_PERIOD) ] ) def test_determine_date_period(start_date, end_date, period): """Test the determine period.""" assert date.determine_date_period(start_date, end_date).message == period @pytest.mark.parametrize( 'start_date, end_date', [ ('2017-01-01', '2017-01-10'), ('2017-01-01', '2017-01-02'), ('2017-01-01', '2017-03-03'), ('2016-01-03', '2017-01-01'), ('2017-01-02', '2018-01-01'), ('2017-01-01', '2018-01-09') ] ) def test_determine_date_period_invalid_range(start_date, end_date): """Test the determine period for invalid date ranges.""" assert not date.determine_date_period(start_date, end_date)