Skip to content

Commit 02fc73f

Browse files
EZoniax3l
andauthored
Add test to validate examples in the main PALS repository (#55)
Co-authored-by: Axel Huebl <axel.huebl@plasma.ninja>
1 parent 4eae795 commit 02fc73f

5 files changed

Lines changed: 143 additions & 7 deletions

File tree

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ on:
77
pull_request:
88

99
concurrency:
10-
group: ${{ github.ref }}-${{ github.head_ref }}-pals-python
10+
group: ${{ github.ref }}-${{ github.head_ref }}-examples
1111
cancel-in-progress: true
1212

1313
permissions:
1414
contents: read # access to check out code and install dependencies
1515

1616
jobs:
17-
tests:
18-
name: tests
17+
examples:
18+
name: examples
1919
runs-on: ubuntu-latest
2020
strategy:
2121
matrix:
@@ -30,9 +30,6 @@ jobs:
3030
run: |
3131
python -m pip install --upgrade pip
3232
pip install ".[test]"
33-
- name: Test
34-
run: |
35-
pytest tests -v
36-
- name: Examples
33+
- name: Run examples
3734
run: |
3835
python examples/fodo.py
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
name: pals
2+
3+
on:
4+
push:
5+
branches:
6+
- "main"
7+
pull_request:
8+
9+
concurrency:
10+
group: ${{ github.ref }}-${{ github.head_ref }}-standard-examples
11+
cancel-in-progress: true
12+
13+
permissions:
14+
contents: read # access to check out code and install dependencies
15+
16+
jobs:
17+
standard-examples:
18+
name: standard examples
19+
runs-on: ubuntu-latest
20+
strategy:
21+
matrix:
22+
python-version: ["3.11", "3.12", "3.13", "3.14"]
23+
steps:
24+
- uses: actions/checkout@v6
25+
- name: Checkout PALS repository
26+
uses: actions/checkout@v6
27+
with:
28+
repository: pals-project/pals
29+
path: pals_temp
30+
fetch-depth: 1
31+
sparse-checkout: |
32+
examples/
33+
- name: Set up Python
34+
uses: actions/setup-python@v6
35+
with:
36+
python-version: ${{ matrix.python-version }}
37+
- name: Install
38+
run: |
39+
python -m pip install --upgrade pip
40+
pip install ".[test]"
41+
- name: Run standard examples
42+
run: |
43+
for file in pals_temp/examples/*.pals.yaml; do
44+
python tests/validate_standard_examples.py --path "${file}"
45+
done

.github/workflows/unit_tests.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: pals
2+
3+
on:
4+
push:
5+
branches:
6+
- "main"
7+
pull_request:
8+
9+
concurrency:
10+
group: ${{ github.ref }}-${{ github.head_ref }}-unit-tests
11+
cancel-in-progress: true
12+
13+
permissions:
14+
contents: read # access to check out code and install dependencies
15+
16+
jobs:
17+
unit-tests:
18+
name: unit tests
19+
runs-on: ubuntu-latest
20+
strategy:
21+
matrix:
22+
python-version: ["3.11", "3.12", "3.13", "3.14"]
23+
steps:
24+
- uses: actions/checkout@v6
25+
- name: Set up Python
26+
uses: actions/setup-python@v6
27+
with:
28+
python-version: ${{ matrix.python-version }}
29+
- name: Install
30+
run: |
31+
python -m pip install --upgrade pip
32+
pip install ".[test]"
33+
- name: Run unit tests
34+
run: |
35+
pytest tests -v

src/pals/kinds/mixin/all_element_mixin.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,17 @@ def unpack_element_list_structure(
6161
f"but we got {item!r}"
6262
)
6363
name, fields = list(item.items())[0]
64+
# In addition to the existing shorthand `- element_name`
65+
# (a plain string reference), also allow the alternative
66+
# reference syntax `- use: element_name`.
67+
# If the value is not a dict but the key is 'use',
68+
# treat it as a reference to an existing element name
69+
# and wrap it in a PlaceholderName so downstream code
70+
# can resolve it.
6471
if not isinstance(fields, dict):
72+
if name == "use" and isinstance(fields, str):
73+
new_list.append(PlaceholderName(fields))
74+
continue
6575
raise TypeError(
6676
f"Value for element key {name!r} must be a dict (the element's properties), "
6777
f"but we got {fields!r}"
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
"""Utility script to validate standard PALS example files.
2+
3+
This script is not run by pytest and is intended to be used as a standalone script.
4+
Run it from the repository root like:
5+
6+
python tests/validate_standard_examples.py --path /path/to/example.pals.yaml
7+
8+
Before running, download the desired standard PALS example files from pals-project/pals/examples.
9+
"""
10+
11+
import argparse
12+
13+
from pals import load
14+
from pals.kinds import PlaceholderName
15+
from pals.kinds.BeamLine import BeamLine
16+
from pals.kinds.Drift import Drift
17+
from pals.kinds.Lattice import Lattice
18+
from pals.kinds.Quadrupole import Quadrupole
19+
20+
21+
def main():
22+
# Parse command-line arguments
23+
parser = argparse.ArgumentParser()
24+
parser.add_argument(
25+
"--path",
26+
required=True,
27+
help="Path to the example file",
28+
)
29+
args = parser.parse_args()
30+
example_file = args.path
31+
# Parse and validate YAML data from file
32+
lattice = load(example_file)
33+
# The following assertions are based on the standard PALS example file
34+
# fodo.pals.yaml from pals-project/pals/examples
35+
assert isinstance(lattice.facility[0], Drift)
36+
assert lattice.facility[0].name == "drift1"
37+
assert isinstance(lattice.facility[1], Quadrupole)
38+
assert lattice.facility[1].name == "quad1"
39+
assert isinstance(lattice.facility[2], BeamLine)
40+
assert lattice.facility[2].name == "fodo_cell"
41+
assert isinstance(lattice.facility[3], BeamLine)
42+
assert lattice.facility[3].name == "fodo_channel"
43+
assert isinstance(lattice.facility[4], Lattice)
44+
assert lattice.facility[4].name == "fodo_lattice"
45+
assert isinstance(lattice.facility[5], PlaceholderName)
46+
47+
48+
if __name__ == "__main__":
49+
main()

0 commit comments

Comments
 (0)