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