Skip to content

Commit

Permalink
[Fixes #233] add test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
mattiagiupponi committed Apr 22, 2024
1 parent 9b80331 commit 7491390
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
FROM geonode/geonode-base:latest-ubuntu-22.04

RUN rm -rf /usr/src/geonode
RUN git clone https://github.com/GeoNode/geonode.git /usr/src/geonode
RUN mkdir -p /usr/src/importer

Expand Down
19 changes: 19 additions & 0 deletions importer/handlers/common/tests_vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from importer.handlers.common.vector import BaseVectorFileHandler, import_with_ogr2ogr
from django.contrib.auth import get_user_model
from importer import project_dir
from importer.handlers.gpkg.handler import GPKGFileHandler
from importer.orchestrator import orchestrator
from geonode.base.populate_test_data import create_single_dataset
from geonode.resource.models import ExecutionRequest
Expand All @@ -23,6 +24,7 @@ def setUpClass(cls):
cls.handler = BaseVectorFileHandler()
cls.valid_gpkg = f"{project_dir}/tests/fixture/valid.gpkg"
cls.invalid_gpkg = f"{project_dir}/tests/fixture/invalid.gpkg"
cls.no_crs_gpkg = f"{project_dir}/tests/fixture/noCrsTable.gpkg"
cls.user, _ = get_user_model().objects.get_or_create(username="admin")
cls.invalid_files = {"base_file": cls.invalid_gpkg}
cls.valid_files = {"base_file": cls.valid_gpkg}
Expand Down Expand Up @@ -296,3 +298,20 @@ def test_import_with_ogr2ogr_without_errors_should_call_the_right_command_if_dum
self.assertTrue("-f PGDump /vsistdout/" in _call_as_string)
self.assertTrue("psql -d" in _call_as_string)
self.assertFalse("-f PostgreSQL PG" in _call_as_string)

def test_select_valid_layers(self):
"""
The function should return only the datasets with a geometry
The other one are discarded
"""
all_layers = GPKGFileHandler().get_ogr2ogr_driver().Open(self.no_crs_gpkg)

with self.assertLogs(level="ERROR") as _log:
valid_layer = GPKGFileHandler()._select_valid_layers(all_layers)

self.assertIn(
"The following layer layer_styles does not have a Coordinate Reference System (CRS) and will be skipped.",
[x.message for x in _log.records],
)
self.assertEqual(1, len(valid_layer))
self.assertEqual("mattia_test", valid_layer[0].GetName())
26 changes: 25 additions & 1 deletion importer/tests/end2end/test_end2end.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def setUpClass(cls) -> None:
super().setUpClass()
cls.valid_gkpg = f"{project_dir}/tests/fixture/valid.gpkg"
cls.valid_geojson = f"{project_dir}/tests/fixture/valid.geojson"
cls.no_crs_gpkg = f"{project_dir}/tests/fixture/noCrsTable.gpkg"
file_path = gisdata.VECTOR_DATA
filename = os.path.join(file_path, "san_andres_y_providencia_highway.shp")
cls.valid_shp = {
Expand Down Expand Up @@ -101,8 +102,12 @@ def _assertimport(self, payload, initial_name, overwrite=False, last_update=None
)
self.assertTrue(dataset.exists())

# check if the resource is in geoserver
resources = self.cat.get_resources()
print("#################")
print(dataset.first().title)
print([y.name for y in resources])
print("#################")
# check if the resource is in geoserver
self.assertTrue(dataset.first().title in [y.name for y in resources])
if overwrite:
self.assertTrue(dataset.first().last_updated > last_update)
Expand Down Expand Up @@ -131,6 +136,25 @@ def test_import_geopackage(self):
if layer:
self.cat.delete(layer)

def test_import_geopackage_with_no_crs_table(self):
layer = self.cat.get_layer("geonode:mattia_test")
if layer:
self.cat.delete(layer)
payload = {
"base_file": open(self.no_crs_gpkg, "rb"),
}
initial_name = "mattia_test"
with self.assertLogs(level="ERROR") as _log:
self._assertimport(payload, initial_name)

self.assertIn(
"The following layer layer_styles does not have a Coordinate Reference System (CRS) and will be skipped.",
[x.message for x in _log.records],
)
layer = self.cat.get_layer("geonode:mattia_test")
if layer:
self.cat.delete(layer)

@mock.patch.dict(os.environ, {"GEONODE_GEODATABASE": "test_geonode_data"})
@override_settings(
GEODATABASE_URL=f"{geourl.split('/geonode_data')[0]}/test_geonode_data"
Expand Down
Binary file added importer/tests/fixture/noCrsTable.gpkg
Binary file not shown.
2 changes: 1 addition & 1 deletion runtest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
set -a
. ./.env_test
set +a
coverage run --append --source='.' /usr/src/geonode/manage.py test importer -v2 --noinput
coverage run --append --source='.' /usr/src/geonode/manage.py test importer -v2 --noinput

0 comments on commit 7491390

Please sign in to comment.