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