Skip to content

refactor: remove unused code #537

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
33 changes: 0 additions & 33 deletions sketch_map_tool/database/client_flask.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@
from werkzeug.utils import secure_filename

from sketch_map_tool.config import get_config_value
from sketch_map_tool.definitions import REQUEST_TYPES
from sketch_map_tool.exceptions import (
CustomFileDoesNotExistAnymoreError,
CustomFileNotFoundError,
UUIDNotFoundError,
)
from sketch_map_tool.helpers import N_, to_array
from sketch_map_tool.models import Bbox, Layer
Expand All @@ -33,37 +31,6 @@ def close_connection(e=None):
db_conn.close()


# TODO: Legacy support: Delete this function after PR 515 has been deployed for 1 day
def _select_id_map(uuid) -> dict:
query = "SELECT map FROM uuid_map WHERE uuid = %s"
db_conn = open_connection()
with db_conn.cursor() as curs:
curs.execute(query, [uuid])
raw = curs.fetchall()
if raw:
return raw[0][0]
else:
raise UUIDNotFoundError(
N_("There are no tasks in the broker for UUID: {UUID}"), {"UUID": uuid}
)


# TODO: Legacy support: Delete this function after PR 515 has been deployed for 1 day
def get_async_result_id(request_uuid: str, request_type: REQUEST_TYPES) -> str:
"""Get the Celery Async Result IDs for a request."""
map_ = _select_id_map(request_uuid)
try:
return map_[request_type] # AsyncResult ID
except KeyError as error:
raise UUIDNotFoundError(
N_(
"There are no tasks in the broker for UUID and request type:"
" {REQUEST_UUID}, {REQUEST_TYPE}"
),
{"REQUEST_UUID": request_uuid, "REQUEST_TYPE": request_type},
) from error


def insert_files(
files, consent: bool
) -> tuple[list[int], list[str], list[str], list[Bbox], list[Layer]]:
Expand Down
26 changes: 3 additions & 23 deletions sketch_map_tool/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
send_from_directory,
url_for,
)
from psycopg2.errors import UndefinedTable
from werkzeug import Response

from sketch_map_tool import celery_app, config, definitions, tasks
Expand Down Expand Up @@ -166,22 +165,6 @@ def create_results_post(lang="en") -> Response:
)


def get_async_result_id(uuid: str, type_: REQUEST_TYPES):
"""Get Celery Async or Group Result UUID for given request UUID.

Try to get Celery UUID for given request from datastore.
If no Celery UUID has been found the request UUID is the same as the Celery UUID.

This function exists only for legacy support.
"""
# TODO: Legacy support: Delete this function after PR 515 has been deployed
# for 1 day
try:
return db_client_flask.get_async_result_id(uuid, type_)
except (UUIDNotFoundError, UndefinedTable):
return uuid


@app.get("/create/results")
@app.get("/<lang>/create/results")
@app.get("/create/results/<uuid>")
Expand All @@ -197,8 +180,7 @@ def create_results_get(
validate_bbox(bbox)
validate_uuid(uuid)
# Check if celery tasks for UUID exists
id_ = get_async_result_id(uuid, "sketch-map")
_ = get_async_result(id_, "sketch-map")
_ = get_async_result(uuid, "sketch-map")
return render_template("create-results.html", lang=lang, bbox=bbox)


Expand Down Expand Up @@ -311,8 +293,7 @@ def status(uuid: str, type_: REQUEST_TYPES, lang="en") -> Response:
validate_uuid(uuid)
validate_type(type_)

id_ = get_async_result_id(uuid, type_)
async_result = get_async_result(id_, type_)
async_result = get_async_result(uuid, type_)

href = ""
info = ""
Expand Down Expand Up @@ -370,8 +351,7 @@ def download(uuid: str, type_: REQUEST_TYPES, lang="en") -> Response:
validate_uuid(uuid)
validate_type(type_)

id_ = get_async_result_id(uuid, type_)
async_result = get_async_result(id_, type_)
async_result = get_async_result(uuid, type_)

# Abort if result not ready or failed.
# No nice error message here because user should first check /api/status.
Expand Down
18 changes: 1 addition & 17 deletions tests/unit/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from celery.result import AsyncResult, GroupResult
from werkzeug.datastructures import FileStorage

from sketch_map_tool.exceptions import QRCodeError, UUIDNotFoundError
from sketch_map_tool.exceptions import QRCodeError
from sketch_map_tool.models import Bbox, Layer, PaperFormat, Size
from sketch_map_tool.routes import app
from tests import FIXTURE_DIR
Expand Down Expand Up @@ -171,22 +171,6 @@ def files(file):
return [file, file]


@pytest.fixture(autouse=True)
def mock_request_task_mapping(monkeypatch):
"""Mock every request id to task id mapping .

This mapping is only present because of legacy support.
"""

def raise_(exception):
raise exception

monkeypatch.setattr(
"sketch_map_tool.routes.db_client_flask.get_async_result_id",
lambda *_: raise_(UUIDNotFoundError("")),
)


@pytest.fixture(autouse=True)
def mock_request_download_update_map_frame_downloaded(monkeypatch):
monkeypatch.setattr(
Expand Down
18 changes: 7 additions & 11 deletions tests/unit/test_routes_api_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,26 @@
from celery.result import GroupResult


@pytest.mark.usefixtures("mock_request_task_mapping", "mock_async_result_success")
@pytest.mark.usefixtures("mock_async_result_success")
def test_download_success(client, uuid):
resp = client.get("/api/download/{0}/sketch-map".format(uuid))
assert resp.status_code == 200
assert resp.mimetype == "application/pdf"


@pytest.mark.usefixtures("mock_request_task_mapping", "mock_async_result_started")
@pytest.mark.usefixtures("mock_async_result_started")
def test_download_started(client, uuid):
resp = client.get("/api/download/{0}/sketch-map".format(uuid))
assert resp.status_code == 500


@pytest.mark.usefixtures("mock_request_task_mapping", "mock_async_result_failure")
@pytest.mark.usefixtures("mock_async_result_failure")
def test_download_failure(client, uuid):
resp = client.get("/api/download/{0}/sketch-map".format(uuid))
assert resp.status_code == 500


@pytest.mark.usefixtures("mock_request_task_mapping", "mock_group_result_success")
@pytest.mark.usefixtures("mock_group_result_success")
@pytest.mark.parametrize("type_", ["raster-results", "vector-results"])
def test_group_download_success(client, uuid, type_, monkeypatch):
monkeypatch.setattr(
Expand All @@ -40,31 +40,27 @@ def test_group_download_success(client, uuid, type_, monkeypatch):
assert resp.mimetype in ["application/zip", "application/geo+json"]


@pytest.mark.usefixtures("mock_request_task_mapping", "mock_group_result_started")
@pytest.mark.usefixtures("mock_group_result_started")
@pytest.mark.parametrize("type_", ("raster-results", "vector-results"))
def test_group_started(client, uuid, type_):
resp = client.get("/api/download/{0}/{1}".format(uuid, type_))
assert resp.status_code == 500


@pytest.mark.usefixtures("mock_request_task_mapping", "mock_group_result_failure")
@pytest.mark.usefixtures("mock_group_result_failure")
@pytest.mark.parametrize("type_", ("raster-results", "vector-results"))
def test_group_failure(client, uuid, type_):
resp = client.get("/api/download/{0}/{1}".format(uuid, type_))
assert resp.status_code == 500


@pytest.mark.usefixtures(
"mock_request_task_mapping",
"mock_group_result_started_success_failure",
)
@pytest.mark.usefixtures("mock_group_result_started_success_failure")
@pytest.mark.parametrize("type_", ("raster-results", "vector-results"))
def test_group_started_success_failure(client, uuid, type_):
resp = client.get("/api/download/{0}/{1}".format(uuid, type_))
assert resp.status_code == 500


@pytest.mark.usefixtures("mock_request_task_mapping")
@pytest.mark.parametrize("type_", ("raster-results", "vector-results"))
def test_group_success_failure(
client,
Expand Down
10 changes: 0 additions & 10 deletions tests/unit/test_routes_api_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
def test_status_success(
client,
uuid,
mock_request_task_mapping,
mock_async_result_success,
):
resp = client.get("/api/status/{0}/sketch-map".format(uuid))
Expand All @@ -20,7 +19,6 @@ def test_status_success(
def test_status_started(
client,
uuid,
mock_request_task_mapping,
mock_async_result_started,
):
resp = client.get("/api/status/{0}/sketch-map".format(uuid))
Expand All @@ -36,7 +34,6 @@ def test_status_started(
def test_status_failure(
client,
uuid,
mock_request_task_mapping,
mock_async_result_failure,
):
resp = client.get("/api/status/{0}/sketch-map".format(uuid))
Expand All @@ -51,7 +48,6 @@ def test_status_failure(
def test_status_failure_hard(
client,
uuid,
mock_request_task_mapping,
mock_async_result_failure_hard,
):
resp = client.get("/api/status/{0}/sketch-map".format(uuid))
Expand All @@ -63,7 +59,6 @@ def test_group_status_success(
client,
uuid,
type_,
mock_request_task_mapping,
mock_group_result_success,
):
resp = client.get("/api/status/{0}/{1}".format(uuid, type_))
Expand All @@ -80,7 +75,6 @@ def test_group_status_started(
client,
uuid,
type_,
mock_request_task_mapping,
mock_group_result_started,
):
resp = client.get("/api/status/{0}/{1}".format(uuid, type_))
Expand All @@ -97,7 +91,6 @@ def test_group_status_failure(
client,
uuid,
type_,
mock_request_task_mapping,
mock_group_result_failure,
):
resp = client.get("/api/status/{0}/{1}".format(uuid, type_))
Expand All @@ -114,7 +107,6 @@ def test_group_status_failure_hard(
client,
uuid,
type_,
mock_request_task_mapping,
mock_group_result_failure_hard,
):
resp = client.get("/api/status/{0}/{1}".format(uuid, type_))
Expand All @@ -126,7 +118,6 @@ def test_group_status_started_success_failure(
client,
uuid,
type_,
mock_request_task_mapping,
mock_group_result_started_success_failure,
):
resp = client.get("/api/status/{0}/{1}".format(uuid, type_))
Expand All @@ -144,7 +135,6 @@ def test_group_status_success_failure(
client,
uuid,
type_,
mock_request_task_mapping,
mock_group_result_success_failure,
):
resp = client.get("/api/status/{0}/{1}".format(uuid, type_))
Expand Down