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

TESTS: adding model tests #20

Merged
merged 13 commits into from
Feb 23, 2024
70 changes: 49 additions & 21 deletions .github/workflows/ci_cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,24 @@ jobs:
python-version: ${{ env.MAIN_PYTHON_VERSION }}
use-python-cache: false

smoke-tests:
name: Build and Smoke tests
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest]
python-version: ['3.9', '3.10', '3.11']
should-release:
- ${{ github.event_name == 'push' && contains(github.ref, 'refs/tags') }}
steps:
- name: Build wheelhouse and perform smoke test
uses: ansys/actions/build-wheelhouse@v5
with:
library-name: ${{ env.PACKAGE_NAME }}
operating-system: ${{ matrix.os }}
python-version: ${{ matrix.python-version }}

doc-style:
name: Documentation style
runs-on: ubuntu-latest
Expand All @@ -38,6 +56,26 @@ jobs:
with:
token: ${{ secrets.GITHUB_TOKEN }}

tests:
name: Test and coverage
runs-on: ubuntu-latest
steps:
- uses: ansys/actions/tests-pytest@v5
with:
token: ${{ secrets.GITHUB_TOKEN }}

- name: Upload Coverage Results
if: always()
uses: actions/upload-artifact@v4
with:
name: coverage-html
path: .cov/html
retention-days: 7

# =================================================================================================
# vvvvvvvvvvvvvvvvvvvvvvvvvvvvvv RUNNING ON SELF-HOSTED RUNNER vvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
# =================================================================================================

doc-build:
name: Build documentation
runs-on: [pyaedt, Windows]
Expand Down Expand Up @@ -108,30 +146,20 @@ jobs:
single-commit: true

release:
name: Release project
if: github.event_name == 'push' && contains(github.ref, 'refs/tags')
needs: [code-style, doc-build]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- uses: actions/download-artifact@v3

- name: Display structure of downloaded files
run: ls -R

- name: Zip HTML documentation
uses: vimtor/[email protected]
with:
files: documentation-html
dest: documentation-html.zip
# TODO: update if ever public
# - name: Release to the public PyPI repository
# uses: ansys/actions/release-pypi-public@v5
# with:
# library-name: ${{ env.PACKAGE_NAME }}
# twine-username: "__token__"
# twine-token: ${{ secrets.PYPI_TOKEN }}

- name: Release
uses: softprops/action-gh-release@v1
- name: Release to GitHub
uses: ansys/actions/release-github@v5
with:
generate_release_notes: true
files: |
./documentation-html.zip
./documentation-pdf/*.pdf



library-name: ${{ env.PACKAGE_NAME }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pip-delete-this-directory.txt
# Unit test / coverage reports
.tox/
.coverage
.cov
.cache
nosetests.xml
coverage.xml
Expand Down
19 changes: 19 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ dependencies = [
"twine==5.0.0",
"pydantic",
"pyvista",
"jinja2",
]

[project.optional-dependencies]
Expand Down Expand Up @@ -65,6 +66,10 @@ doc = [
"vtk==9.2.6",
"pypandoc",
]
tests = [
"pytest==7.4.3",
"pytest-cov==4.1.0",
]
ci = [
"vtk-osmesa==9.2.20230527.dev0",
"pypandoc-binary==1.13",
Expand All @@ -91,3 +96,17 @@ line_length=100
[tool.codespell]
skip = '*.css,*.pyc,*.txt,*.gif,*.png,*.jpg,*.js,*.html,*.doctree,*.ttf,*.woff,*.woff2,*.eot,*.mp4,*.inv,*.pickle,*.ipynb,flycheck*,./.git/*,./.hypothesis/*,*.yml,./doc/build/*,./doc/images/*,./dist/*,*~,.hypothesis*,./doc/source/examples/*,*cover,*.dat,*.mac,build,./docker/mapdl/v*,./factory/*,./ansys/mapdl/core/mapdl_functions.py,PKG-INFO,*.mypy_cache/*,./docker/mapdl/*,./_unused/*'
quiet-level = 3

[tool.coverage.run]
source = ["ansys.pyaedt.examples"]

[tool.coverage.report]
show_missing = true
exclude_lines = [
'if __name__ == .__main__.:'
]

[tool.pytest.ini_options]
minversion = "7.1"
addopts = "-ra --cov=ansys.pyaedt.examples --cov-report html:.cov/html --cov-report xml:.cov/xml --cov-report term -vv"
testpaths = ["tests"]
3 changes: 2 additions & 1 deletion src/ansys/pyaedt/examples/edb_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
from jinja2 import Environment, FileSystemLoader

PARENT_DIR = os.path.dirname(os.path.abspath(__file__))
EXAMPLE_TITLE = "Simple workflow using EDB"


def write_example(example_path, data: EDBModel):
Expand All @@ -59,7 +60,7 @@ def write_example(example_path, data: EDBModel):
data = {
"header_required": False,
"header": HEADER,
"example_title": "Simple workflow using EDB",
"example_title": EXAMPLE_TITLE,
"example_preamble": TextCell(EDB_PREAMBLE),
"step_imports": CodeCell(EDB_IMPORTS),
"step_temp_dir": CodeCell(EDB_TMP_DIR),
Expand Down
2 changes: 1 addition & 1 deletion src/ansys/pyaedt/examples/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class EDBModel(BaseModel):
header_required: bool = False
header: Union[TextCell, str] = ""
example_title: str = "Simple workflow using EDB"
example_preamble: Union[TextCell, str] = ""
example_preamble: TextCell = TextCell("# This example contains multiple steps...")
step_imports: CodeCell = CodeCell(EDB_IMPORTS)
step_temp_dir: CodeCell = CodeCell(EDB_TMP_DIR)
download_required: bool = False
Expand Down
40 changes: 0 additions & 40 deletions src/ansys/pyaedt/examples/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,43 +33,3 @@ def download_file(url, local_filename):
for chunk in r.iter_content(chunk_size=4096):
f.write(chunk)
return local_filename


import os

from jinja2 import Environment, FileSystemLoader

# Charger le template Jinja
file_loader = FileSystemLoader(os.path.dirname(os.path.abspath(__file__)))
env = Environment(loader=file_loader)
template = env.get_template("template.j2")

# Données pour le template
data = {
"imports": "import os\nimport sys",
"functions": [
{"name": "my_function", "description": "Cette function fait quelque chose."},
],
"environment_setup": "Création d'un répertoire temporaire...",
"app_setup": "Configuration de l'application...",
"processing": "Traitement des données...",
"cleanup": "Nettoyage et suppression du répertoire temporaire...",
}

# Générer le fichier Markdown
output = template.render(**data)
with open("example.md", "w") as f:
f.write(output)
# Données pour le template
data = {
"imports": "import os\nimport sys",
"functions": [
{"name": "my_function", "description": "Cette function fait quelque chose."},
],
"environment_setup": "Création d'un répertoire temporaire...",
"download_required": True, # ou False, en function de vos besoins
"download_step": "Téléchargement des fichiers nécessaires...",
"app_setup": "Configuration de l'application...",
"processing": "Traitement des données...",
"cleanup": "Nettoyage et suppression du répertoire temporaire...",
}
Empty file added tests/__init__.py
Empty file.
36 changes: 36 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Copyright (C) 2024 ANSYS, Inc. and/or its affiliates.
# SPDX-License-Identifier: MIT
#
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

"""
"""
import shutil

import pytest


@pytest.fixture(scope="session")
def common_temp_dir(tmp_path_factory):
tmp_dir = tmp_path_factory.mktemp("test_examples", numbered=True)

yield tmp_dir

shutil.rmtree(str(tmp_dir), ignore_errors=True)
76 changes: 76 additions & 0 deletions tests/test_edb_writer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import os

from ansys.pyaedt.examples.constants import (
EDB_APP_SETUP,
EDB_IMPORTS,
EDB_TMP_DIR,
EDB_VERSION,
HEADER,
)
from ansys.pyaedt.examples.edb_writer import EXAMPLE_TITLE, write_example
from ansys.pyaedt.examples.models import CodeCell, EDBModel, TextCell

EXAMPLE_PREAMBLE = "# Here is a simple workflow without processings"
EXPECTED_RESULT = f"""# ## {EXAMPLE_TITLE}
#
{EXAMPLE_PREAMBLE}

# ## Perform required imports and create a temporary folder.
#
# Perform imports required to run the example and create a temporary folder in which to save files.
{EDB_IMPORTS}

{EDB_TMP_DIR}

# ## Create an instance of the Electronics Database using the `pyaedt.Edb` class.
#
# > Note that units are SI.

# Select EDB version (change it manually if needed, e.g. "2023.2")
edb_version = {EDB_VERSION}
print(f"EDB version: {{edb_version}}")

{EDB_APP_SETUP}



# ## Save and clean up
#
# The following commands save and close the EDB. After that, the temporary
# directory containing the project is deleted. To keep this project, save it to
# another folder of your choice prior to running the following cell.

# +
edb.save_edb()
edb.close_edb()
print("EDB saved correctly to {{}}. You can import in AEDT.".format(aedb_path))

# Removing the temporary directory
temp_dir.cleanup()
# -"""


def test_edb_writer(common_temp_dir):
""""""
example_path = os.path.join(common_temp_dir, "dummy_example.py")
data = {
"header_required": False,
"header": HEADER,
"example_title": "Simple workflow using EDB",
"example_preamble": TextCell(EXAMPLE_PREAMBLE),
"step_imports": CodeCell(EDB_IMPORTS),
"step_temp_dir": CodeCell(EDB_TMP_DIR),
"download_required": False,
"step_download": CodeCell(""),
"edb_version": EDB_VERSION,
"step_app_setup": CodeCell(EDB_APP_SETUP),
"step_processing": "",
}

model_data = EDBModel(**data)
write_example(example_path, model_data)

with open(example_path, "r") as file:
example_content = file.read()

assert EXPECTED_RESULT == example_content
Loading
Loading