Skip to content

Commit

Permalink
fix: using lower scope fixture on high scope fixture in database module
Browse files Browse the repository at this point in the history
  • Loading branch information
germa89 committed Nov 19, 2024
1 parent 8dc0d33 commit 4726fd5
Showing 1 changed file with 131 additions and 141 deletions.
272 changes: 131 additions & 141 deletions tests/test_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from ansys.mapdl.core.database import MINIMUM_MAPDL_VERSION, DBDef, MapdlDb
from ansys.mapdl.core.errors import MapdlRuntimeError, MapdlVersionError
from ansys.mapdl.core.misc import random_string
from conftest import ON_CI
from conftest import ON_CI, TestClass


@pytest.fixture(scope="session")
Expand Down Expand Up @@ -73,25 +73,6 @@ def test_failure_on_non_allowed_versions(mapdl):
pytest.skip(f"Should run only on MAPDL 24.1 and 24.2")


@pytest.fixture(scope="session")
def gen_block(mapdl, cleared):
"""Generate nodes and elements in a simple block."""
mapdl.block(0, 1, 0, 1, 0, 1)
mapdl.et(1, 186)
mapdl.esize(0.25)
mapdl.vmesh("ALL")


@pytest.fixture(scope="session")
def nodes(gen_block, db):
return db.nodes


@pytest.fixture(scope="session")
def elems(gen_block, db):
return db.elems


def test_database_start_stop(mapdl):
if mapdl._server_version < (0, 4, 1): # 2021R2
pytest.skip("requires 2021R2 or newer")
Expand Down Expand Up @@ -152,175 +133,184 @@ def test_clear(db):
assert db._mapdl.get_value("KP", 0, "count") == 0.0


def test_nodes_repr(nodes):
assert "425" in str(nodes)
assert "Number of nodes" in str(nodes)


def test_nodes_first(nodes):
assert nodes.first() == 1
assert nodes.first(inod=10) == 11


def test_nodes_next(nodes):
nodes._itnod = -1 # resets nodes state

with pytest.raises(
MapdlRuntimeError, match="You first have to call the `DbNodes.first` method"
):
nodes.next()

nodes.first()
assert nodes.next() == 2


def test_nodes_info(nodes):
assert nodes.info(1, DBDef.DB_SELECTED) == 1

def test__channel_str(db):
assert db._channel_str is not None
assert ":" in db._channel_str
assert re.search("\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}", db._channel_str)
assert re.search("\d{4,6}", db._channel_str)

@pytest.mark.parametrize("selected", [True, False])
def test_nodes_num(nodes, selected):
assert nodes.num(selected=selected) == 425

def test_off_db(mapdl, db):
"""Testing that when there is no active database"""
if db.active:
db.stop()
assert not mapdl.db.active
assert mapdl.db.nodes is None
assert mapdl.db.elems is None

def test_nodes_max_num(nodes):
assert nodes.max_num == 425

def test_wrong_api_version(mapdl, db):
mapdl.db.stop()
mapdl.__server_version = (0, 1, 1)
mapdl._MapdlGrpc__server_version = (0, 1, 1)

def test_nodes_coord(nodes):
sel, coord = nodes.coord(22)
assert sel == 0 # selected
assert coord == (1.0, 0.5, 0.0, 0.0, 0.0, 0.0)
from ansys.mapdl.core.errors import MapdlVersionError

with pytest.raises(MapdlVersionError):
mapdl.db.start()

def test_nodes_asarray(nodes):
ind, coords, angles = nodes.all_asarray()
assert np.allclose(ind, np.arange(1, 426))
mapdl.__sever_version = None
mapdl._MapdlGrpc__server_version = None

assert np.allclose(coords, nodes._db._mapdl.mesh.nodes)
assert np.allclose(angles, 0)
mapdl._server_version # resetting
mapdl.db.start()

assert "is currently running" in mapdl.db._status()

def test_nodes_push(nodes):
nnum = 100000
x, y, z, xang, yang, zang = 1, 5, 10, 30, 40, 50
nodes.push(nnum, x, y, z, xang, yang, zang)

selected, coord = nodes.coord(nnum)
assert selected == 0
assert coord == (x, y, z, xang, yang, zang)
def test_repr(mapdl, db):
elems = mapdl.db.elems
nodes = mapdl.db.nodes

with pytest.raises(ValueError, match="X angle must be input"):
nodes.push(nnum, x, y, z, yang=1)
assert elems
assert nodes

with pytest.raises(ValueError, match="X and Y angles must be input"):
nodes.push(nnum, x, y, z, zang=1)
assert isinstance(elems.__repr__(), str)
assert isinstance(nodes.__repr__(), str)

assert isinstance(elems.__str__(), str)
assert isinstance(nodes.__str__(), str)

def test_elems_repr(elems):
assert "64" in str(elems)
assert "Number of elements" in str(elems)

def gen_block(mapdl):
"""Generate nodes and elements in a simple block."""
mapdl.block(0, 1, 0, 1, 0, 1)
mapdl.et(1, 186)
mapdl.esize(0.25)
mapdl.vmesh("ALL")

def test_elems_first(elems):
assert elems.first() == 1
assert elems.first(ielm=10) == 11

class Test_Nodes(TestClass):

def test_elems_next(elems):
elems._itelm = -1 # resets elems state
@staticmethod
@pytest.fixture(scope="class")
def nodes(mapdl, db):
gen_block(mapdl)
return db.nodes

with pytest.raises(
MapdlRuntimeError, match="You first have to call the `DbElems.first` method"
):
elems.next()
def test_nodes_repr(nodes):
assert "425" in str(nodes)
assert "Number of nodes" in str(nodes)

elems.first()
assert elems.next() == 2
def test_nodes_first(nodes):
assert nodes.first() == 1
assert nodes.first(inod=10) == 11

def test_nodes_next(nodes):
nodes._itnod = -1 # resets nodes state

def test_elems_info(elems):
assert elems.info(1, DBDef.DB_SELECTED) == 1
with pytest.raises(
MapdlRuntimeError, match="You first have to call the `DbNodes.first` method"
):
nodes.next()

nodes.first()
assert nodes.next() == 2

@pytest.mark.parametrize("selected", [True, False])
def test_elems_num(elems, selected):
assert elems.num(selected=selected) == 64
def test_nodes_info(nodes):
assert nodes.info(1, DBDef.DB_SELECTED) == 1

@pytest.mark.parametrize("selected", [True, False])
def test_nodes_num(nodes, selected):
assert nodes.num(selected=selected) == 425

def test_elems_max_num(elems):
assert elems.max_num == 64
def test_nodes_max_num(nodes):
assert nodes.max_num == 425

def test_nodes_coord(nodes):
sel, coord = nodes.coord(22)
assert sel == 0 # selected
assert coord == (1.0, 0.5, 0.0, 0.0, 0.0, 0.0)

def test_elems_get(elems):
ielm = 1
elem_info = elems.get(ielm)
def test_nodes_asarray(nodes):
ind, coords, angles = nodes.all_asarray()
assert np.allclose(ind, np.arange(1, 426))

assert len(elem_info.nodes) == elem_info.nnod
assert len(elem_info.elmdat) == 10
assert elem_info.ielem == ielm
assert np.allclose(coords, nodes._db._mapdl.mesh.nodes)
assert np.allclose(angles, 0)

def test_nodes_push(nodes):
nnum = 100000
x, y, z, xang, yang, zang = 1, 5, 10, 30, 40, 50
nodes.push(nnum, x, y, z, xang, yang, zang)

def test_elems_push(elems):
ielm = 1
elem_info = elems.get(ielm)
selected, coord = nodes.coord(nnum)
assert selected == 0
assert coord == (x, y, z, xang, yang, zang)

ielm_new = 10000
elems.push(ielm_new, elem_info.elmdat, elem_info.nodes)
with pytest.raises(ValueError, match="X angle must be input"):
nodes.push(nnum, x, y, z, yang=1)

elem_info_new = elems.get(ielm_new)
assert elem_info.elmdat == elem_info_new.elmdat
assert elem_info.nnod == elem_info_new.nnod
assert elem_info.nodes == elem_info_new.nodes
with pytest.raises(ValueError, match="X and Y angles must be input"):
nodes.push(nnum, x, y, z, zang=1)

with pytest.raises(ValueError, match="`elmdat` must be length 10"):
elems.push(ielm_new, [1, 2, 3], elem_info.nodes)

class Test_Elems(TestClass):

def test__channel_str(db):
assert db._channel_str is not None
assert ":" in db._channel_str
assert re.search("\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}", db._channel_str)
assert re.search("\d{4,6}", db._channel_str)
@staticmethod
@pytest.fixture(scope="class")
def elems(mapdl, db):
gen_block(mapdl)
return db.elems

def test_elems_repr(elems):
assert "64" in str(elems)
assert "Number of elements" in str(elems)

def test_off_db(mapdl, db):
"""Testing that when there is no active database"""
if db.active:
db.stop()
assert not mapdl.db.active
assert mapdl.db.nodes is None
assert mapdl.db.elems is None
def test_elems_first(elems):
assert elems.first() == 1
assert elems.first(ielm=10) == 11

def test_elems_next(elems):
elems._itelm = -1 # resets elems state

def test_wrong_api_version(mapdl, db):
mapdl.db.stop()
mapdl.__server_version = (0, 1, 1)
mapdl._MapdlGrpc__server_version = (0, 1, 1)
with pytest.raises(
MapdlRuntimeError, match="You first have to call the `DbElems.first` method"
):
elems.next()

from ansys.mapdl.core.errors import MapdlVersionError
elems.first()
assert elems.next() == 2

with pytest.raises(MapdlVersionError):
mapdl.db.start()
def test_elems_info(elems):
assert elems.info(1, DBDef.DB_SELECTED) == 1

mapdl.__sever_version = None
mapdl._MapdlGrpc__server_version = None
@pytest.mark.parametrize("selected", [True, False])
def test_elems_num(elems, selected):
assert elems.num(selected=selected) == 64

mapdl._server_version # resetting
mapdl.db.start()
def test_elems_max_num(elems):
assert elems.max_num == 64

assert "is currently running" in mapdl.db._status()
def test_elems_get(elems):
ielm = 1
elem_info = elems.get(ielm)

assert len(elem_info.nodes) == elem_info.nnod
assert len(elem_info.elmdat) == 10
assert elem_info.ielem == ielm

def test_repr(mapdl, db):
elems = mapdl.db.elems
nodes = mapdl.db.nodes
def test_elems_push(elems):
ielm = 1
elem_info = elems.get(ielm)

assert elems
assert nodes
ielm_new = 10000
elems.push(ielm_new, elem_info.elmdat, elem_info.nodes)

assert isinstance(elems.__repr__(), str)
assert isinstance(nodes.__repr__(), str)
elem_info_new = elems.get(ielm_new)
assert elem_info.elmdat == elem_info_new.elmdat
assert elem_info.nnod == elem_info_new.nnod
assert elem_info.nodes == elem_info_new.nodes

assert isinstance(elems.__str__(), str)
assert isinstance(nodes.__str__(), str)
with pytest.raises(ValueError, match="`elmdat` must be length 10"):
elems.push(ielm_new, [1, 2, 3], elem_info.nodes)

0 comments on commit 4726fd5

Please sign in to comment.