Skip to content

Commit 3ef3930

Browse files
committed
Updated tests
1 parent b84c7f0 commit 3ef3930

File tree

4 files changed

+64
-18
lines changed

4 files changed

+64
-18
lines changed

tests/test_00_version.py

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,44 @@
44
########################################### #
55

66
import pytest
7-
from xboinc import __version__
7+
import sys
8+
9+
from xboinc import __version__, __xsuite__versions__
810
from xboinc.simulation_io import SimVersion
9-
from xboinc.simulation_io.version import _version_to_int, _int_to_version
11+
from xboinc.simulation_io.version import _version_to_int, _int_to_version, assert_versions
12+
1013

1114
def test_version():
12-
assert __version__ == '0.1.3'
15+
assert __version__ == '0.1.4'
16+
1317

1418
def test_sim_ver():
1519
simver = SimVersion()
1620
simver.assert_version()
17-
print(_int_to_version(simver.xboinc_version))
18-
with pytest.raises(Exception):
21+
with pytest.raises(ImportError):
1922
simver.xboinc_version = _version_to_int('0.0.0')
2023
simver.assert_version()
24+
25+
26+
def test_xsuite_versions():
27+
expected_version = {
28+
'xobjects' : '0.2.8',
29+
'xdeps' : '0.4.2',
30+
'xpart' : '0.15.3',
31+
'xtrack' : '0.42.0',
32+
'xfields' : '0.13.1',
33+
'xcoll' : '0.2.5',
34+
}
35+
current_version = {}
36+
37+
for mod in expected_version.keys():
38+
__import__(mod)
39+
current_version[mod] = sys.modules[mod].__version__
40+
sys.modules[mod].__version__ = expected_version[mod]
41+
assert_versions()
42+
43+
for mod in expected_version.keys():
44+
sys.modules[mod].__version__ = '0.0.0'
45+
with pytest.raises(ImportError):
46+
assert_versions()
47+
sys.modules[mod].__version__ = current_version[mod]

tests/test_01_compilation_and_running.py

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222

2323
def test_compilation():
24+
xb._skip_xsuite_version_check = True
2425
source_files = xb.generate_executable_source()
2526
assert Path(Path.cwd() / "main.c").exists()
2627
assert Path(Path.cwd() / "sim_config.h").exists()
@@ -31,29 +32,35 @@ def test_compilation():
3132
assert len(exec_file) == 1
3233
assert exec_file[0].exists()
3334
assert os.access(exec_file[0], os.X_OK)
35+
xb._skip_xsuite_version_check = False
3436

3537

36-
def test_generate_input():
37-
# Simulation input
38+
def _make_input():
3839
line = xt.Line.from_json(line_file)
3940
line.build_tracker()
4041
x_norm = np.linspace(-.5, .5, 50)
4142
delta = np.linspace(-1.e-5, 1.e-5, 50)
4243
part = line.build_particles(x_norm=x_norm, delta=delta, nemitt_x=3.5e-6, nemitt_y=3.5e-6)
44+
return line, part
45+
4346

44-
# Assemble data structure
47+
def test_generate_input():
48+
xb._skip_xsuite_version_check = True
49+
line, part = _make_input()
4550
xb.SimConfig.build(line=line, particles=part, num_turns=num_turns, checkpoint_every=10,
4651
filename='xboinc_input.bin')
47-
4852
input_file = Path.cwd() / "xboinc_input.bin"
4953
assert input_file.exists()
54+
xb._skip_xsuite_version_check = False
5055

5156

5257
def test_track():
58+
xb._skip_xsuite_version_check = True
5359
# If no executable is present, make one
5460
exec_file = list(Path.cwd().glob(f'xboinc_{xb.app_version}-*'))
5561
if len(exec_file)==0 or not exec_file[0].exists():
56-
xb.generate_executable()
62+
test_compilation()
63+
exec_file = list(Path.cwd().glob(f'xboinc_{xb.app_version}-*'))
5764
exec_file = exec_file[0]
5865

5966
# If no input file is present, make one
@@ -79,15 +86,24 @@ def test_track():
7986
assert np.all(part_xboinc.at_turn == num_turns), "Unexpected survivals (particles)"
8087
assert sim_state.i_turn == num_turns, "Unexpecteds survival (sim_state)"
8188

89+
# Check that the tracking made sense, i.e. that not all values are the same
90+
assert not np.allclose(part_xboinc.x, part_xboinc.x[0], rtol=1e-4, atol=0)
91+
assert not np.allclose(part_xboinc.px, part_xboinc.px[0], rtol=1e-4, atol=0)
92+
assert not np.allclose(part_xboinc.y, part_xboinc.y[0], rtol=1e-4, atol=0)
93+
assert not np.allclose(part_xboinc.py, part_xboinc.py[0], rtol=1e-4, atol=0)
94+
8295
# Rename file for comparison in next test
8396
output_file.rename(output_file.parent / f'{output_file.name}_2')
97+
xb._skip_xsuite_version_check = False
8498

8599

86100
def test_checkpoint():
101+
xb._skip_xsuite_version_check = True
87102
# If no executable is present, make one
88103
exec_file = list(Path.cwd().glob(f'xboinc_{xb.app_version}-*'))
89104
if len(exec_file)==0 or not exec_file[0].exists():
90-
xb.generate_executable()
105+
test_compilation()
106+
exec_file = list(Path.cwd().glob(f'xboinc_{xb.app_version}-*'))
91107
exec_file = exec_file[0]
92108

93109
# If no input file is present, make one
@@ -123,22 +139,20 @@ def test_checkpoint():
123139
output_file = Path.cwd() / 'sim_state_out.bin'
124140
assert output_file.exists()
125141
assert filecmp.cmp(output_file, output_file.parent / f'{output_file.name}_2', shallow=False)
142+
xb._skip_xsuite_version_check = False
126143

127144

128145
def test_vs_xtrack():
146+
xb._skip_xsuite_version_check = True
129147
# If no output is present, make one
130-
output_file = Path.cwd() / 'sim_state_out.bin'
148+
output_file = Path.cwd() / 'sim_state_out.bin_2'
131149
if not output_file.exists():
132150
test_track()
133151
sim_state = xb.SimState.from_binary(output_file)
134152
part_xboinc = sim_state.particles
135153

136154
# Testing results with xtrack
137-
line = xt.Line.from_json(line_file)
138-
line.build_tracker()
139-
x_norm = np.linspace(-.5, .5, 50)
140-
delta = np.linspace(-1.e-5, 1.e-5, 50)
141-
part = line.build_particles(x_norm=x_norm, delta=delta, nemitt_x=3.5e-6, nemitt_y=3.5e-6)
155+
line, part = _make_input()
142156
line.track(part, num_turns=num_turns)
143157

144158
assert np.array_equal(part.at_turn, part_xboinc.at_turn), "Fail to match xtrack: survivals are not equal"
@@ -148,3 +162,4 @@ def test_vs_xtrack():
148162
assert np.array_equal(part.px, part_xboinc.px), "Fail to match xtrack: px are not equal"
149163
assert np.array_equal(part.py, part_xboinc.py), "Fail to match xtrack: py are not equal"
150164
assert np.array_equal(part.delta, part_xboinc.delta), "Fail to match xtrack: delta are not equal"
165+
xb._skip_xsuite_version_check = False

tests/test_02_user.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99

1010
import xboinc as xb
1111

12+
1213
user = 'sixtadm'
1314
folder_afs = Path('/afs/cern.ch/user/s/sixtadm/public/test_xboinc').resolve()
1415
folder_eos = Path('/afs/cern.ch/user/s/sixtadm/eos_sixtadm/test_xboinc').resolve()
15-
1616
user_data_file = xb._pkg_root / 'user_data.json'
1717

1818

tests/test_03_submission_and_retrieval.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222

2323
def test_submission():
24+
xb._skip_xsuite_version_check = True
2425
num_turns = 100
2526
num_particles = 5000
2627
checkpoint_every = 25
@@ -79,6 +80,7 @@ def test_submission():
7980
assert np.all([member[:len(user)+2] == f'{user}__' for member in member_names])
8081
assert len([member for member in member_names if member[-5:]=='.json']) == num_jobs
8182
assert len([member for member in member_names if member[-4:]=='.bin']) == num_jobs
83+
xb._skip_xsuite_version_check = False
8284

8385

8486
def test_running():
@@ -127,6 +129,7 @@ def test_running():
127129

128130

129131
def test_retrieval():
132+
xb._skip_xsuite_version_check = True
130133
for studyname in ['test_study_1', 'test_study_2']:
131134
num_jobs = 0
132135
x_mean_prev = 0
@@ -153,4 +156,5 @@ def test_retrieval():
153156
+ f"y = {y_mean:.4} +- {y_std:.4}")
154157

155158
assert num_jobs == 5
159+
xb._skip_xsuite_version_check = False
156160

0 commit comments

Comments
 (0)