Skip to content

Commit

Permalink
delete_user_models test works
Browse files Browse the repository at this point in the history
  • Loading branch information
robnagler committed Jan 16, 2024
1 parent 60f5334 commit 031c3ea
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 16 deletions.
30 changes: 17 additions & 13 deletions sirepo/template/srw.py
Original file line number Diff line number Diff line change
Expand Up @@ -792,18 +792,22 @@ def stateful_compute_create_shadow_simulation(data, **kwargs):

def stateful_compute_delete_user_models(data, **kwargs):
"""Remove the beam and undulator user model list files"""
electron_beam = data.args.electron_beam
tabulated_undulator = data.args.tabulated_undulator
for model_name in _USER_MODEL_LIST_FILENAME.keys():
model = electron_beam if model_name == "electronBeam" else tabulated_undulator
if not model or "id" not in model:
continue
user_model_list = _load_user_model_list(model_name)
for i, m in enumerate(user_model_list):
if m.id == model.id:
del user_model_list[i]
_save_user_model_list(model_name, user_model_list)
break

def _delete(model_name, model):
if model and "id" in model:
# optimistic: model is very likely to be there
_save_user_model_list(
model_name,
list(
filter(
lambda m: m.id != model.id,
_load_user_model_list(model_name),
),
),
)

_delete("electronBeam", data.args.get("electron_beam"))
_delete("tabulatedUndulator", data.args.get("tabulated_undulator"))
return PKDict()


Expand Down Expand Up @@ -2354,7 +2358,7 @@ def _safe_beamline_item_name(name, names):
return current


def _save_user_model_list(model_name, beam_list, qcall):
def _save_user_model_list(model_name, beam_list, qcall=None):
_SIM_DATA.lib_file_write(
_USER_MODEL_LIST_FILENAME[model_name],
pkjson.dump_bytes(beam_list),
Expand Down
24 changes: 21 additions & 3 deletions tests/stateful_compute_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,27 @@ def test_srw_model_list(fc):
args=PKDict(model_name="electronBeam"),
),
)
# pkdp(pkdpretty(r))
m = next(filter(lambda x: x.name == "xyzzy", r.modelList))
pkunit.pkok(m, "user model not in reply={}", r)
r = fc.sr_post(
"statefulCompute",
PKDict(
method="delete_user_models",
simulationType=fc.sr_sim_type,
args=PKDict(electron_beam=m),
),
)
pkunit.pkeq("completed", r.state)
r = fc.sr_post(
"statefulCompute",
PKDict(
method="model_list",
simulationType=fc.sr_sim_type,
args=PKDict(model_name="electronBeam"),
),
)
pkunit.pkok(
list(filter(lambda x: x.name == "xyzzy", r.modelList)),
"user model not in reply={}",
not any(filter(lambda x: x.name == "xyzzy", r.modelList)),
"user model in reply={}",
r,
)

0 comments on commit 031c3ea

Please sign in to comment.