Skip to content

Commit

Permalink
test: refactor test following rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxime Vergez committed May 12, 2023
1 parent 17bd2ff commit 9b9f147
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
2 changes: 0 additions & 2 deletions backend/gn_module_export/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
from geonature.tests.fixtures import *
from geonature.tests.fixtures import _session, app, users

from gn_module_export.tests.fixtures import export, export_query
1 change: 1 addition & 0 deletions backend/gn_module_export/tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,5 @@ def export_synthese_sinp_query(export_synthese_sinp):
export_synthese_sinp.view_name,
export_synthese_sinp.schema_name,
geometry_field=export_synthese_sinp.geometry_field,
limit=10
)
31 changes: 20 additions & 11 deletions backend/gn_module_export/tests/test_utils/test_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,56 @@
import pytest

from gn_module_export.utils.export import _export_as_file, export_as_file
from gn_module_export.tests.fixtures import (
export_synthese_sinp,
export_synthese_sinp_query,
)


@pytest.mark.usefixtures("temporary_transaction")
class TestUtilsExport:
def test_export_as_file(self, synthese_data, export, export_query):
def test_export_as_file(
self, synthese_data, export_synthese_sinp, export_synthese_sinp_query
):
with tempfile.NamedTemporaryFile(suffix=".csv") as f:
export_as_file(export, "csv", f.name, export_query)
export_as_file(
export_synthese_sinp, "csv", f.name, export_synthese_sinp_query
)

def test_export_csv(self, synthese_data, export_query):
def test_export_csv(self, synthese_data, export_synthese_sinp_query):
with tempfile.NamedTemporaryFile(suffix=".csv") as f:
_export_as_file("csv", f.name, export_query)
_export_as_file("csv", f.name, export_synthese_sinp_query)
with open(f.name, "r") as csvfile:
dialect = csv.Sniffer().sniff(csvfile.read(1024))
csvfile.seek(0)
reader = csv.DictReader(csvfile, dialect=dialect)
ids = {int(row["id_synthese"]) for row in reader}
ids_synthese = {
synthese["id_synthese"] for synthese in export_query.return_query()["items"]
synthese["id_synthese"]
for synthese in export_synthese_sinp_query.return_query()["items"]
}
assert ids_synthese == ids

def test_export_geojson(self, synthese_data, export_query):
def test_export_geojson(self, synthese_data, export_synthese_sinp_query):
with tempfile.NamedTemporaryFile(suffix=".geojson") as f:
_export_as_file("geojson", f.name, export_query)
_export_as_file("geojson", f.name, export_synthese_sinp_query)
with open(f.name, "r") as json_file:
res_geojson = json.load(json_file)

assert res_geojson["type"] == "FeatureCollection"
assert len(res_geojson["features"]) > 0

def test_export_json(self, synthese_data, export_query):
def test_export_json(self, synthese_data, export_synthese_sinp_query):
with tempfile.NamedTemporaryFile(suffix=".json") as f:
_export_as_file("json", f.name, export_query)
_export_as_file("json", f.name, export_synthese_sinp_query)
with open(f.name, "r") as json_file:
res_json = json.load(json_file)

assert len(res_json) > 0

def test_export_geopackage(self, synthese_data, export_query):
def test_export_geopackage(self, synthese_data, export_synthese_sinp_query):
file_name = "/tmp/test_fiona.gpkg"
_export_as_file("gpkg", file_name, export_query)
_export_as_file("gpkg", file_name, export_synthese_sinp_query)

with fiona.open(file_name, "r", "GPKG", overwrite=True) as gpkg:
result = {data["properties"]["id_synthese"] for data in gpkg}
Expand Down

0 comments on commit 9b9f147

Please sign in to comment.