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

Added new test infrastructure #29

Merged
merged 23 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
7ad77d5
Seperated nox sessions for templates and tests and, new testing infra…
santacodes Jul 21, 2024
0c8c51a
Merge branch 'pybamm-team:main' into newtestinfra
santacodes Jul 22, 2024
f8b7f04
Merge branch 'main' into newtestinfra
arjxn-py Jul 22, 2024
4146ac9
Merge branch 'main' into newtestinfra
santacodes Jul 23, 2024
607ffaf
made hatch vcs optional
santacodes Jul 23, 2024
62f8713
Apply suggestions from code review
santacodes Jul 23, 2024
572c190
fixed version modules
santacodes Jul 24, 2024
6a7eb84
added _version.pyi with annotations
santacodes Jul 24, 2024
65a7eb4
Update {{cookiecutter.project_name}}/src/{{cookiecutter.__project_slu…
santacodes Jul 24, 2024
cebcd8a
fixed hatch vcs and versioning
santacodes Jul 24, 2024
5afccee
Apply suggestions from code review
santacodes Jul 24, 2024
9571750
code review suggestion in the parent yml workflow
santacodes Jul 24, 2024
8bc8a67
restored git history for doctests
santacodes Jul 24, 2024
d6fd552
changed project name to project slug in module imports
santacodes Jul 24, 2024
621b05b
changed project name in workflow
santacodes Jul 24, 2024
ef3ef2f
changed vcs configuration
santacodes Jul 26, 2024
fd76ebc
Apply suggestions from code review
santacodes Jul 27, 2024
30cd4b1
made the workflow uniform and fixed workflow for generated project tests
santacodes Jul 27, 2024
a9aa352
Merge branch 'pybamm-team:main' into newtestinfra
santacodes Jul 30, 2024
fd6f428
Apply suggestions from code review
santacodes Jul 31, 2024
4ced431
added session setup function in noxfile
santacodes Jul 31, 2024
518428f
Apply suggestions from code review
santacodes Jul 31, 2024
bd4bb98
Update .github/workflows/test_on_push.yml
santacodes Jul 31, 2024
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
49 changes: 41 additions & 8 deletions .github/workflows/test_on_push.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Test template generation
name: Test template and generated project

on:
workflow_dispatch:
Expand All @@ -7,7 +7,6 @@ on:
branches:
- main


jobs:
style:
runs-on: ubuntu-latest
Expand All @@ -32,7 +31,7 @@ jobs:
os: [ubuntu-latest, macos-13, macos-14, windows-latest]
python-version: ["3.9", "3.10", "3.11", "3.12"]
name:
Template Tests (${{ matrix.os }} / Python ${{ matrix.python-version }})
Template Generation/Project Tests (${{ matrix.os }} / Python ${{ matrix.python-version }})
steps:
- name: Checkout pybamm-cookiecutter
uses: actions/checkout@v4
Expand All @@ -51,13 +50,15 @@ jobs:
- name: Install nox
run: uv pip install nox[uv]

- name: Test Template Generation
run: |
nox -s test-generation
- name: Test template generation
run: nox -s template-tests

- name: Test project
run: nox -s project-tests

- name: Run coverage tests
if: matrix.os == 'ubuntu-latest' && matrix.python-version == '3.12'
run: python -m nox -s coverage
run: nox -s coverage

- name: Upload coverage report
if: matrix.os == 'ubuntu-latest' && matrix.python-version == '3.12'
Expand Down Expand Up @@ -86,4 +87,36 @@ jobs:
run: python -m pip install nox

- name: Check if the documentation can be built
run: python -m nox -s docs
run: nox -s docs

santacodes marked this conversation as resolved.
Show resolved Hide resolved
generated_project_tests:
needs: [template_test]

Saransh-cpp marked this conversation as resolved.
Show resolved Hide resolved
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-13, macos-14, windows-latest]
python-version: ["3.9", "3.10", "3.11", "3.12"]
name:
Generated Project Tests (${{ matrix.os }} / Python ${{ matrix.python-version }})
steps:
- name: Checkout pybamm-cookiecutter
uses: actions/checkout@v4

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}

- name: Install cookiecutter and pipx
run: python -m pip install cookiecutter pipx

- name: Generate a project using cookiecutter
run: cookiecutter . --no-input

- name: Install nox and test generated project
working-directory: ./pybamm-example-project
run: |
pipx install nox
pipx run nox -s generated-project-tests
5 changes: 4 additions & 1 deletion cookiecutter.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
"backend": [
"hatch"
],
"hatch_vcs": false,
"mypy": false,
"__year": "{% now 'utc', '%Y' %}",
"__project_slug": "{{ cookiecutter.project_name | lower | replace('-', '_') | replace('.', '_') }}",
"_copy_without_render": ["*.yml"],
"__type": "{{ 'pure' }}",
"__answers": "",
"__ci": "{{ cookiecutter.platform }}",
Expand All @@ -40,7 +42,8 @@
"license": "Select a license",
"backend": {
"__prompt__": "Choose a build backend",
"hatch": "Hatchling (recommended for pure Python projects)"
"hatch": "Hatchling (recommended for pure Python projects)",
"hatch_vcs": "Enable hatch version control (vcs)?"
santacodes marked this conversation as resolved.
Show resolved Hide resolved
},
"mypy": "Use mypy and static types"
}
Expand Down
1 change: 0 additions & 1 deletion hooks/post_gen_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,4 @@ def prepare_git() -> None:
)

if __name__ == "__main__":
print("Executing script!!")
prepare_git()
arjxn-py marked this conversation as resolved.
Show resolved Hide resolved
11 changes: 9 additions & 2 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,19 @@ def build_docs(session: nox.Session) -> None:
"build/html/",
)

@nox.session(name="test-generation")
@nox.session(name="template-tests")
def run_template_generation(session):
"""Run the tests for testing template generation"""
santacodes marked this conversation as resolved.
Show resolved Hide resolved
session.install("setuptools", silent=False)
session.install("-e", ".[dev]", silent=False)
session.run("pytest", "tests")
session.run("pytest", "tests/template_tests")

@nox.session(name="project-tests")
santacodes marked this conversation as resolved.
Show resolved Hide resolved
def run_project_tests(session):
"""Run the tests for testing template generation"""
santacodes marked this conversation as resolved.
Show resolved Hide resolved
session.install("setuptools", silent=False)
session.install("-e", ".[dev]", silent=False)
session.run("pytest", "tests/project_tests")

@nox.session(name="coverage")
def run_coverage(session):
Expand Down
File renamed without changes.
90 changes: 90 additions & 0 deletions {{cookiecutter.project_name}}/.github/workflows/test_on_push.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
name: Project Tests
santacodes marked this conversation as resolved.
Show resolved Hide resolved

on:
workflow_dispatch:
pull_request:
push:
branches:
- main

jobs:
style:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: 3.12

- name: Check style
run: |
python -m pip install pre-commit
pre-commit run -a

test_project:
needs: style
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-13, macos-14, windows-latest]
python-version: ["3.9", "3.10", "3.11", "3.12"]
name:
Template Generation/Project Tests (${{ matrix.os }} / Python ${{ matrix.python-version }})
steps:
- name: Checkout pybamm-cookiecutter
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Set up Python ${{ matrix.python-version }}
id: setup-python
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}

- name: Set up uv
uses: yezz123/setup-uv@v4
with:
uv-venv: ".venv"

- name: Install nox
run: uv pip install nox[uv]

- name: Test project units
run: nox -s generated-project-tests

- name: Test user tests
run: nox -s user-tests

- name: Run coverage tests
if: matrix.os == 'ubuntu-latest' && matrix.python-version == '3.12'
run: nox -s coverage

- name: Upload coverage report
if: matrix.os == 'ubuntu-latest' && matrix.python-version == '3.12'
uses: codecov/[email protected]
with:
token: ${{ secrets.CODECOV_TOKEN }}

run_doctests:
needs: style
runs-on: ubuntu-latest
name: Doctests (ubuntu-latest / Python 3.12)

steps:
- name: Check out pybamm-cookiecutter repository
uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: 3.12
cache: 'pip'

- name: Install nox
run: python -m pip install nox

- name: Check if the documentation can be built
run: nox -s docs
68 changes: 57 additions & 11 deletions {{cookiecutter.project_name}}/noxfile.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,66 @@
import nox
from pathlib import Path
import os

# Options to modify nox behaviour
nox.options.default_venv_backend = "uv|virtualenv"
nox.options.reuse_existing_virtualenvs = True

VENV_DIR = Path("./venv").resolve()

@nox.session(name="docs")
def build_docs(session):
def build_docs(session: nox.Session) -> None:
"""Build the documentation and load it in a browser tab, rebuilding on changes."""
envbindir = session.bin
session.install("-e", ".[docs]")
with session.chdir("docs/"):
session.run(
"sphinx-autobuild",
"-j",
"auto",
"--open-browser",
"-qT",
".",
f"{envbindir}/../tmp/html",
)
# For local development
if session.interactive:
session.run(
"sphinx-autobuild",
"-j",
agriyakhetarpal marked this conversation as resolved.
Show resolved Hide resolved
"auto",
"--open-browser",
"-qT",
".",
"build/html/",
)
# For CI testing if documentation builds
else:
session.run(
"sphinx-build",
"-b",
"html",
"-W",
"--keep-going",
".",
"build/html/",
)

@nox.session(name="generated-project-tests")
santacodes marked this conversation as resolved.
Show resolved Hide resolved
def run_generated_project_tests(session):
"""Run the tests for testing units inside generated project"""
session.install("setuptools", silent=False)
session.install("-e", ".[dev]", silent=False)
session.run("pytest", "tests/generated_project_tests")
santacodes marked this conversation as resolved.
Show resolved Hide resolved

@nox.session(name="user-tests")
santacodes marked this conversation as resolved.
Show resolved Hide resolved
def run_user_tests(session):
"""Run the tests for testing user written tests"""
santacodes marked this conversation as resolved.
Show resolved Hide resolved
session.install("setuptools", silent=False)
session.install("-e", ".[dev]", silent=False)
session.run("pytest", "tests/user_tests")

@nox.session(name="coverage")
def run_coverage(session):
santacodes marked this conversation as resolved.
Show resolved Hide resolved
"""Run the coverage tests and generate an XML report."""
session.posargs.append("--cov=src/pybamm_cookiecutter")
session.posargs.append("--cov-report=xml")
run_user_tests(session)

@nox.session(name="dev")
santacodes marked this conversation as resolved.
Show resolved Hide resolved
def set_dev(session):
"""Install pybamm-cookiecutter in editable mode"""
session.install("virtualenv")
session.run("virtualenv", os.fsdecode(VENV_DIR), silent=True)
python = os.fsdecode(VENV_DIR.joinpath("bin/python"))
session.run(python, "-m", "pip", "install", "-e", ".[dev]")
11 changes: 10 additions & 1 deletion {{cookiecutter.project_name}}/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
{%- if cookiecutter.backend == "hatch" %}
requires = [
"hatchling",
"hatch-vcs"
{%- if cookiecutter.hatch_vcs == true %}
"hatch-vcs",
{%- endif %}
]
build-backend = "hatchling.build"
{%- endif %}
Expand Down Expand Up @@ -86,12 +88,19 @@ SPM = "{{ cookiecutter.__project_slug }}.models.input.SPM:SPM"
{# keep this line here for newline #}
{%- if cookiecutter.backend == "hatch" %}
[tool.hatch]
{%- if cookiecutter.hatch_vcs == true %}
version.source = "vcs"
build.hooks.vcs.version-file = "src/{{ cookiecutter.__project_slug }}/_version.py"
{%- endif %}
envs.default.dependencies = [
"pybamm",
]
{%- endif %}

{%- if cookiecutter.hatch_vcs == false %}
[tool.hatch.version]
path = "src/{{ cookiecutter.__project_slug }}/__init__.py"
{%- endif %}
{# keep this line here for newline #}
{%- if cookiecutter.mypy %}
[tool.mypy]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
from __future__ import annotations
{%- endif %}

{%- if cookiecutter.hatch_vcs %}
from ._version import version as __version__
{%- else %}
__version__ = "0.1.0"
{%- endif %}
import pybamm
from .entry_point import Model, parameter_sets, models
{# keep this line here for newline #}
Expand All @@ -16,7 +20,9 @@
{%- else %}
__all__ = [
{%- endif %}
{%- if cookiecutter.hatch_vcs %}
"__version__",
{%- endif %}
"pybamm",
"parameter_sets",
"Model",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import pytest
santacodes marked this conversation as resolved.
Show resolved Hide resolved
import {{ cookiecutter.__project_slug }}
import importlib.util
import sys
from pathlib import Path


def test_parameter_sets_entry_points():
"""Test if the parameter_sets via entry points are loaded correctly."""

entry_points = list({{ cookiecutter.__project_slug }}.parameter_sets)
parameter_sets = Path("src/{{ cookiecutter.__project_slug }}/parameters/input/").glob("*.py")
# Making a list of parameter sets in the parameters/input directory
parameter_sets = [x.stem for x in parameter_sets]

assert parameter_sets == entry_points, "Entry points missing either in pyproject.toml or in the input directory"


def test_parameter_sets_entry_point_load():
"""Testing if the values get loaded via parameter entry points and are equal when loaded through entry points"""
# Loading parameter_sets through entry points
parameters = {{ cookiecutter.__project_slug }}.parameter_sets['Chen2020']
# Loading parameter sets through the source file by dynamically loading Chen2020.py as a module
spec = importlib.util.spec_from_file_location("Chen2020mod", "src/{{ cookiecutter.__project_slug }}/parameters/input/Chen2020.py")
chen_module = importlib.util.module_from_spec(spec)
sys.modules["Chen2020mod"] = chen_module
spec.loader.exec_module(chen_module)
parameters_from_file = chen_module.get_parameter_values()
assert parameters.keys() == parameters_from_file.keys(), f"The keys in the module and local input file are not the same, expected {parameters.keys} got {parameters_from_file.keys()}"
santacodes marked this conversation as resolved.
Show resolved Hide resolved

def test_model_entry_points():
"""Test if the models via entry points are loaded correctly."""

entry_points = list({{ cookiecutter.__project_slug }}.models)
models = Path("src/{{ cookiecutter.__project_slug }}/models/input/").glob("*.py")
# Making a list Parameter sets in the parameters/input directory
models = [x.stem for x in models]

assert models == entry_points, "Entry points missing either in pyproject.toml or in the input directory"

def test_model_entry_point_load():
santacodes marked this conversation as resolved.
Show resolved Hide resolved
"""Testing if the model gets initialised and returned."""
# Loading parameter_sets through entry points
model_instance = {{ cookiecutter.__project_slug }}.Model("SPM")
assert model_instance is not None
Loading