Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiasschaub committed Nov 14, 2023
1 parent 88fdbff commit ff78c58
Show file tree
Hide file tree
Showing 7 changed files with 3,897 additions and 771 deletions.
960 changes: 960 additions & 0 deletions tests/fixtures/cassette/test_generate_quality_report

Large diffs are not rendered by default.

1,528 changes: 817 additions & 711 deletions tests/fixtures/cassette/test_get_report

Large diffs are not rendered by default.

960 changes: 960 additions & 0 deletions tests/fixtures/cassette/test_task

Large diffs are not rendered by default.

984 changes: 984 additions & 0 deletions tests/fixtures/cassette/test_uuid

Large diffs are not rendered by default.

107 changes: 107 additions & 0 deletions tests/fixtures/cassette/uuid
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
interactions:
- request:
body: null
headers:
Accept:
- '*/*'
Accept-Encoding:
- gzip, deflate, br
Connection:
- keep-alive
User-Agent:
- python-requests/2.31.0
method: GET
uri: https://maps.heigit.org/osm-carto/service?SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&FORMAT=image%2Fpng&TRANSPARENT=FALSE&LAYERS=heigit%3Aosm-carto%402xx&WIDTH=1716&HEIGHT=1436&SRS=EPSG%3A3857&STYLES=&BBOX=964445.3646475708%2C6343463.48326091%2C967408.255014792%2C6345943.466874749
response:
body:
string: !!binary |
iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAADElEQVR4nGNgYGAAAAAEAAH2FzhV
AAAAAElFTkSuQmCC
headers:
Access-control-allow-origin:
- '*'
Cache-Control:
- max-age=86400
Connection:
- Upgrade, Keep-Alive
Content-length:
- '753661'
Content-type:
- image/png
Date:
- Thu, 09 Nov 2023 14:57:50 GMT
Expires:
- Fri, 10 Nov 2023 14:57:50 GMT
Keep-Alive:
- timeout=5, max=100
Server:
- Apache/2.4.52 (Ubuntu)
Strict-Transport-Security:
- max-age=63072000; includeSubdomains;
Upgrade:
- h2
status:
code: 200
message: OK
- request:
body: '{"bpolys": {"type": "FeatureCollection", "features": [{"type": "Feature",
"geometry": {"type": "Polygon", "coordinates": [[[8.66376011761138, 49.40266507327297],
[8.690376214631833, 49.40266507327297], [8.690376214631833, 49.41716014123875],
[8.66376011761138, 49.41716014123875], [8.66376011761138, 49.40266507327297]]]}}]}}'
headers:
Accept:
- '*/*'
Accept-Encoding:
- gzip, deflate, br
Connection:
- keep-alive
Content-Length:
- '325'
Content-Type:
- application/json
User-Agent:
- python-requests/2.31.0
method: POST
uri: https://oqt.ohsome.org/api/reports/sketchmap-fitness
response:
body:
string: '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">

<html><head>

<title>308 Permanent Redirect</title>

</head><body>

<h1>Permanent Redirect</h1>

<p>The document has moved <a href="https://api.quality.ohsome.org/v1/reports/sketchmap-fitness">here</a>.</p>

<hr>

<address>Apache/2.4.52 (Ubuntu) Server at oqt.ohsome.org Port 443</address>

</body></html>

'
headers:
Connection:
- Keep-Alive
Content-Length:
- '350'
Content-Type:
- text/html; charset=iso-8859-1
Date:
- Thu, 09 Nov 2023 14:57:52 GMT
Keep-Alive:
- timeout=5, max=100
Location:
- https://api.quality.ohsome.org/v1/reports/sketchmap-fitness
Server:
- Apache/2.4.52 (Ubuntu)
Strict-Transport-Security:
- max-age=63072000; includeSubdomains;
status:
code: 308
message: Permanent Redirect
version: 1
19 changes: 13 additions & 6 deletions tests/integration/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,24 @@ def celery_config(postgres_container, redis_container):
CELERY_CONFIG["broker_url"] = redis_container["connection_url"]
return CELERY_CONFIG

# @pytest.fixture(scope="session", autouse=True)
# def celery_app(celery_session_app, celery_config):
# """Configure Celery test app."""
# smt_celery_app.conf.update(celery_config)
# celery_session_app.conf.update(celery_config)
# return smt_celery_app

@pytest.fixture(scope="session", autouse=True)
def celery_app(celery_config):
def celery_session_app(celery_session_app, celery_config):
"""Configure Celery test app."""
smt_celery_app.conf.update(celery_config)
return smt_celery_app
celery_session_app.conf.update(celery_config)
return celery_session_app


@pytest.fixture(autouse=True)
def celery_worker(celery_worker):
return celery_worker
@pytest.fixture(scope="session", autouse=True)
def celery_session_worker(celery_session_worker):
return celery_session_worker


@pytest.fixture(scope="session")
Expand Down Expand Up @@ -195,7 +202,7 @@ def sketch_map_buffer():


@pytest.fixture
def map_frame_buffer():
def map_frame_buffgr():
"""Map frame of original Sketch Map."""
with open(str(FIXTURE_DIR / "map-frame.png"), "rb") as file:
return BytesIO(file.read())
Expand Down
110 changes: 56 additions & 54 deletions tests/integration/test_routes.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import os.path
from pathlib import Path
from io import BytesIO
from time import sleep
from uuid import UUID
from sketch_map_tool.database import client_flask as db_client_flask

import fitz
import geojson
Expand Down Expand Up @@ -29,26 +31,35 @@ def params(request):

@pytest.fixture(scope="session")
@vcr.use_cassette
def uuid(params, flask_client):
def uuid(params, flask_client, flask_app, celery_session_app, tmp_path_factory):
response = flask_client.post("/create/results", data=params, follow_redirects=True)

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

# Wait for task to be finished and retrieve result (the sketch map)
with flask_app.app_context():
id_ = db_client_flask.get_async_result_id(uuid, "sketch-map")
task = celery_session_app.AsyncResult(id_)
result = task.get(timeout=60)

@pytest.fixture(scope="session")
def sketch_map_pdf(uuid, tmp_path_factory, celery_app) -> Path:
# Write sketch map to temporary test directory
fn = tmp_path_factory.mktemp(uuid, numbered=False) / "sketch-map.pdf"
probe_status_endpoint(uuid, "sketch-map")
response = requests.get(f"http://localhost:8081/api/download/{uuid}/sketch-map")
with open(fn, "wb") as file:
file.write(response.content)
return fn
file.write(result.getbuffer())

return uuid


@pytest.fixture(scope="session")
def sketch_map_pdf(uuid, tmp_path_factory) -> bytes:
path = tmp_path_factory.getbasetemp() / uuid / "sketch-map.pdf"
with open(path, "rb") as file:
return file.read()


def draw_line_on_png(png):
Expand All @@ -65,17 +76,15 @@ def draw_line_on_png(png):

@pytest.fixture(scope="session")
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()

png = draw_line_on_png(png) # mock sketches on map

buffer = BytesIO()
path = tmp_path_factory.getbasetemp() / uuid / "sketch-map.png"
png.save(path, output="png")
return path
with open(path, "rb") as file:
return file.read()


@pytest.fixture(scope="session")
Expand All @@ -94,16 +103,12 @@ def uuid_result_uploaded(sketch_map_png):

def test_sketch_map_pdf(sketch_map_pdf):
# test if 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 if 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

Expand All @@ -121,61 +126,58 @@ def test_create_results_post(params, flask_client):
assert url_rest == "/create/results"


def test_api_status_uuid_sketch_map(uuid):
# for 30 seconds check status
probe_status_endpoint(uuid, "sketch-map")
# def test_api_status_uuid_sketch_map(uuid):
# # for 30 seconds check status
# probe_status_endpoint(uuid, "sketch-map")


def test_api_download_uuid_sketch_map(uuid):
# for 30 seconds check status
probe_status_endpoint(uuid, "sketch-map")
# def test_api_download_uuid_sketch_map(uuid):
# # for 30 seconds check status
# probe_status_endpoint(uuid, "sketch-map")

response = requests.get(f"http://localhost:8081/api/download/{uuid}/sketch-map")
assert response.status_code == 200
assert len(response.content) > 0
# response = requests.get(f"http://localhost:8081/api/download/{uuid}/sketch-map")
# assert response.status_code == 200
# assert len(response.content) > 0


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

# Extract UUID from response
url_parts = response.url.rsplit("/")
url_parts = response.request.path.rsplit("/")
uuid = url_parts[-1]
url_rest = "/".join(url_parts[:-1])

assert UUID(uuid).version == 4
assert url_rest == "http://localhost:8081/digitize/results"
assert url_rest == "/digitize/results"


def probe_status_endpoint(uuid, endpoint):
"""Wait for computations to be finished and status to return 200 or fail."""
# for 30 seconds check status
for _ in range(30):
response = requests.get(f"http://localhost:8081/api/status/{uuid}/{endpoint}")
assert response.status_code in (200, 202)
result = response.json()
# def probe_status_endpoint(uuid, endpoint):
# """Wait for computations to be finished and status to return 200 or fail."""
# # for 30 seconds check status
# for _ in range(30):
# response = requests.get(f"http://localhost:8081/api/status/{uuid}/{endpoint}")
# assert response.status_code in (200, 202)
# result = response.json()

status = result.pop("status")
assert status in ("SUCCESS", "PENDING", "RETRY", "STARTED")
# status = result.pop("status")
# assert status in ("SUCCESS", "PENDING", "RETRY", "STARTED")

href = result.pop("href", None)
assert result == {"id": uuid, "type": endpoint}
# href = result.pop("href", None)
# assert result == {"id": uuid, "type": endpoint}

if response.status_code == 200:
assert href == "/api/download/" + uuid + f"/{endpoint}"
assert status == "SUCCESS"
break
sleep(1)
assert response.status_code == 200, "Status not 200 after 30 seconds"
# if response.status_code == 200:
# assert href == "/api/download/" + uuid + f"/{endpoint}"
# assert status == "SUCCESS"
# break
# sleep(1)
# assert response.status_code == 200, "Status not 200 after 30 seconds"


def test_api_status_uuid_raster_and_vector_results(uuid_result_uploaded):
probe_status_endpoint(uuid_result_uploaded, "raster-results")
probe_status_endpoint(uuid_result_uploaded, "vector-results")
# def test_api_status_uuid_raster_and_vector_results(uuid_result_uploaded):
# probe_status_endpoint(uuid_result_uploaded, "raster-results")
# probe_status_endpoint(uuid_result_uploaded, "vector-results")


def test_api_download_uuid_vector_result(uuid_result_uploaded):
Expand Down

0 comments on commit ff78c58

Please sign in to comment.