Skip to content

Commit

Permalink
test: fix or remove tests during project creation
Browse files Browse the repository at this point in the history
  • Loading branch information
spwoodcock committed Feb 12, 2024
1 parent 3d4f233 commit 9615d99
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 45 deletions.
2 changes: 1 addition & 1 deletion src/backend/app/projects/project_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ async def update_project_boundary(
):
"""Update the boundary for a project and update tasks.
TODO this needs a big refactor / removal
TODO this needs a big refactor or removal
#
"""
# verify project exists in db
Expand Down
16 changes: 16 additions & 0 deletions src/backend/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,22 @@ async def project(db, admin_user, organisation):
odk_central_password=os.getenv("ODK_CENTRAL_PASSWD"),
hashtags=["hot-fmtm"],
organisation_id=organisation.id,
outline_geojson={
"type": "Feature",
"properties": {},
"geometry": {
"coordinates": [
[
[85.317028828, 27.7052522097],
[85.317028828, 27.7041424888],
[85.318844411, 27.7041424888],
[85.318844411, 27.7052522097],
[85.317028828, 27.7052522097],
]
],
"type": "Polygon",
},
},
)

odk_creds_decrypted = ODKCentralDecrypted(
Expand Down
104 changes: 60 additions & 44 deletions src/backend/tests/test_projects_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
"""Tests for project routes."""

import functools
import json
import os
import uuid
from io import BytesIO
Expand All @@ -30,8 +29,7 @@
from fastapi.concurrency import run_in_threadpool
from geoalchemy2.elements import WKBElement
from loguru import logger as log
from shapely import Polygon, wkb
from shapely.geometry import shape
from shapely import Polygon

from app.central.central_crud import create_odk_project
from app.config import encrypt_value, settings
Expand Down Expand Up @@ -62,6 +60,18 @@ async def test_create_project(client, admin_user, organisation):
},
"xform_title": "buildings",
"hashtags": ["#FMTM"],
"outline_geojson": {
"coordinates": [
[
[85.317028828, 27.7052522097],
[85.317028828, 27.7041424888],
[85.318844411, 27.7041424888],
[85.318844411, 27.7052522097],
[85.317028828, 27.7052522097],
]
],
"type": "Polygon",
},
}
project_data.update(**odk_credentials.model_dump())

Expand Down Expand Up @@ -97,12 +107,11 @@ async def test_convert_to_app_project():
"""Test conversion ot app project."""
polygon = Polygon(
[
(85.924707758, 26.727727503),
(85.922703741, 26.732440043),
(85.928284549, 26.735158727),
(85.930643709, 26.734365785),
(85.932368686, 26.732372075),
(85.924707758, 26.727727503),
(85.317028828, 27.7052522097),
(85.317028828, 27.7041424888),
(85.318844411, 27.7041424888),
(85.318844411, 27.7052522097),
(85.317028828, 27.7052522097),
]
)

Expand Down Expand Up @@ -142,41 +151,6 @@ async def test_generate_appuser_files(db, project):
project_id = project.id
log.debug(f"Testing project ID: {project_id}")

# Set project boundary
boundary_geojson = json.loads(
json.dumps(
{
"type": "Polygon",
"coordinates": [
[
[8.539551723844, 47.3765788922656],
[8.539551723844, 47.37303247378486],
[8.547135285454686, 47.37303247378486],
[8.547135285454686, 47.3765788922656],
[8.539551723844, 47.3765788922656],
]
],
}
)
)
log.debug(f"Creating project boundary: {boundary_geojson}")
boundary_created = await project_crud.update_project_boundary(
db, project_id, boundary_geojson, 500
)
assert boundary_created is True
# Check updated locations
db_project = await project_crud.get_project_by_id(db, project_id)
# Outline
project_outline = db_project.outline.data.tobytes()
file_outline = shape(boundary_geojson)
assert wkb.loads(project_outline).wkt == file_outline.wkt
# Centroid
project_centroid = wkb.loads(db_project.centroid.data.tobytes()).wkt
file_centroid = file_outline.centroid.wkt
assert project_centroid == file_centroid
# Location string
assert db_project.location_str == "Zurich,Switzerland"

# Load data extracts
data_extracts_file = f"{test_data_path}/building_footprint.zip"
with zipfile.ZipFile(data_extracts_file, "r") as zip_archive:
Expand Down Expand Up @@ -239,6 +213,48 @@ async def test_generate_appuser_files(db, project):
assert result is None


# async def test_update_project_boundary(db, project):
# """Test updating project boundary."""
# project_id = project.id
# log.debug(f"Testing updating boundary for project ID: {project_id}")

# db_project = await project_crud.get_project_by_id(db, project_id)

# # Outline
# boundary_geojson = json.loads(
# json.dumps(
# {
# "type": "Polygon",
# "coordinates": [
# [
# [85.317028828, 27.7052522097],
# [85.317028828, 27.7041424888],
# [85.318844411, 27.7041424888],
# [85.318844411, 27.7052522097],
# [85.317028828, 27.7052522097],
# ]
# ],
# }
# )
# )
# log.debug(f"Creating project boundary: {boundary_geojson}")
# boundary_created = await project_crud.update_project_boundary(
# db, project_id, boundary_geojson, 500
# )
# assert boundary_created is True
# project_outline = db_project.outline.data.tobytes()
# file_outline = shape(boundary_geojson)
# assert wkb.loads(project_outline).wkt == file_outline.wkt

# # Centroid
# project_centroid = wkb.loads(db_project.centroid.data.tobytes()).wkt
# file_centroid = file_outline.centroid.wkt
# assert project_centroid == file_centroid

# # Location string
# assert db_project.location_str == "Zurich,Switzerland"


if __name__ == "__main__":
"""Main func if file invoked directly."""
pytest.main()

0 comments on commit 9615d99

Please sign in to comment.