Skip to content

Commit eceeae1

Browse files
committed
add tests
1 parent 2504922 commit eceeae1

File tree

3 files changed

+132
-1
lines changed

3 files changed

+132
-1
lines changed

doc/source/serverless/templates.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ Example using the ADR entrypoint:
412412
# Export the report as a directory with linked assets
413413
output_path = adr.export_report_as_html(
414414
output_directory=export_dir,
415-
name="Wing Simulation Report", # kwarg to find the template
415+
name="Wing Simulation Report", # kwarg to find the template
416416
context={"key": "value"},
417417
item_filter="A|i_tags|cont|project=wing_sim;",
418418
)

tests/serverless/test_adr.py

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,6 +1024,136 @@ def fake_render_pptx_fails(self, context, item_filter, request):
10241024
adr_serverless.render_report_as_pptx(name="FailingPPTXReport")
10251025

10261026

1027+
@pytest.mark.ado_test
1028+
def test_export_report_as_html(adr_serverless, tmp_path, monkeypatch):
1029+
"""
1030+
Tests the successful export of a report to a directory, ensuring all arguments
1031+
are correctly passed to the underlying exporter class.
1032+
"""
1033+
from ansys.dynamicreporting.core.serverless import BasicLayout
1034+
from ansys.dynamicreporting.core.serverless.adr import ServerlessReportExporter
1035+
1036+
# 1. Arrange: Create a dummy template that the export method can find.
1037+
adr_serverless.create_template(BasicLayout, name="TestExportReport", parent=None)
1038+
1039+
# Define the context and filter to be used in the export call.
1040+
test_context = {"key": "value"}
1041+
test_filter = "A|i_tags|cont|project=wing_sim;"
1042+
1043+
# 2. Mock: Patch the ServerlessReportExporter's export method to intercept its call.
1044+
# This avoids the overhead of actual file operations and lets us verify the inputs.
1045+
def mock_export(self):
1046+
# Assert that the exporter was initialized with the correct, expected values.
1047+
assert self._output_dir == tmp_path
1048+
assert self._static_dir == adr_serverless.static_directory
1049+
assert "<h1>" in self._html_content # A simple check for rendered HTML
1050+
assert self._single_file is False
1051+
# Verify that context and filter were passed through correctly during render
1052+
# Note: We can't directly check the context/filter on the exporter, but we
1053+
# can infer it was used because the render call would fail otherwise.
1054+
1055+
monkeypatch.setattr(ServerlessReportExporter, "export", mock_export)
1056+
1057+
# 3. Act: Call the method under test with a full set of arguments.
1058+
output_path = adr_serverless.export_report_as_html(
1059+
output_directory=tmp_path,
1060+
name="TestExportReport", # kwarg to find the template
1061+
context=test_context,
1062+
item_filter=test_filter,
1063+
)
1064+
1065+
# 4. Assert: Verify the returned path is correctly constructed.
1066+
assert output_path == tmp_path / "index.html"
1067+
1068+
1069+
@pytest.mark.ado_test
1070+
def test_export_report_no_static_dir_fails(adr_serverless, tmp_path, monkeypatch):
1071+
"""
1072+
Ensures that the export fails with a configuration error if the
1073+
static directory has not been set on the ADR instance.
1074+
"""
1075+
# Temporarily remove the static_directory attribute to simulate the error condition.
1076+
monkeypatch.setattr(adr_serverless, "_static_directory", None)
1077+
1078+
with pytest.raises(
1079+
ImproperlyConfiguredError, match="The 'static_directory' must be configured"
1080+
):
1081+
adr_serverless.export_report_as_html(
1082+
output_directory=tmp_path,
1083+
name="AnyReport",
1084+
)
1085+
1086+
1087+
@pytest.mark.ado_test
1088+
def test_export_report_no_kwarg_fails(adr_serverless, tmp_path):
1089+
"""
1090+
Ensures that the export fails if no keyword argument (like 'name' or 'guid')
1091+
is provided to find the template.
1092+
"""
1093+
with pytest.raises(ADRException, match="At least one keyword argument must be provided"):
1094+
adr_serverless.export_report_as_html(output_directory=tmp_path)
1095+
1096+
1097+
@pytest.mark.ado_test
1098+
def test_export_report_as_html_directory_mode(adr_serverless, tmp_path, monkeypatch):
1099+
"""
1100+
Tests the successful export of a report to a directory.
1101+
"""
1102+
from ansys.dynamicreporting.core.serverless import BasicLayout
1103+
from ansys.dynamicreporting.core.serverless.adr import ServerlessReportExporter
1104+
1105+
# Create a dummy template to be exported
1106+
adr_serverless.create_template(BasicLayout, name="TestExportReport", parent=None)
1107+
1108+
# Mock the ServerlessReportExporter's export method to prevent actual file operations
1109+
# during the test, making it faster and more isolated.
1110+
def mock_export(self):
1111+
# Verify that the exporter was initialized with the correct directories
1112+
assert self._output_dir == tmp_path
1113+
assert self._static_dir == adr_serverless.static_directory
1114+
assert "<h1>" in self._html_content # Check if some HTML content was passed
1115+
assert self._single_file is False
1116+
1117+
monkeypatch.setattr(ServerlessReportExporter, "export", mock_export)
1118+
1119+
# Call the method under test
1120+
output_path = adr_serverless.export_report_as_html(
1121+
output_directory=tmp_path,
1122+
single_file=False,
1123+
name="TestExportReport",
1124+
)
1125+
1126+
# Verify the returned path is correct
1127+
assert output_path == tmp_path / "index.html"
1128+
1129+
1130+
@pytest.mark.ado_test
1131+
def test_export_report_as_html_single_file_mode(adr_serverless, tmp_path, monkeypatch):
1132+
"""
1133+
Tests the successful export of a report in single-file mode.
1134+
"""
1135+
from ansys.dynamicreporting.core.serverless import BasicLayout
1136+
from ansys.dynamicreporting.core.serverless.adr import ServerlessReportExporter
1137+
1138+
adr_serverless.create_template(BasicLayout, name="TestExportSingleFile", parent=None)
1139+
1140+
def mock_export(self):
1141+
# Verify the exporter is correctly configured for single-file mode
1142+
assert self._single_file is True
1143+
assert self._filename == "report.html"
1144+
1145+
monkeypatch.setattr(ServerlessReportExporter, "export", mock_export)
1146+
1147+
output_path = adr_serverless.export_report_as_html(
1148+
output_directory=tmp_path,
1149+
filename="report.html",
1150+
single_file=True,
1151+
name="TestExportSingleFile",
1152+
)
1153+
1154+
assert output_path == tmp_path / "report.html"
1155+
1156+
10271157
@pytest.mark.ado_test
10281158
def test_copy_sessions(adr_serverless):
10291159
from ansys.dynamicreporting.core.serverless import Session

tests/serverless/test_common_utils.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from pathlib import Path
22

33
import pytest
4+
45
from ansys.dynamicreporting.core import DEFAULT_ANSYS_VERSION
56
from ansys.dynamicreporting.core.common_utils import get_install_info, get_install_version
67
from ansys.dynamicreporting.core.exceptions import InvalidAnsysPath

0 commit comments

Comments
 (0)