-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_newSpecFile.py
98 lines (77 loc) · 2.92 KB
/
test_newSpecFile.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import datetime
import os
import pathlib
import shutil
import tempfile
import pytest
from apstools.utils import cleanupText
from instrument.callbacks.spec_data_file_writer import newSpecFile, specwriter
from spec2nexus.spec import SpecDataFile, is_spec_file
from bluesky.run_engine import RunEngine
INITIAL_SCAN_ID = 1234
RE = RunEngine(dict(scan_id=INITIAL_SCAN_ID))
# databroker is not necessary
TEST_DATA_FILE = pathlib.Path(__file__).parent / "20230509-191819.spec_data"
@pytest.fixture
def tempdir():
t = pathlib.Path(tempfile.mkdtemp())
yield t
shutil.rmtree(t)
@pytest.mark.parametrize(
"title, scan_id",
[
["title", None], # most common use
["title", 1],
["title", 555],
["x+y = 12% (by weight)", None], # title is not safe
[None, None], # title cannot be None, will raise a TypeError
],
)
def test_newSpecFile(title, scan_id, tempdir):
assert tempdir.exists()
os.chdir(tempdir) # newSpecFile() works in pwd
# make sure test case does not match initial value
assert scan_id != INITIAL_SCAN_ID
RE.md["scan_id"] = INITIAL_SCAN_ID
assert RE.md["scan_id"] == INITIAL_SCAN_ID
kwargs = dict(RE=RE)
if scan_id is not None:
kwargs["scan_id"] = scan_id
if title is None:
with pytest.raises(TypeError):
newSpecFile(title, **kwargs)
else:
newSpecFile(title, **kwargs)
assert RE.md["scan_id"] == 1 if scan_id is None else scan_id
safe_title = cleanupText(title)
assert safe_title in specwriter.spec_filename.name
assert specwriter.spec_filename.name[5:] == f"_{safe_title}.dat"
assert isinstance(specwriter.spec_filename, pathlib.Path)
def test_newSpecFile_with_existing(tempdir):
assert tempdir.exists()
os.chdir(tempdir) # newSpecFile() works in pwd
assert TEST_DATA_FILE.exists()
assert is_spec_file(TEST_DATA_FILE)
mmdd = str(datetime.datetime.now()).split()[0][5:].replace("-", "_")
title = "testfile"
testfile = tempdir / f"{mmdd}_{title}.dat"
assert not testfile.exists()
# make the file to be named by newSpecFile() below
shutil.copy2(TEST_DATA_FILE, testfile)
assert testfile.exists()
assert is_spec_file(testfile)
sdf = SpecDataFile(testfile)
last_scan_number_in_file = int(sdf.getLastScanNumber())
assert last_scan_number_in_file == 983
RE.md["scan_id"] = INITIAL_SCAN_ID
assert RE.md["scan_id"] == INITIAL_SCAN_ID
scan_id = max(last_scan_number_in_file, INITIAL_SCAN_ID) + 10
newSpecFile(title, RE=RE)
assert RE.md["scan_id"] != INITIAL_SCAN_ID
assert RE.md["scan_id"] != scan_id
assert RE.md["scan_id"] == last_scan_number_in_file
RE.md["scan_id"] = INITIAL_SCAN_ID # reset it before next call
newSpecFile(title, scan_id=scan_id, RE=RE)
assert RE.md["scan_id"] != INITIAL_SCAN_ID
assert RE.md["scan_id"] != scan_id
assert RE.md["scan_id"] == last_scan_number_in_file