Skip to content

Commit

Permalink
Improve testing: remove unnecessary steps, tests, dependencies (#208)
Browse files Browse the repository at this point in the history
  • Loading branch information
viseshrp authored Dec 18, 2024
1 parent 82b592f commit 699d4d0
Show file tree
Hide file tree
Showing 16 changed files with 122 additions and 285 deletions.
3 changes: 0 additions & 3 deletions .ci/pull_adr_image.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,3 @@ _IMAGE_NAME="ghcr.io/ansys-internal/nexus_dev"

# Pull Ansys Dynamic Reporting image based on tag
docker pull $_IMAGE_NAME

# Remove all dangling images
docker image prune -f
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
CODESPELL_DIRS ?= ./pydynamicreporting
CODESPELL_SKIP ?= "*.pyc,*.xml,*.txt,*.gif,*.png,*.jpg,*.js,*.html,*.doctree,*.ttf,*.woff,*.woff2,*.eot,*.mp4,*.inv,*.pickle,*.ipynb,flycheck*,./.git/*,./.hypothesis/*,*.yml,./docs/build/*,./docs/images/*,./dist/*,*~,.hypothesis*,./docs/source/examples/*,*cover,*.dat,*.mac,\#*,PKG-INFO,*.mypy_cache/*,*.xml,*.aedt,*.svg"
CODESPELL_IGNORE ?= "ignore_words.txt"
TEST_FILE ?= "tests\test_service.py"
INSTALL_PATH ?= "C:\Program Files\ANSYS Inc\v252"

doctest: codespell

Expand Down Expand Up @@ -30,6 +32,10 @@ test:
pip install -e .[test]
pytest -rvx --setup-show --cov=ansys.dynamicreporting.core --cov-report html:coverage-html --cov-report term --cov-report xml:coverage.xml

test-dev:
pip install -e .[test]
pytest -rvx --setup-show "$(TEST_FILE)" --use-local-launcher --install-path "$(INSTALL_PATH)"

smoketest:
python tests/smoketest.py

Expand Down
19 changes: 16 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,12 @@ run this code:
The preceding code creates an "editable" installation that lets you develop and test
PyDynamicReporting at the same time.

To build and create a production-like installation, first install
`chocolatey <https://chocolatey.org/install>`_. Then:
To build and create a production-like installation on Windows (not required on other OSes),
first install `chocolatey <https://chocolatey.org/install>`_. Then:

.. code::
choco install make # install make
choco install make # install make on Windows
make clean # clean
make build # build
# this replaces the editable installation done previously. If you don't want to replace,
Expand Down Expand Up @@ -128,6 +128,19 @@ Deploy and upload steps **must always** be ignored. If they are not ignored,
before running GitHub Actions locally, add ``if: ${{ !env.ACT }}`` to the
workflow step and commit this change if required.

Local tests
^^^^^^^^^^^
To run tests on your local desktop (recommended), use the `make` target
`test-dev`. This target runs the tests in the same way as GitHub Actions but using
a local Ansys installation instead of Docker. You must specify the path to your Ansys
installation and the test file you are trying to run.

.. code::
make test-dev TEST_FILE="tests/test_service.py" INSTALL_PATH="C:\Program Files\ANSYS Inc\v252"
Note that any tests that require Docker will obviously fail.

Dependencies
------------
To use PyDynamicReporting, you must have a locally installed and licensed copy
Expand Down
13 changes: 9 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,16 @@ ci = "https://github.com/ansys/pydynamicreporting/actions"

[project.optional-dependencies]
test = [
"pytest>=8.3.3",
"pytest-cov>=6.0.0",
"psutil==6.0.0",
]
test-extra = [
"docker>=7.1.0",
"psutil==6.0.0",
"exceptiongroup==1.0.0",
"pytest==8.3.3",
"pytest-cov==6.0.0",
"pytest>=8.3.3",
"pytest-cov>=6.0.0",
"pyvista==0.44.1",
"vtk==9.4.0",
"ansys-dpf-core==0.13.0",
Expand All @@ -102,8 +107,8 @@ dev = [
"pillow==10.4.0",
"psutil==6.0.0",
"docker>=7.1.0",
"pytest==8.3.3",
"pytest-cov==6.0.0",
"pytest>=8.3.3",
"pytest-cov>=6.0.0",
"Sphinx==8.0.2",
"sphinx-copybutton==0.5.2",
"sphinx-gallery==0.18.0",
Expand Down
6 changes: 3 additions & 3 deletions src/ansys/dynamicreporting/core/adr_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -599,14 +599,14 @@ def stop(self) -> None:
# If coming from a docker image, clean that up
try:
if self._container:
self.logger.info("Told service Container to shutdown.\n")
self.logger.info("Shutting down container.\n")
self._container.stop()
self._container = None
else:
self.logger.info("Told service to shutdown.\n")
self.logger.info("Shutting down service.\n")
self.serverobj.stop_local_server()
except Exception as e:
self.logger.error(f"Problem shutting down service.\n{str(e)}\n")
self.logger.error(f"Problem shutting down container/service.\n{str(e)}\n")
pass

if self._delete_db and self._db_directory:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1189,7 +1189,7 @@ def delete_database(db_dir: str):
if not validate_local_db(db_dir):
# Validate the directory database before deleting it
if print_allowed():
print(f"Error: we are asked to delete the database but {db_dir} is not a database dir")
print(f"Error: Unable to delete the database: {db_dir} is not a database dir")
else:
try:
# Check if there is a nexus.status file. If yes, it means there is a Nexus service running on that
Expand Down
98 changes: 49 additions & 49 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
"""Global fixtures go here."""
import os

from pathlib import Path
from random import choice, random
import shutil
from string import ascii_letters
import subprocess

import pytest

Expand All @@ -16,26 +15,7 @@ def pytest_addoption(parser):
parser.addoption("--install-path", action="store", default="dev.json")


def cleanup_docker(request) -> None:
# Stop and remove 'nexus' containers. This needs to be deleted once we address the issue
# in the pynexus code by giving unique names to the containers
try:
subprocess.run(["docker", "stop", "nexus"])
subprocess.run(["docker", "rm", "nexus"])
except Exception:
# There might not be a running nexus container. That is fine, just continue
pass
try:
querydb_dir = os.path.join(os.path.join(request.fspath.dirname, "test_data"), "query_db")
os.remove(os.path.join(querydb_dir, "nexus.log"))
os.remove(os.path.join(querydb_dir, "nexus.status"))
shutil.rmtree(os.path.join(querydb_dir, "nginx"))
except Exception:
# There might not be these files / directories. In which case, nothing to do
pass


@pytest.fixture
@pytest.fixture(scope="module")
def get_exec(pytestconfig: pytest.Config) -> str:
exec_basis = ""
use_local = pytestconfig.getoption("use_local_launcher")
Expand All @@ -44,52 +24,72 @@ def get_exec(pytestconfig: pytest.Config) -> str:
return exec_basis


@pytest.fixture
def adr_service_create(request, pytestconfig: pytest.Config) -> Service:
@pytest.fixture(scope="module")
def adr_service_create(pytestconfig: pytest.Config) -> Service:
use_local = pytestconfig.getoption("use_local_launcher")
dir_name = "auto_delete_" + "".join(choice(ascii_letters) for x in range(5))
db_dir = os.path.join(os.path.join(request.fspath.dirname, "test_data"), dir_name)
tmp_docker_dir = os.path.join(os.path.join(request.fspath.dirname, "test_data"), "tmp_docker")

# Paths setup
base_dir = Path(__file__).parent / "test_data"
dir_name = "auto_delete_" + "".join(choice(ascii_letters) for _ in range(5))
db_dir = base_dir / dir_name
tmp_docker_dir = base_dir / "tmp_docker"

if use_local:
tmp_service = Service(
adr_service = Service(
ansys_installation=pytestconfig.getoption("install_path"),
docker_image=DOCKER_DEV_REPO_URL,
db_directory=db_dir,
db_directory=str(db_dir),
port=8000 + int(random() * 4000),
)
else:
cleanup_docker(request)
tmp_service = Service(
adr_service = Service(
ansys_installation="docker",
docker_image=DOCKER_DEV_REPO_URL,
db_directory=db_dir,
data_directory=tmp_docker_dir,
db_directory=str(db_dir),
data_directory=str(tmp_docker_dir),
port=8000 + int(random() * 4000),
)
return tmp_service

_ = adr_service.start(
create_db=True,
exit_on_close=True,
delete_db=True,
)

yield adr_service # Return to running the test session

# Cleanup
adr_service.stop()

@pytest.fixture
def adr_service_query(request, pytestconfig: pytest.Config) -> Service:

@pytest.fixture(scope="module")
def adr_service_query(pytestconfig: pytest.Config) -> Service:
use_local = pytestconfig.getoption("use_local_launcher")
local_db = os.path.join("test_data", "query_db")
db_dir = os.path.join(request.fspath.dirname, local_db)
tmp_docker_dir = os.path.join(
os.path.join(request.fspath.dirname, "test_data"), "tmp_docker_query"
)

# Paths setup
base_dir = Path(__file__).parent / "test_data"
local_db = base_dir / "query_db"
tmp_docker_dir = base_dir / "tmp_docker_query"

if use_local:
ansys_installation = pytestconfig.getoption("install_path")
else:
cleanup_docker(request)
ansys_installation = "docker"
tmp_service = Service(

adr_service = Service(
ansys_installation=ansys_installation,
docker_image=DOCKER_DEV_REPO_URL,
db_directory=db_dir,
data_directory=tmp_docker_dir,
db_directory=str(local_db),
data_directory=str(tmp_docker_dir),
port=8000 + int(random() * 4000),
)

if not use_local:
tmp_service._container.save_config()
tmp_service.start(create_db=False, exit_on_close=True, delete_db=False)
return tmp_service
adr_service._container.save_config()

adr_service.start(create_db=False, exit_on_close=True, delete_db=False)

yield adr_service # Return to running the test session

# Cleanup
adr_service.stop()
5 changes: 0 additions & 5 deletions tests/test_download_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ def test_download_use_data(request, adr_service_query) -> bool:
my_url += "/reports/report_display/?report_table_length=10&view=c4afe878-a4fe-11ed-a616-747827182a82&usemenus=on&dpi=96&pwidth=19.41&query="
a = rd.ReportDownloadHTML(url=my_url, directory=test_dir, debug=True)
test_res = a._should_use_data_uri(size=5)
adr_service_query.stop()
assert test_res


Expand All @@ -22,7 +21,6 @@ def test_download_nourl(request, adr_service_query) -> bool:
success = False
except ValueError:
success = True
adr_service_query.stop()
assert success


Expand All @@ -35,7 +33,6 @@ def test_download_nodir(request, adr_service_query) -> bool:
success = False
except ValueError:
success = True
adr_service_query.stop()
assert success


Expand All @@ -49,7 +46,6 @@ def test_download_sqlite(request, adr_service_query) -> bool:
success = False
except Exception:
success = True
adr_service_query.stop()
assert success


Expand All @@ -59,5 +55,4 @@ def test_download(request, adr_service_query) -> bool:
my_url += "/reports/report_display/?report_table_length=10&view=c4afe878-a4fe-11ed-a616-747827182a82&usemenus=on&dpi=96&pwidth=19.41&query="
a = rd.ReportDownloadHTML(url=my_url, directory=test_dir, debug=True)
test_res = a.download()
adr_service_query.stop()
assert test_res is None
6 changes: 6 additions & 0 deletions tests/test_geofile_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ def test_file_is_3d_geometry(request) -> bool:
assert scene is True and img is False


@pytest.mark.ado_test
def test_get_avz_directory(request) -> bool:
avz_dir = gp.get_avz_directory(return_file_paths(request)[1])
assert isinstance(avz_dir, str) and avz_dir != ""


@pytest.mark.ado_test
def test_rebuild_3d_geom_avz(request) -> bool:
_ = gp.rebuild_3d_geometry(
Expand Down
Loading

0 comments on commit 699d4d0

Please sign in to comment.