From 496a8bd5fbd3bb6c4f76e21ddf054946d63df339 Mon Sep 17 00:00:00 2001
From: Alessia Saccardo <s212246@dtu.dk>
Date: Tue, 18 Feb 2025 10:32:36 +0100
Subject: [PATCH] add pytest for mesh conversion function

---
 qim3d/mesh/_common_mesh_methods.py |  5 +++-
 qim3d/tests/mesh/test_mesh.py      | 42 ++++++++++++++++++++++++++++++
 2 files changed, 46 insertions(+), 1 deletion(-)
 create mode 100644 qim3d/tests/mesh/test_mesh.py

diff --git a/qim3d/mesh/_common_mesh_methods.py b/qim3d/mesh/_common_mesh_methods.py
index 317affbd..c062029a 100644
--- a/qim3d/mesh/_common_mesh_methods.py
+++ b/qim3d/mesh/_common_mesh_methods.py
@@ -16,7 +16,7 @@ def from_volume(
         **kwargs: Additional arguments to pass to the Pygel3D volumetric_isocontour function.
 
     Raises:
-        ValueError: If the input volume is not a 3D numpy array.
+        ValueError: If the input volume is not a 3D numpy array or if the input volume is empty.
 
     Returns:
         hmesh.Manifold: A Pygel3D mesh object representing the input volume.
@@ -37,5 +37,8 @@ def from_volume(
     if volume.ndim != 3:
         raise ValueError("The input volume must be a 3D numpy array.")
     
+    if volume.size == 0:
+        raise ValueError("The input volume must not be empty.")
+
     mesh = hmesh.volumetric_isocontour(volume, **kwargs)
     return mesh
\ No newline at end of file
diff --git a/qim3d/tests/mesh/test_mesh.py b/qim3d/tests/mesh/test_mesh.py
new file mode 100644
index 00000000..9e19475b
--- /dev/null
+++ b/qim3d/tests/mesh/test_mesh.py
@@ -0,0 +1,42 @@
+import pytest
+import numpy as np
+from pygel3d import hmesh
+import qim3d
+
+def test_from_volume_valid_input():
+    """Test that from_volume returns a hmesh.Manifold object for a valid 3D input."""
+    volume = np.random.rand(50, 50, 50).astype(np.float32)  # Generate a random 3D volume
+    mesh = qim3d.mesh.from_volume(volume)
+    assert isinstance(mesh, hmesh.Manifold)  # Check if output is a Manifold object
+
+def test_from_volume_invalid_input():
+    """Test that from_volume raises ValueError for non-3D input."""
+    volume = np.random.rand(50, 50)  # A 2D array
+    with pytest.raises(ValueError, match="The input volume must be a 3D numpy array."):
+        qim3d.mesh.from_volume(volume)
+
+def test_from_volume_empty_array():
+    """Test how from_volume handles an empty 3D array."""
+    volume = np.empty((0, 0, 0))  # Empty 3D array
+    with pytest.raises(ValueError):  # It should fail because it doesn't make sense to generate a mesh from empty data
+        qim3d.mesh.from_volume(volume)
+
+def test_from_volume_with_kwargs():
+    """Test that from_volume correctly passes kwargs."""
+    volume = np.random.rand(50, 50, 50).astype(np.float32)
+
+    # Mock volumetric_isocontour to check if kwargs are passed
+    def mock_volumetric_isocontour(vol, **kwargs):
+        assert "isovalue" in kwargs
+        assert kwargs["isovalue"] == 0.5
+        return hmesh.Manifold()
+
+    # Replace the function temporarily
+    original_function = hmesh.volumetric_isocontour
+    hmesh.volumetric_isocontour = mock_volumetric_isocontour
+
+    try:
+        qim3d.mesh.from_volume(volume, isovalue=0.5)
+    finally:
+        hmesh.volumetric_isocontour = original_function  # Restore original function
+
-- 
GitLab