Skip to content

Commit

Permalink
test: add GPX tests
Browse files Browse the repository at this point in the history
  • Loading branch information
paulschreiber committed Nov 28, 2023
1 parent 2943dd4 commit 399351a
Showing 1 changed file with 186 additions and 0 deletions.
186 changes: 186 additions & 0 deletions terraso_backend/tests/core/gis/test_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,36 @@
</Document>
</kml>"""

GPX_CONTENT = """<?xml version="1.0" standalone="yes"?>
<gpx xmlns="http://www.topografix.com/GPX/1/1" version="1.1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd">
<wpt lat="45.52" lon="-122.681944">
<ele>0</ele>
<name><![CDATA[Portland]]></name>
<cmt><![CDATA[Waypoint no: 1]]></cmt>
<desc><![CDATA[This is waypoint no: 1]]></desc>
</wpt>
<wpt lat="-22.908333" lon="-43.196389">
<ele>0</ele>
<name><![CDATA[Rio de Janeiro]]></name>
<cmt><![CDATA[Waypoint no: 2]]></cmt>
<desc><![CDATA[This is waypoint no: 2]]></desc>
</wpt>
<wpt lat="41.01224" lon="28.976018">
<ele>0</ele>
<name><![CDATA[Istanbul]]></name>
<cmt><![CDATA[Waypoint no: 3]]></cmt>
<desc><![CDATA[This is waypoint no: 3]]></desc>
</wpt>
<wpt lat="64.133333" lon="-21.933333">
<ele>0</ele>
<name><![CDATA[Reykjavik]]></name>
<cmt><![CDATA[Waypoint no: 4]]></cmt>
<desc><![CDATA[This is waypoint no: 4]]></desc>
</wpt>
</gpx>"""

KML_GEOJSON = {
"type": "FeatureCollection",
"features": [
Expand Down Expand Up @@ -115,6 +145,132 @@
],
}

GPX_GEOJSON = {
"type": "FeatureCollection",
"features": [
{
"id": "0",
"type": "Feature",
"properties": {
"ele": 0.0,
"time": None,
"magvar": None,
"geoidheight": None,
"name": "Portland",
"cmt": "Waypoint no: 1",
"desc": "This is waypoint no: 1",
"src": None,
"link1_href": None,
"link1_text": None,
"link1_type": None,
"link2_href": None,
"link2_text": None,
"link2_type": None,
"sym": None,
"type": None,
"fix": None,
"sat": None,
"hdop": None,
"vdop": None,
"pdop": None,
"ageofdgpsdata": None,
"dgpsid": None,
},
"geometry": {"type": "Point", "coordinates": [-122.681944, 45.52]},
},
{
"id": "1",
"type": "Feature",
"properties": {
"ele": 0.0,
"time": None,
"magvar": None,
"geoidheight": None,
"name": "Rio de Janeiro",
"cmt": "Waypoint no: 2",
"desc": "This is waypoint no: 2",
"src": None,
"link1_href": None,
"link1_text": None,
"link1_type": None,
"link2_href": None,
"link2_text": None,
"link2_type": None,
"sym": None,
"type": None,
"fix": None,
"sat": None,
"hdop": None,
"vdop": None,
"pdop": None,
"ageofdgpsdata": None,
"dgpsid": None,
},
"geometry": {"type": "Point", "coordinates": [-43.196389, -22.908333]},
},
{
"id": "2",
"type": "Feature",
"properties": {
"ele": 0.0,
"time": None,
"magvar": None,
"geoidheight": None,
"name": "Istanbul",
"cmt": "Waypoint no: 3",
"desc": "This is waypoint no: 3",
"src": None,
"link1_href": None,
"link1_text": None,
"link1_type": None,
"link2_href": None,
"link2_text": None,
"link2_type": None,
"sym": None,
"type": None,
"fix": None,
"sat": None,
"hdop": None,
"vdop": None,
"pdop": None,
"ageofdgpsdata": None,
"dgpsid": None,
},
"geometry": {"type": "Point", "coordinates": [28.976018, 41.01224]},
},
{
"id": "3",
"type": "Feature",
"properties": {
"ele": 0.0,
"time": None,
"magvar": None,
"geoidheight": None,
"name": "Reykjavik",
"cmt": "Waypoint no: 4",
"desc": "This is waypoint no: 4",
"src": None,
"link1_href": None,
"link1_text": None,
"link1_type": None,
"link2_href": None,
"link2_text": None,
"link2_type": None,
"sym": None,
"type": None,
"fix": None,
"sat": None,
"hdop": None,
"vdop": None,
"pdop": None,
"ageofdgpsdata": None,
"dgpsid": None,
},
"geometry": {"type": "Point", "coordinates": [-21.933333, 64.133333]},
},
],
}


@pytest.fixture
def shapefile_zip(request):
Expand Down Expand Up @@ -181,3 +337,33 @@ def test_parse_kml_file(kml_file):

# Assert that the output of the parse_kml_file function is as expected
assert kml_json == KML_GEOJSON


@pytest.fixture
def gpx_file(request):
gpx_contents, file_extension = request.param
# Create a temporary file
with tempfile.NamedTemporaryFile(mode="w", suffix=f".{file_extension}", delete=False) as f:
# Write the GPX content to the file
f.write(gpx_contents)

# Return the file path
yield f.name

# Clean up: delete the temporary file
os.unlink(f.name)


@pytest.mark.parametrize(
"gpx_file",
[
(GPX_CONTENT, "gpx"),
],
indirect=True,
)
def test_parse_gpx_file(gpx_file):
with open(gpx_file, "rb") as file:
gpx_json = parse_file_to_geojson(file)

# Assert that the output of the parse_gpx_file function is as expected
assert gpx_json == GPX_GEOJSON

0 comments on commit 399351a

Please sign in to comment.