Skip to content

Commit 162e71f

Browse files
committed
Add tests for inherited properties
Add tests for inherited properties that should also raise errors when accessed before the mesh is finalized.
1 parent 552f7e9 commit 162e71f

File tree

1 file changed

+50
-23
lines changed

1 file changed

+50
-23
lines changed

tests/tree/test_safeguards.py

+50-23
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,6 @@
99
from discretize.tree_mesh import TreeMeshNotFinalizedError
1010

1111

12-
@pytest.fixture
13-
def mesh():
14-
"""Return a sample TreeMesh"""
15-
nc = 16
16-
h = [nc, nc, nc]
17-
origin = (-32.4, 245.4, 192.3)
18-
mesh = TreeMesh(h, origin, diagonal_balance=True)
19-
return mesh
20-
21-
22-
def refine_mesh(mesh):
23-
"""
24-
Refine the sample tree mesh.
25-
26-
Don't finalize the mesh.
27-
"""
28-
origin = mesh.origin
29-
p1 = (origin[0] + 0.4, origin[1] + 0.4, origin[2] + 0.7)
30-
p2 = (origin[0] + 0.6, origin[1] + 0.6, origin[2] + 0.9)
31-
mesh.refine_box(p1, p2, levels=5, finalize=False)
32-
33-
3412
PROPERTIES = [
3513
"average_cell_to_face",
3614
"average_cell_to_face_x",
@@ -69,6 +47,39 @@ def refine_mesh(mesh):
6947
"total_nodes",
7048
]
7149

50+
INHERITED_PROPERTIES = [
51+
"boundary_edge_vector_integral",
52+
"boundary_face_scalar_integral",
53+
"boundary_node_vector_integral",
54+
"edges",
55+
"faces",
56+
"permute_cells",
57+
"permute_edges",
58+
"permute_faces",
59+
]
60+
61+
62+
@pytest.fixture
63+
def mesh():
64+
"""Return a sample TreeMesh"""
65+
nc = 16
66+
h = [nc, nc, nc]
67+
origin = (-32.4, 245.4, 192.3)
68+
mesh = TreeMesh(h, origin, diagonal_balance=True)
69+
return mesh
70+
71+
72+
def refine_mesh(mesh):
73+
"""
74+
Refine the sample tree mesh.
75+
76+
Don't finalize the mesh.
77+
"""
78+
origin = mesh.origin
79+
p1 = (origin[0] + 0.4, origin[1] + 0.4, origin[2] + 0.7)
80+
p2 = (origin[0] + 0.6, origin[1] + 0.6, origin[2] + 0.9)
81+
mesh.refine_box(p1, p2, levels=5, finalize=False)
82+
7283

7384
class TestSafeGuards:
7485

@@ -82,7 +93,7 @@ def test_errors(self, mesh, prop_name, refine):
8293
"""
8394
if refine:
8495
refine_mesh(mesh)
85-
msg = re.escape(f"`TreeMesh.{prop_name}` requires a finalized mesh. ")
96+
msg = re.escape(f"`TreeMesh.{prop_name}` requires a finalized mesh.")
8697
with pytest.raises(TreeMeshNotFinalizedError, match=msg):
8798
getattr(mesh, prop_name)
8899

@@ -96,3 +107,19 @@ def test_no_errors(self, mesh, prop_name):
96107
mesh.finalize()
97108
# Accessing the property should not error out
98109
getattr(mesh, prop_name)
110+
111+
@pytest.mark.parametrize("prop_name", INHERITED_PROPERTIES)
112+
@pytest.mark.parametrize("refine", [True, False], ids=["refined", "non-refined"])
113+
def test_errors_inherited_properties(self, mesh, prop_name, refine):
114+
"""
115+
Test errors when accessing inherited properties before finalizing the mesh.
116+
117+
These inherited properties are the ones that ``TreeMesh`` inherit, but
118+
they depend on the ones defined by it that should not be accessed
119+
before finalizing the mesh (e.g. ``edges`` and ``faces``).
120+
"""
121+
if refine:
122+
refine_mesh(mesh)
123+
msg = r"\`TreeMesh\.[a-z_]+\`" + re.escape(" requires a finalized mesh.")
124+
with pytest.raises(TreeMeshNotFinalizedError, match=msg):
125+
getattr(mesh, prop_name)

0 commit comments

Comments
 (0)