From b7f518f8d78e103c8a5c3535d672b6cc5bda0cc1 Mon Sep 17 00:00:00 2001 From: Axel Huebl Date: Wed, 22 Oct 2025 14:13:27 -0700 Subject: [PATCH 1/7] Clean & Split Tests Preparing for adding new tests for more elements, parameters, serializations. --- tests/{test_schema.py => test_elements.py} | 60 +--------------------- tests/test_serialization.py | 57 ++++++++++++++++++++ 2 files changed, 59 insertions(+), 58 deletions(-) rename tests/{test_schema.py => test_elements.py} (66%) create mode 100644 tests/test_serialization.py diff --git a/tests/test_schema.py b/tests/test_elements.py similarity index 66% rename from tests/test_schema.py rename to tests/test_elements.py index 74e271b..940bf1c 100644 --- a/tests/test_schema.py +++ b/tests/test_elements.py @@ -1,11 +1,3 @@ -import json -import os -import sys -import yaml - -# Add the parent directory to sys.path -sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "../src/"))) - from pydantic import ValidationError from pals import MagneticMultipoleParameters @@ -68,6 +60,8 @@ def test_Drift(): def test_Quadrupole(): + import yaml + # Create one drift element with custom name and length element_name = "quadrupole_element" element_length = 1.0 @@ -118,53 +112,3 @@ def test_BeamLine(): # Extend first line with second line line1.line.extend(line2.line) assert line1.line == [element1, element2, element3] - - -def test_yaml(): - # Create one base element - element1 = BaseElement(name="element1") - # Create one thick element - element2 = ThickElement(name="element2", length=2.0) - # Create line with both elements - line = BeamLine(name="line", line=[element1, element2]) - # Serialize the BeamLine object to YAML - yaml_data = yaml.dump(line.model_dump(), default_flow_style=False) - print(f"\n{yaml_data}") - # Write the YAML data to a test file - test_file = "line.yaml" - with open(test_file, "w") as file: - file.write(yaml_data) - # Read the YAML data from the test file - with open(test_file, "r") as file: - yaml_data = yaml.safe_load(file) - # Parse the YAML data back into a BeamLine object - loaded_line = BeamLine(**yaml_data) - # Remove the test file - os.remove(test_file) - # Validate loaded BeamLine object - assert line == loaded_line - - -def test_json(): - # Create one base element - element1 = BaseElement(name="element1") - # Create one thick element - element2 = ThickElement(name="element2", length=2.0) - # Create line with both elements - line = BeamLine(name="line", line=[element1, element2]) - # Serialize the BeamLine object to JSON - json_data = json.dumps(line.model_dump(), sort_keys=True, indent=2) - print(f"\n{json_data}") - # Write the JSON data to a test file - test_file = "line.json" - with open(test_file, "w") as file: - file.write(json_data) - # Read the JSON data from the test file - with open(test_file, "r") as file: - json_data = json.loads(file.read()) - # Parse the JSON data back into a BeamLine object - loaded_line = BeamLine(**json_data) - # Remove the test file - os.remove(test_file) - # Validate loaded BeamLine object - assert line == loaded_line diff --git a/tests/test_serialization.py b/tests/test_serialization.py new file mode 100644 index 0000000..eb718cd --- /dev/null +++ b/tests/test_serialization.py @@ -0,0 +1,57 @@ +import json +import os +import yaml + +from pals import BaseElement +from pals import ThickElement +from pals import BeamLine + + +def test_yaml(): + # Create one base element + element1 = BaseElement(name="element1") + # Create one thick element + element2 = ThickElement(name="element2", length=2.0) + # Create line with both elements + line = BeamLine(name="line", line=[element1, element2]) + # Serialize the BeamLine object to YAML + yaml_data = yaml.dump(line.model_dump(), default_flow_style=False) + print(f"\n{yaml_data}") + # Write the YAML data to a test file + test_file = "line.yaml" + with open(test_file, "w") as file: + file.write(yaml_data) + # Read the YAML data from the test file + with open(test_file, "r") as file: + yaml_data = yaml.safe_load(file) + # Parse the YAML data back into a BeamLine object + loaded_line = BeamLine(**yaml_data) + # Remove the test file + os.remove(test_file) + # Validate loaded BeamLine object + assert line == loaded_line + + +def test_json(): + # Create one base element + element1 = BaseElement(name="element1") + # Create one thick element + element2 = ThickElement(name="element2", length=2.0) + # Create line with both elements + line = BeamLine(name="line", line=[element1, element2]) + # Serialize the BeamLine object to JSON + json_data = json.dumps(line.model_dump(), sort_keys=True, indent=2) + print(f"\n{json_data}") + # Write the JSON data to a test file + test_file = "line.json" + with open(test_file, "w") as file: + file.write(json_data) + # Read the JSON data from the test file + with open(test_file, "r") as file: + json_data = json.loads(file.read()) + # Parse the JSON data back into a BeamLine object + loaded_line = BeamLine(**json_data) + # Remove the test file + os.remove(test_file) + # Validate loaded BeamLine object + assert line == loaded_line From 3dda253d0ff2caeb32d36d46c119d84a50d6e293 Mon Sep 17 00:00:00 2001 From: Axel Huebl Date: Wed, 22 Oct 2025 14:16:28 -0700 Subject: [PATCH 2/7] Clean Up Example --- examples/fodo.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/examples/fodo.py b/examples/fodo.py index 5757603..a90cbe3 100644 --- a/examples/fodo.py +++ b/examples/fodo.py @@ -1,11 +1,6 @@ import json -import os -import sys import yaml -# Add the parent directory to sys.path -sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "../src/"))) - from pals import MagneticMultipoleParameters from pals import Drift from pals import Quadrupole From 9cbaa45718ac6a25dc0f3925a8a18e75baa9cb93 Mon Sep 17 00:00:00 2001 From: Axel Huebl Date: Wed, 22 Oct 2025 14:17:52 -0700 Subject: [PATCH 3/7] CI: Update --- .github/workflows/unit_tests.yml | 26 +++++--------------------- 1 file changed, 5 insertions(+), 21 deletions(-) diff --git a/.github/workflows/unit_tests.yml b/.github/workflows/unit_tests.yml index 839c93f..2b680cd 100644 --- a/.github/workflows/unit_tests.yml +++ b/.github/workflows/unit_tests.yml @@ -16,36 +16,20 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - python-version: ["3.9", "3.10", "3.11", "3.12", "3.13"] + python-version: ["3.10", "3.11", "3.12", "3.13", "3.14"] steps: - uses: actions/checkout@v4 - name: Set up Python uses: actions/setup-python@v5 with: python-version: ${{ matrix.python-version }} - - name: Install dependencies + - name: Install run: | - python -m pip install --upgrade pip - pip install -r requirements.txt + python -m pip install --upgrade pip pytest + pip install . - name: Test run: | pytest tests -v - examples: - name: examples - runs-on: ubuntu-latest - strategy: - matrix: - python-version: ["3.9", "3.10", "3.11", "3.12", "3.13"] - steps: - - uses: actions/checkout@v4 - - name: Set up Python - uses: actions/setup-python@v5 - with: - python-version: ${{ matrix.python-version }} - - name: Install dependencies - run: | - python -m pip install --upgrade pip - pip install -r requirements.txt - - name: Test + - name: Examples run: | python examples/fodo.py From 5336228ca272b00fc9865e65d4f5b882439003da Mon Sep 17 00:00:00 2001 From: Axel Huebl Date: Wed, 22 Oct 2025 14:19:58 -0700 Subject: [PATCH 4/7] Update README --- README.md | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index f9ccd1f..81dc289 100644 --- a/README.md +++ b/README.md @@ -65,11 +65,18 @@ In order to develop and test this Python implementation locally, please follow t ```bash conda activate pals-python ``` - Please double check the environment name in the `environment.yml` file. + (This is the environment name in the `environment.yml` file.) +3. Install Python PALS in development (editable) mode: + ```bash + pip install -e . + ``` Once you have created the environment with all the required dependencies, you can run the examples available in the [examples](https://github.com/campa-consortium/pals-python/tree/main/examples) directory. -You can also run the unit tests available in the [tests](https://github.com/campa-consortium/pals-python/tree/main/tests) directory via ``pytest tests -v``. +You can also run the unit tests available in the [tests](https://github.com/campa-consortium/pals-python/tree/main/tests) directory via +```bash +pytest tests -v +``` Here, the command line option `-v` increases the verbosity of the output. You can also use the command line option `-s` to display any test output directly in the console (useful for debugging). Please refer to [pytest's documentation](https://docs.pytest.org/en/stable/) for further details on the available command line options and/or run `pytest --help`. From 2a7ac7947834980d55be4d72457a7f5a65aa5e07 Mon Sep 17 00:00:00 2001 From: Axel Huebl Date: Thu, 23 Oct 2025 10:56:17 -0700 Subject: [PATCH 5/7] CI w/ Test Component --- .github/workflows/unit_tests.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/unit_tests.yml b/.github/workflows/unit_tests.yml index 2b680cd..687bd1c 100644 --- a/.github/workflows/unit_tests.yml +++ b/.github/workflows/unit_tests.yml @@ -25,8 +25,8 @@ jobs: python-version: ${{ matrix.python-version }} - name: Install run: | - python -m pip install --upgrade pip pytest - pip install . + python -m pip install --upgrade pip + pip install ".[test]" - name: Test run: | pytest tests -v From 55c8af6115375979aa52bdbfbb33c92f64c468ba Mon Sep 17 00:00:00 2001 From: Axel Huebl Date: Thu, 23 Oct 2025 10:57:04 -0700 Subject: [PATCH 6/7] Drop Python 3.9, Add 3.14 --- pyproject.toml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 62041ca..1c15359 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -10,7 +10,7 @@ dependencies = [ "pyyaml", "toml", ] -requires-python = ">=3.9" +requires-python = ">=3.10" authors = [ { name="Axel Huebl", email="axelhuebl@lbl.gov" }, { name="Edoardo Zoni", email="ezoni@lbl.gov" }, @@ -30,11 +30,11 @@ classifiers = [ "Intended Audience :: Science/Research", "Natural Language :: English", "Programming Language :: Python :: 3", - "Programming Language :: Python :: 3.9", "Programming Language :: Python :: 3.10", "Programming Language :: Python :: 3.11", "Programming Language :: Python :: 3.12", "Programming Language :: Python :: 3.13", + "Programming Language :: Python :: 3.14", "Operating System :: OS Independent", "Topic :: Scientific/Engineering", "Topic :: Software Development", @@ -49,4 +49,4 @@ Repository = "https://github.com/campa-consortium/pals-python" Issues = "https://github.com/campa-consortium/pals-python/issues" [tool.setuptools.packages.find] -where = ["src"] \ No newline at end of file +where = ["src"] From 27b276b91f55c5d0ce8823d83eee7c63f24f8017 Mon Sep 17 00:00:00 2001 From: Axel Huebl Date: Thu, 23 Oct 2025 10:58:42 -0700 Subject: [PATCH 7/7] README: Include Test Dependencies --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 81dc289..247faba 100644 --- a/README.md +++ b/README.md @@ -68,7 +68,7 @@ In order to develop and test this Python implementation locally, please follow t (This is the environment name in the `environment.yml` file.) 3. Install Python PALS in development (editable) mode: ```bash - pip install -e . + pip install -e ".[test]" ``` Once you have created the environment with all the required dependencies, you can run the examples available in the [examples](https://github.com/campa-consortium/pals-python/tree/main/examples) directory.