From c66b10c36a6d74006c498a533e0a70eee619e8e4 Mon Sep 17 00:00:00 2001 From: German <28149841+germa89@users.noreply.github.com> Date: Tue, 29 Oct 2024 12:31:23 +0100 Subject: [PATCH 1/2] refactor: creating test classes --- tests/conftest.py | 64 ----------- .../test_component_queries.py | 107 +++++++++++++++--- .../test_connectivity_queries.py | 14 +++ .../test_field_component_queries.py | 31 +++++ .../test_line_component_queries.py | 27 +++++ .../test_nearest_queries.py | 15 +++ tests/test_inline_functions/test_query.py | 13 +++ 7 files changed, 190 insertions(+), 81 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 703bd3a36d..6d732605ba 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -708,44 +708,6 @@ def cube_solve(cleared, mapdl, cube_geom_and_mesh): out = mapdl.modal_analysis(nmode=10, freqb=1) -@pytest.fixture -def box_with_fields(cleared, mapdl): - mapdl.prep7() - mapdl.mp("kxx", 1, 45) - mapdl.mp("ex", 1, 2e10) - mapdl.mp("perx", 1, 1) - mapdl.mp("murx", 1, 1) - if mapdl.version >= 25.1: - mapdl.tb("pm", 1, "", "", "perm") - mapdl.tbdata("", 0) - - mapdl.et(1, "SOLID70") - mapdl.et(2, "CPT215") - mapdl.keyopt(2, 12, 1) # Activating PRES DOF - mapdl.et(3, "SOLID122") - mapdl.et(4, "SOLID96") - mapdl.block(0, 1, 0, 1, 0, 1) - mapdl.esize(0.5) - return mapdl - - -@pytest.fixture -def box_geometry(mapdl, cleared): - areas, keypoints = create_geometry(mapdl) - q = mapdl.queries - return q, keypoints, areas, get_details_of_nodes(mapdl) - - -@pytest.fixture -def line_geometry(mapdl, cleared): - mapdl.prep7(mute=True) - k0 = mapdl.k(1, 0, 0, 0) - k1 = mapdl.k(2, 1, 2, 2) - l0 = mapdl.l(k0, k1) - q = mapdl.queries - return q, [k0, k1], l0 - - @pytest.fixture def query(mapdl, cleared): return mapdl.queries @@ -802,32 +764,6 @@ def selection_test_geometry(mapdl, cleared): return mapdl.queries -@pytest.fixture -def twisted_sheet(mapdl, cleared): - mapdl.prep7() - mapdl.et(1, "SHELL181") - mapdl.mp("EX", 1, 2e5) - mapdl.mp("PRXY", 1, 0.3) # Poisson's Ratio - mapdl.rectng(0, 1, 0, 1) - mapdl.sectype(1, "SHELL") - mapdl.secdata(0.1) - mapdl.esize(0.5) - mapdl.amesh("all") - mapdl.run("/SOLU") - mapdl.antype("STATIC") - mapdl.nsel("s", "loc", "x", 0) - mapdl.d("all", "all") - mapdl.nsel("s", "loc", "x", 1) - mapdl.d("all", "ux", -0.1) - mapdl.d("all", "uy", -0.1) - mapdl.d("all", "uz", -0.1) - mapdl.allsel("all") - mapdl.solve() - mapdl.finish() - q = mapdl.queries - return q, get_details_of_nodes(mapdl) - - def create_geometry(mapdl): mapdl.prep7() k0 = mapdl.k(1, 0, 0, 0) diff --git a/tests/test_inline_functions/test_component_queries.py b/tests/test_inline_functions/test_component_queries.py index b2f5bfa8d1..d06100a81b 100644 --- a/tests/test_inline_functions/test_component_queries.py +++ b/tests/test_inline_functions/test_component_queries.py @@ -22,78 +22,120 @@ import pytest +from conftest import create_geometry, get_details_of_nodes + class TestCentroidGetter: - def test_x(self, box_geometry): + + @pytest.fixture(scope="class") + def box_geometry(self, mapdl): + mapdl.finish(mute=True) + mapdl.clear("NOSTART", mute=True) + mapdl.prep7(mute=True) + areas, keypoints = create_geometry(mapdl) + q = mapdl.queries + return q, keypoints, areas, get_details_of_nodes(mapdl) + + def test_centroid_getter_x(self, box_geometry): q, kps, areas, nodes = box_geometry x = q.centrx(1) assert x is not None - def test_y(self, box_geometry): + def test_centroid_getter_y(self, box_geometry): q, kps, areas, nodes = box_geometry y = q.centry(1) assert y is not None - def test_z(self, box_geometry): + def test_centroid_getter_z(self, box_geometry): q, kps, areas, nodes = box_geometry z = q.centrz(1) assert z is not None - -class TestComponentQueries: - def test_nx(self, box_geometry): + def test_component_query_nx(self, box_geometry): q, kps, areas, nodes = box_geometry x = q.nx(1) assert x == nodes[1].x - def test_ny(self, box_geometry): + def test_component_query_ny(self, box_geometry): q, kps, areas, nodes = box_geometry y = q.ny(1) assert y == nodes[1].y - def test_nz(self, box_geometry): + def test_component_query_nz(self, box_geometry): q, kps, areas, nodes = box_geometry z = q.nz(1) assert z == nodes[1].z - def test_kx(self, box_geometry): + def test_component_query_kx(self, box_geometry): q, kps, areas, nodes = box_geometry # first kp at (0, 0, 0) x = q.kx(kps[0]) assert x == 0.0 - def test_ky(self, box_geometry): + def test_component_query_ky(self, box_geometry): q, kps, areas, nodes = box_geometry y = q.ky(kps[0]) assert y == 0.0 - def test_kz(self, box_geometry): + def test_component_query_kz(self, box_geometry): q, kps, areas, nodes = box_geometry z = q.kz(kps[0]) assert z == 0.0 - -class TestInverseGetFunctions: @pytest.mark.parametrize("coords", [(0, 0, 0), (0.5, 0.5, 0.5), (100, 100, 100)]) - def test_get_node_at_coordinates(self, box_geometry, coords): + def test_inverse_get_node_at_coordinates(self, box_geometry, coords): q, kps, areas, nodes = box_geometry node = q.node(*coords) assert node in nodes - def test_get_node_matches_known_node(self, box_geometry): + def test_inverse_get_node_matches_known_node(self, box_geometry): q, kps, areas, nodes = box_geometry for number, node in nodes.items(): calculated_node = q.node(node.x, node.y, node.z) assert number == calculated_node @pytest.mark.parametrize("coords", [(0, 0, 0), (0.5, 0.5, 0.5), (100, 100, 100)]) - def test_get_keypoints_at_coordinates(self, box_geometry, coords): + def test_inverse_get_keypoints_at_coordinates(self, box_geometry, coords): q, kps, areas, nodes = box_geometry kp = q.kp(*coords) assert kp in kps -class TestDisplacementComponentQueries: +class TestDisplacementComponentQueriesBox: + + @pytest.fixture(scope="class") + def solved_box(self, mapdl): + mapdl.mute = True # improve stability + mapdl.finish(mute=True) + mapdl.clear("NOSTART", mute=True) + + mapdl.prep7() + mapdl.et(1, "SOLID5") + mapdl.block(0, 10, 0, 20, 0, 30) + mapdl.esize(10) + mapdl.vmesh("ALL") + mapdl.units("SI") # SI - International system (m, kg, s, K). + # Define a material (nominal steel in SI) + mapdl.mp("EX", 1, 210e9) # Elastic moduli in Pa (kg/(m*s**2)) + mapdl.mp("DENS", 1, 7800) # Density in kg/m3 + mapdl.mp("PRXY", 1, 0.3) # Poisson's Ratio + # Fix the left-hand side. + mapdl.nsel("S", "LOC", "Z", 0) + mapdl.d("ALL", "UX") + mapdl.d("ALL", "UY") + mapdl.d("ALL", "UZ") + + mapdl.nsel("S", "LOC", "Z", 30) + mapdl.f("ALL", "FX", 1000) + mapdl.run("/SOLU") + mapdl.antype("STATIC") + mapdl.solve() + mapdl.finish() + mapdl.mute = False + + q = mapdl.queries + return q, get_details_of_nodes(mapdl) + def test_ux(self, solved_box): q, nodes = solved_box displaced_nodes = [node for node in nodes if abs(q.ux(node)) > 0] @@ -109,6 +151,37 @@ def test_uz(self, solved_box): displaced_nodes = [node for node in nodes if abs(q.uz(node)) > 0] assert len(displaced_nodes) > 0 + +class TestDisplacementComponentQueriesSheet: + + @pytest.fixture(scope="class") + def twisted_sheet(self, mapdl): + mapdl.finish(mute=True) + mapdl.clear("NOSTART", mute=True) + + mapdl.prep7() + mapdl.et(1, "SHELL181") + mapdl.mp("EX", 1, 2e5) + mapdl.mp("PRXY", 1, 0.3) # Poisson's Ratio + mapdl.rectng(0, 1, 0, 1) + mapdl.sectype(1, "SHELL") + mapdl.secdata(0.1) + mapdl.esize(0.5) + mapdl.amesh("all") + mapdl.run("/SOLU") + mapdl.antype("STATIC") + mapdl.nsel("s", "loc", "x", 0) + mapdl.d("all", "all") + mapdl.nsel("s", "loc", "x", 1) + mapdl.d("all", "ux", -0.1) + mapdl.d("all", "uy", -0.1) + mapdl.d("all", "uz", -0.1) + mapdl.allsel("all") + mapdl.solve() + mapdl.finish() + q = mapdl.queries + return q, get_details_of_nodes(mapdl) + def test_rotx(self, twisted_sheet): q, nodes = twisted_sheet displaced_nodes = [node for node in nodes if abs(q.rotx(node)) > 0] diff --git a/tests/test_inline_functions/test_connectivity_queries.py b/tests/test_inline_functions/test_connectivity_queries.py index 2078d2fb3c..0831fd3d9c 100644 --- a/tests/test_inline_functions/test_connectivity_queries.py +++ b/tests/test_inline_functions/test_connectivity_queries.py @@ -20,8 +20,22 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. +import pytest + +from conftest import create_geometry, get_details_of_nodes + class TestConnectivityQueries: + + @pytest.fixture(scope="function") + def box_geometry(self, mapdl): + mapdl.finish(mute=True) + mapdl.clear("NOSTART", mute=True) + mapdl.prep7(mute=True) + areas, keypoints = create_geometry(mapdl) + q = mapdl.queries + return q, keypoints, areas, get_details_of_nodes(mapdl) + def test_nelem(self, box_geometry): q, kps, areas, nodes = box_geometry ns = [q.nelem(1, i) for i in range(1, 21)] diff --git a/tests/test_inline_functions/test_field_component_queries.py b/tests/test_inline_functions/test_field_component_queries.py index b1dfeaea86..69b7913851 100644 --- a/tests/test_inline_functions/test_field_component_queries.py +++ b/tests/test_inline_functions/test_field_component_queries.py @@ -20,10 +20,38 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. +import pytest + class TestFieldComponentValueGetter: + + # The tests change the mesh so this fixture must be function scoped. + @pytest.fixture(scope="function") + def box_with_fields(self, mapdl): + mapdl.finish(mute=True) + mapdl.clear("NOSTART", mute=True) + + mapdl.prep7() + mapdl.mp("kxx", 1, 45) + mapdl.mp("ex", 1, 2e10) + mapdl.mp("perx", 1, 1) + mapdl.mp("murx", 1, 1) + if mapdl.version >= 25.1: + mapdl.tb("pm", 1, "", "", "perm") + mapdl.tbdata("", 0) + + mapdl.et(1, "SOLID70") + mapdl.et(2, "CPT215") + mapdl.keyopt(2, 12, 1) # Activating PRES DOF + mapdl.et(3, "SOLID122") + mapdl.et(4, "SOLID96") + mapdl.block(0, 1, 0, 1, 0, 1) + mapdl.esize(0.5) + return mapdl + def test_temp(self, box_with_fields): mapdl = box_with_fields + mapdl.prep7() mapdl.type(1) mapdl.vmesh(1) mapdl.d("all", "temp", 5.0) @@ -34,6 +62,7 @@ def test_temp(self, box_with_fields): def test_pressure(self, box_with_fields): mapdl = box_with_fields + mapdl.prep7() mapdl.type(2) mapdl.vmesh(1) mapdl.d("all", "pres", 5.0) @@ -45,6 +74,7 @@ def test_pressure(self, box_with_fields): def test_volt(self, box_with_fields): mapdl = box_with_fields + mapdl.prep7() mapdl.type(3) mapdl.vmesh(1) mapdl.d("all", "volt", 5.0) @@ -55,6 +85,7 @@ def test_volt(self, box_with_fields): def test_mag(self, box_with_fields): mapdl = box_with_fields + mapdl.prep7() mapdl.type(4) mapdl.vmesh(1) mapdl.d("all", "mag", 5.0) diff --git a/tests/test_inline_functions/test_line_component_queries.py b/tests/test_inline_functions/test_line_component_queries.py index e7916a0ec3..e6250ab918 100644 --- a/tests/test_inline_functions/test_line_component_queries.py +++ b/tests/test_inline_functions/test_line_component_queries.py @@ -22,8 +22,23 @@ from math import isclose +import pytest + class TestLineCoordinateQueries: + + @pytest.fixture(scope="class") + def line_geometry(self, mapdl): + mapdl.finish(mute=True) + mapdl.clear("NOSTART", mute=True) + + mapdl.prep7(mute=True) + k0 = mapdl.k(1, 0, 0, 0) + k1 = mapdl.k(2, 1, 2, 2) + l0 = mapdl.l(k0, k1) + q = mapdl.queries + return q, [k0, k1], l0 + def test_lx(self, line_geometry): q, kps, line = line_geometry x = q.lx(line, 0.5) @@ -41,6 +56,18 @@ def test_lz(self, line_geometry): class TestLineSlopeQueries: + @pytest.fixture(scope="class") + def line_geometry(self, mapdl): + mapdl.finish(mute=True) + mapdl.clear("NOSTART", mute=True) + + mapdl.prep7(mute=True) + k0 = mapdl.k(1, 0, 0, 0) + k1 = mapdl.k(2, 1, 2, 2) + l0 = mapdl.l(k0, k1) + q = mapdl.queries + return q, [k0, k1], l0 + def test_lsx(self, line_geometry): q, kps, line = line_geometry sx = q.lsx(line, 0.5) diff --git a/tests/test_inline_functions/test_nearest_queries.py b/tests/test_inline_functions/test_nearest_queries.py index 08b57cfdbc..8b935d64ba 100644 --- a/tests/test_inline_functions/test_nearest_queries.py +++ b/tests/test_inline_functions/test_nearest_queries.py @@ -20,8 +20,23 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. +import pytest + +from conftest import create_geometry, get_details_of_nodes + class TestNearestEntityQueries: + + @pytest.fixture(scope="class") + def box_geometry(self, mapdl): + mapdl.finish(mute=True) + mapdl.clear("NOSTART", mute=True) + mapdl.prep7(mute=True) + + areas, keypoints = create_geometry(mapdl) + q = mapdl.queries + return q, keypoints, areas, get_details_of_nodes(mapdl) + def test_nnear(self, box_geometry): q, kps, areas, nodes = box_geometry nearest_node = q.nnear(1) diff --git a/tests/test_inline_functions/test_query.py b/tests/test_inline_functions/test_query.py index 067d98d05c..c24f6ad327 100644 --- a/tests/test_inline_functions/test_query.py +++ b/tests/test_inline_functions/test_query.py @@ -85,6 +85,19 @@ def test_parse_int_type_error(self, value, query): class TestRunQuery: + + @pytest.fixture(scope="class") + def line_geometry(self, mapdl): + mapdl.finish(mute=True) + mapdl.clear("NOSTART", mute=True) + + mapdl.prep7(mute=True) + k0 = mapdl.k(1, 0, 0, 0) + k1 = mapdl.k(2, 1, 2, 2) + l0 = mapdl.l(k0, k1) + q = mapdl.queries + return q, [k0, k1], l0 + @pytest.mark.parametrize("command", [("KX(1)", float), ("KP(1,1,1)", int)]) def test_run_query_returned_type(self, line_geometry, command): q, kps, l0 = line_geometry From 2da243e1e0a93849181119ee0a943a8807fccd4c Mon Sep 17 00:00:00 2001 From: pyansys-ci-bot <92810346+pyansys-ci-bot@users.noreply.github.com> Date: Tue, 29 Oct 2024 11:35:58 +0000 Subject: [PATCH 2/2] chore: adding changelog file 3524.added.md [dependabot-skip] --- doc/changelog.d/3524.added.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 doc/changelog.d/3524.added.md diff --git a/doc/changelog.d/3524.added.md b/doc/changelog.d/3524.added.md new file mode 100644 index 0000000000..f25a84e3c7 --- /dev/null +++ b/doc/changelog.d/3524.added.md @@ -0,0 +1 @@ +refactor: using test classes in test_inline tests \ No newline at end of file