Skip to content

Commit c357052

Browse files
authored
Allow doctest to deal with features requiring future server version (#797)
* Fix doctest for features requiring future server version. * Make the unit_systems class treat the case of server version inferior to 6.1. * Update conftest.py for doctest to treat unexpected DpfServerVersionNotSupported error. * Update unit_system.py to create the unit_systems class only if possible. * Revert UnitSystem requiring 6.2 * Do not try-catch the unit_systems class. * Add a Required Version note in the uni_systems class docstring, and add a try-catch when trying to instantiate the UnitSystems, making the class empty for DPF server versions below 6.1 * Update src/ansys/dpf/core/unit_system.py
1 parent 2eeac47 commit c357052

File tree

2 files changed

+39
-8
lines changed

2 files changed

+39
-8
lines changed

conftest.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
Launch or connect to a persistent local DPF service to be shared in
44
pytest as a session fixture
55
"""
6+
import doctest
7+
from doctest import DocTestRunner
8+
from unittest import mock
9+
10+
import pytest
11+
612
from ansys.dpf import core
713
from ansys.dpf.core.misc import module_exists
814

@@ -17,3 +23,21 @@
1723
# enable off_screen plotting to avoid test interruption
1824
core.settings.disable_off_screen_rendering()
1925
core.settings.bypass_pv_opengl_osmesa_crash()
26+
27+
28+
class DPFDocTestRunner(DocTestRunner):
29+
def run(self, test, compileflags=None, out=None, clear_globs=True):
30+
try:
31+
return DocTestRunner.run(self, test, compileflags, out, clear_globs)
32+
except doctest.UnexpectedException as e:
33+
feature_str = "Feature not supported. Upgrade the server to"
34+
if feature_str in str(e.exc_info):
35+
pass
36+
else:
37+
raise e
38+
39+
40+
@pytest.fixture(autouse=True)
41+
def doctest_runner_dpf():
42+
with mock.patch("doctest.DocTestRunner", DPFDocTestRunner):
43+
yield

src/ansys/dpf/core/unit_system.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ def unit_names(self) -> str:
9898
class unit_systems:
9999
"""Contains common Ansys predefined UnitSystems.
100100
101+
Notes
102+
-----
103+
Class available with server's version starting at 6.1 (Ansys 2023R2).
104+
101105
Attributes
102106
-----------
103107
solver_mks : Metric (m, kg, N, s, J, Pa, degC, C, rad)
@@ -118,11 +122,14 @@ class unit_systems:
118122
119123
"""
120124

121-
solver_mks = UnitSystem("solver_mks", ID=11)
122-
solver_cgs = UnitSystem("solver_cgs", ID=5)
123-
solver_nmm = UnitSystem("solver_nmm", ID=6)
124-
solver_umks = UnitSystem("solver_umks", ID=10)
125-
solver_knms = UnitSystem("solver_knms", ID=16)
126-
solver_bft = UnitSystem("solver_bft", ID=7)
127-
solver_bin = UnitSystem("solver_bin", ID=8)
128-
undefined = UnitSystem("undefined", ID=-1)
125+
try:
126+
solver_mks = UnitSystem("solver_mks", ID=11)
127+
solver_cgs = UnitSystem("solver_cgs", ID=5)
128+
solver_nmm = UnitSystem("solver_nmm", ID=6)
129+
solver_umks = UnitSystem("solver_umks", ID=10)
130+
solver_knms = UnitSystem("solver_knms", ID=16)
131+
solver_bft = UnitSystem("solver_bft", ID=7)
132+
solver_bin = UnitSystem("solver_bin", ID=8)
133+
undefined = UnitSystem("undefined", ID=-1)
134+
except dpf_errors.DpfVersionNotSupported as e: # pragma: no cover
135+
pass

0 commit comments

Comments
 (0)