Skip to content

Commit

Permalink
test: Add test for settingsgen edge cases (#3392)
Browse files Browse the repository at this point in the history
* test: Add test for settingsgen edge cases

* test: Add test for settingsgen edge cases
  • Loading branch information
mkundu1 authored Oct 18, 2024
1 parent ed11995 commit c84f7cd
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/ansys/fluent/core/codegen/settingsgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,8 @@ def generate(version: str, static_infos: dict) -> None:
# _populate_data() collects all strings to write to the file in a nested dict.
# which is then written to the file using _write_data().
data = _populate_data(cls, api_tree, version)
_NAME_BY_HASH.clear()
_CLASS_WRITTEN.clear()
with open(output_file, "w") as f, open(output_stub_file, "w") as f_stub:
header = StringIO()
header.write("#\n")
Expand Down
110 changes: 110 additions & 0 deletions tests/test_codegen.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import ast
import importlib
from pathlib import Path
import pickle
Expand Down Expand Up @@ -1081,3 +1082,112 @@ def test_codegen_with_settings_static_info(monkeypatch):
api_tree_expected[f"<solver_session>"] = settings_tree
assert api_tree == api_tree_expected
shutil.rmtree(str(codegen_outdir))


_settings_static_info_duplicate_parameters = {
"children": (
_get_group_settings_static_info(
"G1",
(
(
_get_group_settings_static_info(
"G2",
_get_parameter_settings_static_info("P1", "string"),
{},
{},
)
)
| _get_parameter_settings_static_info("P1", "string")
),
{},
{},
)
),
"commands": {},
"queries": {},
"type": "group",
}

_settings_static_info_different_parameters_with_same_name = {
"children": (
_get_group_settings_static_info(
"G1",
(
(
_get_group_settings_static_info(
"G2",
_get_parameter_settings_static_info("P1", "real"),
{},
{},
)
)
| _get_parameter_settings_static_info("P1", "string")
),
{},
{},
)
),
"commands": {},
"queries": {},
"type": "group",
}


_settings_static_info_combined_case = {
"children": (
_get_group_settings_static_info(
"G1",
(
(
_get_group_settings_static_info(
"G2",
(
_get_parameter_settings_static_info("P1", "real")
| _get_parameter_settings_static_info("P2", "string")
),
{},
{},
)
)
| _get_parameter_settings_static_info("P1", "string")
| _get_parameter_settings_static_info("P2", "string")
),
{},
{},
)
),
"commands": {},
"queries": {},
"type": "group",
}


@pytest.mark.parametrize(
"settings_static_info,class_names",
[
(_settings_static_info_duplicate_parameters, ["P1", "G2", "G1", "root"]),
(
_settings_static_info_different_parameters_with_same_name,
["P1_1", "G2", "P1", "G1", "root"],
),
(_settings_static_info_combined_case, ["P1_1", "P2", "G2", "P1", "G1", "root"]),
],
)
def test_codegen_with_settings_static_info_edge_cases(
monkeypatch, settings_static_info, class_names
):

codegen_outdir = Path(tempfile.mkdtemp())
monkeypatch.setattr(pyfluent, "CODEGEN_OUTDIR", codegen_outdir)
version = "251"
static_infos = {}
static_infos[StaticInfoType.SETTINGS] = settings_static_info
allapigen.generate(version, static_infos)
with open(codegen_outdir / "solver" / f"settings_{version}.py", "r") as f:
module_def = ast.parse(f.read())
class_names_from_file = [
x.name for x in module_def.body if isinstance(x, ast.ClassDef)
]
# The order of classes is important.
assert class_names_from_file == class_names
shutil.rmtree(str(codegen_outdir))

0 comments on commit c84f7cd

Please sign in to comment.