Debian Codegraph @

tests/test_backends.py

raw
1 from mpi4py import MPI
2
3 import dolfinx
4 import h5py
5 import pytest
6
7 import io4dolfinx
8 from io4dolfinx.backends import BUILTIN_BAKENDS
9
10
11 @pytest.fixture(autouse=True)
12 def reset_default_backend():
13 """
14 Fixture to ensure the global default backend is always reset to 'adios2'
15 after each test, preventing state leakage to other tests.
16 """
17 # Setup: Ensure starting state is adios2
18 io4dolfinx.set_default_backend("adios2")
19 yield
20 # Teardown: Reset back to adios2
21 io4dolfinx.set_default_backend("adios2")
22
23
24 def test_explicit_backend_overrides_default(tmp_path):
25 """
26 Test that explicitly passing `backend="h5py"` overrides the global
27 default backend (which is "adios2").
28 """
29 comm = MPI.COMM_WORLD
30
31 # Ensure default is currently adios2
32 assert io4dolfinx.backends._DEFAULT_BACKEND == "adios2"
33
34 mesh = dolfinx.mesh.create_unit_square(comm, 5, 5)
35
36 # We use .h5 suffix, but the backend argument is what actually dictates the writer
37 fname = comm.bcast(tmp_path, root=0) / "override_test.h5"
38
39 # Explicitly pass the h5py backend
40 io4dolfinx.write_mesh(fname, mesh, backend="h5py")
41
42 comm.Barrier()
43
44 # Verify that h5py was actually used by attempting to open it as an HDF5 file.
45 # If adios2 was used, this would raise an OSError/ValueError.
46 if comm.rank == 0:
47 assert fname.exists()
48 with h5py.File(fname, "r") as f:
49 assert "mesh" in f.keys()
50
51
52 def test_set_default_backend_takes_effect(tmp_path):
53 """
54 Test that calling `set_default_backend("h5py")` successfully changes the
55 default behavior for API calls where `backend` is not explicitly provided.
56 """
57 comm = MPI.COMM_WORLD
58
59 # Update the global default backend to h5py
60 io4dolfinx.set_default_backend("h5py")
61
62 mesh = dolfinx.mesh.create_unit_square(comm, 5, 5)
63
64 fname = comm.bcast(tmp_path, root=0) / "default_update_test.h5"
65
66 # Call the API without providing the `backend` argument
67 io4dolfinx.write_mesh(fname, mesh)
68
69 comm.Barrier()
70
71 # Verify that h5py was implicitly used based on the new default
72 if comm.rank == 0:
73 assert fname.exists()
74 with h5py.File(fname, "r") as f:
75 assert "mesh" in f.keys()
76 assert "Topology" in f["mesh"].keys()
77
78
79 def test_list_builtin_backends():
80 """
81 Test that list_builtin_backends returns a valid list containing
82 a subset of the supported built-in backends based on the current environment.
83 """
84 # Call the function to get the list of available backends
85 available_backends = io4dolfinx.backends.list_builtin_backends()
86
87 # Verify the return type is a list
88 assert isinstance(available_backends, list)
89
90 # Depending on the test environment, at least one backend should be available
91 assert len(available_backends) > 0
92
93 # Verify that all returned backends are recognized as built-in backends
94 for backend in available_backends:
95 assert isinstance(backend, str)
96 assert backend in BUILTIN_BAKENDS
97
98 # We can be reasonably certain that 'h5py' or 'adios2' should be
99 # present if the io4dolfinx test suite is running successfully
100 assert "h5py" in available_backends or "adios2" in available_backends