from datetime import timedelta
import pytest
from markupsafe import Markup
from fietsboek import util
@pytest.mark.parametrize('md_source, expected', [
("**foobar**", Markup("
foobar
")),
("*foobar*", Markup("foobar
")),
("# foobar", Markup("foobar
")),
("",
Markup("<script>alert('evil')</script>")),
])
def test_safe_markdown(md_source, expected):
assert util.safe_markdown(md_source) == expected
@pytest.mark.parametrize('timestamp, fixed', [
("2022-03-14T13:37:42", "2022-03-14T13:37:42"),
("2022-03-14T13:37:42Z", "2022-03-14T13:37:42+00:00"),
("2022-03-14T13:37:42+02:00", "2022-03-14T13:37:42+02:00"),
])
def test_fix_iso_timestamp(timestamp, fixed):
assert util.fix_iso_timestamp(timestamp) == fixed
@pytest.mark.parametrize('delta, multiple, expected', [
(
timedelta(minutes=42),
timedelta(minutes=15),
timedelta(minutes=45),
),
(
timedelta(minutes=33),
timedelta(minutes=15),
timedelta(minutes=30),
),
(
timedelta(minutes=-12),
timedelta(minutes=15),
timedelta(minutes=-15),
),
(
timedelta(minutes=-31),
timedelta(minutes=15),
timedelta(minutes=-30),
),
])
def test_round_timedelta_to_multiple(delta, multiple, expected):
assert util.round_timedelta_to_multiple(delta, multiple) == expected
@pytest.mark.parametrize('mps, kph', [(1, 3.6), (10, 36)])
def test_mps_to_kph(mps, kph):
assert util.mps_to_kph(mps) == pytest.approx(kph, 0.1)