Skip to content

Commit e33048b

Browse files
Added tests for generate.py (#157)
1 parent 83c699b commit e33048b

File tree

4 files changed

+234
-15
lines changed

4 files changed

+234
-15
lines changed

src/techui_builder/generate.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ def _get_widget_position(
145145
def _get_group_dimensions(self, widget_list: list[EmbeddedDisplay | ActionButton]):
146146
"""
147147
Takes in a list of widgets and finds the
148-
maximum height in the list
148+
maximum height and maximum width in the list
149149
"""
150150
x_list: list[int] = []
151151
y_list: list[int] = []
@@ -297,7 +297,6 @@ def _create_widget(
297297
new_widget.append(self._allocate_widget(value, component))
298298
else:
299299
new_widget = self._allocate_widget(scrn_mapping, component)
300-
301300
return new_widget
302301

303302
def layout_widgets(self, widgets: list[EmbeddedDisplay | ActionButton]):
@@ -391,7 +390,6 @@ def build_groups(self):
391390
return
392391

393392
self.widgets = self.layout_widgets(self.widgets)
394-
395393
# Create a list of dimensions for the groups
396394
# that will be created.
397395
height, width = self._get_group_dimensions(self.widgets)

tests/test_files/widget.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" ?>
2+
<widget type="embedded" version="2.0.0">
3+
<name>CAM:</name>
4+
<x>0</x>
5+
<y>0</y>
6+
<width>1280</width>
7+
<height>800</height>
8+
<file>techui_support/bob/ADAravis/ADAravis_summary.bob</file>
9+
<macros>
10+
<P>BL23B-DI-MOD-02</P>
11+
<R>CAM:</R>
12+
</macros>
13+
</widget>

tests/test_files/widget_list.yaml

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
- <?xml version="1.0" ?>
2+
<widget type='embedded' version='2.0.0'>
3+
<name>pmac.autohome</name>
4+
<width>205</width>
5+
<height>120</height>
6+
<file>../../techui-support/bob/motor_embed.bob</file>
7+
<macros>
8+
<P>BL01T-MO-MAP-01:STAGE</P>
9+
<M/>
10+
</macros>
11+
<x>0</x>
12+
<y>0</y>
13+
</widget>
14+
15+
- <?xml version="1.0" ?>
16+
<widget type='embedded' version='2.0.0'>
17+
<name>X</name>
18+
<width>205</width>
19+
<height>120</height>
20+
<file>../../techui-support/bob/motor_embed.bob</file>
21+
<macros>
22+
<P>BL01T-MO-MAP-01:STAGE</P>
23+
<M>X</M>
24+
</macros>
25+
<x>0</x>
26+
<y>150</y>
27+
</widget>
28+
29+
- <?xml version="1.0" ?>
30+
<widget type="embedded" version="2.0.0">
31+
<name>A</name>
32+
<width>205</width>
33+
<height>120</height>
34+
<file>../../techui-support/bob/motor_embed.bob</file>
35+
<macros>
36+
<P>BL01T-MO-MAP-01:STAGE</P>
37+
<M>A</M>
38+
</macros>
39+
<x>0</x>
40+
<y>300</y>
41+
</widget>
42+
43+
- <?xml version="1.0" ?>
44+
<widget type="action_button" version="2.0.0">
45+
<name>pmac.GeoBrick</name>
46+
<width>100</width>
47+
<height>40</height>
48+
<pv_name>BL01T-MO-BRICK-01:None</pv_name>
49+
<text>BL01T-MO-BRICK-01</text>
50+
<actions>
51+
<action type="open_display">
52+
<description>Open Display</description>
53+
<macros>
54+
<P>BL01T-MO-BRICK-01</P>
55+
<M>None</M>
56+
</macros>
57+
<file>../../techui-support/bob/pmacController.bob</file>
58+
<target>tab</target>
59+
</action>
60+
</actions>
61+
<x>0</x>
62+
<y>450</y>
63+
</widget>

tests/test_generate.py

Lines changed: 157 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
# from pathlib import Path
1+
from pathlib import Path
22

3-
# import pytest
4-
5-
# from techui_builder.builder import Builder
3+
import pytest
4+
import yaml
5+
from lxml import objectify
6+
from phoebusgen import widget as Widget
67

78
from techui_builder.models import Entity
89

@@ -24,11 +25,155 @@ def test_generator_get_screen_dimensions(generator):
2425
assert y == 205
2526

2627

27-
# def test_build_groups(gb: Builder):
28-
# generator = Generator(
29-
# gb.entities, gb._gui_map, gb.components[4].name
30-
# ) # TODO: remove hardcoded index
31-
# generator.build_groups()
32-
# with open("./tests/test_files/group.xml") as f:
33-
# control = f.read()
34-
# assert str(generator.group) == control
28+
def test_generator_get_widget_dimensions(generator):
29+
widget = Path("tests/test_files/widget.xml")
30+
31+
with open(widget) as f:
32+
xml_content = f.read()
33+
34+
height, width = generator._get_widget_dimensions(xml_content)
35+
assert height == 800
36+
assert width == 1280
37+
38+
39+
def test_generator_get_widget_position(generator):
40+
widget = Path("tests/test_files/widget.xml")
41+
42+
with open(widget) as f:
43+
xml_content = f.read()
44+
45+
y, x = generator._get_widget_position(xml_content)
46+
assert x == 0
47+
assert y == 0
48+
49+
50+
def test_generator_get_group_dimensions(generator):
51+
group = Path("tests/test_files/widget_list.yaml")
52+
53+
with open(group) as f:
54+
widgets_list = yaml.safe_load(f.read())
55+
56+
height, width = generator._get_group_dimensions(widget_list=widgets_list)
57+
assert height == 620
58+
assert width == 255
59+
60+
61+
def test_generator_initialise_name_suffix(generator):
62+
component = Entity(type="test", P="TEST", desc=None, M="T1", R=None)
63+
64+
name, suffix, suffix_label = generator._initialise_name_suffix(component)
65+
66+
assert name == "T1"
67+
assert suffix == "T1"
68+
assert suffix_label == "M"
69+
70+
71+
def test_generator_is_list_of_dicts(generator):
72+
list_of_dicts = [{"a": 1}, {"b": 2}]
73+
not_list_of_dicts = {"a": 1}
74+
75+
assert generator._is_list_of_dicts(list_of_dicts) is True
76+
assert generator._is_list_of_dicts(not_list_of_dicts) is False
77+
78+
79+
def test_generator_allocate_widget(generator):
80+
scrn_mapping = {
81+
"file": "ADAravis/ADAravis_summary.bob",
82+
"prefix": "$(P)$(R)",
83+
"type": "embedded",
84+
}
85+
component = Entity(
86+
type="ADAravis.aravisCamera", P="BL23B-DI-MOD-02", desc=None, M=None, R="CAM:"
87+
)
88+
89+
widget = generator._allocate_widget(scrn_mapping, component)
90+
91+
control_widget = Path("tests/test_files/widget.xml")
92+
93+
with open(control_widget) as f:
94+
xml_content = f.read()
95+
96+
assert str(widget) == xml_content
97+
98+
99+
def test_generator_create_widget(generator):
100+
component = Entity(
101+
type="ADAravis.aravisCamera", P="BL23B-DI-MOD-02", desc=None, M=None, R="CAM:"
102+
)
103+
104+
widget = generator._create_widget(
105+
component=component,
106+
)
107+
108+
control_widget = Path("tests/test_files/widget.xml")
109+
110+
with open(control_widget) as f:
111+
xml_content = f.read()
112+
113+
assert str(widget) == xml_content
114+
115+
116+
@pytest.mark.parametrize(
117+
"index, x, y",
118+
[
119+
(0, 0, 0),
120+
(1, 0, 150),
121+
(2, 0, 300),
122+
(3, 0, 450),
123+
(4, 120, 450),
124+
(5, 0, 520),
125+
],
126+
)
127+
def test_generator_layout_widgets(generator, index, x, y):
128+
widgets_list = [
129+
Widget.EmbeddedDisplay(name="A", file="", x=0, y=0, width=205, height=120),
130+
Widget.EmbeddedDisplay(name="X", file="", x=0, y=0, width=205, height=120),
131+
Widget.EmbeddedDisplay(
132+
name="pmac.autohome", file="", x=0, y=0, width=205, height=120
133+
),
134+
Widget.EmbeddedDisplay(
135+
name="pmac.GeoBrick", file="", x=0, y=0, width=100, height=40
136+
),
137+
Widget.ActionButton(
138+
name="pmac.Action1",
139+
text="test1",
140+
pv_name="abc",
141+
x=0,
142+
y=0,
143+
width=100,
144+
height=40,
145+
),
146+
Widget.ActionButton(
147+
name="pmac.Action2",
148+
text="test2",
149+
pv_name="xyz",
150+
x=0,
151+
y=0,
152+
width=100,
153+
height=40,
154+
),
155+
]
156+
arranged_widgets = generator.layout_widgets(widgets_list)
157+
assert objectify.fromstring(str(arranged_widgets[index])).x == x
158+
assert objectify.fromstring(str(arranged_widgets[index])).y == y
159+
160+
161+
def test_generator_build_groups(generator):
162+
entity = Entity(
163+
type="ADAravis.aravisCamera", P="BL23B-DI-MOD-02", desc=None, M=None, R="CAM:"
164+
)
165+
generator.load_screen("test", [entity])
166+
generator.build_groups()
167+
print(generator.screen_)
168+
assert objectify.fromstring(str(generator.screen_)).xpath("//widget[@type='group']")
169+
170+
171+
def test_generator_write_screen(generator):
172+
entity = Entity(
173+
type="ADAravis.aravisCamera", P="BL23B-DI-MOD-02", desc=None, M=None, R="CAM:"
174+
)
175+
generator.load_screen("test", [entity])
176+
generator.build_groups()
177+
generator.write_screen(Path("tests/test_files/"))
178+
assert Path("tests/test_files/test.bob").exists()
179+
Path("tests/test_files/test.bob").unlink()

0 commit comments

Comments
 (0)