/
test_widgets.py
executable file
·153 lines (128 loc) · 4.64 KB
/
test_widgets.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# -*- coding: utf-8 -*-
"""
Created on Sun Apr 19 12:04:58 2020
@author: phygbu
"""
import sys
import pytest
from matplotlib.backend_bases import Event
import threading
import time
import numpy as np
import Stoner
ret_pth = Stoner.__homepath__ / ".." / "sample-data" / "TDI_Format_RT.txt"
# Horrible hack to patch QFileDialog for testing
import Stoner.tools.widgets as widgets
from Stoner import Data, DataFolder
def _fix_widgets():
def dummy(mode="getOpenFileName"):
modes = {
"getOpenFileName": ret_pth,
"getOpenFileNames": [ret_pth],
"getSaveFileName": None,
"getExistingDirectory": ret_pth.parent,
}
return lambda *args, **kargs: (modes[mode], None)
modes = {
"OpenFile": {
"method": dummy("getOpenFileName"),
"caption": "Select file to open...",
"arg": ["parent", "caption", "directory", "filter", "options"],
},
"OpenFiles": {
"method": dummy("getOpenFileNames"),
"caption": "Select file(s_ to open...",
"arg": ["parent", "caption", "directory", "filter", "options"],
},
"SaveFile": {
"method": dummy("getSaveFileName"),
"caption": "Save file as...",
"arg": ["parent", "caption", "directory", "filter", "options"],
},
"SelectDirectory": {
"method": dummy("getExistingDirectory"),
"caption": "Select folder...",
"arg": ["parent", "caption", "directory", "options"],
},
}
widgets = sys.modules["Stoner.tools.widgets"]
app = getattr(widgets, "App")
setattr(app, "modes", modes)
return widgets
def test_filedialog():
widgets=_fix_widgets()
assert widgets.fileDialog.openDialog() == ret_pth
assert widgets.fileDialog.openDialog(title="Test", start=".") == ret_pth
assert widgets.fileDialog.openDialog(patterns={"*.bad": "Very bad files"}) == ret_pth
assert widgets.fileDialog.openDialog(mode="OpenFiles") == [ret_pth]
assert widgets.fileDialog.openDialog(mode="SaveFile") is None
assert widgets.fileDialog.openDialog(mode="SelectDirectory") == ret_pth.parent
with pytest.raises(ValueError):
widgets.fileDialog.openDialog(mode="Whateve")
def test_loader():
_fix_widgets()
d = Data(False)
assert d.shape == (1676, 3), "Failed to load data with dialog box"
with pytest.raises(RuntimeError):
d.save(False)
fldr = DataFolder(False)
assert fldr.shape == (
52,
{
"attocube_scan": (15, {}),
"maximus_scan": (2, {"MPI_210127021": (3, {})}),
"NLIV": (11, {}),
"recursivefoldertest": (1, {}),
"working": (4, {}),
},
)
fldr = DataFolder(False, multifile=True)
assert fldr.shape == (1, {}), "multifile mode failed!"
def _event(data, name, **kargs):
"""Make a fake event."""
select = data._select
event = Event("fake", select.data.fig.canvas)
for k, v in kargs.items():
setattr(event, k, v)
try:
getattr(select, name)(event)
except Exception:
breakpoint()
pass
def _trigger0(data):
time.sleep(1)
select = data._select
select.onselect(50, 100)
_event(data, "keypress", key="escape")
def _trigger1(data):
time.sleep(1)
select = data._select
select.onselect(50, 100)
_event(data, "keypress", key="enter")
def _trigger2(data):
time.sleep(1)
select = data._select
select.onselect(50, 100)
select.onselect(150, 200)
_event(data, "keypress", key="i")
_event(data, "keypress", key="backspace")
_event(data, "keypress", key="enter")
def test_range_select():
data = Stoner.Data(ret_pth, setas="xy")
thread = threading.Thread(target=_trigger0, args=(data,))
thread.start()
result = data.search()
xmin, xmax = result.x.min(), result.x.max()
assert xmin < 4.4 and xmax > 291, "Failed to select and clear"
thread = threading.Thread(target=_trigger1, args=(data,))
thread.start()
result = data.search()
xmin1, xmax1 = result.x.min(), result.x.max()
assert np.isclose(xmin1, 50, atol=1) and np.isclose(xmax1, 100, 1), "Single selection failed."
thread = threading.Thread(target=_trigger2, args=(data,))
thread.start()
result = data.search()
xmin2, xmax2 = result.x.min(), result.x.max()
assert np.isclose(xmin, xmin2) and np.isclose(xmax, xmax2), "Selection with keypresses failed"
if __name__ == "__main__":
pytest.main(["--pdb", __file__])