Salome HOME
Issue #2024: Redesign of circle and arc of circle: Unit tests for creation of a circl...
[modules/shaper.git] / src / PythonAPI / model / tests / tests.py
1 from GeomAlgoAPI import *
2 from GeomAPI import *
3 from ModelAPI import ModelAPI_Feature
4 import math
5
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 = 10):
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   aNbResults = len(theFeature.results())
102   aListSize = len(theExpectedResultsVolumes)
103   assert (aNbResults == aListSize), "Number of results: {} not equal to list size: {}.".format(aNbResults, aListSize)
104   for anIndex in range(0, aNbResults):
105     aResultVolume = GeomAlgoAPI_ShapeTools_volume(theFeature.results()[anIndex].resultSubShapePair()[0].shape())
106     aResultVolumeStr = "{:0.27f}".format(aResultVolume).lstrip("0").lstrip(".").lstrip("0")
107     anExpectedResultVolume = theExpectedResultsVolumes[anIndex]
108     anExpectedResultVolumeStr = "{:0.27f}".format(anExpectedResultVolume).lstrip("0").lstrip(".").lstrip("0")
109     assert (aResultVolumeStr[:theNbSignificantDigits] == anExpectedResultVolumeStr[:theNbSignificantDigits]), "Volume of result[{}]: {:0.27f}. Expected: {:0.27f}. The first {} significant digits not equal.".format(anIndex, aResultVolume, anExpectedResultVolume, theNbSignificantDigits)
110
111 def testHaveNamingFaces(theFeature, theModel, thePartDoc) :
112   """ Tests if all faces of result have a name
113   :param theFeature: feature to test.
114   """
115   # Get feature result/sub-result
116   aResult = theFeature.results()[0].resultSubShapePair()[0]
117   # Get result/sub-result shape
118   shape = aResult.shape()
119   # Create shape explorer with desired shape type
120   shapeExplorer = GeomAPI_ShapeExplorer(shape, GeomAPI_Shape.FACE)
121   # Create list, and store selections in it
122   selectionList = []
123   while shapeExplorer.more():
124     selection = theModel.selection(aResult, shapeExplorer.current()) # First argument should be result/sub-result, second is sub-shape on this result/sub-result
125     selectionList.append(selection)
126     shapeExplorer.next()
127   # Create group with this selection list
128   Group_1 = theModel.addGroup(thePartDoc, selectionList)
129   theModel.do()
130   theModel.end()
131
132   # Now you can check that all selected shapes in group have right shape type and name.
133   groupFeature = Group_1.feature()
134   groupSelectionList = groupFeature.selectionList("group_list")
135   theModel.end()
136   assert(groupSelectionList.size() == len(selectionList))
137   for index in range(0, groupSelectionList.size()):
138     attrSelection = groupSelectionList.value(index)
139     shape = attrSelection.value()
140     name = attrSelection.namingName()
141     assert(shape.isFace())
142     assert(name != ""), "String empty"
143
144 def testNbSubFeatures(theComposite, theKindOfSub, theExpectedCount):
145   """ Tests number of sub-features of the given type
146   :param theComposite     composite feature to check its subs
147   :param theKindOfSub     kind of sub-feature to calculate count
148   :param theExpectedCount expected number of sub-features
149   """
150   count = 0
151   for aSub in theComposite.features().list():
152     aFeature = ModelAPI_Feature.feature(aSub)
153     if aFeature is not None and aFeature.getKind() == theKindOfSub:
154        count += 1
155   assert (count == theExpectedCount), "Number of sub-features of type {}: {}, expected {}".format(theKindOfSub, count, theExpectedCount)