Skip to content

Commit 5b727e5

Browse files
authored
Clean & Split Tests (#36)
1 parent 7b575b9 commit 5b727e5

File tree

6 files changed

+75
-88
lines changed

6 files changed

+75
-88
lines changed

.github/workflows/unit_tests.yml

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,36 +16,20 @@ jobs:
1616
runs-on: ubuntu-latest
1717
strategy:
1818
matrix:
19-
python-version: ["3.9", "3.10", "3.11", "3.12", "3.13"]
19+
python-version: ["3.10", "3.11", "3.12", "3.13", "3.14"]
2020
steps:
2121
- uses: actions/checkout@v4
2222
- name: Set up Python
2323
uses: actions/setup-python@v5
2424
with:
2525
python-version: ${{ matrix.python-version }}
26-
- name: Install dependencies
26+
- name: Install
2727
run: |
2828
python -m pip install --upgrade pip
29-
pip install -r requirements.txt
29+
pip install ".[test]"
3030
- name: Test
3131
run: |
3232
pytest tests -v
33-
examples:
34-
name: examples
35-
runs-on: ubuntu-latest
36-
strategy:
37-
matrix:
38-
python-version: ["3.9", "3.10", "3.11", "3.12", "3.13"]
39-
steps:
40-
- uses: actions/checkout@v4
41-
- name: Set up Python
42-
uses: actions/setup-python@v5
43-
with:
44-
python-version: ${{ matrix.python-version }}
45-
- name: Install dependencies
46-
run: |
47-
python -m pip install --upgrade pip
48-
pip install -r requirements.txt
49-
- name: Test
33+
- name: Examples
5034
run: |
5135
python examples/fodo.py

README.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,18 @@ In order to develop and test this Python implementation locally, please follow t
6565
```bash
6666
conda activate pals-python
6767
```
68-
Please double check the environment name in the `environment.yml` file.
68+
(This is the environment name in the `environment.yml` file.)
69+
3. Install Python PALS in development (editable) mode:
70+
```bash
71+
pip install -e ".[test]"
72+
```
6973

7074
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.
7175

72-
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``.
76+
You can also run the unit tests available in the [tests](https://github.com/campa-consortium/pals-python/tree/main/tests) directory via
77+
```bash
78+
pytest tests -v
79+
```
7380
Here, the command line option `-v` increases the verbosity of the output.
7481
You can also use the command line option `-s` to display any test output directly in the console (useful for debugging).
7582
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`.

examples/fodo.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
import json
2-
import os
3-
import sys
42
import yaml
53

6-
# Add the parent directory to sys.path
7-
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "../src/")))
8-
94
from pals import MagneticMultipoleParameters
105
from pals import Drift
116
from pals import Quadrupole

pyproject.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ dependencies = [
1010
"pyyaml",
1111
"toml",
1212
]
13-
requires-python = ">=3.9"
13+
requires-python = ">=3.10"
1414
authors = [
1515
{ name="Axel Huebl", email="[email protected]" },
1616
{ name="Edoardo Zoni", email="[email protected]" },
@@ -30,11 +30,11 @@ classifiers = [
3030
"Intended Audience :: Science/Research",
3131
"Natural Language :: English",
3232
"Programming Language :: Python :: 3",
33-
"Programming Language :: Python :: 3.9",
3433
"Programming Language :: Python :: 3.10",
3534
"Programming Language :: Python :: 3.11",
3635
"Programming Language :: Python :: 3.12",
3736
"Programming Language :: Python :: 3.13",
37+
"Programming Language :: Python :: 3.14",
3838
"Operating System :: OS Independent",
3939
"Topic :: Scientific/Engineering",
4040
"Topic :: Software Development",
@@ -49,4 +49,4 @@ Repository = "https://github.com/campa-consortium/pals-python"
4949
Issues = "https://github.com/campa-consortium/pals-python/issues"
5050

5151
[tool.setuptools.packages.find]
52-
where = ["src"]
52+
where = ["src"]

tests/test_schema.py renamed to tests/test_elements.py

Lines changed: 2 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
import json
2-
import os
3-
import sys
4-
import yaml
5-
6-
# Add the parent directory to sys.path
7-
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "../src/")))
8-
91
from pydantic import ValidationError
102

113
from pals import MagneticMultipoleParameters
@@ -68,6 +60,8 @@ def test_Drift():
6860

6961

7062
def test_Quadrupole():
63+
import yaml
64+
7165
# Create one drift element with custom name and length
7266
element_name = "quadrupole_element"
7367
element_length = 1.0
@@ -118,53 +112,3 @@ def test_BeamLine():
118112
# Extend first line with second line
119113
line1.line.extend(line2.line)
120114
assert line1.line == [element1, element2, element3]
121-
122-
123-
def test_yaml():
124-
# Create one base element
125-
element1 = BaseElement(name="element1")
126-
# Create one thick element
127-
element2 = ThickElement(name="element2", length=2.0)
128-
# Create line with both elements
129-
line = BeamLine(name="line", line=[element1, element2])
130-
# Serialize the BeamLine object to YAML
131-
yaml_data = yaml.dump(line.model_dump(), default_flow_style=False)
132-
print(f"\n{yaml_data}")
133-
# Write the YAML data to a test file
134-
test_file = "line.yaml"
135-
with open(test_file, "w") as file:
136-
file.write(yaml_data)
137-
# Read the YAML data from the test file
138-
with open(test_file, "r") as file:
139-
yaml_data = yaml.safe_load(file)
140-
# Parse the YAML data back into a BeamLine object
141-
loaded_line = BeamLine(**yaml_data)
142-
# Remove the test file
143-
os.remove(test_file)
144-
# Validate loaded BeamLine object
145-
assert line == loaded_line
146-
147-
148-
def test_json():
149-
# Create one base element
150-
element1 = BaseElement(name="element1")
151-
# Create one thick element
152-
element2 = ThickElement(name="element2", length=2.0)
153-
# Create line with both elements
154-
line = BeamLine(name="line", line=[element1, element2])
155-
# Serialize the BeamLine object to JSON
156-
json_data = json.dumps(line.model_dump(), sort_keys=True, indent=2)
157-
print(f"\n{json_data}")
158-
# Write the JSON data to a test file
159-
test_file = "line.json"
160-
with open(test_file, "w") as file:
161-
file.write(json_data)
162-
# Read the JSON data from the test file
163-
with open(test_file, "r") as file:
164-
json_data = json.loads(file.read())
165-
# Parse the JSON data back into a BeamLine object
166-
loaded_line = BeamLine(**json_data)
167-
# Remove the test file
168-
os.remove(test_file)
169-
# Validate loaded BeamLine object
170-
assert line == loaded_line

tests/test_serialization.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import json
2+
import os
3+
import yaml
4+
5+
from pals import BaseElement
6+
from pals import ThickElement
7+
from pals import BeamLine
8+
9+
10+
def test_yaml():
11+
# Create one base element
12+
element1 = BaseElement(name="element1")
13+
# Create one thick element
14+
element2 = ThickElement(name="element2", length=2.0)
15+
# Create line with both elements
16+
line = BeamLine(name="line", line=[element1, element2])
17+
# Serialize the BeamLine object to YAML
18+
yaml_data = yaml.dump(line.model_dump(), default_flow_style=False)
19+
print(f"\n{yaml_data}")
20+
# Write the YAML data to a test file
21+
test_file = "line.yaml"
22+
with open(test_file, "w") as file:
23+
file.write(yaml_data)
24+
# Read the YAML data from the test file
25+
with open(test_file, "r") as file:
26+
yaml_data = yaml.safe_load(file)
27+
# Parse the YAML data back into a BeamLine object
28+
loaded_line = BeamLine(**yaml_data)
29+
# Remove the test file
30+
os.remove(test_file)
31+
# Validate loaded BeamLine object
32+
assert line == loaded_line
33+
34+
35+
def test_json():
36+
# Create one base element
37+
element1 = BaseElement(name="element1")
38+
# Create one thick element
39+
element2 = ThickElement(name="element2", length=2.0)
40+
# Create line with both elements
41+
line = BeamLine(name="line", line=[element1, element2])
42+
# Serialize the BeamLine object to JSON
43+
json_data = json.dumps(line.model_dump(), sort_keys=True, indent=2)
44+
print(f"\n{json_data}")
45+
# Write the JSON data to a test file
46+
test_file = "line.json"
47+
with open(test_file, "w") as file:
48+
file.write(json_data)
49+
# Read the JSON data from the test file
50+
with open(test_file, "r") as file:
51+
json_data = json.loads(file.read())
52+
# Parse the JSON data back into a BeamLine object
53+
loaded_line = BeamLine(**json_data)
54+
# Remove the test file
55+
os.remove(test_file)
56+
# Validate loaded BeamLine object
57+
assert line == loaded_line

0 commit comments

Comments
 (0)