From 114274f900904070e941c4a544426b5d9d1267d2 Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Fri, 31 Mar 2023 23:57:59 +0200 Subject: hide ugliness of tile url function We basically do the same hacky trick in two different places, so maybe we should put it into a separate function, test it, and if a better implementation arises, swap it. --- tests/unit/test_util.py | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'tests') diff --git a/tests/unit/test_util.py b/tests/unit/test_util.py index b35f218..949acb5 100644 --- a/tests/unit/test_util.py +++ b/tests/unit/test_util.py @@ -82,3 +82,12 @@ def test_tour_metadata(gpx_file): @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) + + +def test_tile_url(app_request): + route_url = util.tile_url(app_request, "tile-proxy", provider="bobby") + + assert "{x}" in route_url + assert "{y}" in route_url + assert "{z}" in route_url + assert "bobby" in route_url -- cgit v1.2.3 From 982d6c8cd5ba6ade04683e5699fc9fc170e4c109 Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Sat, 1 Apr 2023 13:05:40 +0200 Subject: fix round_to_multiple for second-level accuracy It was ovious that this is broken when you try to round "0s" to second-level granularity, and you end up with "1s". The problem comes from the fact that we use the integer divison when checking whether we should round up or down, but then also use strict inequality. To fix this, we now also round down if the second_offset is equal to the halfway point (which in the case of second-level granularity is 0). --- tests/unit/test_util.py | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'tests') diff --git a/tests/unit/test_util.py b/tests/unit/test_util.py index 949acb5..0ac5c33 100644 --- a/tests/unit/test_util.py +++ b/tests/unit/test_util.py @@ -29,6 +29,11 @@ def test_fix_iso_timestamp(timestamp, fixed): @pytest.mark.parametrize('delta, multiple, expected', [ + ( + timedelta(seconds=0), + timedelta(seconds=1), + timedelta(seconds=0), + ), ( timedelta(minutes=42), timedelta(minutes=15), -- cgit v1.2.3 From e37e056c36272e5f0c0e23bd993290208e14a979 Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Sat, 1 Apr 2023 13:08:02 +0200 Subject: add some very basic tests for profiles I'd like to have more, but this is a start (and already caught some errors, see the last two commits). --- tests/playwright/test_profiles.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 tests/playwright/test_profiles.py (limited to 'tests') diff --git a/tests/playwright/test_profiles.py b/tests/playwright/test_profiles.py new file mode 100644 index 0000000..40d8ba8 --- /dev/null +++ b/tests/playwright/test_profiles.py @@ -0,0 +1,27 @@ +from playwright.sync_api import Page, expect + +from .conftest import Helper + + +def test_forbidden(page: Page, playwright_helper: Helper): + john = playwright_helper.john_doe() + + with page.expect_response(lambda resp: resp.status == 403): + page.goto(f"/user/{john.id}") + + +def test_profile(page: Page, playwright_helper: Helper): + playwright_helper.login() + + page.goto("/") + page.get_by_role("button", name="User").click() + page.get_by_role("link", name="Profile").click() + + expect(page.locator("#profileLength")).to_have_text("0 km") + expect(page.locator("#profileUphill")).to_have_text("0 m") + expect(page.locator("#profileDownhill")).to_have_text("0 m") + expect(page.locator("#profileMovingTime")).to_have_text("0:00:00") + expect(page.locator("#profileStoppedTime")).to_have_text("0:00:00") + expect(page.locator("#profileMaxSpeed")).to_have_text("0 km/h") + expect(page.locator("#profileAvgSpeed")).to_have_text("0 km/h") + expect(page.locator("#profileNumberOfTracks")).to_have_text("0") -- cgit v1.2.3 From 78ea2acd5950919ee65960ef8d87359d223a0068 Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Sat, 1 Apr 2023 13:14:17 +0200 Subject: fix isolated running of unit tests They caused issues because they might not have created the database tables or the data directory. Since the cleanup job runs globally after every test, it should take that into consideration and not error out. --- tests/conftest.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) (limited to 'tests') diff --git a/tests/conftest.py b/tests/conftest.py index a499bec..d4394cd 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -12,7 +12,7 @@ import pytest import transaction import webtest -from sqlalchemy import delete, select +from sqlalchemy import delete, inspect, select from fietsboek import main, models from fietsboek.data import DataManager @@ -59,12 +59,18 @@ def data_manager(app_settings): def _cleanup_data(app_settings): yield engine = models.get_engine(app_settings) + db_meta = inspect(engine) with engine.begin() as connection: for table in reversed(Base.metadata.sorted_tables): - connection.execute(table.delete()) - data_dir = Path(app_settings["fietsboek.data_dir"]) - if (data_dir / "tracks").is_dir(): - shutil.rmtree(data_dir / "tracks") + # The unit tests don't always set up the tables, so be gentle when + # tearing them down + if db_meta.has_table(table.name): + connection.execute(table.delete()) + # The unit tests also often don't have a data directory, so be gentle here as well + if "fietsboek.data_dir" in app_settings: + data_dir = Path(app_settings["fietsboek.data_dir"]) + if (data_dir / "tracks").is_dir(): + shutil.rmtree(data_dir / "tracks") @pytest.fixture(scope='session') def app(app_settings, dbengine, tmp_path_factory): -- cgit v1.2.3 From e3b9e0e1de60acb441794c36b1aee55ff8f1d94f Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Sat, 1 Apr 2023 13:17:40 +0200 Subject: remove import across toplevel Python seems to do fine, but pylint complains (probably rightfully, since the tests do not represent packages). We lose type hinting for the playwright_helper, but there's probably a better way to do it in the future. --- tests/playwright/test_profiles.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'tests') diff --git a/tests/playwright/test_profiles.py b/tests/playwright/test_profiles.py index 40d8ba8..7e5fb3c 100644 --- a/tests/playwright/test_profiles.py +++ b/tests/playwright/test_profiles.py @@ -1,16 +1,14 @@ from playwright.sync_api import Page, expect -from .conftest import Helper - -def test_forbidden(page: Page, playwright_helper: Helper): +def test_forbidden(page: Page, playwright_helper): john = playwright_helper.john_doe() with page.expect_response(lambda resp: resp.status == 403): page.goto(f"/user/{john.id}") -def test_profile(page: Page, playwright_helper: Helper): +def test_profile(page: Page, playwright_helper): playwright_helper.login() page.goto("/") -- cgit v1.2.3