Salome HOME
Adjust test cases according to updated Arc behavior
[modules/shaper.git] / src / PythonAPI / model / tests / tests.py
1 from GeomAlgoAPI import *
2 from GeomAPI import *
3 from GeomDataAPI import *
4 from ModelAPI import ModelAPI_Feature
5 import math
6 from salome.shaper.model import sketcher
7
8 TOLERANCE = 1.e-7
9
10 aShapeTypes = {
11   GeomAPI_Shape.SOLID:  "GeomAPI_Shape.SOLID",
12   GeomAPI_Shape.FACE:   "GeomAPI_Shape.FACE",
13   GeomAPI_Shape.EDGE:   "GeomAPI_Shape.EDGE",
14   GeomAPI_Shape.VERTEX: "GeomAPI_Shape.VERTEX"}
15
16
17 def generateTests(theFeature, theFeatureName, theTestsList = []):
18   """ Generates tests for theFeature.
19   :param theFeature: feature to test. Should be ModelHighAPI_Interface.
20   :param theFeatureName: feature name to put in test commands.
21   :param theTestsList: list of test to be generated. If empty generates all tests.
22   """
23   if "testNbResults" in theTestsList or len(theTestsList) == 0:
24     aNbResults = len(theFeature.results())
25     print "model.testNbResults({}, {})".format(theFeatureName, aNbResults)
26
27   if "testNbSubResults" in theTestsList or len(theTestsList) == 0:
28     aNbResults = len(theFeature.results())
29     aNbSubResults = []
30     for anIndex in range(0, aNbResults):
31       aNbSubResults.append(theFeature.results()[anIndex].numberOfSubs())
32     print "model.testNbSubResults({}, {})".format(theFeatureName, aNbSubResults)
33
34   if "testNbSubShapes" in theTestsList or len(theTestsList) == 0:
35     aNbResults = len(theFeature.results())
36     for aShapeType in aShapeTypes:
37       aNbSubShapes = []
38       for anIndex in range(0, aNbResults):
39         aShape = theFeature.results()[anIndex].resultSubShapePair()[0].shape()
40         aNbResultSubShapes = 0
41         aShapeExplorer = GeomAPI_ShapeExplorer(aShape, aShapeType)
42         while aShapeExplorer.more():
43           aNbResultSubShapes += 1
44           aShapeExplorer.next();
45         aNbSubShapes.append(aNbResultSubShapes)
46       print "model.testNbSubShapes({}, {}, {})".format(theFeatureName, aShapeTypes[aShapeType], aNbSubShapes)
47
48   if "testResultsVolumes" in theTestsList or len(theTestsList) == 0:
49     aNbResults = len(theFeature.results())
50     aResultsVolumes = []
51     for anIndex in range(0, aNbResults):
52       aResultsVolumes.append(GeomAlgoAPI_ShapeTools_volume(theFeature.results()[anIndex].resultSubShapePair()[0].shape()))
53     print "model.testResultsVolumes({}, [{}])".format(theFeatureName, ", ".join("{:0.27f}".format(i) for i in aResultsVolumes))
54
55
56 def testNbResults(theFeature, theExpectedNbResults):
57   """ Tests number of feature results.
58   :param theFeature: feature to test.
59   :param theExpectedNbResults: expected number of results.
60   """
61   aNbResults = len(theFeature.results())
62   assert (aNbResults == theExpectedNbResults), "Number of results: {}. Expected: {}.".format(aNbResults, theExpectedNbResults)
63
64
65 def testNbSubResults(theFeature, theExpectedNbSubResults):
66   """ Tests number of feature sub-results for each result.
67   :param theFeature: feature to test.
68   :param theExpectedNbSubResults: list of sub-results numbers. Size of list should be equal to len(theFeature.results()).
69   """
70   aNbResults = len(theFeature.results())
71   aListSize = len(theExpectedNbSubResults)
72   assert (aNbResults == aListSize), "Number of results: {} not equal to list size: {}.".format(aNbResults, aListSize)
73   for anIndex in range(0, aNbResults):
74     aNbSubResults = theFeature.results()[anIndex].numberOfSubs()
75     anExpectedNbSubResults = theExpectedNbSubResults[anIndex]
76     assert (aNbSubResults == anExpectedNbSubResults), "Number of sub-results for result[{}]: {}. Expected: {}.".format(anIndex, aNbSubResults, anExpectedNbSubResults)
77
78
79 def testNbSubShapes(theFeature, theShapeType, theExpectedNbSubShapes):
80   """ Tests number of feature sub-shapes of passed type for each result.
81   :param theFeature: feature to test.
82   :param theShapeType: shape type of sub-shapes to test.
83   :param theExpectedNbSubShapes: list of sub-shapes numbers. Size of list should be equal to len(theFeature.results()).
84   """
85   aNbResults = len(theFeature.results())
86   aListSize = len(theExpectedNbSubShapes)
87   assert (aNbResults == aListSize), "Number of results: {} not equal to list size: {}.".format(aNbResults, aListSize)
88   for anIndex in range(0, aNbResults):
89     aNbResultSubShapes = 0
90     anExpectedNbSubShapes = theExpectedNbSubShapes[anIndex]
91     aShape = theFeature.results()[anIndex].resultSubShapePair()[0].shape()
92     aShapeExplorer = GeomAPI_ShapeExplorer(aShape, theShapeType)
93     while aShapeExplorer.more():
94       aNbResultSubShapes += 1
95       aShapeExplorer.next();
96     assert (aNbResultSubShapes == anExpectedNbSubShapes), "Number of sub-shapes of type {} for result[{}]: {}. Expected: {}.".format(aShapeTypes[theShapeType], anIndex, aNbResultSubShapes, anExpectedNbSubShapes)
97
98
99 def testResultsVolumes(theFeature, theExpectedResultsVolumes, theNbSignificantDigits = 10):
100   """ Tests results volumes.
101   :param theFeature: feature to test.
102   :param theExpectedResultsVolumes: list of results volumes. Size of list should be equal to len(theFeature.results()).
103   """
104   aNbResults = len(theFeature.results())
105   aListSize = len(theExpectedResultsVolumes)
106   assert (aNbResults == aListSize), "Number of results: {} not equal to list size: {}.".format(aNbResults, aListSize)
107   for anIndex in range(0, aNbResults):
108     aResultVolume = GeomAlgoAPI_ShapeTools_volume(theFeature.results()[anIndex].resultSubShapePair()[0].shape())
109     aResultVolumeStr = "{:0.27f}".format(aResultVolume).lstrip("0").lstrip(".").lstrip("0")
110     anExpectedResultVolume = theExpectedResultsVolumes[anIndex]
111     anExpectedResultVolumeStr = "{:0.27f}".format(anExpectedResultVolume).lstrip("0").lstrip(".").lstrip("0")
112     assert (aResultVolumeStr[:theNbSignificantDigits] == anExpectedResultVolumeStr[:theNbSignificantDigits]), "Volume of result[{}]: {:0.27f}. Expected: {:0.27f}. The first {} significant digits not equal.".format(anIndex, aResultVolume, anExpectedResultVolume, theNbSignificantDigits)
113
114 def testHaveNamingFaces(theFeature, theModel, thePartDoc) :
115   """ Tests if all faces of result have a name
116   :param theFeature: feature to test.
117   """
118   # Get feature result/sub-result
119   aResult = theFeature.results()[0].resultSubShapePair()[0]
120   # Get result/sub-result shape
121   shape = aResult.shape()
122   # Create shape explorer with desired shape type
123   shapeExplorer = GeomAPI_ShapeExplorer(shape, GeomAPI_Shape.FACE)
124   # Create list, and store selections in it
125   selectionList = []
126   while shapeExplorer.more():
127     selection = theModel.selection(aResult, shapeExplorer.current()) # First argument should be result/sub-result, second is sub-shape on this result/sub-result
128     selectionList.append(selection)
129     shapeExplorer.next()
130   # Create group with this selection list
131   Group_1 = theModel.addGroup(thePartDoc, selectionList)
132   theModel.do()
133   theModel.end()
134
135   # Now you can check that all selected shapes in group have right shape type and name.
136   groupFeature = Group_1.feature()
137   groupSelectionList = groupFeature.selectionList("group_list")
138   theModel.end()
139   assert(groupSelectionList.size() == len(selectionList))
140   for index in range(0, groupSelectionList.size()):
141     attrSelection = groupSelectionList.value(index)
142     shape = attrSelection.value()
143     name = attrSelection.namingName()
144     assert(shape.isFace())
145     assert(name != ""), "String empty"
146
147 def testNbSubFeatures(theComposite, theKindOfSub, theExpectedCount):
148   """ Tests number of sub-features of the given type
149   :param theComposite     composite feature to check its subs
150   :param theKindOfSub     kind of sub-feature to calculate count
151   :param theExpectedCount expected number of sub-features
152   """
153   count = 0
154   for aSub in theComposite.features().list():
155     aFeature = ModelAPI_Feature.feature(aSub)
156     if aFeature is not None and aFeature.getKind() == theKindOfSub:
157        count += 1
158   assert (count == theExpectedCount), "Number of sub-features of type {}: {}, expected {}".format(theKindOfSub, count, theExpectedCount)
159
160 def assertSketchArc(theArcFeature):
161   """ Tests whether the arc is correctly defined
162   """
163   aCenterPnt = geomDataAPI_Point2D(theArcFeature.attribute("center_point"))
164   aStartPnt = geomDataAPI_Point2D(theArcFeature.attribute("start_point"))
165   aEndPnt = geomDataAPI_Point2D(theArcFeature.attribute("end_point"))
166   aRadius = theArcFeature.real("radius")
167   aDistCS = sketcher.tools.distancePointPoint(aCenterPnt, aStartPnt)
168   aDistCE = sketcher.tools.distancePointPoint(aCenterPnt, aEndPnt)
169   assert math.fabs(aDistCS - aDistCE) < TOLERANCE, "Wrong arc: center-start distance {}, center-end distance {}".format(aDistCS, aDistCE)
170   assert math.fabs(aRadius.value() -aDistCS) < TOLERANCE, "Wrong arc: radius is {0}, expected {1}".format(aRadius.value(), aDistCS)