Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiasschaub committed Jul 16, 2024
1 parent 1ea2414 commit 847bde9
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 29 deletions.
2 changes: 1 addition & 1 deletion docs/development-setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ pytest

To get live logs, INFO log level and ignore verbose logging messages of VCR run:
```bash
pytest -s --log-level="INFO" --log-disable="vcr"
pytest --capture=no --log-level="INFO" --log-disable="vcr"
```

The integration test suite utilizes the [Testcontainers framework](https://testcontainers.com/)
Expand Down
4 changes: 2 additions & 2 deletions sketch_map_tool/database/client_flask.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ def select_file_name(id_: int) -> str:

def select_map_frame(uuid: UUID) -> tuple[bytes, str, str]:
"""Select map frame, bbox and layer of the associated UUID."""
query = "SELECT file, bbox, layer FROM map_frame WHERE uuid = %s"
query = "SELECT file FROM map_frame WHERE uuid = %s"
db_conn = open_connection()
with db_conn.cursor() as curs:
try:
Expand All @@ -210,7 +210,7 @@ def select_map_frame(uuid: UUID) -> tuple[bytes, str, str]:
N_("The file with the id: {UUID} does not exist anymore"),
{"UUID", uuid},
)
return raw
return raw[0]
else:
raise CustomFileNotFoundError(
N_(
Expand Down
5 changes: 4 additions & 1 deletion sketch_map_tool/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,10 @@ def digitize_results_post(lang="en") -> Response:
layers = dict()
map_frames = dict()
for uuid in set(uuids): # Only retrieve map_frame once per uuid to save memory
map_frame, bbox, layer = db_client_flask.select_map_frame(UUID(uuid))
# NOTE: bbox and layer could be return once per uuid from DB here
# instead of multiple times from QR code above.
# But this does not work with legacy map frames (version 2024.04.15).
map_frame = db_client_flask.select_map_frame(UUID(uuid))
map_frames[uuid] = to_array(BytesIO(map_frame).read())
for bbox, layer, uuid in zip(bboxes_, layers_, uuids):
bboxes[uuid] = bbox
Expand Down

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions tests/integration/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ def sketch_map(uuid_create, tmp_path_factory) -> bytes:
def map_frame(uuid_create, flask_app, tmp_path_factory) -> BytesIO:
"""Map Frame as PNG."""
with flask_app.app_context():
map_frame, _, _ = db_client_flask.select_map_frame(UUID(uuid_create))
map_frame = db_client_flask.select_map_frame(UUID(uuid_create))
path = tmp_path_factory.getbasetemp() / uuid_create / "map-frame.png"
with open(path, "wb") as file:
file.write(map_frame)
Expand Down Expand Up @@ -331,7 +331,7 @@ def map_frame_marked(
) -> NDArray:
"""Sketch map frame with markings as PNG."""
with flask_app.app_context():
map_frame, _, _ = db_client_flask.select_map_frame(UUID(uuid_create))
map_frame = db_client_flask.select_map_frame(UUID(uuid_create))
return clip(
to_array(sketch_map_marked),
to_array(map_frame),
Expand Down
12 changes: 5 additions & 7 deletions tests/integration/test_database_client_celery.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,8 @@ def test_write_map_frame(flask_app, map_frame, bbox, format_, orientation, layer
uuid = uuid4()
client_celery.insert_map_frame(map_frame, uuid, bbox, format_, orientation, layer)
with flask_app.app_context():
file, bbox, layer = client_flask.select_map_frame(uuid)
file = client_flask.select_map_frame(uuid)
assert isinstance(file, bytes)
assert bbox == str(bbox)
assert layer == (layer)


def test_delete_map_frame(flask_app, map_frame, bbox, format_, orientation, layer):
Expand All @@ -111,7 +109,7 @@ def test_cleanup_map_frames_recent(
client_celery.cleanup_map_frames()
with flask_app.app_context():
# should not raise an error / should not delete the map frame
map_frame_, _, _ = client_flask.select_map_frame(UUID(uuid_create))
map_frame_ = client_flask.select_map_frame(UUID(uuid_create))
assert map_frame_ == map_frame.getvalue()


Expand All @@ -128,7 +126,7 @@ def test_cleanup_map_frames_recent_with_consent(
client_celery.cleanup_map_frames()
with flask_app.app_context():
# should not raise an error / should not delete the map frame
map_frame_received, _, _ = client_flask.select_map_frame(UUID(uuid_create))
map_frame_received = client_flask.select_map_frame(UUID(uuid_create))
assert map_frame_received == map_frame.getvalue()


Expand All @@ -146,7 +144,7 @@ def test_cleanup_map_frames_recent_without_consent(
client_celery.cleanup_map_frames()
with flask_app.app_context():
# should not raise an error / should not delete the map frame
map_frame_received, _, _ = client_flask.select_map_frame(UUID(uuid_create))
map_frame_received = client_flask.select_map_frame(UUID(uuid_create))
assert map_frame_received == map_frame.getvalue()


Expand All @@ -163,7 +161,7 @@ def test_cleanup_map_frames_old_with_consent(
client_celery.cleanup_map_frames()
# should not raise an error / should not delete the map frame
with flask_app.app_context():
map_frame_received, _, _ = client_flask.select_map_frame(UUID(uuid_create))
map_frame_received = client_flask.select_map_frame(UUID(uuid_create))
assert map_frame_received == map_frame_old.getvalue()


Expand Down
25 changes: 20 additions & 5 deletions tests/integration/test_database_client_flask.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,26 @@ def test_get_async_result_id(flask_app, uuid):

def test_insert_files(flask_app, files, uuid_create, bbox, layer):
with flask_app.app_context():
file_ids, uuids, file_names, bboxes, layers = client_flask.insert_files(files, consent=True)
file_ids, uuids, file_names, bboxes, layers = client_flask.insert_files(
files,
consent=True,
)
assert len(file_ids) == len(uuids) == len(file_names) == 2
for i, (id, uuid, name, bbox_, layer_) in enumerate(zip(file_ids, uuids, file_names, bboxes, layers)):
for i, (
id,
uuid,
name,
bbox_,
layer_,
) in enumerate(
zip(
file_ids,
uuids,
file_names,
bboxes,
layers,
)
):
assert isinstance(id, int) # file id
assert uuid == uuid_create
assert name == files[i].filename
Expand Down Expand Up @@ -121,10 +138,8 @@ def test_select_file_name(file_ids):

def test_select_map_frame(flask_app, uuid_create, bbox, layer):
with flask_app.app_context():
file, bbox_, layer_ = client_flask.select_map_frame(uuid_create)
file = client_flask.select_map_frame(uuid_create)
assert isinstance(file, bytes)
assert bbox_ == str(bbox)
assert layer_ == str(layer)


def test_select_map_frame_file_not_found(flask_app):
Expand Down
30 changes: 20 additions & 10 deletions tests/integration/test_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,18 @@


@pytest.fixture
def map_frame_legacy(flask_app, uuid_create, map_frame):
"""Mock map frame which is uploaded a year ago."""
def map_frame_legacy_2024_04_15(flask_app, uuid_create, map_frame):
"""Legacy map frames in DB do not have bbox, lon, lat and format set."""
with flask_app.app_context():
select_query = "SELECT bbox, lat, lon, format, orientation, layer, version FROM map_frame WHERE uuid = %s"
update_query = "UPDATE map_frame SET bbox = NULL, lat = NULL, lon = NULL, format = NULL, orientation = NULL, layer = NULL, version = NULL WHERE uuid = %s"
select_query = """
SELECT bbox, lat, lon, format, orientation, layer, version
FROM map_frame WHERE uuid = %s
"""
update_query = """
UPDATE map_frame SET bbox = NULL, lat = NULL, lon = NULL, format = NULL,
orientation = NULL, layer = NULL, version = NULL
WHERE uuid = %s
"""
with client_flask.open_connection().cursor() as curs:
curs.execute(select_query, [uuid_create])
vals = curs.fetchone()
Expand All @@ -22,9 +29,14 @@ def map_frame_legacy(flask_app, uuid_create, map_frame):

map_frame.seek(0)
with flask_app.app_context():
update_query = "UPDATE map_frame SET bbox = %s, lat = %s, lon = %s, format = %s, orientation = %s, layer = %s, version = %s WHERE uuid = %s"
update_query = """
UPDATE map_frame
SET bbox = %s, lat = %s, lon = %s, format = %s,
orientation = %s, layer = %s, version = %s
WHERE uuid = %s
"""
with client_flask.open_connection().cursor() as curs:
curs.execute(update_query, vals+(uuid_create,))
curs.execute(update_query, vals + tuple([uuid_create]))


def get_consent_flag_from_db(file_name: str) -> bool:
Expand Down Expand Up @@ -80,15 +92,13 @@ def test_digitize_results_post_no_consent(sketch_map_marked, flask_client):
with app.app_context():
assert get_consent_flag_from_db(unique_file_name) is False

# TODO: check consent flag in database


def test_digitize_results_legacy_2024_04_15(
sketch_map_marked,
map_frame_legacy,
map_frame_legacy_2024_04_15,
flask_client,
):
"""Legacy map frames in DB do not have bbox set."""
"""Legacy map frames in DB do not have bbox, lon, lat and format set."""
unique_file_name = str(uuid4())
data = {"file": [(BytesIO(sketch_map_marked), unique_file_name)], "consent": "True"}
response = flask_client.post("/digitize/results", data=data, follow_redirects=True)
Expand Down

0 comments on commit 847bde9

Please sign in to comment.