Esempio n. 1
0
def test_repeating_end_after_repeat():
    rr = RepeatingTimeReference(start=dt("2013-01-01 09:00"),
                                frequency=10,
                                end_after_repeat=2)
    assert rr.next_after(dt("2013-01-01 09:19")) == dt("2013-01-01 09:20")
    assert rr.next_after(dt("2013-01-01 09:20")) is None
    assert rr.next_after(dt("2013-01-01 09:21")) is None
Esempio n. 2
0
def test_format_index():
    pattern = 'logstash-%Y.%m.%d'
    pattern2 = 'logstash-%Y.%W'
    date = dt('2018-06-25T12:00:00Z')
    date2 = dt('2018-06-26T12:00:00Z')
    assert sorted(format_index(pattern, date, date).split(',')) == ['logstash-2018.06.25']
    assert sorted(format_index(pattern, date, date2).split(',')) == ['logstash-2018.06.25', 'logstash-2018.06.26']
    assert sorted(format_index(pattern, date, date2, True).split(',')) == ['logstash-2018.06.24',
                                                                           'logstash-2018.06.25',
                                                                           'logstash-2018.06.26']
    assert sorted(format_index(pattern2, date, date2, True).split(',')) == ['logstash-2018.25', 'logstash-2018.26']
Esempio n. 3
0
 def utcnow():
     return dt('2017-07-07T10:00:00.000Z')
Esempio n. 4
0
from elastalert.util import resolve_string
from elastalert.util import set_es_key


@pytest.mark.parametrize('spec, expected_delta', [
    ('hours=2', timedelta(hours=2)),
    ('minutes=30', timedelta(minutes=30)),
    ('seconds=45', timedelta(seconds=45)),
])
def test_parse_duration(spec, expected_delta):
    """``unit=num`` specs can be translated into ``timedelta`` instances."""
    assert parse_duration(spec) == expected_delta


@pytest.mark.parametrize('spec, expected_deadline', [
    ('hours=2', dt('2017-07-07T12:00:00.000Z')),
    ('minutes=30', dt('2017-07-07T10:30:00.000Z')),
    ('seconds=45', dt('2017-07-07T10:00:45.000Z')),
])
def test_parse_deadline(spec, expected_deadline):
    """``unit=num`` specs can be translated into ``datetime`` instances."""

    # Note: Can't mock ``utcnow`` directly because ``datetime`` is a built-in.
    class MockDatetime(datetime):
        @staticmethod
        def utcnow():
            return dt('2017-07-07T10:00:00.000Z')

    with mock.patch('datetime.datetime', MockDatetime):
        assert parse_deadline(spec) == expected_deadline
Esempio n. 5
0
def test_pytzfy2():
    assert pytzfy(datetime(2018, 12, 31, 5, 0, 30,
                           1000)) == dt('2018-12-31 05:00:30.001000')
Esempio n. 6
0
def test_pytzfy1():
    assert pytzfy(
        dt('2021-02-01 12:30:00+00:00')) == dt('2021-02-01 12:30:00+00:00')
Esempio n. 7
0
def test_dt_to_ts_with_format2():
    assert dt_to_ts_with_format(dt('2021-02-01 12:30:00+00:00'),
                                '%d/%m/%Y %H:%M:%S') == '01/02/2021 12:30:00'
Esempio n. 8
0
def test_dt_to_ts_with_format1():
    assert dt_to_ts_with_format(dt('2021-02-01 12:30:00+00:00'),
                                '%Y/%m/%d %H:%M:%S') == '2021/02/01 12:30:00'
Esempio n. 9
0
def test_ts_to_dt_with_format4():
    assert ts_to_dt_with_format(
        '01/02/2021 12:30:00 +0900',
        '%d/%m/%Y %H:%M:%S %z') == dt('2021-02-01 12:30:00+09:00')
Esempio n. 10
0
def test_ts_to_dt_with_format3():
    date = datetime(2021, 7, 6, hour=0, minute=0, second=0)
    assert ts_to_dt_with_format(date, '') == dt('2021-7-6 00:00')
Esempio n. 11
0
 def topic_data(page):
     return (((forum, topic), link, html.unescape(title), author, dt(date))
             for link, forum, topic, title, author, date in
             TOPIC_DATA.findall(str(page)))
Esempio n. 12
0
 def utcnow():
     return dt('2017-07-07T10:00:00.000Z')
Esempio n. 13
0
)
from dateutil.parser import parse as dt


@pytest.mark.parametrize('spec, expected_delta', [
    ('hours=2',    timedelta(hours=2)),
    ('minutes=30', timedelta(minutes=30)),
    ('seconds=45', timedelta(seconds=45)),
])
def test_parse_duration(spec, expected_delta):
    """``unit=num`` specs can be translated into ``timedelta`` instances."""
    assert parse_duration(spec) == expected_delta


@pytest.mark.parametrize('spec, expected_deadline', [
    ('hours=2',    dt('2017-07-07T12:00:00.000Z')),
    ('minutes=30', dt('2017-07-07T10:30:00.000Z')),
    ('seconds=45', dt('2017-07-07T10:00:45.000Z')),
])
def test_parse_deadline(spec, expected_deadline):
    """``unit=num`` specs can be translated into ``datetime`` instances."""

    # Note: Can't mock ``utcnow`` directly because ``datetime`` is a built-in.
    class MockDatetime(datetime):
        @staticmethod
        def utcnow():
            return dt('2017-07-07T10:00:00.000Z')

    with mock.patch('datetime.datetime', MockDatetime):
        assert parse_deadline(spec) == expected_deadline
Esempio n. 14
0
 def topic_data(page):
     return (((forum, topic), link, html.unescape(title), author, dt(date))
             for link, forum, topic, title, author, date
             in TOPIC_DATA.findall(str(page)))
Esempio n. 15
0
def test_repeating_end_after_time():
    rr = RepeatingTimeReference(start=dt("2013-01-01 09:00"),
                                frequency=10,
                                end_after_time=dt("2013-01-01 10:00"))
    assert rr.next_after(dt("2013-01-02 10:00")) is None
Esempio n. 16
0
    assert dr.minutes == 60 * 24


def test_daterange_minutes_zero():
    dr = DateRange('20010101', '20010101')
    assert dr.minutes == 0


def test_daterange_minutes_negative():
    dr = DateRange('20010102', '20010101')
    assert dr.minutes == -60 * 24


@pytest.mark.parametrize(('start_date', 'freq', 'dt', 'expected'), [
    # 10 min freq
    (dt("2013-01-01 09:00"), 10, dt("2013-01-01 09:53"),
     dt("2013-01-01 10:00")),
    # 5 min freq
    (dt("2013-01-01 09:00"), 5, dt("2013-01-01 09:53"),
     dt("2013-01-01 09:55")),
    # Next day
    (dt("2013-01-01 09:00"), 60 * 24, dt("2013-01-01 09:53"),
     dt("2013-01-02 09:00")),
    # Before series start
    (dt("2013-01-01 09:00"), 60 * 24, dt("2012-01-01 09:45"),
     dt("2013-01-01 09:00")),
])
def test_repeating(start_date, freq, dt, expected):
    assert RepeatingTimeReference(start_date, freq).next_after(dt) == expected