Skip to content

Commit

Permalink
build: Merge branch 'feat/data-entry-geojson' into staging
Browse files Browse the repository at this point in the history
  • Loading branch information
josebui committed Oct 25, 2023
2 parents 6d05893 + a93cec3 commit 1b80fd2
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 3 deletions.
2 changes: 1 addition & 1 deletion terraso_backend/apps/core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def post(self, request, **kwargs):
geojson = parse_file_to_geojson(file)
except ValueError as error:
return JsonResponse(
{"errors": [{"message": json.dumps([{"code": error.message}])}]}, status=400
{"errors": [{"message": json.dumps([{"code": str(error)}])}]}, status=400
)

return JsonResponse({"geojson": geojson})
Expand Down
2 changes: 1 addition & 1 deletion terraso_backend/apps/graphql/schema/data_entries.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def resolve_url(self, info):
def resolve_geojson(self, info):
if f".{self.resource_type}" not in settings.DATA_ENTRY_GIS_TYPES.keys():
return None
file = data_entry_upload_service.get_file(self.s3_object_name, "rt")
file = data_entry_upload_service.get_file(self.s3_object_name, "rb")
try:
return parse_file_to_geojson(file)
except ValueError:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def _get_geojson_from_dataset(data_entry, visualization):


def _get_geojson_from_gis(data_entry):
file = data_entry_upload_service.get_file(data_entry.s3_object_name, "rt")
file = data_entry_upload_service.get_file(data_entry.s3_object_name, "rb")
return parse_file_to_geojson(file)


Expand Down
15 changes: 15 additions & 0 deletions terraso_backend/tests/graphql/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,21 @@ def data_entry_kml(users, groups):
)


@pytest.fixture
def data_entry_shapefile(users, groups):
creator = users[0]
creator_group = groups[0]
creator_group.members.add(creator)
return mixer.blend(
DataEntry,
created_by=creator,
size=100,
groups=creator_group,
entry_type=DataEntry.ENTRY_TYPE_FILE,
resource_type="zip",
)


@pytest.fixture
def visualization_config_current_user(users, data_entry_current_user_file, groups):
creator = users[0]
Expand Down
47 changes: 47 additions & 0 deletions terraso_backend/tests/graphql/test_shared_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@
import json
import os
import tempfile
import zipfile
from unittest import mock

import geopandas as gpd
import pytest

from apps.core.gis.utils import DEFAULT_CRS

from ..core.gis.test_parsers import KML_CONTENT, KML_GEOJSON

pytestmark = pytest.mark.django_db
Expand Down Expand Up @@ -345,6 +349,49 @@ def test_data_entry_kml_to_geojson(get_file_mock, client_query, data_entry_kml,
assert data_entry_result["geojson"] == json.dumps(KML_GEOJSON)


@mock.patch("apps.shared_data.services.data_entry_upload_service.get_file")
def test_data_entry_shapefil_to_geojson(get_file_mock, client_query, data_entry_shapefile):
gdf = gpd.GeoDataFrame({"geometry": gpd.points_from_xy([0], [0])}, crs=DEFAULT_CRS)
with tempfile.TemporaryDirectory() as tmpdir:
shapefile_zip = tempfile.NamedTemporaryFile(suffix=".zip")
shapefile_path = os.path.join(tmpdir, "test.shp")
gdf.to_file(shapefile_path)

with zipfile.ZipFile(shapefile_zip.name, "w") as zf:
for component in ["shp", "shx", "prj"]:
zf.write(os.path.join(tmpdir, f"test.{component}"), f"test.{component}")

with open(shapefile_zip.name, "rb") as file:
get_file_mock.return_value = file
response = client_query(
"""
{dataEntry(id: "%s") {
id
name
geojson
}}
"""
% data_entry_shapefile.id
)
json_response = response.json()
data_entry_result = json_response["data"]["dataEntry"]

assert data_entry_result["id"] == str(data_entry_shapefile.id)
assert data_entry_result["name"] == data_entry_shapefile.name
assert json.loads(data_entry_result["geojson"]) == {
"type": "FeatureCollection",
"features": [
{
"id": "0",
"type": "Feature",
"properties": {},
"geometry": {"type": "Point", "coordinates": [0.0, 0.0]},
}
],
"crs": {"type": "name", "properties": {"name": "urn:ogc:def:crs:OGC::CRS84"}},
}


@mock.patch("apps.shared_data.services.data_entry_upload_service.get_file")
def test_data_entry_avoid_fetching_file_for_not_gis_file(get_file_mock, client_query, data_entries):
response = client_query(
Expand Down

0 comments on commit 1b80fd2

Please sign in to comment.