def test_pattern_with_one_period_in_result():
    start_date = parse_datetime("2015-02-01T06:52:00Z").replace(tzinfo=None)
    end_date = parse_datetime("2015-02-01T23:52:00Z").replace(tzinfo=None)
    weekly_pattern = "1111111"
    time_slots = [{"begin": "07:45", "end": "09:30"}]
    app_periods = get_application_periods_by_pattern(start_date, end_date, weekly_pattern, time_slots, 'Europe/Paris')
    eq_(len(app_periods), 1)
def test_pattern_midnight_change_one_day():
    date_format = "%Y-%m-%dT%H:%M:%SZ"
    start_date = parse_datetime("2015-09-21T00:00:00Z").replace(tzinfo=None)
    end_date = parse_datetime("2015-09-21T00:00:00Z").replace(tzinfo=None)
    weekly_pattern = "1111111"
    time_slots = [{"begin": "18:00", "end": "03:00"}]
    app_periods = get_application_periods_by_pattern(start_date, end_date, weekly_pattern, time_slots, 'Europe/Paris')
    eq_(len(app_periods), 1)
    eq_(app_periods[0][0].strftime(date_format), "2015-09-21T16:00:00Z")
    eq_(app_periods[0][1].strftime(date_format), "2015-09-22T01:00:00Z")
def test_pattern_with_wrong_time_slots():
    application_period_pattern = {
        "start_date": "2015-02-01T06:52:00Z",
        "end_date": "2015-02-01T23:52:00Z",
        "weekly_pattern": "0000000"
    }
    validate(application_period_pattern, formats.pattern_input_format)
    app_periods = get_application_periods_by_pattern(
        application_period_pattern)
    eq_(len(app_periods), 0)
def test_pattern_with_one_period_in_result():
    start_date = parse_datetime("2015-02-01T06:52:00Z").replace(tzinfo=None)
    end_date = parse_datetime("2015-02-01T23:52:00Z").replace(tzinfo=None)
    weekly_pattern = "1111111"
    time_slots = [{"begin": "07:45", "end": "09:30"}]
    app_periods = get_application_periods_by_pattern(start_date, end_date,
                                                     weekly_pattern,
                                                     time_slots,
                                                     'Europe/Paris')
    eq_(len(app_periods), 1)
def test_pattern_midnight_change_to_winter_2_oclock():
    date_format = "%Y-%m-%dT%H:%M:%SZ"
    start_date = parse_datetime("2015-10-24T00:00:00Z").replace(tzinfo=None)
    end_date = parse_datetime("2015-10-24T00:00:00Z").replace(tzinfo=None)
    weekly_pattern = "1111111"
    time_slots = [{"begin": "22:00", "end": "02:00"}]
    app_periods = get_application_periods_by_pattern(start_date, end_date, weekly_pattern, time_slots, 'Europe/Paris')
    eq_(len(app_periods), 1)
    #Difference of one hour for begin and two hours for end between UTC and Europe/Paris
    eq_(app_periods[0][0].strftime(date_format), "2015-10-24T20:00:00Z")
    eq_(app_periods[0][1].strftime(date_format), "2015-10-25T01:00:00Z")
def test_pattern_with_multi_periods_in_result():
    date_format = "%Y-%m-%dT%H:%M:%SZ"
    start_date = parse_datetime("2015-02-02T06:52:00Z").replace(tzinfo=None)
    end_date = parse_datetime("2015-02-15T23:52:00Z").replace(tzinfo=None)
    weekly_pattern = "1111111"
    time_slots = [{"begin": "07:45", "end": "09:30"}]
    app_periods = get_application_periods_by_pattern(start_date, end_date, weekly_pattern, time_slots, 'Europe/Paris')
    eq_(len(app_periods), 14)
    eq_(app_periods[0][0].strftime(date_format), "2015-02-02T06:45:00Z")
    eq_(app_periods[0][1].strftime(date_format), "2015-02-02T08:30:00Z")
    eq_(app_periods[13][0].strftime(date_format), "2015-02-15T06:45:00Z")
    eq_(app_periods[13][1].strftime(date_format), "2015-02-15T08:30:00Z")
def test_pattern_midnight_change_one_day_one_oclock():
    date_format = "%Y-%m-%dT%H:%M:%SZ"
    start_date = parse_datetime("2015-09-21T00:00:00Z").replace(tzinfo=None)
    end_date = parse_datetime("2015-09-21T00:00:00Z").replace(tzinfo=None)
    weekly_pattern = "1111111"
    time_slots = [{"begin": "18:00", "end": "01:00"}]
    app_periods = get_application_periods_by_pattern(start_date, end_date,
                                                     weekly_pattern,
                                                     time_slots,
                                                     'Europe/Paris')
    eq_(len(app_periods), 1)
    eq_(app_periods[0][0].strftime(date_format), "2015-09-21T16:00:00Z")
    eq_(app_periods[0][1].strftime(date_format), "2015-09-21T23:00:00Z")
def test_pattern_midnight_change_to_summer():
    date_format = "%Y-%m-%dT%H:%M:%SZ"
    start_date = parse_datetime("2016-03-26T00:00:00Z").replace(tzinfo=None)
    end_date = parse_datetime("2016-03-27T00:00:00Z").replace(tzinfo=None)
    weekly_pattern = "1111111"
    time_slots = [{"begin": "22:00", "end": "02:00"}]
    app_periods = get_application_periods_by_pattern(start_date, end_date, weekly_pattern, time_slots, 'Europe/Paris')
    eq_(len(app_periods), 2)
    #Difference of two hours between UTC and Europe/Paris
    eq_(app_periods[0][0].strftime(date_format), "2016-03-26T21:00:00Z")
    eq_(app_periods[0][1].strftime(date_format), "2016-03-27T01:00:00Z")
    #Difference of one hour between UTC and Europe/Paris
    eq_(app_periods[1][0].strftime(date_format), "2016-03-27T20:00:00Z")
    eq_(app_periods[1][1].strftime(date_format), "2016-03-28T00:00:00Z")
def test_pattern_midnight_change_to_winter_2_oclock():
    date_format = "%Y-%m-%dT%H:%M:%SZ"
    start_date = parse_datetime("2015-10-24T00:00:00Z").replace(tzinfo=None)
    end_date = parse_datetime("2015-10-24T00:00:00Z").replace(tzinfo=None)
    weekly_pattern = "1111111"
    time_slots = [{"begin": "22:00", "end": "02:00"}]
    app_periods = get_application_periods_by_pattern(start_date, end_date,
                                                     weekly_pattern,
                                                     time_slots,
                                                     'Europe/Paris')
    eq_(len(app_periods), 1)
    #Difference of one hour for begin and two hours for end between UTC and Europe/Paris
    eq_(app_periods[0][0].strftime(date_format), "2015-10-24T20:00:00Z")
    eq_(app_periods[0][1].strftime(date_format), "2015-10-25T01:00:00Z")
def test_pattern_with_multi_periods_in_result():
    date_format = "%Y-%m-%dT%H:%M:%SZ"
    start_date = parse_datetime("2015-02-02T06:52:00Z").replace(tzinfo=None)
    end_date = parse_datetime("2015-02-15T23:52:00Z").replace(tzinfo=None)
    weekly_pattern = "1111111"
    time_slots = [{"begin": "07:45", "end": "09:30"}]
    app_periods = get_application_periods_by_pattern(start_date, end_date,
                                                     weekly_pattern,
                                                     time_slots,
                                                     'Europe/Paris')
    eq_(len(app_periods), 14)
    eq_(app_periods[0][0].strftime(date_format), "2015-02-02T06:45:00Z")
    eq_(app_periods[0][1].strftime(date_format), "2015-02-02T08:30:00Z")
    eq_(app_periods[13][0].strftime(date_format), "2015-02-15T06:45:00Z")
    eq_(app_periods[13][1].strftime(date_format), "2015-02-15T08:30:00Z")
def test_pattern_midnight_change_several_days_with_last_day_valid_():
    date_format = "%Y-%m-%dT%H:%M:%SZ"
    start_date = parse_datetime("2015-09-22T00:00:00Z").replace(tzinfo=None)
    end_date = parse_datetime("2015-09-24T00:00:00Z").replace(tzinfo=None)
    weekly_pattern = "1110111"
    time_slots = [{"begin": "18:00", "end": "03:00"}]
    app_periods = get_application_periods_by_pattern(start_date, end_date,
                                                     weekly_pattern,
                                                     time_slots,
                                                     'Europe/Paris')
    eq_(len(app_periods), 2)
    eq_(app_periods[0][0].strftime(date_format), "2015-09-22T16:00:00Z")
    eq_(app_periods[0][1].strftime(date_format), "2015-09-23T01:00:00Z")
    eq_(app_periods[1][0].strftime(date_format), "2015-09-23T16:00:00Z")
    eq_(app_periods[1][1].strftime(date_format), "2015-09-24T01:00:00Z")
def test_pattern_midnight_change_several_days_with_last_day_valid():
    date_format = "%Y-%m-%dT%H:%M:%SZ"
    start_date = parse_datetime("2015-09-21T00:00:00Z").replace(tzinfo=None)
    end_date = parse_datetime("2015-09-26T00:00:00Z").replace(tzinfo=None)
    weekly_pattern = "1101010"
    time_slots = [{"begin": "18:00", "end": "03:00"}]
    app_periods = get_application_periods_by_pattern(start_date, end_date, weekly_pattern, time_slots, 'Europe/Paris')
    eq_(len(app_periods), 4)
    eq_(app_periods[0][0].strftime(date_format), "2015-09-21T16:00:00Z")
    eq_(app_periods[0][1].strftime(date_format), "2015-09-22T01:00:00Z")
    eq_(app_periods[1][0].strftime(date_format), "2015-09-22T16:00:00Z")
    eq_(app_periods[1][1].strftime(date_format), "2015-09-23T01:00:00Z")
    eq_(app_periods[2][0].strftime(date_format), "2015-09-24T16:00:00Z")
    eq_(app_periods[2][1].strftime(date_format), "2015-09-25T01:00:00Z")
    eq_(app_periods[3][0].strftime(date_format), "2015-09-26T16:00:00Z")
    eq_(app_periods[3][1].strftime(date_format), "2015-09-27T01:00:00Z")
def test_pattern_midnight_change_to_summer():
    date_format = "%Y-%m-%dT%H:%M:%SZ"
    start_date = parse_datetime("2016-03-26T00:00:00Z").replace(tzinfo=None)
    end_date = parse_datetime("2016-03-27T00:00:00Z").replace(tzinfo=None)
    weekly_pattern = "1111111"
    time_slots = [{"begin": "22:00", "end": "02:00"}]
    app_periods = get_application_periods_by_pattern(start_date, end_date,
                                                     weekly_pattern,
                                                     time_slots,
                                                     'Europe/Paris')
    eq_(len(app_periods), 2)
    #Difference of two hours between UTC and Europe/Paris
    eq_(app_periods[0][0].strftime(date_format), "2016-03-26T21:00:00Z")
    eq_(app_periods[0][1].strftime(date_format), "2016-03-27T01:00:00Z")
    #Difference of one hour between UTC and Europe/Paris
    eq_(app_periods[1][0].strftime(date_format), "2016-03-27T20:00:00Z")
    eq_(app_periods[1][1].strftime(date_format), "2016-03-28T00:00:00Z")
def test_pattern_with_wrong_time_slots():
    application_period_pattern = {"start_date": "2015-02-01T06:52:00Z", "end_date": "2015-02-01T23:52:00Z", "weekly_pattern": "0000000"}
    validate(application_period_pattern, formats.pattern_input_format)
    app_periods = get_application_periods_by_pattern(application_period_pattern)
    eq_(len(app_periods), 0)