Skip to content

Commit 699d4d0

Browse files
authored
Improve testing: remove unnecessary steps, tests, dependencies (#208)
1 parent 82b592f commit 699d4d0

16 files changed

+122
-285
lines changed

.ci/pull_adr_image.sh

-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,3 @@ _IMAGE_NAME="ghcr.io/ansys-internal/nexus_dev"
66

77
# Pull Ansys Dynamic Reporting image based on tag
88
docker pull $_IMAGE_NAME
9-
10-
# Remove all dangling images
11-
docker image prune -f

Makefile

+6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
CODESPELL_DIRS ?= ./pydynamicreporting
22
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"
33
CODESPELL_IGNORE ?= "ignore_words.txt"
4+
TEST_FILE ?= "tests\test_service.py"
5+
INSTALL_PATH ?= "C:\Program Files\ANSYS Inc\v252"
46

57
doctest: codespell
68

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

35+
test-dev:
36+
pip install -e .[test]
37+
pytest -rvx --setup-show "$(TEST_FILE)" --use-local-launcher --install-path "$(INSTALL_PATH)"
38+
3339
smoketest:
3440
python tests/smoketest.py
3541

README.rst

+16-3
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,12 @@ run this code:
9393
The preceding code creates an "editable" installation that lets you develop and test
9494
PyDynamicReporting at the same time.
9595

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

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

131+
Local tests
132+
^^^^^^^^^^^
133+
To run tests on your local desktop (recommended), use the `make` target
134+
`test-dev`. This target runs the tests in the same way as GitHub Actions but using
135+
a local Ansys installation instead of Docker. You must specify the path to your Ansys
136+
installation and the test file you are trying to run.
137+
138+
.. code::
139+
140+
make test-dev TEST_FILE="tests/test_service.py" INSTALL_PATH="C:\Program Files\ANSYS Inc\v252"
141+
142+
Note that any tests that require Docker will obviously fail.
143+
131144
Dependencies
132145
------------
133146
To use PyDynamicReporting, you must have a locally installed and licensed copy

pyproject.toml

+9-4
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,16 @@ ci = "https://github.com/ansys/pydynamicreporting/actions"
7171

7272
[project.optional-dependencies]
7373
test = [
74+
"pytest>=8.3.3",
75+
"pytest-cov>=6.0.0",
76+
"psutil==6.0.0",
77+
]
78+
test-extra = [
7479
"docker>=7.1.0",
7580
"psutil==6.0.0",
7681
"exceptiongroup==1.0.0",
77-
"pytest==8.3.3",
78-
"pytest-cov==6.0.0",
82+
"pytest>=8.3.3",
83+
"pytest-cov>=6.0.0",
7984
"pyvista==0.44.1",
8085
"vtk==9.4.0",
8186
"ansys-dpf-core==0.13.0",
@@ -102,8 +107,8 @@ dev = [
102107
"pillow==10.4.0",
103108
"psutil==6.0.0",
104109
"docker>=7.1.0",
105-
"pytest==8.3.3",
106-
"pytest-cov==6.0.0",
110+
"pytest>=8.3.3",
111+
"pytest-cov>=6.0.0",
107112
"Sphinx==8.0.2",
108113
"sphinx-copybutton==0.5.2",
109114
"sphinx-gallery==0.18.0",

src/ansys/dynamicreporting/core/adr_service.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -599,14 +599,14 @@ def stop(self) -> None:
599599
# If coming from a docker image, clean that up
600600
try:
601601
if self._container:
602-
self.logger.info("Told service Container to shutdown.\n")
602+
self.logger.info("Shutting down container.\n")
603603
self._container.stop()
604604
self._container = None
605605
else:
606-
self.logger.info("Told service to shutdown.\n")
606+
self.logger.info("Shutting down service.\n")
607607
self.serverobj.stop_local_server()
608608
except Exception as e:
609-
self.logger.error(f"Problem shutting down service.\n{str(e)}\n")
609+
self.logger.error(f"Problem shutting down container/service.\n{str(e)}\n")
610610
pass
611611

612612
if self._delete_db and self._db_directory:

src/ansys/dynamicreporting/core/utils/report_remote_server.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1189,7 +1189,7 @@ def delete_database(db_dir: str):
11891189
if not validate_local_db(db_dir):
11901190
# Validate the directory database before deleting it
11911191
if print_allowed():
1192-
print(f"Error: we are asked to delete the database but {db_dir} is not a database dir")
1192+
print(f"Error: Unable to delete the database: {db_dir} is not a database dir")
11931193
else:
11941194
try:
11951195
# Check if there is a nexus.status file. If yes, it means there is a Nexus service running on that

tests/conftest.py

+49-49
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
"""Global fixtures go here."""
2-
import os
2+
3+
from pathlib import Path
34
from random import choice, random
4-
import shutil
55
from string import ascii_letters
6-
import subprocess
76

87
import pytest
98

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

1817

19-
def cleanup_docker(request) -> None:
20-
# Stop and remove 'nexus' containers. This needs to be deleted once we address the issue
21-
# in the pynexus code by giving unique names to the containers
22-
try:
23-
subprocess.run(["docker", "stop", "nexus"])
24-
subprocess.run(["docker", "rm", "nexus"])
25-
except Exception:
26-
# There might not be a running nexus container. That is fine, just continue
27-
pass
28-
try:
29-
querydb_dir = os.path.join(os.path.join(request.fspath.dirname, "test_data"), "query_db")
30-
os.remove(os.path.join(querydb_dir, "nexus.log"))
31-
os.remove(os.path.join(querydb_dir, "nexus.status"))
32-
shutil.rmtree(os.path.join(querydb_dir, "nginx"))
33-
except Exception:
34-
# There might not be these files / directories. In which case, nothing to do
35-
pass
36-
37-
38-
@pytest.fixture
18+
@pytest.fixture(scope="module")
3919
def get_exec(pytestconfig: pytest.Config) -> str:
4020
exec_basis = ""
4121
use_local = pytestconfig.getoption("use_local_launcher")
@@ -44,52 +24,72 @@ def get_exec(pytestconfig: pytest.Config) -> str:
4424
return exec_basis
4525

4626

47-
@pytest.fixture
48-
def adr_service_create(request, pytestconfig: pytest.Config) -> Service:
27+
@pytest.fixture(scope="module")
28+
def adr_service_create(pytestconfig: pytest.Config) -> Service:
4929
use_local = pytestconfig.getoption("use_local_launcher")
50-
dir_name = "auto_delete_" + "".join(choice(ascii_letters) for x in range(5))
51-
db_dir = os.path.join(os.path.join(request.fspath.dirname, "test_data"), dir_name)
52-
tmp_docker_dir = os.path.join(os.path.join(request.fspath.dirname, "test_data"), "tmp_docker")
30+
31+
# Paths setup
32+
base_dir = Path(__file__).parent / "test_data"
33+
dir_name = "auto_delete_" + "".join(choice(ascii_letters) for _ in range(5))
34+
db_dir = base_dir / dir_name
35+
tmp_docker_dir = base_dir / "tmp_docker"
36+
5337
if use_local:
54-
tmp_service = Service(
38+
adr_service = Service(
5539
ansys_installation=pytestconfig.getoption("install_path"),
5640
docker_image=DOCKER_DEV_REPO_URL,
57-
db_directory=db_dir,
41+
db_directory=str(db_dir),
5842
port=8000 + int(random() * 4000),
5943
)
6044
else:
61-
cleanup_docker(request)
62-
tmp_service = Service(
45+
adr_service = Service(
6346
ansys_installation="docker",
6447
docker_image=DOCKER_DEV_REPO_URL,
65-
db_directory=db_dir,
66-
data_directory=tmp_docker_dir,
48+
db_directory=str(db_dir),
49+
data_directory=str(tmp_docker_dir),
6750
port=8000 + int(random() * 4000),
6851
)
69-
return tmp_service
7052

53+
_ = adr_service.start(
54+
create_db=True,
55+
exit_on_close=True,
56+
delete_db=True,
57+
)
58+
59+
yield adr_service # Return to running the test session
60+
61+
# Cleanup
62+
adr_service.stop()
7163

72-
@pytest.fixture
73-
def adr_service_query(request, pytestconfig: pytest.Config) -> Service:
64+
65+
@pytest.fixture(scope="module")
66+
def adr_service_query(pytestconfig: pytest.Config) -> Service:
7467
use_local = pytestconfig.getoption("use_local_launcher")
75-
local_db = os.path.join("test_data", "query_db")
76-
db_dir = os.path.join(request.fspath.dirname, local_db)
77-
tmp_docker_dir = os.path.join(
78-
os.path.join(request.fspath.dirname, "test_data"), "tmp_docker_query"
79-
)
68+
69+
# Paths setup
70+
base_dir = Path(__file__).parent / "test_data"
71+
local_db = base_dir / "query_db"
72+
tmp_docker_dir = base_dir / "tmp_docker_query"
73+
8074
if use_local:
8175
ansys_installation = pytestconfig.getoption("install_path")
8276
else:
83-
cleanup_docker(request)
8477
ansys_installation = "docker"
85-
tmp_service = Service(
78+
79+
adr_service = Service(
8680
ansys_installation=ansys_installation,
8781
docker_image=DOCKER_DEV_REPO_URL,
88-
db_directory=db_dir,
89-
data_directory=tmp_docker_dir,
82+
db_directory=str(local_db),
83+
data_directory=str(tmp_docker_dir),
9084
port=8000 + int(random() * 4000),
9185
)
86+
9287
if not use_local:
93-
tmp_service._container.save_config()
94-
tmp_service.start(create_db=False, exit_on_close=True, delete_db=False)
95-
return tmp_service
88+
adr_service._container.save_config()
89+
90+
adr_service.start(create_db=False, exit_on_close=True, delete_db=False)
91+
92+
yield adr_service # Return to running the test session
93+
94+
# Cleanup
95+
adr_service.stop()

tests/test_download_html.py

-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ def test_download_use_data(request, adr_service_query) -> bool:
99
my_url += "/reports/report_display/?report_table_length=10&view=c4afe878-a4fe-11ed-a616-747827182a82&usemenus=on&dpi=96&pwidth=19.41&query="
1010
a = rd.ReportDownloadHTML(url=my_url, directory=test_dir, debug=True)
1111
test_res = a._should_use_data_uri(size=5)
12-
adr_service_query.stop()
1312
assert test_res
1413

1514

@@ -22,7 +21,6 @@ def test_download_nourl(request, adr_service_query) -> bool:
2221
success = False
2322
except ValueError:
2423
success = True
25-
adr_service_query.stop()
2624
assert success
2725

2826

@@ -35,7 +33,6 @@ def test_download_nodir(request, adr_service_query) -> bool:
3533
success = False
3634
except ValueError:
3735
success = True
38-
adr_service_query.stop()
3936
assert success
4037

4138

@@ -49,7 +46,6 @@ def test_download_sqlite(request, adr_service_query) -> bool:
4946
success = False
5047
except Exception:
5148
success = True
52-
adr_service_query.stop()
5349
assert success
5450

5551

@@ -59,5 +55,4 @@ def test_download(request, adr_service_query) -> bool:
5955
my_url += "/reports/report_display/?report_table_length=10&view=c4afe878-a4fe-11ed-a616-747827182a82&usemenus=on&dpi=96&pwidth=19.41&query="
6056
a = rd.ReportDownloadHTML(url=my_url, directory=test_dir, debug=True)
6157
test_res = a.download()
62-
adr_service_query.stop()
6358
assert test_res is None

tests/test_geofile_processing.py

+6
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ def test_file_is_3d_geometry(request) -> bool:
4747
assert scene is True and img is False
4848

4949

50+
@pytest.mark.ado_test
51+
def test_get_avz_directory(request) -> bool:
52+
avz_dir = gp.get_avz_directory(return_file_paths(request)[1])
53+
assert isinstance(avz_dir, str) and avz_dir != ""
54+
55+
5056
@pytest.mark.ado_test
5157
def test_rebuild_3d_geom_avz(request) -> bool:
5258
_ = gp.rebuild_3d_geometry(

0 commit comments

Comments
 (0)