Skip to content

Commit

Permalink
Add tests and improve coverage
Browse files Browse the repository at this point in the history
Include a test for the ENS_GROUP remote object.
Re-organize the code a bit to improve coverage.
  • Loading branch information
randallfrank committed Dec 6, 2023
1 parent 5193b62 commit 2a82489
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/ansys/pyensight/core/ensight_grpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,8 @@ def _metadata(self) -> List[Tuple[bytes, Union[str, bytes]]]:
if type(s) == str:
s = s.encode("utf-8")
ret.append((b"shared_secret", s))
if self._session_name:
s = self._session_name.encode("utf-8")
if self.session_name:
s = self.session_name.encode("utf-8")
ret.append((b"session_name", s))
return ret

Expand Down
2 changes: 1 addition & 1 deletion src/ansys/pyensight/core/ensobj.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def __del__(self):
try:
cmd = f"ensight.objs.release_id('{tmp_session.name}', {self.__OBJID__})"
tmp_session.cmd(cmd, do_eval=False)
except Exception:
except Exception: # pragma: no cover
# This could happen at any time, including outside
# the scope of the session, so we need to be
# ready for any error.
Expand Down
1 change: 0 additions & 1 deletion src/ansys/pyensight/core/listobj.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@ def set_attr(self, attr: Any, value: Any) -> int:
>>> session.ensight.objs.core.PARTS.set_attr("VISIBLE", True)
"""
objid_list = []
session = None
objid_list = [x.__OBJID__ for x in self if isinstance(x, ENSOBJ)]
for item in self:
Expand Down
4 changes: 2 additions & 2 deletions src/ansys/pyensight/core/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ def _check_rest_connection(self) -> None:
except Exception:
pass
time.sleep(0.5)
raise RuntimeError("Unable to establish a REST connection to EnSight.")
raise RuntimeError("Unable to establish a REST connection to EnSight.") # pragma: no cover

@property
def name(self) -> str:
Expand Down Expand Up @@ -1024,7 +1024,7 @@ def close(self) -> None:
if self.cei_suffix >= "242":
try:
self._release_remote_objects()
except RuntimeError:
except RuntimeError: # pragma: no cover
# handle some intermediate EnSight builds.
pass
if self._launcher and self._halt_ensight_on_close:
Expand Down
31 changes: 31 additions & 0 deletions tests/example_tests/test_remote_objects.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from ansys.pyensight.core import DockerLauncher, LocalLauncher

import gc
import pytest


def test_remote_objects(tmpdir, pytestconfig: pytest.Config):
data_dir = tmpdir.mkdir("datadir")
use_local = pytestconfig.getoption("use_local_launcher")
if use_local:
launcher = LocalLauncher()
else:
launcher = DockerLauncher(data_directory=data_dir, use_dev=True)
session = launcher.start()
session.load_data(f"{session.cei_home}/ensight{session.cei_suffix}/data/guard_rail/crash.case")

# call __str__ on an ENSOBJ object w/o DESCRIPTION attribute (for coverage)
print(session.ensight.objs.core)

if session.cei_suffix >= "242":
# Create an ENS_GROUP object (a remote object)
g = session.ensight.objs.core.PARTS.find('*rail*', wildcard=1, group=1)
assert "ENS_GROUP" in g.__str__(), "ensobjlist.find() did not return an ENS_GROUP instance"
assert "Owned" in g.__str__(), "Remote ENS_GROUP is not 'Owned'"
assert "Owned" not in g.CHILDREN.__str__(), "Objects in ENS_GROUP are incorrectly 'Owned'"

# Exercise the custom __del__() method
g = None
gc.collect()

session.close()

0 comments on commit 2a82489

Please sign in to comment.