Skip to content

Commit

Permalink
test(readme): add test for the example in the readme
Browse files Browse the repository at this point in the history
  • Loading branch information
gilrrei committed Jan 10, 2025
1 parent ca5a657 commit 4d496ba
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
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
45 changes: 45 additions & 0 deletions test_utils/get_example_in_read_me.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#
# SPDX-License-Identifier: LGPL-3.0-or-later
# Copyright (c) 2024, 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 pathlib import Path

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 = Path(__file__).parents[1] / "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
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) 2024, 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_example_in_read_me 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

0 comments on commit 4d496ba

Please sign in to comment.