Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integration tests #294

Merged
merged 2 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion scripts/flask.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/bash
# Run Flask in debug mode
poetry run flask --app sketch_map_tool/routes.py --debug run
poetry run flask --app sketch_map_tool/routes.py --debug run --port 8081
2 changes: 2 additions & 0 deletions sketch_map_tool/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ def validate_uploaded_sketchmaps(files: list[FileStorage]):
f"You can only upload pictures up to "
f"a total pixel count of {max_pixel_per_image}."
)
del img
file.seek(0)


def validate_uuid(uuid: str):
Expand Down
1 change: 1 addition & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,5 @@ def before_record_response(response):
record_mode="new_episodes",
match_on=["uri", "method"],
before_record_response=replace_body(["image/png"], DUMMY_PNG),
ignore_localhost=True,
)
110 changes: 100 additions & 10 deletions tests/integration/test_create_sketch_map.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,73 @@
from pathlib import Path
from time import sleep
from uuid import UUID

import fitz
import pytest
import requests


@pytest.fixture
def url():
return "http://localhost:8081/create/results"
from tests import vcr_app as vcr


@pytest.fixture
@pytest.fixture(scope="session")
def params():
return {
"format": "A4",
"orientation": "landscape",
"bbox": (
"[964774.123559138,6343689.928073838,967198.3717426618,6345719.012706632]"
"[964445.3646475708,6343463.48326091,967408.255014792,6345943.466874749]"
),
"bboxWGS84": (
"[8.666713409161893,49.40398878092867,8.688490801119476,49.41584842225927]"
"[8.66376011761138,49.40266507327297,8.690376214631833,49.41716014123875]"
),
"size": '{"width": 1716,"height": 1436}',
"scale": "9051.161965312804",
}


@pytest.fixture
def uuid(url, params):
@pytest.fixture(scope="session")
@vcr.use_cassette
def uuid(params):
url = "http://localhost:8081/create/results"
response = requests.post(url, data=params)
url_parts = response.url.rsplit("/")
uuid = url_parts[-1]
return uuid


def test_create_results_post(url, params):
@pytest.fixture(scope="session")
def sketch_map_pdf(uuid, tmp_path_factory) -> Path:
fn = tmp_path_factory.mktemp(uuid, numbered=False) / "sketch-map.pdf"
# for 20 seconds check status
# TODO: decide on time to wait
for _ in range(20):
response = requests.get(f"http://localhost:8081/api/status/{uuid}/sketch-map")
if response.status_code == 200:
response = requests.get(
f"http://localhost:8081/api/download/{uuid}/sketch-map"
)
with open(fn, "wb") as file:
file.write(response.content)
return fn
sleep(1)
raise TimeoutError("Sketch map PDF not created")


@pytest.fixture
def sketch_map_png(sketch_map_pdf, tmp_path_factory, uuid):
with open(sketch_map_pdf, "rb") as file:
sketch_map_pdf = file.read()
pdf = fitz.open(stream=sketch_map_pdf)
page = pdf.load_page(0)
png = page.get_pixmap()
path = tmp_path_factory.getbasetemp() / uuid / "sketch-map.png"
png.save(path, output="png")
return path


@vcr.use_cassette
def test_create_results_post(params):
url = "http://localhost:8081/create/results"
response = requests.post(url, data=params)
assert response.status_code == 200

Expand All @@ -44,3 +77,60 @@ def test_create_results_post(url, params):
url_rest = "/".join(url_parts[:-1])
assert UUID(uuid).version == 4
assert url_rest == "http://localhost:8081/create/results"


def test_api_status_uuid_sketch_map(uuid):
# for 20 seconds check status
# TODO: decide on time to wait
for _ in range(20):
response = requests.get(f"http://localhost:8081/api/status/{uuid}/sketch-map")
assert response.status_code in (200, 202)
if response.status_code == 200:
break
sleep(1)
assert response.status_code == 200


def test_api_download_uuid_sketch_map(uuid):
# for 20 seconds check status
# TODO: decide on time to wait
for _ in range(20):
response = requests.get(f"http://localhost:8081/api/status/{uuid}/sketch-map")
if response.status_code == 200:
response = requests.get(
f"http://localhost:8081/api/download/{uuid}/sketch-map"
)
break
sleep(1)
assert response.status_code == 200


def test_digitize_results_post(sketch_map_png):
url = "http://localhost:8081/digitize/results"
with open(sketch_map_png, "rb") as file:
files = {"file": file}
response = requests.post(url, files=files)
assert response.status_code == 200

# Extract UUID from response
url_parts = response.url.rsplit("/")
uuid = url_parts[-1]
url_rest = "/".join(url_parts[:-1])
assert UUID(uuid).version == 4
assert url_rest == "http://localhost:8081/digitize/results"


def test_sketch_map_pdf(sketch_map_pdf):
# test fixture is created successfully
with open(sketch_map_pdf, "rb") as file:
sketch_map_pdf = file.read()
assert sketch_map_pdf is not None
assert len(sketch_map_pdf) > 0


def test_sketch_map_png(sketch_map_png):
# test fixture is created successfully
with open(sketch_map_png, "rb") as file:
sketch_map_png = file.read()
assert sketch_map_png is not None
assert len(sketch_map_png) > 0
Loading