예제 #1
0
def test_args_parsearea(testapp, timestamp, parsearea):
    """Todatetime processor has to respect "parsearea" argument."""

    stream = todatetime.process(
        testapp,
        [
            holocron.Item({
                "content": "the Force is strong with this one",
                "timestamp": timestamp,
            })
        ],
        todatetime="timestamp",
        parsearea=parsearea,
        fuzzy=True,
    )

    assert isinstance(stream, collections.abc.Iterable)
    assert list(stream) == [
        holocron.Item({
            "content":
            "the Force is strong with this one",
            "timestamp":
            datetime.datetime(2019, 1, 11, tzinfo=_TZ_UTC),
        })
    ]
예제 #2
0
def test_args_todatetime(testapp, timestamp):
    """Todatetime processor has to respect "writeto" argument."""

    stream = todatetime.process(
        testapp,
        [
            holocron.Item({
                "content": "the Force is strong with this one",
                "timestamp": timestamp,
            })
        ],
        todatetime=["timestamp", "published"],
    )

    assert isinstance(stream, collections.abc.Iterable)
    assert list(stream) == [
        holocron.Item({
            "content":
            "the Force is strong with this one",
            "timestamp":
            timestamp,
            "published":
            datetime.datetime(2019, 1, 11, 0, 0, 0, tzinfo=_TZ_UTC),
        })
    ]
예제 #3
0
def test_item_timestamp_missing(testapp):
    """Todatetime processor has to ignore items with missing timestamp."""

    stream = todatetime.process(
        testapp,
        [holocron.Item({"content": "the Force is strong with this one"})],
        todatetime="timestamp",
    )

    assert isinstance(stream, collections.abc.Iterable)
    assert list(stream) == [
        holocron.Item({"content": "the Force is strong with this one"})
    ]
예제 #4
0
def test_item_timestamp_bad_value(testapp):
    """Todatetime processor has to error if a timestamp cannot be parsed."""

    stream = todatetime.process(
        testapp,
        [
            holocron.Item({
                "content": "the Force is strong with this one",
                "timestamp": "yoda",
            })
        ],
        todatetime="timestamp",
    )

    assert isinstance(stream, collections.abc.Iterable)

    with pytest.raises(Exception) as excinfo:
        next(stream)
        assert str(excinfo.value) == "('Unknown string format:', 'yoda')"
예제 #5
0
def test_item(testapp, timestamp, parsed):
    """Todatetime processor has to work."""

    stream = todatetime.process(
        testapp,
        [
            holocron.Item({
                "content": "the Force is strong with this one",
                "timestamp": timestamp,
            })
        ],
        todatetime="timestamp",
    )

    assert isinstance(stream, collections.abc.Iterable)
    assert list(stream) == [
        holocron.Item({
            "content": "the Force is strong with this one",
            "timestamp": parsed,
        })
    ]
예제 #6
0
def test_args_timezone_fallback(testapp, tz):
    """Todatetime processor has to respect "timezone" argument (fallback)."""

    # Custom timezone has to be attached only to timestamps without
    # explicit timezone information. So this option is nothing more
    # but a fallback.
    testapp.metadata.update({"timezone": tz})

    stream = todatetime.process(
        testapp,
        [
            holocron.Item({
                "content": "the Force is strong with this one",
                "timestamp": "2019-01-15T21:07+00:00",
            }),
            holocron.Item({
                "content": "may the Force be with you",
                "timestamp": "2019-01-15T21:07",
            }),
        ],
        todatetime="timestamp",
    )

    assert isinstance(stream, collections.abc.Iterable)
    assert list(stream) == [
        holocron.Item({
            "content":
            "the Force is strong with this one",
            "timestamp":
            datetime.datetime(2019, 1, 15, 21, 7, tzinfo=_TZ_UTC),
        }),
        holocron.Item({
            "content":
            "may the Force be with you",
            "timestamp":
            datetime.datetime(2019, 1, 15, 21, 7,
                              tzinfo=dateutil.tz.gettz(tz)),
        }),
    ]
예제 #7
0
def test_args_parsearea_not_found(testapp):
    """Todatetime processor has to respect "parsearea" argument."""

    stream = todatetime.process(
        testapp,
        [
            holocron.Item({
                "content": "the Force is strong with this one",
                "timestamp": "luke-skywalker-part-1.txt",
            })
        ],
        todatetime="timestamp",
        parsearea=r"\d{4}-\d{2}-\d{2}",
    )

    assert isinstance(stream, collections.abc.Iterable)
    assert list(stream) == [
        holocron.Item({
            "content": "the Force is strong with this one",
            "timestamp": "luke-skywalker-part-1.txt",
        })
    ]
예제 #8
0
def test_item_many(testapp, amount):
    """Todatetime processor has to work with stream."""

    stream = todatetime.process(
        testapp,
        [
            holocron.Item({
                "content": "the Force is strong with this one",
                "timestamp": "2019-01-%d" % (i + 1),
            }) for i in range(amount)
        ],
        todatetime="timestamp",
    )

    assert isinstance(stream, collections.abc.Iterable)
    assert list(stream) == [
        holocron.Item({
            "content":
            "the Force is strong with this one",
            "timestamp":
            datetime.datetime(2019, 1, i + 1, tzinfo=_TZ_UTC),
        }) for i in range(amount)
    ]
예제 #9
0
def test_args_bad_value(testapp, args, error):
    """Todatetime processor has to validate input arguments."""

    with pytest.raises(ValueError) as excinfo:
        next(todatetime.process(testapp, [], **args))
    assert str(excinfo.value) == error