Skip to content

Commit 5bbd89a

Browse files
committed
Fixed tests, all now running
Repopulated conftest.py with the pre-copier test setup for ATIP. Fixed error in test_toggle_calculations_and_wait_for_calculations that I made. Deleted ghost atip/ dir which shouldnt have been there and was stopping ATIP from running. Added testfixtures dependency to pyproject
1 parent 790dca5 commit 5bbd89a

File tree

4 files changed

+178
-7
lines changed

4 files changed

+178
-7
lines changed

atip/__init__.py

Lines changed: 0 additions & 7 deletions
This file was deleted.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ dev = [
2626
"pre-commit",
2727
"pytest",
2828
"pytest-cov",
29+
"testfixtures",
2930
"ruff",
3031
"tox-direct",
3132
"types-mock",

tests/conftest.py

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,179 @@ def pytest_exception_interact(call: pytest.CallInfo[Any]):
1919
@pytest.hookimpl(tryfirst=True)
2020
def pytest_internalerror(excinfo: pytest.ExceptionInfo[Any]):
2121
raise excinfo.value
22+
23+
24+
import os
25+
import unittest.mock as mock
26+
from typing import Any
27+
28+
import at
29+
import numpy
30+
import pytest
31+
from pytac import cs, load_csv
32+
33+
import atip
34+
35+
# Prevent pytest from catching exceptions when debugging in vscode so that break on
36+
# exception works correctly (see: https://github.com/pytest-dev/pytest/issues/7409)
37+
if os.getenv("PYTEST_RAISE", "0") == "1":
38+
39+
@pytest.hookimpl(tryfirst=True)
40+
def pytest_exception_interact(call: pytest.CallInfo[Any]):
41+
if call.excinfo is not None:
42+
raise call.excinfo.value
43+
else:
44+
raise RuntimeError(
45+
f"{call} has no exception data, an unknown error has occurred"
46+
)
47+
48+
@pytest.hookimpl(tryfirst=True)
49+
def pytest_internalerror(excinfo: pytest.ExceptionInfo[Any]):
50+
raise excinfo.value
51+
52+
53+
@pytest.fixture(scope="session")
54+
def at_elem():
55+
e = at.elements.Drift(
56+
"D1",
57+
0.0,
58+
KickAngle=[0, 0],
59+
Frequency=0,
60+
k=0.0,
61+
PolynomA=[0, 0, 0, 0],
62+
PolynomB=[0, 0, 0, 0],
63+
BendingAngle=0.0,
64+
)
65+
return e
66+
67+
68+
@pytest.fixture(scope="session")
69+
def at_elem_preset():
70+
e = at.elements.Drift(
71+
"D1",
72+
0.5,
73+
KickAngle=[0.1, 0.01],
74+
k=-0.07,
75+
Frequency=500,
76+
PolynomA=[1.3, 13, 22, 90],
77+
PolynomB=[8, -0.07, 42, 1],
78+
BendingAngle=0.13,
79+
)
80+
return e
81+
82+
83+
@pytest.fixture(scope="session")
84+
def atlds():
85+
return atip.sim_data_sources.ATLatticeDataSource(mock.Mock())
86+
87+
88+
@pytest.fixture()
89+
def at_lattice():
90+
return atip.utils.load_at_lattice("HMBA")
91+
92+
93+
@pytest.fixture(scope="session")
94+
def pytac_lattice():
95+
return load_csv.load("DIAD", cs.ControlSystem())
96+
97+
98+
@pytest.fixture(scope="session")
99+
def mat_filepath():
100+
here = os.path.dirname(__file__)
101+
return os.path.realpath(os.path.join(here, "../src/atip/rings/DIAD.mat"))
102+
103+
104+
@pytest.fixture(scope="session")
105+
def at_diad_lattice(mat_filepath):
106+
return at.load.load_mat(mat_filepath)
107+
108+
109+
@pytest.fixture()
110+
def atsim(at_lattice):
111+
return atip.simulator.ATSimulator(at_lattice)
112+
113+
114+
@pytest.fixture()
115+
def mocked_atsim(at_lattice):
116+
length = len(at_lattice) + 1
117+
base = numpy.ones((length, 4))
118+
atsim = atip.simulator.ATSimulator(at_lattice)
119+
atsim._at_lat = mock.PropertyMock(energy=5, circumference=(length * 0.1))
120+
emitdata = [{"emitXY": numpy.array([1.4, 0.45])}]
121+
twiss = {
122+
"closed_orbit": (base * numpy.array([0.6, 57, 0.2, 9])),
123+
"dispersion": (base * numpy.array([8.8, 1.7, 23, 3.5])),
124+
"s_pos": numpy.array([0.1 * (i + 1) for i in range(length)]),
125+
"alpha": (base[:, :2] * numpy.array([-0.03, 0.03])),
126+
"beta": (base[:, :2] * numpy.array([9.6, 6])),
127+
"M": (numpy.ones((length, 6, 6)) * (numpy.eye(6) * 0.8)),
128+
"mu": (base[:, :2] * numpy.array([176, 82])),
129+
}
130+
radint = (1.0, 2.0, 3.0, 4.0, 5.0)
131+
lattice_data = atip.simulator.LatticeData(
132+
twiss, [3.14, 0.12], [2, 1], emitdata, radint
133+
)
134+
atsim._lattice_data = lattice_data
135+
return atsim
136+
137+
138+
@pytest.fixture()
139+
def ba_atsim(at_lattice):
140+
dr = at.elements.Drift("d1", 1)
141+
dr.BendingAngle = 9001
142+
lat = [at.elements.Dipole("b1", 1, 1.3), at.elements.Dipole("b2", 1, -0.8)]
143+
at_sim = atip.simulator.ATSimulator(at_lattice)
144+
at_sim._at_lat = lat
145+
return at_sim
146+
147+
148+
@pytest.fixture()
149+
def initial_phys_data(at_lattice):
150+
return {
151+
"tune": numpy.array([0.38156245, 0.85437543]),
152+
"chromaticity": numpy.array([0.17919002, 0.12242263]),
153+
"closed_orbit": numpy.zeros((6, len(at_lattice))),
154+
"dispersion": numpy.array(
155+
[1.72682010e-3, 4.04368254e-9, 5.88659608e-28, -8.95277691e-29]
156+
),
157+
"s_pos": numpy.cumsum(
158+
[0.0] + [getattr(elem, "Length", 0) for elem in at_lattice[:-1]]
159+
),
160+
"alpha": numpy.array([0.384261343, 1.00253822]),
161+
"beta": numpy.array([7.91882634, 5.30280084]),
162+
"m66": numpy.array(
163+
[
164+
[-0.47537132, 6.62427828, 0.0, 0.0, 2.55038448e-03, -5.33885495e-07],
165+
[-0.09816788, -0.73565385, 0.0, 0.0, 1.69015229e-04, -3.53808533e-08],
166+
[0.0, 0.0, -0.18476435, -3.7128728, 0.0, 0.0],
167+
[0.0, 0.0, 0.29967874, 0.60979916, 0.0, 0.0],
168+
[
169+
1.24684834e-06,
170+
2.15443495e-05,
171+
0.0,
172+
0.0,
173+
9.99980691e-01,
174+
2.09331256e-04,
175+
],
176+
[
177+
1.70098195e-04,
178+
2.99580152e-03,
179+
0.0,
180+
0.0,
181+
2.24325864e-03,
182+
9.99999530e-01,
183+
],
184+
]
185+
),
186+
"mu": numpy.array([14.59693301, 4.58153046, 6.85248778e-04]),
187+
"emitXY": numpy.array([1.32528e-10, 0.0]),
188+
"rad_int": numpy.array(
189+
[
190+
2.2435734416179783e-3,
191+
4.3264360771244244e-3,
192+
1.049245018317141e-4,
193+
-2.3049140720439194e-3,
194+
1.6505019559193616e-8,
195+
]
196+
),
197+
}

tests/test_at_simulator_object.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ def test_toggle_calculations_and_wait_for_calculations(atsim, initial_phys_data)
200200
atsim.queue_set(mock.Mock(), "f", 0)
201201
assert atsim.wait_for_calculations(2) is False
202202
_check_initial_phys_data(atsim, initial_phys_data)
203+
atsim.toggle_calculations()
203204
atsim.queue_set(mock.Mock(), "f", 0)
204205
assert atsim.wait_for_calculations() is True
205206
# Physics data has changed.

0 commit comments

Comments
 (0)