Skip to content
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
4 changes: 2 additions & 2 deletions stormpiper/stormpiper/api/endpoints/tileserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ async def get_tile_zxy_redirect(
y: int,
s: str = "none",
tile_registry: dict = Depends(get_tile_registry),
) -> RedirectResponse:
) -> RedirectResponse: # pragma: no cover
url = tile_registry.get(tilename, "").format(**dict(x=x, y=y, z=z, s=s))

if not url:
Expand All @@ -35,7 +35,7 @@ async def get_tile_file_zxy(
y: int,
s: str = "a",
tile_registry: dict = Depends(get_tile_registry),
) -> StreamingResponse:
) -> StreamingResponse: # pragma: no cover
url = tile_registry.get(tilename, "").format(**dict(x=x, y=y, z=z, s=s))

if not url:
Expand Down
25 changes: 11 additions & 14 deletions stormpiper/stormpiper/database/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"pool_size": 5,
# Temporarily exceeds the set pool_size if no connections are available.
"max_overflow": 2,
"pool_timeout": 10, # minutes
"pool_timeout": 10 * 60, # seconds
"pool_recycle": 6 * 3600, # seconds
"pool_pre_ping": True,
}

Expand All @@ -30,22 +31,18 @@
**engine_params,
)

async_engine = create_async_engine(
settings.DATABASE_URL_ASYNC,
**engine_params,
)

_there_can_be_only_one = None

async_session_maker = sessionmaker(
async_engine, class_=AsyncSession, expire_on_commit=False
)

async def get_async_session() -> AsyncIterator[AsyncSession]:
if _there_can_be_only_one is None:
async_engine = create_async_engine(
settings.DATABASE_URL_ASYNC,
**engine_params,
)
async_session_maker = sessionmaker(
async_engine, class_=AsyncSession, expire_on_commit=False
)
else:
async_session_maker = _there_can_be_only_one

async def get_async_session() -> AsyncIterator[AsyncSession]:
async with async_session_maker() as session:
yield session

Expand All @@ -63,7 +60,7 @@ def get_session(engine=engine):
before=before_log(logger, logging.INFO),
after=after_log(logger, logging.WARN),
)
def reconnect_engine(engine=engine):
def reconnect_engine(engine=engine): # pragma: no cover
try:
with engine.begin() as conn:
# this should connect/login and ensure that the database is available.
Expand Down
2 changes: 1 addition & 1 deletion stormpiper/stormpiper/earth_engine/ee.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from ee import FeatureCollection as FeatureCollection
from ee import Geometry, Initialize, ServiceAccountCredentials

if TYPE_CHECKING:
if TYPE_CHECKING: # pragma: no cover

class Image(Any):
...
Expand Down
12 changes: 6 additions & 6 deletions stormpiper/stormpiper/earth_engine/layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
logger = logging.getLogger(__name__)


def _init_url(layer_spec):
def _init_url(layer_spec): # pragma: no cover
logger.debug("calling _init_url")
return __init_url(layer_spec_json=json.dumps(layer_spec, sort_keys=True))


@lru_cache
def __init_url(layer_spec_json):
def __init_url(layer_spec_json): # pragma: no cover
logger.debug("calling __init_url")
layer_spec = json.loads(layer_spec_json)
eeObject = layer_spec.get("layer", {}).get("eeObject")
Expand All @@ -37,7 +37,7 @@ def __init_url(layer_spec_json):


@lru_cache
def _init_layers():
def _init_layers(): # pragma: no cover
logger.debug("calling _init_layers")
layer_json = stormpiper_path / "data" / "ee" / "layers.json"

Expand Down Expand Up @@ -170,7 +170,7 @@ def _init_layers():


@lru_cache
def _init_tile_registry():
def _init_tile_registry(): # pragma: no cover
logger.debug("calling _init_tile_registry")

tile_registry = {}
Expand All @@ -193,11 +193,11 @@ def _init_tile_registry():
return tile_registry


def get_layers():
def get_layers(): # pragma: no cover
logger.debug("calling get_layers")
return _init_layers()


def get_tile_registry():
def get_tile_registry(): # pragma: no cover
logger.debug("calling get_tile_registry")
return _init_tile_registry()
2 changes: 1 addition & 1 deletion stormpiper/stormpiper/earth_engine/loading.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def get_poc_loading_Image(
concentration_path: str,
runoff_band: int | str | None = None,
) -> Image:
if runoff_band is None:
if runoff_band is None: # pragma: no cover
runoff_band = 0

runoff = Image(runoff_path)
Expand Down
6 changes: 3 additions & 3 deletions stormpiper/stormpiper/earth_engine/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ async def async_login(**kwargs):
try:
return await base_async_login(**kwargs)

except Exception as e:
except Exception as e: # pragma: no cover
logger.exception("Error logging in to earth engine", e, exc_info=True)
raise e

Expand All @@ -79,12 +79,12 @@ def login(**kwargs):
try:
return base_login(**kwargs)

except Exception as e:
except Exception as e: # pragma: no cover
logger.exception("Error logging in to earth engine", e, exc_info=True)
raise e


async def ee_continuous_login(sleep_seconds: int = 3600) -> None:
async def ee_continuous_login(sleep_seconds: int = 3600) -> None: # pragma: no cover
logger.debug("Calling ee_continuous_login...")
logged_in = await async_login()

Expand Down
2 changes: 1 addition & 1 deletion stormpiper/stormpiper/site/views/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ async def _init():
)
async def tileservice_view(
request: Request, layers: dict[str, str] = Depends(_init)
) -> Response:
) -> Response: # pragma: no cover
return templates.TemplateResponse(
"tileserver.html", {"request": request, "layers": layers.values()}
)
Expand Down
95 changes: 55 additions & 40 deletions stormpiper/stormpiper/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import asyncio

import pytest
from fastapi.testclient import TestClient

from stormpiper.database.connection import engine
from stormpiper.earth_engine import get_layers, get_tile_registry
from stormpiper.database.connection import engine, get_async_session
from stormpiper.earth_engine import get_tile_registry
from stormpiper.email_helper import email
from stormpiper.factory import create_app

Expand All @@ -21,100 +23,113 @@ def db():
yield


@pytest.fixture(scope="module")
def client(db):
@pytest.fixture
def app(db):
app = create_app()
app.dependency_overrides[get_async_session] = utils.get_async_session
return app


@pytest.fixture
def readonly_token(app):
token = ""
with TestClient(app) as client:
user_token = utils.reader_token(client)
client.headers = {"Authorization": f"Bearer {user_token['access_token']}"}
readonly_user_data = utils.get_my_data(client)
token = readonly_user_data.get("readonly_token", None)
return token


@pytest.fixture
def public_token(app):
token = ""
with TestClient(app) as client:
user_token = utils.public_token(client)
client.headers = {"Authorization": f"Bearer {user_token['access_token']}"}
readonly_user_data = utils.get_my_data(client)
token = readonly_user_data.get("readonly_token", None)
return token


@pytest.fixture
def client(app):
with TestClient(app) as client:
user_token = utils.user_token(client)
client.headers = {"Authorization": f"Bearer {user_token['access_token']}"}
yield client


@pytest.fixture(scope="module")
def admin_client(db):
app = create_app()
@pytest.fixture
def admin_client(app):
with TestClient(app) as client:
user_token = utils.admin_token(client)
client.headers = {"Authorization": f"Bearer {user_token['access_token']}"}
yield client


@pytest.fixture(scope="module")
def user_admin_client(db):
app = create_app()
@pytest.fixture
def user_admin_client(app):
with TestClient(app) as client:
user_token = utils.user_admin_token(client)
client.headers = {"Authorization": f"Bearer {user_token['access_token']}"}
yield client


@pytest.fixture(scope="module")
def readonly_client(db):
app = create_app()
@pytest.fixture
def readonly_client(app):
with TestClient(app) as client:
token = utils.reader_token(client)
client.headers = {"Authorization": f"Bearer {token['access_token']}"}
user_token = utils.reader_token(client)
client.headers = {"Authorization": f"Bearer {user_token['access_token']}"}
yield client


@pytest.fixture(scope="module")
def readonly_token(readonly_client):
client = readonly_client
readonly_user_data = utils.get_my_data(client)
token = readonly_user_data.get("readonly_token", None)
return token


@pytest.fixture(scope="module")
def public_client(db):
app = create_app()
@pytest.fixture
def reg_public_client(app):
with TestClient(app) as client:
token = utils.public_token(client)
client.headers = {"Authorization": f"Bearer {token['access_token']}"}
yield client


@pytest.fixture(scope="module")
def client_local(db):
app = create_app()
@pytest.fixture
def public_client(app):
with TestClient(app) as client:
yield client


@pytest.fixture
def client_local(db):
class Reg:
def get(self, *args):
return "./ping"

override_get_tile_registry = lambda: Reg()

def override_get_layers():
return {
"param": {
"safe_name": "param",
"layer": {"url": "string", "image": "image_obj"},
}
}

app = create_app()
app.dependency_overrides[get_tile_registry] = override_get_tile_registry
app.dependency_overrides[get_layers] = override_get_layers

with TestClient(app) as client:
yield client


@pytest.fixture(scope="module")
@pytest.fixture
def client_lookup(
db,
client,
admin_client,
user_admin_client,
readonly_client,
client_local,
public_client,
reg_public_client,
):
return {
"client": client,
"admin_client": admin_client,
"user_admin_client": user_admin_client,
"editor_client": client,
"readonly_client": readonly_client,
"reg_public_client": reg_public_client,
"public_client": public_client,
"client_local": client_local,
}
Expand Down
1 change: 0 additions & 1 deletion stormpiper/stormpiper/tests/integration/test_ee.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ def test_tileserver_api_response(client, route):
"route",
[
"/api/rest/tileserver/redirect/esri/11/355/821/a",
"/api/rest/tileserver/redirect/carto-db/9/89/206/b",
],
)
@test_utils.with_ee_login
Expand Down
2 changes: 1 addition & 1 deletion stormpiper/stormpiper/tests/integration/test_scenario.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def test_solve_new_scenario(client, blob):
if task_response:
rjson = task_response.json()
assert rjson.get("status", "").lower() == "success"
else:
else: # pragma: no cover
response = client.get(f"/api/rest/tasks/{task_id}")

raise ValueError(
Expand Down
22 changes: 0 additions & 22 deletions stormpiper/stormpiper/tests/integration/test_views.py

This file was deleted.

Loading