Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Caha committed Dec 6, 2023
1 parent 12b2ac8 commit 1005b5c
Show file tree
Hide file tree
Showing 4 changed files with 198 additions and 0 deletions.
20 changes: 20 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from pathlib import Path

import pytest


@pytest.fixture
def test_data_folder() -> Path:
return Path(__file__).parent / "data"


@pytest.fixture
def test_data_clean(test_data_folder: Path) -> str:
file_path = test_data_folder / "Example_Clean.xml"
return file_path.as_posix()


@pytest.fixture
def test_data_mesh2dm(test_data_folder: Path) -> str:
file_path = test_data_folder / "mesh.2dm"
return file_path.as_posix()
18 changes: 18 additions & 0 deletions tests/test_2dm_reader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from landxmlconvertor.classes.mesh2dm_reader import Mesh2DMReader
from landxmlconvertor.classes.mesh_elements import MeshFace, MeshVertex


def test_read(test_data_mesh2dm):
mesh_2d = Mesh2DMReader(test_data_mesh2dm)

assert isinstance(mesh_2d, Mesh2DMReader)
assert all([isinstance(x, MeshVertex) for x in mesh_2d.points])
assert all([isinstance(x, MeshFace) for x in mesh_2d.faces])

assert all([isinstance(x.id, int) for x in mesh_2d.points])
assert all([isinstance(x.x, float) for x in mesh_2d.points])
assert all([isinstance(x.y, float) for x in mesh_2d.points])
assert all([isinstance(x.z, float) for x in mesh_2d.points])

assert all([isinstance(x.id, int) for x in mesh_2d.faces])
assert all([all([isinstance(y, int) for y in x.points_ids]) for x in mesh_2d.faces])
55 changes: 55 additions & 0 deletions tests/test_conversions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import tempfile
from pathlib import Path

import pytest
from qgis.core import QgsCoordinateReferenceSystem, QgsMeshLayer

from landxmlconvertor.classes.landxml_reader import LandXMLReader
from landxmlconvertor.classes.landxml_writer import LandXMLWriter
from landxmlconvertor.classes.mesh2dm_writer import Mesh2DMWriter


@pytest.mark.parametrize(
"filename",
[
("Example_Clean.xml"),
("Example_Clean_without_schema.xml"),
],
)
def test_landxml_to_2dm(test_data_folder, filename):
current_filename = test_data_folder / filename

land_xml = LandXMLReader(current_filename.as_posix())

mesh_2dm_writer = Mesh2DMWriter(land_xml.all_points, land_xml.all_faces)

with tempfile.TemporaryDirectory() as tmpdir:
tmp_2dm_file = Path(tmpdir) / "file.2dm"

mesh_2dm_writer.write(tmp_2dm_file.as_posix())

mesh = QgsMeshLayer(tmp_2dm_file.as_posix(), "layer", "mdal")

assert mesh.dataProvider().vertexCount() == 264
assert mesh.dataProvider().faceCount() == 362


def test_2dm_to_landxml(test_data_folder):
current_filename = test_data_folder / "mesh.2dm"

mesh_2dm = QgsMeshLayer(current_filename.as_posix(), "layer", "mdal")

with tempfile.TemporaryDirectory() as tmpdir:
tmp_landxml_file = Path(tmpdir) / "file.xml"

landxml_writer = LandXMLWriter()
landxml_writer.add_surface(mesh_2dm)
landxml_writer.write(tmp_landxml_file.as_posix())

landxml_reader = LandXMLReader(tmp_landxml_file.as_posix())

assert landxml_reader.namespace_prefix == "LandXML-1.2:"
assert landxml_reader.crs() == QgsCoordinateReferenceSystem()
assert landxml_reader.surface_count == 1
assert len(landxml_reader.all_faces) == 45
assert len(landxml_reader.all_points) == 29
105 changes: 105 additions & 0 deletions tests/test_landxml_reader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import xml.etree.ElementTree as ET

import pytest
from qgis.core import QgsCoordinateReferenceSystem

from landxmlconvertor.classes.landxml_elements import LandXMLSurface
from landxmlconvertor.classes.landxml_reader import LandXMLReader
from landxmlconvertor.classes.mesh_elements import MeshFace, MeshVertex


@pytest.mark.parametrize(
"filename",
[
("Example_Clean.xml"),
("Example_Clean_without_schema.xml"),
],
)
def test_clean_data(test_data_folder, filename):
current_filename = test_data_folder / filename

land_xml = LandXMLReader(current_filename.as_posix())

assert land_xml.path == current_filename.as_posix()
assert isinstance(land_xml.xml_tree, ET.ElementTree)

assert isinstance(land_xml.xml_root, ET.Element)
assert "LandXML" in land_xml.xml_root.tag

assert land_xml.crs() == QgsCoordinateReferenceSystem()

assert land_xml.surface_count == 3

assert all([isinstance(x, LandXMLSurface) for x in land_xml.surfaces])

assert len(land_xml.get_surface_points(0)) == 29
assert len(land_xml.get_surface_faces(0)) == 45

assert len(land_xml.get_surface_points(1)) == 92
assert len(land_xml.get_surface_faces(1)) == 138

assert len(land_xml.get_surface_points(2)) == 143
assert len(land_xml.get_surface_faces(2)) == 179

assert all([isinstance(x, MeshVertex) for x in land_xml.get_surface_points(0)])
assert all([isinstance(x, MeshFace) for x in land_xml.get_surface_faces(0)])

assert len(land_xml.all_points) == 29 + 92 + 143
assert len(land_xml.all_faces) == 45 + 138 + 179


def test_non_existing_file():
with pytest.raises(FileNotFoundError, match="No such file"):
LandXMLReader("file_that_does_not_exist.xml")


def test_non_LandXML_file(test_data_folder):
filename = test_data_folder / "just_xml.xml"
with pytest.raises(ValueError, match="Not a valid LandXML file"):
LandXMLReader(filename.as_posix())


def test_LandXML_without_surface_1(test_data_folder):
filename = test_data_folder / "land_xml_no_surface.xml"

land_xml = LandXMLReader(filename.as_posix())

assert land_xml.surface_count == 0


def test_LandXML_without_surface_2(test_data_folder):
filename = test_data_folder / "land_xml_empty_surface.xml"

land_xml = LandXMLReader(filename.as_posix())

assert land_xml.surface_count == 1
assert land_xml.surfaces[0].empty()


def test_LandXML_unsupported_schema(test_data_folder):
filename = test_data_folder / "land_xml_with_unsupported_schema.xml"

with pytest.raises(ValueError, match="Unsupported namespace: "):
LandXMLReader(filename.as_posix())


def test_data_1(test_data_folder):
filename = test_data_folder / "Total topp.xml"

landxml_reader = LandXMLReader(filename.as_posix())

assert landxml_reader.crs() == QgsCoordinateReferenceSystem()
assert landxml_reader.surface_count == 1
assert len(landxml_reader.all_faces) == 36093
assert len(landxml_reader.all_points) == 22273


def test_data_2(test_data_folder):
filename = test_data_folder / "3D_modell_underlag_skyfall_20230915.xml"

landxml_reader = LandXMLReader(filename.as_posix())

assert landxml_reader.crs() == QgsCoordinateReferenceSystem("EPSG:3007")
assert landxml_reader.surface_count == 1
assert len(landxml_reader.all_faces) == 6448
assert len(landxml_reader.all_points) == 3330

0 comments on commit 1005b5c

Please sign in to comment.