This commit is contained in:
@@ -1,211 +0,0 @@
|
||||
import pytest
|
||||
from slic.core.scanner.scaninfo import ScanInfo
|
||||
|
||||
class DummyAdjustable:
|
||||
def __init__(self, name="adj", ID="id", units="u"):
|
||||
self.name = name
|
||||
self.ID = ID
|
||||
self.units = units
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"adjustables,values,suffix,expected_filename,expected_params",
|
||||
[
|
||||
(
|
||||
[DummyAdjustable()],
|
||||
[1, 2, 3],
|
||||
"_scan_info.json",
|
||||
"fileA_scan_info.json",
|
||||
{"name": ["adj"], "Id": ["id"], "units": ["u"]},
|
||||
),
|
||||
|
||||
(
|
||||
[DummyAdjustable("motorX", "M1", "mm")],
|
||||
[10, 20],
|
||||
".meta",
|
||||
"fileB.meta",
|
||||
{"name": ["motorX"], "Id": ["M1"], "units": ["mm"]},
|
||||
),
|
||||
|
||||
(
|
||||
[
|
||||
DummyAdjustable("motorX", "M1", "mm"),
|
||||
DummyAdjustable("stageY", "S2", "deg"),
|
||||
DummyAdjustable("lensZ", "L3", "cm"),
|
||||
],
|
||||
[1, 2, 3],
|
||||
"_extra.json",
|
||||
"fileC_extra.json",
|
||||
{
|
||||
"name": ["motorX", "stageY", "lensZ"],
|
||||
"Id": ["M1", "S2", "L3"],
|
||||
"units": ["mm", "deg", "cm"],
|
||||
},
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_init_creates_expected_filename(tmp_path, adjustables, values, suffix, expected_filename, expected_params):
|
||||
# Test correct filename construction and metadata extraction for different adjustables
|
||||
base_dir = tmp_path
|
||||
filename_base = expected_filename.split("_")[0].split(".")[0]
|
||||
si = ScanInfo(filename_base, base_dir, adjustables, values, suffix=suffix)
|
||||
|
||||
# Verify final file path is correct
|
||||
assert si.filename.endswith(expected_filename)
|
||||
|
||||
# Verify extracted metadata
|
||||
assert si.parameters == expected_params
|
||||
|
||||
# Verify storage lists are empty at initialization
|
||||
assert si.values == []
|
||||
assert si.readbacks == []
|
||||
assert si.files == []
|
||||
assert si.info == []
|
||||
|
||||
def test_append(tmp_path):
|
||||
# Verify append() correctly adds multiple values per call
|
||||
si = ScanInfo("fileX", tmp_path, [DummyAdjustable("A", "1", "u")], [0])
|
||||
|
||||
# First call: multiple input values
|
||||
si.append([1, 2, 3], [10, 20, 30], ["f1.dat", "f2.dat", "f3.dat"], {"note": "phase1"})
|
||||
assert si.values == [[1, 2, 3]]
|
||||
assert si.readbacks == [[10, 20, 30]]
|
||||
assert si.files == [["f1.dat", "f2.dat", "f3.dat"]]
|
||||
assert si.info == [{"note": "phase1"}]
|
||||
|
||||
# Second call: new data with info as function
|
||||
si.append([4, 5], [40, 50], ["f4.dat", "f5.dat"], lambda: {"note": "auto_phase2"})
|
||||
|
||||
# Verify both calls were recorded
|
||||
assert si.values == [[1, 2, 3], [4, 5]]
|
||||
assert si.readbacks == [[10, 20, 30], [40, 50]]
|
||||
assert si.files == [["f1.dat", "f2.dat", "f3.dat"], ["f4.dat", "f5.dat"]]
|
||||
assert si.info == [{"note": "phase1"}, {"note": "auto_phase2"}]
|
||||
|
||||
|
||||
def test_write_and_to_dict(tmp_path, monkeypatch):
|
||||
# Test that write() calls json_save correctly and to_dict() returns proper structure
|
||||
base_dir = tmp_path
|
||||
si = ScanInfo("scanTest", base_dir, [
|
||||
DummyAdjustable("motorX", "M1", "mm"),
|
||||
DummyAdjustable("stageY", "S2", "deg"),
|
||||
], [0], suffix="_info.json")
|
||||
|
||||
si.append([1.0, 2.0], [1.1, 2.1], ["f1.dat", "f2.dat"], {"phase": "init"})
|
||||
si.append([3.0, 4.0], [3.1, 4.1], ["f3.dat", "f4.dat"], {"phase": "end"})
|
||||
|
||||
# Mock json_save to capture calls
|
||||
last_call = {}
|
||||
def mock_json_save(data, filename):
|
||||
last_call['data'] = data
|
||||
last_call['filename'] = filename
|
||||
|
||||
monkeypatch.setattr('slic.utils.json_save', mock_json_save)
|
||||
|
||||
# Call write()
|
||||
si.write()
|
||||
|
||||
# Verify json_save was called with correct arguments
|
||||
assert last_call['filename'] == si.filename
|
||||
|
||||
expected = si.to_dict()
|
||||
assert last_call['data'] == expected
|
||||
|
||||
|
||||
def test_update_integration(tmp_path, monkeypatch):
|
||||
# Integration test: update() calls append() and json_save consistently
|
||||
si = ScanInfo("scanX", tmp_path, [DummyAdjustable("M", "ID", "mm")], [0], suffix=".json")
|
||||
|
||||
# Mock json_save to capture calls
|
||||
last_call = {}
|
||||
def mock_json_save(data, filename):
|
||||
last_call['data'] = data
|
||||
last_call['filename'] = filename
|
||||
|
||||
monkeypatch.setattr('slic.utils.json_save', mock_json_save)
|
||||
|
||||
si.update([1, 2], [10, 20], ["f1.dat", "f2.dat"], {"phase": "start"})
|
||||
|
||||
# Verify internal state
|
||||
assert si.values == [[1, 2]]
|
||||
assert si.readbacks == [[10, 20]]
|
||||
assert si.files == [["f1.dat", "f2.dat"]]
|
||||
assert si.info == [{"phase": "start"}]
|
||||
|
||||
# Verify json_save was called with to_dict()
|
||||
assert last_call['filename'] == si.filename
|
||||
assert last_call['data'] == si.to_dict()
|
||||
|
||||
def test_to_sfdaq_dict_filled_example(tmp_path):
|
||||
# Test to_sfdaq_dict() with all fields filled, including offsets and coefficients
|
||||
si = ScanInfo(
|
||||
filename_base="scanAlpha",
|
||||
base_dir=tmp_path,
|
||||
adjustables=[
|
||||
DummyAdjustable("motorX", "M1", "mm"),
|
||||
DummyAdjustable("stageY", "S2", "deg"),
|
||||
DummyAdjustable("lensZ", "L3", "cm"),
|
||||
],
|
||||
values=[0, 1, 2],
|
||||
suffix="_scan_info.json",
|
||||
)
|
||||
|
||||
# Case 1: nothing added yet
|
||||
result_empty = si.to_sfdaq_dict()
|
||||
assert result_empty["scan_values"] is None
|
||||
assert result_empty["scan_readbacks"] is None
|
||||
|
||||
# Case 2: first dataset
|
||||
si.append(
|
||||
[1.0, 2.0, 3.0], # values
|
||||
[1.1, 2.1, 3.1], # readbacks
|
||||
["f1.dat"], # files
|
||||
{"note": "first run"} # info
|
||||
)
|
||||
|
||||
# Case 3: second dataset
|
||||
si.append(
|
||||
[4.0, 5.0, 6.0], # values
|
||||
[4.1, 5.1, 6.1], # readbacks
|
||||
["f2.dat"],
|
||||
{"note": "second run"}
|
||||
)
|
||||
|
||||
# Call the method
|
||||
result = si.to_sfdaq_dict()
|
||||
|
||||
# Verify main keys
|
||||
expected_keys = {
|
||||
"scan_name", "name", "Id", "units",
|
||||
"offset", "conversion_factor",
|
||||
"scan_values", "scan_readbacks", "scan_readbacks_raw",
|
||||
}
|
||||
assert set(result.keys()) == expected_keys
|
||||
|
||||
# Verify latest values were taken
|
||||
assert result["scan_values"] == [4.0, 5.0, 6.0]
|
||||
assert result["scan_readbacks"] == [4.1, 5.1, 6.1]
|
||||
assert result["scan_readbacks_raw"] == [4.1, 5.1, 6.1]
|
||||
|
||||
# Verify other fields
|
||||
assert result["scan_name"] == "scanAlpha"
|
||||
assert result["name"] == ["motorX", "stageY", "lensZ"]
|
||||
assert result["Id"] == ["M1", "S2", "L3"]
|
||||
assert result["units"] == ["mm", "deg", "cm"]
|
||||
assert result["offset"] == [0, 0, 0]
|
||||
assert result["conversion_factor"] == [1, 1, 1]
|
||||
|
||||
# Verify overall consistency
|
||||
expected_dict = {
|
||||
"scan_name": "scanAlpha",
|
||||
"name": ["motorX", "stageY", "lensZ"],
|
||||
"Id": ["M1", "S2", "L3"],
|
||||
"units": ["mm", "deg", "cm"],
|
||||
"offset": [0, 0, 0],
|
||||
"conversion_factor": [1, 1, 1],
|
||||
"scan_values": [4.0, 5.0, 6.0],
|
||||
"scan_readbacks": [4.1, 5.1, 6.1],
|
||||
"scan_readbacks_raw": [4.1, 5.1, 6.1],
|
||||
}
|
||||
|
||||
assert result == expected_dict
|
||||
Reference in New Issue
Block a user