Repository navigation
Expand file tree
/
Copy pathffDesign_Tests.py
More file actions
282 lines (203 loc) · 9.47 KB
/
Copy pathffDesign_Tests.py
File metadata and controls
282 lines (203 loc) · 9.47 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import abc
import unittest
import pathlib
import os
import FreeCAD as App
import FreeCADGui as Gui
import BOPTools.SplitFeatures
from PySide import QtCore
import ffDesign_Utils as Utils
import ffDesign_CounterboreBridges
import ffDesign_RibThreads
import ffDesign_RoofBridge
TEST_DOCUMENTS_PATH = "ffDesign_TestData"
def get_test_dir() -> pathlib.Path:
test_documents_repo = pathlib.Path(__file__).parent / TEST_DOCUMENTS_PATH
if not test_documents_repo.is_dir():
Utils.Log.error(f"""\
Test documents repository {test_documents_repo.name} does not exist! Maybe you need to download it first?
Full path: {test_documents_repo}
Please read the documentation of {Utils.Log.addon} to understand how to prepare for running this testsuite.""")
raise Exception(f"{Utils.Log.addon} test documents directory is missing")
# Depending on the FreeCAD version, we select the relevant subdirectory.
subdir_name = "FreeCAD_1.0"
if Utils.check_freecad_version(min_version=[1, 1, 0]):
subdir_name = "FreeCAD_1.1"
return test_documents_repo / subdir_name
class ffDesignTestCase(unittest.TestCase, abc.ABC):
test_document = None # Name of the test document for this testcase
def setUp(self):
# Skip all the ffDesign dialogs during test runs
Utils.SKIP_ALL_DIALOGS = True
test_document_path = get_test_dir() / self.test_document
if not test_document_path.exists():
Utils.Log.error(f"""\
Test document {test_document_path.name} is missing.
Do you have the latest version of the test documents repository?
Full path: {test_document_path}
Please read the documentation of {Utils.Log.addon} to understand how to prepare for running this testsuite.""")
raise Exception(f"{test_document_path.name} is missing")
self.doc = App.openDocument(str(test_document_path))
def tearDown(self):
# Re-enable ffDesign dialogs after the test completes
Utils.SKIP_ALL_DIALOGS = False
# You can use FFDESIGN_KEEP_TEST_DOC=1 to keep the test document open to inspect it.
# Only makes sense when running tests with `-r` instead of `-t`.
# You should probably only use this when running a single test case.
if os.environ.get("FFDESIGN_KEEP_TEST_DOC", default="0") == "0":
App.closeDocument(self.doc.Name)
def prepare_regression_test(self):
self.body = self.doc.getObjectsByLabel("BodyUnderTest")[0]
self.body_expected = self.doc.getObjectsByLabel("BodyExpected")[0]
Gui.ActiveDocument.ActiveView.setActiveObject("pdbody", self.body)
def assert_expected_body(self):
# Move the body_expected to the origin where the other body should already be
self.body_expected.Placement = App.Placement(App.Vector(0, 0, 0), App.Rotation(App.Vector(0, 0, 1), 0))
# Run a Part XOR operation between the two bodies to find differences
xor_body = BOPTools.SplitFeatures.makeXOR(name="Comparison_XOR")
xor_body.Objects = [self.body, self.body_expected]
xor_body.Proxy.execute(xor_body)
self.body_expected.ViewObject.hide()
self.body.ViewObject.Transparency = 70
# Make the xor_body bright red
xor_body.ViewObject.ShapeAppearance = [App.Material(DiffuseColor=(255, 0, 0, 255))]
# Check that the resulting shape is utterly empty
self.assertEqual(xor_body.Shape.Volume, 0, "Detected differences between resulting and expected shape")
self.assertEqual(len(xor_body.Shape.Vertexes), 0, "Detected differences between resulting and expected shape")
class ffDesignTestCase_Fc11(ffDesignTestCase):
"""A testcase that only runs on FreeCAD >1.1."""
def setUp(self):
if not Utils.check_freecad_version(min_version=[1, 1, 0]):
raise unittest.SkipTest("This testcase only works on FreeCAD >=1.1")
super().setUp()
class HoleLocations(ffDesignTestCase_Fc11):
test_document = "HoleLocations.FCStd"
@unittest.expectedFailure
def test_plausibility(self):
self.prepare_regression_test()
self.assert_expected_body()
def test_construction_and_non_defining(self):
"""
Test that hole locations are correctly inferred from a sketch with both
construction circles and non-defining external geometry.
"""
self.prepare_regression_test()
ffDesign_CounterboreBridges.CounterboreBridgesCommand().Activated()
self.assert_expected_body()
class PartialHoleProfiles(ffDesignTestCase_Fc11):
test_document = "PartialHoleProfile.FCStd"
@unittest.expectedFailure
def test_plausibility(self):
self.prepare_regression_test()
self.assert_expected_body()
def test_partial_profile(self):
"""
Test that a PartDesign_Hole using a partial sketch as its profile is
correctly handled by the counterbore bridges tool.
"""
self.prepare_regression_test()
ffDesign_CounterboreBridges.CounterboreBridgesCommand().Activated()
self.assert_expected_body()
class TwoSidedTaperBug(ffDesignTestCase):
test_document = "TwoSidedTaperBug.FCStd"
@unittest.expectedFailure
def test_plausibility(self):
self.prepare_regression_test()
self.assert_expected_body()
def test_two_sided_taper_bug(self):
self.prepare_regression_test()
# TODO tiPiL2ai: Use the command rather than directly generating using the addon function
hole = self.body.Tip
Utils.assert_hole(hole)
ffDesign_RibThreads.verify_rib_thread_suitability(hole)
dialog = ffDesign_RibThreads.RibThreadsTaskPanel(self.body, hole)
Gui.Control.showDialog(dialog)
dialog.accept()
self.assert_expected_body()
class RibThreads(ffDesignTestCase):
test_document = "TapHoles.FCStd"
def test_generate_rib_threads(self):
"""
Smoke tests that we can generate rib threads for the predefined metric M4 tap holes.
"""
self.prepare_regression_test()
# TODO tiPiL2ai: Use the command rather than directly generating using the addon function
hole = self.body.Tip
Utils.assert_hole(hole)
ffDesign_RibThreads.verify_rib_thread_suitability(hole)
dialog = ffDesign_RibThreads.RibThreadsTaskPanel(self.body, hole)
Gui.Control.showDialog(dialog)
dialog.accept()
# The file expects an additional rotation of the rib threads to be present
varset = self.body.getObject("Hole_RibThread_Settings")
varset.Rotation = "90deg"
App.ActiveDocument.recompute()
self.assert_expected_body()
class RibThreads_Fc11(ffDesignTestCase_Fc11):
test_document = "TapHoles.FCStd"
def thread_test(self, thread_type: str, thread_size: str):
# Prep for the next test
App.closeDocument(self.doc.Name)
self.setUp()
try:
self.prepare_regression_test()
hole = self.body.Tip
Utils.assert_hole(hole)
hole.ThreadType = thread_type
hole.ThreadSize = thread_size
App.ActiveDocument.recompute()
dialog = ffDesign_RibThreads.RibThreadsTaskPanel(self.body, hole)
Gui.Control.showDialog(dialog)
dialog.accept()
except Exception as e:
Utils.Log.warning(f"Test failure during thread {thread_type} {thread_size}")
raise e from None
def test_known_metric(self):
for thread in ffDesign_RibThreads.RIB_PARAMETERS["ISOMetricProfile"]:
if "x" not in thread or thread == "M6x1":
# This is one of the legacy thread names, skip
continue
self.thread_test("ISOMetricProfile", thread)
def test_known_unc(self):
for thread in ffDesign_RibThreads.RIB_PARAMETERS["UNC"]:
self.thread_test("UNC", thread)
class RoofBridges(ffDesignTestCase):
test_document = "RoofBridges.FCStd"
def apply_roof_bridge_for_hole(
self,
hole_name: str,
*,
double_sided=None,
include_counterbore=None,
clearance=None,
angle_60=False,
ignore_missing=False,
):
hole = self.body.getObject(hole_name)
Utils.assert_hole(hole)
dialog = ffDesign_RoofBridge.RoofBridgeTaskPanel(self.body, hole)
Gui.Control.showDialog(dialog)
if double_sided is not None:
if double_sided:
dialog.form.DoubleSided.setCheckState(QtCore.Qt.CheckState.Checked)
else:
dialog.form.DoubleSided.setCheckState(QtCore.Qt.CheckState.Unchecked)
if include_counterbore is not None:
if include_counterbore:
dialog.form.DoCounterbore.setCheckState(QtCore.Qt.CheckState.Checked)
else:
dialog.form.DoCounterbore.setCheckState(QtCore.Qt.CheckState.Unchecked)
if clearance is not None:
dialog.form.BridgeClearance.setProperty("rawValue", clearance)
if angle_60:
dialog.form.Angle60.toggle()
dialog.accept()
def test_roof_bridges(self):
self.prepare_regression_test()
self.apply_roof_bridge_for_hole("Hole")
self.apply_roof_bridge_for_hole("Hole001", double_sided=True, angle_60=True)
self.apply_roof_bridge_for_hole("Hole002", double_sided=True)
self.apply_roof_bridge_for_hole("Hole003", include_counterbore=False, clearance=0.6)
self.body.getObject("Hole002").RoofBridgeRotation = "0deg"
App.ActiveDocument.recompute()
self.assert_expected_body()