Skip to content
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,3 @@ werkzeug==3.1.8
# flask
# flask-cors

opengeodeweb-microservice==1.*,>=1.4.0
26 changes: 16 additions & 10 deletions src/opengeodeweb_back/utils_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

# Standard library imports
import base64
import contextlib
import hashlib
import logging
import shutil
Expand All @@ -21,7 +22,10 @@
import flask
import opengeode as og
from defusedxml.ElementTree import parse
from opengeodeweb_microservice.database.connection import get_session
from opengeodeweb_microservice.database.connection import (
DatabaseNotInitializedError,
get_session,
)
from opengeodeweb_microservice.database.data import Data
from opengeodeweb_microservice.schemas import ErrorResponse, SchemaDict

Expand Down Expand Up @@ -68,19 +72,21 @@ def update_last_request_time(current_app: flask.Flask) -> None:


def terminate_session(exception: BaseException | None) -> None:
session = flask.g.pop("session", None)
if session is None:
return
if exception is None:
session.commit()
else:
session.rollback()
session.close()
# The database is closed while import_project replaces the project files, and may be
# reopened during the request: the session to end is the one of the current database
with contextlib.suppress(DatabaseNotInitializedError):
session = get_session()
if exception is None:
session.commit()
else:
session.rollback()
session.close()


def before_request(current_app: flask.Flask) -> None:
# No session here: requests that do not use the database (ping) must answer while
# import_project has it closed, the others fail on their own get_session()
increment_request_counter(current_app)
flask.g.session = get_session()
flask.g.start_time = time.perf_counter()


Expand Down
12 changes: 11 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from opengeodeweb_back.app import create_app, register_ogw_back_blueprints

if TYPE_CHECKING:
from collections.abc import Generator
from collections.abc import Callable, Generator

from flask.ctx import AppContext
from flask.testing import FlaskClient
Expand Down Expand Up @@ -70,6 +70,16 @@ def app_context() -> Generator[AppContext]:
yield ctx


@pytest.fixture
def closed_database() -> Generator[Callable[[], None]]:
# import_project closes the database while it replaces the project files.
# Yields a function reopening it; it is reopened at teardown anyway, even if the test fails
db_path = Path(os.environ["TEST_DB_PATH"])
connection.close_database()
yield lambda: init_database(db_path)
init_database(db_path)


@pytest.fixture
def test_id() -> str:
return TEST_ID
9 changes: 8 additions & 1 deletion tests/test_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

# Third party imports
import opengeode as og
import pytest
from opengeodeweb_microservice.database import connection
from opengeodeweb_microservice.database.connection import get_session

Expand All @@ -37,7 +38,6 @@
from opengeodeweb_back.routes.blueprint_routes import extract_valid_attribute_values

if TYPE_CHECKING:
import pytest
from flask.testing import FlaskClient
from werkzeug.test import TestResponse

Expand Down Expand Up @@ -687,6 +687,13 @@ def test_import_project_route(client: FlaskClient, tmp_path: Path) -> None:
client.application.config["DATA_FOLDER_PATH"] = original_data_folder


@pytest.mark.usefixtures("closed_database")
def test_ping_while_project_database_is_closed(client: FlaskClient) -> None:
# The client keeps pinging while import_project has the database closed
response = client.post("/opengeodeweb_back/ping", json={})
assert response.status_code == 200


def test_save_viewable_workflow_from_object(client: FlaskClient) -> None:
route = "/opengeodeweb_back/create/point_set"
point_data = {
Expand Down
25 changes: 25 additions & 0 deletions tests/test_utils_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

# Standard library imports
import base64
import os
import re
import shutil
import sqlite3
import uuid
import zipfile
from pathlib import Path
Expand Down Expand Up @@ -32,6 +34,8 @@
)

if TYPE_CHECKING:
from collections.abc import Callable

from flask.testing import FlaskClient

from opengeodeweb_back.geode_objects.geode_object import GeodeObject
Expand Down Expand Up @@ -77,6 +81,27 @@ def test_teardown_request() -> None:
assert flask.current_app.config.get("LAST_REQUEST_TIME", 0) >= last_request_time


@pytest.mark.usefixtures("app_context")
def test_teardown_commits_a_session_opened_after_the_database_reopened(
closed_database: Callable[[], None],
) -> None:
# A request started while import_project had the database closed, then the route used it
utils_functions.before_request(flask.current_app)
closed_database()
data = Data.create(
geode_id="teardown_test",
geode_object_type=GeodePolygonalSurface3D.geode_object_type(),
viewer_object_type=GeodePolygonalSurface3D.viewer_object_type(),
viewer_elements_type=GeodePolygonalSurface3D.viewer_elements_type(),
)
data_id = data.id
utils_functions.teardown_request(flask.current_app)
with sqlite3.connect(os.environ["TEST_DB_PATH"]) as raw_connection:
row = raw_connection.execute("SELECT id FROM datas WHERE id = ?", (data_id,)).fetchone()
raw_connection.execute("DELETE FROM datas WHERE id = ?", (data_id,))
assert row is not None


def test_versions() -> None:
list_packages = [
"OpenGeode-core",
Expand Down
Loading