From aebf11a0cf6141f287fcbe40fbb094eeb702ec2a Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Thu, 7 Jul 2022 12:31:49 +0200 Subject: start adding some unit tests --- tests/test_functional.py | 13 ----------- tests/test_views.py | 23 ------------------- tests/unit/test_util.py | 57 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 36 deletions(-) delete mode 100644 tests/test_functional.py delete mode 100644 tests/test_views.py create mode 100644 tests/unit/test_util.py (limited to 'tests') diff --git a/tests/test_functional.py b/tests/test_functional.py deleted file mode 100644 index 6102063..0000000 --- a/tests/test_functional.py +++ /dev/null @@ -1,13 +0,0 @@ -from fietsboek import models - -def test_my_view_success(testapp, dbsession): - model = models.MyModel(name='one', value=55) - dbsession.add(model) - dbsession.flush() - - res = testapp.get('/', status=200) - assert res.body - -def test_notfound(testapp): - res = testapp.get('/badurl', status=404) - assert res.status_code == 404 diff --git a/tests/test_views.py b/tests/test_views.py deleted file mode 100644 index 08347b6..0000000 --- a/tests/test_views.py +++ /dev/null @@ -1,23 +0,0 @@ -from fietsboek import models -from fietsboek.views.default import my_view -from fietsboek.views.notfound import notfound_view - - -def test_my_view_failure(app_request): - info = my_view(app_request) - assert info.status_int == 500 - -def test_my_view_success(app_request, dbsession): - model = models.MyModel(name='one', value=55) - dbsession.add(model) - dbsession.flush() - - info = my_view(app_request) - assert app_request.response.status_int == 200 - assert info['one'].name == 'one' - assert info['project'] == 'fietsboek' - -def test_notfound_view(app_request): - info = notfound_view(app_request) - assert app_request.response.status_int == 404 - assert info == {} diff --git a/tests/unit/test_util.py b/tests/unit/test_util.py new file mode 100644 index 0000000..c595e4e --- /dev/null +++ b/tests/unit/test_util.py @@ -0,0 +1,57 @@ +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) -- cgit v1.2.3