diff options
author | Daniel Schadt <kingdread@gmx.de> | 2023-03-04 18:24:11 +0100 |
---|---|---|
committer | Daniel Schadt <kingdread@gmx.de> | 2023-03-04 18:24:11 +0100 |
commit | e9025533b86228f7073a672269c939c5ae5db8e2 (patch) | |
tree | 8a98178672ac4fa5e42e136098c0406cd6c838d0 | |
parent | 63779a61151babf9dc60d55acc81e3a97811a60e (diff) | |
download | fietsboek-e9025533b86228f7073a672269c939c5ae5db8e2.tar.gz fietsboek-e9025533b86228f7073a672269c939c5ae5db8e2.tar.bz2 fietsboek-e9025533b86228f7073a672269c939c5ae5db8e2.zip |
add tests for the new transformer
-rw-r--r-- | fietsboek/templates/details.jinja2 | 20 | ||||
-rw-r--r-- | tests/assets/Synthetic_Steep_Slope.gpx.gz | bin | 0 -> 883 bytes | |||
-rw-r--r-- | tests/assets/Synthetic_Zero_Elevation.gpx.gz | bin | 0 -> 791 bytes | |||
-rw-r--r-- | tests/playwright/test_transformers.py | 87 | ||||
-rw-r--r-- | tests/testutils.py | 24 |
5 files changed, 118 insertions, 13 deletions
diff --git a/fietsboek/templates/details.jinja2 b/fietsboek/templates/details.jinja2 index 2ff492c..7c1ba8c 100644 --- a/fietsboek/templates/details.jinja2 +++ b/fietsboek/templates/details.jinja2 @@ -88,46 +88,46 @@ <tbody> <tr> <th scope="row">{{ _("page.details.date") }}</th> - <td>{{ track.date | format_datetime }}</td> + <td id="detailsDate">{{ track.date | format_datetime }}</td> </tr> {% if show_organic %} <tr> <th scope="row">{{ _("page.details.start_time") }}</th> - <td>{{ track.start_time | format_datetime }}</td> + <td id="detailsStartTime">{{ track.start_time | format_datetime }}</td> </tr> <tr> <th scope="row">{{ _("page.details.end_time") }}</th> - <td>{{ track.end_time | format_datetime }}</td> + <td id="detailsEndTime">{{ track.end_time | format_datetime }}</td> </tr> {% endif %} <tr> <th scope="row">{{ _("page.details.length") }}</th> - <td>{{ (track.length / 1000) | round(2) | format_decimal }} km</td> + <td id="detailsLength">{{ (track.length / 1000) | round(2) | format_decimal }} km</td> </tr> <tr> <th scope="row">{{ _("page.details.uphill") }}</th> - <td>{{ track.uphill | round(2) | format_decimal }} m</td> + <td id="detailsUphill">{{ track.uphill | round(2) | format_decimal }} m</td> </tr> <tr> <th scope="row">{{ _("page.details.downhill") }}</th> - <td>{{ track.downhill | round(2) | format_decimal }} m</td> + <td id="detailsDownhill">{{ track.downhill | round(2) | format_decimal }} m</td> </tr> {% if show_organic %} <tr> <th scope="row">{{ _("page.details.moving_time") }}</th> - <td>{{ track.moving_time }}</td> + <td id="detailsMovingTime">{{ track.moving_time }}</td> </tr> <tr> <th scope="row">{{ _("page.details.stopped_time") }}</th> - <td>{{ track.stopped_time }}</td> + <td id="detailsStoppedTime">{{ track.stopped_time }}</td> </tr> <tr> <th scope="row">{{ _("page.details.max_speed") }}</th> - <td>{{ mps_to_kph(track.max_speed) | round(2) | format_decimal }} km/h</td> + <td id="detailsMaxSpeed">{{ mps_to_kph(track.max_speed) | round(2) | format_decimal }} km/h</td> </tr> <tr> <th scope="row">{{ _("page.details.avg_speed") }}</th> - <td>{{ mps_to_kph(track.avg_speed) | round(2) | format_decimal }} km/h</td> + <td id="detailsAvgSpeed">{{ mps_to_kph(track.avg_speed) | round(2) | format_decimal }} km/h</td> </tr> {% endif %} </tbody> diff --git a/tests/assets/Synthetic_Steep_Slope.gpx.gz b/tests/assets/Synthetic_Steep_Slope.gpx.gz Binary files differnew file mode 100644 index 0000000..244f78a --- /dev/null +++ b/tests/assets/Synthetic_Steep_Slope.gpx.gz diff --git a/tests/assets/Synthetic_Zero_Elevation.gpx.gz b/tests/assets/Synthetic_Zero_Elevation.gpx.gz Binary files differnew file mode 100644 index 0000000..94abb0a --- /dev/null +++ b/tests/assets/Synthetic_Zero_Elevation.gpx.gz diff --git a/tests/playwright/test_transformers.py b/tests/playwright/test_transformers.py new file mode 100644 index 0000000..0fc4ffc --- /dev/null +++ b/tests/playwright/test_transformers.py @@ -0,0 +1,87 @@ +from playwright.sync_api import Page, expect +from sqlalchemy import select + +from testutils import extract_and_upload +from fietsboek import models + + +def test_transformer_zero_elevation_disabled(page: Page, playwright_helper, tmp_path, dbaccess): + playwright_helper.login() + + page.goto("/") + page.get_by_text("Upload").click() + + extract_and_upload(page, "Synthetic_Zero_Elevation.gpx.gz", tmp_path) + + page.locator(".btn", has_text="Upload").click() + + # Once we have finished the upload, extract the ID of the track and check + # the properties + new_track_id = int(page.url.rsplit("/", 1)[1]) + track = dbaccess.execute(select(models.Track).filter_by(id=new_track_id)).scalar_one() + + assert track.cache.uphill > 160 + expect(page.locator("#detailsUphill")).to_contain_text("167.7 m") + + +def test_transformer_zero_elevation_enabled(page: Page, playwright_helper, tmp_path, dbaccess): + playwright_helper.login() + + page.goto("/") + page.get_by_text("Upload").click() + + extract_and_upload(page, "Synthetic_Zero_Elevation.gpx.gz", tmp_path) + + page.locator("#transformer-heading-1 .accordion-button").click() + page.locator("#transformer-enabled-1").click() + + page.locator(".btn", has_text="Upload").click() + + # Once we have finished the upload, extract the ID of the track and check + # the properties + new_track_id = int(page.url.rsplit("/", 1)[1]) + track = dbaccess.execute(select(models.Track).filter_by(id=new_track_id)).scalar_one() + + assert track.cache.uphill < 0.1 + expect(page.locator("#detailsUphill")).to_contain_text("0 m") + + +def test_transformer_steep_slope_disabled(page: Page, playwright_helper, tmp_path, dbaccess): + playwright_helper.login() + + page.goto("/") + page.get_by_text("Upload").click() + + extract_and_upload(page, "Synthetic_Steep_Slope.gpx.gz", tmp_path) + + page.locator(".btn", has_text="Upload").click() + + # Once we have finished the upload, extract the ID of the track and check + # the properties + new_track_id = int(page.url.rsplit("/", 1)[1]) + track = dbaccess.execute(select(models.Track).filter_by(id=new_track_id)).scalar_one() + + assert track.cache.uphill > 60 + expect(page.locator("#detailsUphill")).to_contain_text("61.54 m") + + +def test_transformer_steep_slope_enabled(page: Page, playwright_helper, tmp_path, dbaccess): + playwright_helper.login() + + page.goto("/") + page.get_by_text("Upload").click() + + extract_and_upload(page, "Synthetic_Steep_Slope.gpx.gz", tmp_path) + + page.locator("#transformer-heading-1 .accordion-button").click() + page.locator("#transformer-enabled-1").click() + + page.locator(".btn", has_text="Upload").click() + + # Once we have finished the upload, extract the ID of the track and check + # the properties + new_track_id = int(page.url.rsplit("/", 1)[1]) + track = dbaccess.execute(select(models.Track).filter_by(id=new_track_id)).scalar_one() + + assert track.cache.uphill < 2 + expect(page.locator("#detailsUphill")).to_contain_text("1.2 m") diff --git a/tests/testutils.py b/tests/testutils.py index 3ddbdbe..810bdf7 100644 --- a/tests/testutils.py +++ b/tests/testutils.py @@ -2,19 +2,37 @@ import gzip from pathlib import Path +from playwright.sync_api import Page -def load_gpx_asset(filename): + +def load_gpx_asset(filename: str) -> bytes: """Load a GPX test asset. This looks in the tests/assets/ folder, reads and unzips the file and returns its contents. :param filename: Name of the asset to load. - :type filename: str :return: The content of the asset as bytes. - :rtype: bytes """ asset_dir = Path(__file__).parent / 'assets' test_file = asset_dir / filename with gzip.open(test_file, 'rb') as fobj: return fobj.read() + + +def extract_and_upload(page: Page, filename: str, tmp_path: Path): + """Extracts the given test asset, fills in the upload form and presses + upload. + + :param page: The playwright page on which to execute the actions. + :param filename: The filename. + :param tmp_path: The temporary path (as given by pytest). + """ + gpx_data = load_gpx_asset(filename) + gpx_path = tmp_path / "Upload.gpx" + with open(gpx_path, "wb") as gpx_fobj: + gpx_fobj.write(gpx_data) + + page.get_by_label("GPX file").set_input_files(gpx_path) + + page.locator(".bi-upload").click() |