Example #1
0
def test_slice_without_start():
    dr = DateRange(start=datetime(2016, 1, 1),
                   stop=datetime(2016, 2, 1),
                   step=timedelta(days=1))[:10]

    expected = DateRange(start=datetime(2016, 1, 1),
                         stop=datetime(2016, 1, 11),
                         step=timedelta(days=1))

    assert dr == expected
Example #2
0
def test_full_slice():
    dr = DateRange(start=datetime(2016, 1, 1),
                   stop=datetime(2016, 2, 1),
                   step=timedelta(days=1))[1:-1:2]

    expected = DateRange(start=datetime(2016, 1, 2),
                         stop=datetime(2016, 1, 31),
                         step=timedelta(days=2))

    assert dr == expected
Example #3
0
def test_raises_if_out_of_range():
    dr = DateRange(start=datetime(2016, 1, 1),
                   stop=datetime(2016, 1, 11),
                   step=timedelta(days=1))

    with pytest.raises(IndexError):
        dr[11]
 def test_get_next_valid_thursday(self):
     skipranges = [
         DateRange(start=date(2019, 1, 1),
                   stop=date(2019, 2, 1),
                   step=timedelta(days=1)),
         DateRange(start=date(2019, 2, 1),
                   stop=date(2019, 3, 1),
                   step=timedelta(days=1)),
         DateRange(start=date(2019, 6, 21),
                   stop=date(2019, 6, 22),
                   step=timedelta(days=1))
     ]
     expected = date(2019, 3, 7)
     # start from 1.1 skipranges should push this to first thursday of march
     self.assertEqual(expected,
                      get_next_valid_thursday(date(2019, 1, 1), skipranges))
Example #5
0
def test_negative_index_out_of_range():
    dr = DateRange(start=datetime(2016, 1, 1),
                   stop=datetime(2016, 2, 1),
                   step=timedelta(days=1))

    with pytest.raises(IndexError):
        dr[-32]
Example #6
0
def test_reverses_properly():
    dr = DateRange(start=datetime(2016, 1, 1),
                   stop=datetime(2016, 1, 1, 23),
                   step=timedelta(minutes=60))

    assert list(
        reversed(dr)) == [datetime(2016, 1, 1, x) for x in range(23, 0, -1)]
    def test_valid_date(self):
        # january and february as two different ranges (plus one for the end dates)
        # and one single day (midsummer)
        skipranges = [
            DateRange(start=date(2019, 1, 1),
                      stop=date(2019, 2, 1),
                      step=timedelta(days=1)),
            DateRange(start=date(2019, 2, 1),
                      stop=date(2019, 3, 1),
                      step=timedelta(days=1)),
            DateRange(start=date(2019, 6, 21),
                      stop=date(2019, 6, 22),
                      step=timedelta(days=1))
        ]
        # before our ranges
        self.assertTrue(valid_date(date(2018, 12, 31), skipranges))
        # just on the range start, middle and ends
        self.assertFalse(valid_date(date(2019, 1, 1), skipranges))
        self.assertFalse(valid_date(date(2019, 1, 12), skipranges))
        self.assertFalse(valid_date(date(2019, 1, 31), skipranges))

        # second range
        self.assertFalse(valid_date(date(2019, 2, 1), skipranges))
        self.assertFalse(valid_date(date(2019, 2, 12), skipranges))
        self.assertFalse(valid_date(date(2019, 2, 28), skipranges))

        # just after the ranges
        self.assertTrue(valid_date(date(2019, 3, 1), skipranges))

        # random day not in ranges
        self.assertTrue(valid_date(date(2019, 5, 6), skipranges))

        # single day, just before, at the day and after
        self.assertTrue(valid_date(date(2019, 6, 20), skipranges))
        self.assertFalse(valid_date(date(2019, 6, 21), skipranges))
        self.assertTrue(valid_date(date(2019, 6, 22), skipranges))
def parse_skiprange(s):
    try:
        start, end = s.split('-')
    except ValueError:
        # handle one day as a range of one day
        start = s
        end = s

    try:
        startdate = dateutil.parser.parse(start)
        # important to add one day to end date, the check is still time specific and this way
        # we will check from 2019-01-01 00:00 to 2019-01-02 00:00
        enddate = dateutil.parser.parse(end) + dt.timedelta(days=1)
        return DateRange(start=startdate,
                         stop=enddate,
                         step=dt.timedelta(hours=1))
    except ValueError:
        msg = "Not a valid date range: '{0}'.".format(s)
        raise argparse.ArgumentTypeError(msg)
Example #9
0
def test_does_not_contain_end():
    dr = DateRange(date(2016, 1, 1), date(2016, 1, 31), timedelta(days=2))
    assert date(2016, 1, 31) not in dr
    assert date(2016, 1, 31) == dr.stop
Example #10
0
def test_properly_reports_a_contained_date(when):
    dr = DateRange(start=date(2016, 1, 1),
                   stop=date(2016, 1, 31),
                   step=timedelta(days=2))

    assert when in dr
Example #11
0
def test_raises_error_if_infinite_length():
    dr = DateRange(start=date(2016, 1, 1), step=timedelta(days=1))

    with pytest.raises(TypeError):
        len(dr)
Example #12
0
def test_indexes_properly(idx, expected):
    dr = DateRange(start=datetime(2016, 1, 1), step=timedelta(days=2))

    assert dr[idx] == expected
Example #13
0
def test_iterates_properly():
    dr = DateRange(start=date(2016, 1, 1),
                   stop=date(2016, 1, 31),
                   step=timedelta(days=1))

    assert list(dr) == [date(2016, 1, x) for x in range(1, 31)]
Example #14
0
def test_reports_does_not_contain_greater_than_start_with_neg_step():
    dr = DateRange(start=datetime(2016, 1, 23), step=timedelta(days=-1))

    assert datetime(2016, 1, 31) not in dr
Example #15
0
def test_reports_contains_a_precise_datetime():
    dr = DateRange(start=datetime(2000, 4, 25, 18, 30),
                   step=timedelta(minutes=57))

    assert datetime(2000, 4, 29, 7, 3) in dr
Example #16
0
def test_empty_slice_is_a_copy():
    dr = DateRange(start=datetime(2016, 1, 1), step=timedelta(days=1))
    copy = dr[:]

    assert dr == copy
    assert dr is not copy
Example #17
0
def test_raises_with_infinite_range_and_neg_step():
    dr = DateRange(start=datetime(2016, 1, 1), step=timedelta(days=-1))

    with pytest.raises(IndexError):
        dr[-1]
Example #18
0
def test_complains_if_step_is_zero():
    with pytest.raises(TypeError):
        DateRange(start=date(2016, 1, 1), step=timedelta(0))
Example #19
0
def test_equals_other_daterange():
    dr = DateRange(datetime(2016, 1, 23), step=timedelta(1))
    other_dr = DateRange(datetime(2016, 1, 23), step=timedelta(1))

    assert dr == other_dr
Example #20
0
def test_not_equal_other_daterange():
    dr = DateRange(datetime(2016, 1, 23), step=timedelta(days=10))
    other_dr = DateRange(datetime(2016, 1, 23), step=timedelta(days=1))

    assert dr != other_dr
Example #21
0
def test_negative_index():
    dr = DateRange(start=datetime(2016, 1, 1),
                   stop=datetime(2016, 2, 1),
                   step=timedelta(days=1))

    assert dr[-1] == datetime(2016, 1, 31)
Example #22
0
def test_complains_if_start_not_provided():
    with pytest.raises(TypeError):
        DateRange(start=None,
                  stop=datetime(2016, 1, 1),
                  step=timedelta(days=1))
Example #23
0
def test_properly_reports_a_contained_datetime(when):
    dr = DateRange(start=datetime(2016, 1, 1, 0, 0, 0),
                   stop=datetime(2016, 1, 1, 23, 0, 0),
                   step=timedelta(minutes=60))

    assert when in dr
Example #24
0
def test_empty_if_start_is_higher_than_stop_without_negative_step():
    assert [] == list(
        DateRange(start=date(2016, 1, 31),
                  stop=date(2016, 1, 1),
                  step=timedelta(days=1)))
Example #25
0
def test_reports_contains_a_datetime_with_stop_and_neg_step():
    dr = DateRange(start=datetime(2016, 1, 23),
                   stop=datetime(2016, 1, 1),
                   step=timedelta(days=-1))

    assert datetime(2016, 1, 20) in dr
Example #26
0
def test_cant_reverse_infinite_range():
    with pytest.raises(ValueError):
        reversed(DateRange(datetime(2016, 1, 1), step=timedelta(days=1)))
Example #27
0
def test_reports_contains_with_negative_step(when):
    dr = DateRange(start=datetime(2000, 1, 1, 23),
                   stop=datetime(2000, 1, 1),
                   step=timedelta(minutes=-60))
    assert when in dr
Example #28
0
def test_properly_calculates_length():
    dr = DateRange(start=date(2016, 1, 1),
                   stop=date(2016, 1, 6),
                   step=timedelta(days=1))

    assert len(dr) == 5
Example #29
0
def test_fails_to_negative_slice_infinite_range(s):
    dr = DateRange(start=datetime(2016, 1, 1), step=timedelta(days=-1))

    with pytest.raises(IndexError):
        dr[s]
Example #30
0
def test_raises_with_bad_subscription(idx):
    dr = DateRange(datetime(2016, 1, 23), step=timedelta(days=1))

    with pytest.raises(TypeError):
        dr[idx]