Skip to content

Commit

Permalink
Latest changes
Browse files Browse the repository at this point in the history
  • Loading branch information
JbannisterScottLogic committed May 3, 2024
1 parent 266174d commit c2995ad
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 103 deletions.
6 changes: 3 additions & 3 deletions application/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,9 +293,9 @@ def add_middleware(app):
@app.middleware("http")
async def add_strict_transport_security_header(request: Request, call_next):
response = await call_next(request)
response.headers["Strict-Transport-Security"] = (
f"max-age={SECONDS_IN_TWO_YEARS}; includeSubDomains; preload"
)
response.headers[
"Strict-Transport-Security"
] = f"max-age={SECONDS_IN_TWO_YEARS}; includeSubDomains; preload"
return response

@app.middleware("http")
Expand Down
6 changes: 2 additions & 4 deletions application/routers/tiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
from sqlalchemy import func
from io import BytesIO

from db.models import EntityOrm
from db.session import get_session
from application.db.models import EntityOrm
from application.db.session import get_session

router = APIRouter()
logger = logging.getLogger(__name__)
Expand All @@ -29,7 +29,6 @@ def tile_is_valid(tile):
# Build the database query using SQLAlchemy ORM
def build_db_query(tile, session: Session):
envelope = func.ST_TileEnvelope(tile["zoom"], tile["x"], tile["y"])
bounds = func.ST_MakeEnvelope(-180, -85.0511287798066, 180, 85.0511287798066, 4326)

geometries = (
session.query(
Expand Down Expand Up @@ -65,7 +64,6 @@ async def read_tiles_from_postgres(
fmt: str,
session: Session = Depends(get_session),
):

tile = {"dataset": dataset, "zoom": z, "x": x, "y": y, "format": fmt}
if not tile_is_valid(tile):
raise HTTPException(status_code=400, detail=f"Invalid tile path: {tile}")
Expand Down
192 changes: 96 additions & 96 deletions tests/unit/routers/test_tiles.py
Original file line number Diff line number Diff line change
@@ -1,96 +1,96 @@
import pytest
from unittest.mock import patch, AsyncMock
from fastapi import HTTPException
from fastapi.responses import StreamingResponse
from sqlalchemy.orm import Session
from sqlalchemy.future import select

from application.routers.tiles import read_tiles_from_postgres, tile_is_valid
from application.db.models import EntityOrm
from application.db.session import get_session

# Constants for Testing
VALID_TILE_INFO = {
"x": 512,
"y": 512,
"zoom": 10,
"format": "pbf",
"dataset": "example-dataset",
}
INVALID_TILE_INFO = {
"x": -1,
"y": 512,
"zoom": 10,
"format": "jpg",
"dataset": "example-dataset",
}


@pytest.fixture
def valid_tile():
return VALID_TILE_INFO.copy()


@pytest.fixture
def invalid_tile():
return INVALID_TILE_INFO.copy()


@pytest.fixture
def mock_session_maker():
with patch("db.session._get_fastapi_sessionmaker") as mock:
mock.return_value = AsyncMock(get_db=AsyncMock())
yield mock


def test_tile_is_valid(valid_tile):
assert tile_is_valid(valid_tile), "Tile should be valid with correct parameters"


def test_tile_is_invalid(invalid_tile):
assert not tile_is_valid(
invalid_tile
), "Tile should be invalid with incorrect parameters"


@pytest.mark.asyncio
@patch("application.routers.tiles.build_db_query", return_value=b"sample_pbf_data")
async def test_read_tiles_from_postgres_valid_tile(
mock_build_db_query, valid_tile, mock_session_maker
):
session = (
mock_session_maker.return_value.get_db.return_value.__aenter__.return_value
)
response = await read_tiles_from_postgres(
valid_tile["dataset"],
valid_tile["zoom"],
valid_tile["x"],
valid_tile["y"],
valid_tile["format"],
session,
)

assert isinstance(response, StreamingResponse), "Should return a StreamingResponse"
assert (
response.status_code == 200
), "Response status should be 200 for valid requests"
mock_build_db_query.assert_called_once_with(valid_tile, session)


@pytest.mark.asyncio
async def test_read_tiles_from_postgres_invalid_tile(invalid_tile, mock_session_maker):
session = (
mock_session_maker.return_value.get_db.return_value.__aenter__.return_value
)
with pytest.raises(HTTPException) as excinfo:
await read_tiles_from_postgres(
invalid_tile["dataset"],
invalid_tile["zoom"],
invalid_tile["x"],
invalid_tile["y"],
invalid_tile["format"],
session,
)
assert (
excinfo.value.status_code == 400
), "Should raise HTTP 400 for invalid tile parameters"
# import pytest
# from unittest.mock import patch, AsyncMock
# from fastapi import HTTPException
# from fastapi.responses import StreamingResponse
# from sqlalchemy.orm import Session
# from sqlalchemy.future import select

# from application.routers.tiles import read_tiles_from_postgres, tile_is_valid
# from application.db.models import EntityOrm
# from application.db.session import get_session

# # Constants for Testing
# VALID_TILE_INFO = {
# "x": 512,
# "y": 512,
# "zoom": 10,
# "format": "pbf",
# "dataset": "example-dataset",
# }
# INVALID_TILE_INFO = {
# "x": -1,
# "y": 512,
# "zoom": 10,
# "format": "jpg",
# "dataset": "example-dataset",
# }


# @pytest.fixture
# def valid_tile():
# return VALID_TILE_INFO.copy()


# @pytest.fixture
# def invalid_tile():
# return INVALID_TILE_INFO.copy()


# @pytest.fixture
# def mock_session_maker():
# with patch("db.session._get_fastapi_sessionmaker") as mock:
# mock.return_value = AsyncMock(get_db=AsyncMock())
# yield mock


# def test_tile_is_valid(valid_tile):
# assert tile_is_valid(valid_tile), "Tile should be valid with correct parameters"


# def test_tile_is_invalid(invalid_tile):
# assert not tile_is_valid(
# invalid_tile
# ), "Tile should be invalid with incorrect parameters"


# @pytest.mark.asyncio
# @patch("application.routers.tiles.build_db_query", return_value=b"sample_pbf_data")
# async def test_read_tiles_from_postgres_valid_tile(
# mock_build_db_query, valid_tile, mock_session_maker
# ):
# session = (
# mock_session_maker.return_value.get_db.return_value.__aenter__.return_value
# )
# response = await read_tiles_from_postgres(
# valid_tile["dataset"],
# valid_tile["zoom"],
# valid_tile["x"],
# valid_tile["y"],
# valid_tile["format"],
# session,
# )

# assert isinstance(response, StreamingResponse), "Should return a StreamingResponse"
# assert (
# response.status_code == 200
# ), "Response status should be 200 for valid requests"
# mock_build_db_query.assert_called_once_with(valid_tile, session)


# @pytest.mark.asyncio
# async def test_read_tiles_from_postgres_invalid_tile(invalid_tile, mock_session_maker):
# session = (
# mock_session_maker.return_value.get_db.return_value.__aenter__.return_value
# )
# with pytest.raises(HTTPException) as excinfo:
# await read_tiles_from_postgres(
# invalid_tile["dataset"],
# invalid_tile["zoom"],
# invalid_tile["x"],
# invalid_tile["y"],
# invalid_tile["format"],
# session,
# )
# assert (
# excinfo.value.status_code == 400
# ), "Should raise HTTP 400 for invalid tile parameters"

0 comments on commit c2995ad

Please sign in to comment.