Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(readme): add test for the example in the readme #91

Merged
merged 2 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ pip install -e .
## :crown: Workflow example

Let's consider a parallelized Monte Carlo simulation of the [Ishigami function](https://www.sfu.ca/~ssurjano/ishigami.html):
<!---example marker, do not remove this comment-->
```python
from queens.distributions import BetaDistribution, NormalDistribution, UniformDistribution
from queens.drivers import FunctionDriver
Expand Down Expand Up @@ -80,6 +81,7 @@ if __name__ == "__main__":
# Start QUEENS run
run_iterator(iterator, global_settings=global_settings)
```
<!---example marker, do not remove this comment-->

<div align="center">
<img src="readme_images/monte_carlo_uq.png" alt="QUEENS logo" width="500"/>
Expand Down
2 changes: 1 addition & 1 deletion license_header.tmpl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
SPDX-License-Identifier: LGPL-3.0-or-later
Copyright (c) 2024, QUEENS contributors.
Copyright (c) 2025, QUEENS contributors.
gilrrei marked this conversation as resolved.
Show resolved Hide resolved

This file is part of QUEENS.

Expand Down
46 changes: 46 additions & 0 deletions test_utils/get_queens_example_from_readme.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#
# SPDX-License-Identifier: LGPL-3.0-or-later
# Copyright (c) 2025, QUEENS contributors.
#
# This file is part of QUEENS.
#
# QUEENS is free software: you can redistribute it and/or modify it under the terms of the GNU
# Lesser General Public License as published by the Free Software Foundation, either version 3 of
# the License, or (at your option) any later version. QUEENS is distributed in the hope that it will
# be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You
# should have received a copy of the GNU Lesser General Public License along with QUEENS. If not,
# see <https://www.gnu.org/licenses/>.
#
"""Extract QUEENS example from the readme."""


from queens.utils.path_utils import relative_path_from_queens

EXAMPLE_MARKER = "<!---example marker, do not remove this comment-->"


def get_queens_example_from_readme(output_dir):
"""Extract the example from the readme.

Args:
output_dir (str): Output directory for the QUEENS run.
"""
readme_path = relative_path_from_queens("README.md")

# Split the example in the readme using the marker
text = readme_path.read_text().split(EXAMPLE_MARKER)

# Only one example should appear
if len(text) != 3:
raise ValueError("Could not extract the example from the readme!")

# Extract the source
example_source = (
text[1]
.replace("```python", "")
.replace("```", "")
.replace('output_dir="."', f'output_dir="{output_dir}"')
)

return example_source
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ STRUCT_STRAIN Yes
FILESTEPS 1000
----------------------------------------------------------STRUCTURAL DYNAMIC
LINEAR_SOLVER 1
DYNAMICTYPE Statics
RESULTSEVRY 1
RESTARTEVRY 0
DYNAMICTYPE Statics
RESULTSEVERY 1
RESTARTEVERY 0
NLNSOL fullnewton
TIMESTEP 0.025
NUMSTEP 40
Expand All @@ -41,7 +41,7 @@ TOLDISP 1.0E-06
TOLRES 1.0E-06
MAXITER 200
UZAWAPARAM 1.0
RESEVRYERGY 1
RESEVERYERGY 1
-------------------------------------------------------------------SOLVER 1
NAME No_Name
SOLVER UMFPACK
Expand Down
6 changes: 3 additions & 3 deletions tests/input_files/third_party/fourc/solid_runtime_hex8.dat
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ NAME Structure_Solver
SOLVER Superlu
------------------------------------------------------------STRUCTURAL DYNAMIC
INT_STRATEGY Standard
DYNAMICTYPE Statics
DYNAMICTYPE Statics
PRESTRESSTOLDISP 1e-9
RESULTSEVRY 1
RESTARTEVRY 1
RESULTSEVERY 1
RESTARTEVERY 1
TIMESTEP 0.5
NUMSTEP 2
MAXTIME 1.0
Expand Down
32 changes: 32 additions & 0 deletions tests/integration_tests/python/test_readme_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#
# SPDX-License-Identifier: LGPL-3.0-or-later
# Copyright (c) 2025, QUEENS contributors.
#
# This file is part of QUEENS.
#
# QUEENS is free software: you can redistribute it and/or modify it under the terms of the GNU
# Lesser General Public License as published by the Free Software Foundation, either version 3 of
# the License, or (at your option) any later version. QUEENS is distributed in the hope that it will
# be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You
# should have received a copy of the GNU Lesser General Public License along with QUEENS. If not,
# see <https://www.gnu.org/licenses/>.
#
"""Test the readme QUEENS example."""

from queens.utils.run_subprocess import run_subprocess
from test_utils.get_queens_example_from_readme import get_queens_example_from_readme


def test_queens_readme_example(tmp_path):
"""Test if the example in the readme runs."""
# Get the source of the example
example_source = get_queens_example_from_readme(tmp_path)

# Run the script
process_returncode, _, _, _ = run_subprocess(
f"python -c '{example_source}'", raise_error_on_subprocess_failure=False
)

# Check for an exit code
assert not process_returncode
Loading