Salome HOME
Adjust test cases according to updated Arc behavior
[modules/shaper.git] / src / PythonAPI / model / tests / tests.py
index cb2ce66a2ab9c4f8adc6f2d747d436a165d9d5bc..2cc13febd619db92e70a9ada4c5fa50429de9116 100644 (file)
@@ -1,5 +1,17 @@
 from GeomAlgoAPI import *
+from GeomAPI import *
+from GeomDataAPI import *
+from ModelAPI import ModelAPI_Feature
 import math
+from salome.shaper.model import sketcher
+
+TOLERANCE = 1.e-7
+
+aShapeTypes = {
+  GeomAPI_Shape.SOLID:  "GeomAPI_Shape.SOLID",
+  GeomAPI_Shape.FACE:   "GeomAPI_Shape.FACE",
+  GeomAPI_Shape.EDGE:   "GeomAPI_Shape.EDGE",
+  GeomAPI_Shape.VERTEX: "GeomAPI_Shape.VERTEX"}
 
 
 def generateTests(theFeature, theFeatureName, theTestsList = []):
@@ -19,6 +31,20 @@ def generateTests(theFeature, theFeatureName, theTestsList = []):
       aNbSubResults.append(theFeature.results()[anIndex].numberOfSubs())
     print "model.testNbSubResults({}, {})".format(theFeatureName, aNbSubResults)
 
+  if "testNbSubShapes" in theTestsList or len(theTestsList) == 0:
+    aNbResults = len(theFeature.results())
+    for aShapeType in aShapeTypes:
+      aNbSubShapes = []
+      for anIndex in range(0, aNbResults):
+        aShape = theFeature.results()[anIndex].resultSubShapePair()[0].shape()
+        aNbResultSubShapes = 0
+        aShapeExplorer = GeomAPI_ShapeExplorer(aShape, aShapeType)
+        while aShapeExplorer.more():
+          aNbResultSubShapes += 1
+          aShapeExplorer.next();
+        aNbSubShapes.append(aNbResultSubShapes)
+      print "model.testNbSubShapes({}, {}, {})".format(theFeatureName, aShapeTypes[aShapeType], aNbSubShapes)
+
   if "testResultsVolumes" in theTestsList or len(theTestsList) == 0:
     aNbResults = len(theFeature.results())
     aResultsVolumes = []
@@ -50,6 +76,26 @@ def testNbSubResults(theFeature, theExpectedNbSubResults):
     assert (aNbSubResults == anExpectedNbSubResults), "Number of sub-results for result[{}]: {}. Expected: {}.".format(anIndex, aNbSubResults, anExpectedNbSubResults)
 
 
+def testNbSubShapes(theFeature, theShapeType, theExpectedNbSubShapes):
+  """ Tests number of feature sub-shapes of passed type for each result.
+  :param theFeature: feature to test.
+  :param theShapeType: shape type of sub-shapes to test.
+  :param theExpectedNbSubShapes: list of sub-shapes numbers. Size of list should be equal to len(theFeature.results()).
+  """
+  aNbResults = len(theFeature.results())
+  aListSize = len(theExpectedNbSubShapes)
+  assert (aNbResults == aListSize), "Number of results: {} not equal to list size: {}.".format(aNbResults, aListSize)
+  for anIndex in range(0, aNbResults):
+    aNbResultSubShapes = 0
+    anExpectedNbSubShapes = theExpectedNbSubShapes[anIndex]
+    aShape = theFeature.results()[anIndex].resultSubShapePair()[0].shape()
+    aShapeExplorer = GeomAPI_ShapeExplorer(aShape, theShapeType)
+    while aShapeExplorer.more():
+      aNbResultSubShapes += 1
+      aShapeExplorer.next();
+    assert (aNbResultSubShapes == anExpectedNbSubShapes), "Number of sub-shapes of type {} for result[{}]: {}. Expected: {}.".format(aShapeTypes[theShapeType], anIndex, aNbResultSubShapes, anExpectedNbSubShapes)
+
+
 def testResultsVolumes(theFeature, theExpectedResultsVolumes, theNbSignificantDigits = 10):
   """ Tests results volumes.
   :param theFeature: feature to test.
@@ -64,3 +110,61 @@ def testResultsVolumes(theFeature, theExpectedResultsVolumes, theNbSignificantDi
     anExpectedResultVolume = theExpectedResultsVolumes[anIndex]
     anExpectedResultVolumeStr = "{:0.27f}".format(anExpectedResultVolume).lstrip("0").lstrip(".").lstrip("0")
     assert (aResultVolumeStr[:theNbSignificantDigits] == anExpectedResultVolumeStr[:theNbSignificantDigits]), "Volume of result[{}]: {:0.27f}. Expected: {:0.27f}. The first {} significant digits not equal.".format(anIndex, aResultVolume, anExpectedResultVolume, theNbSignificantDigits)
+
+def testHaveNamingFaces(theFeature, theModel, thePartDoc) :
+  """ Tests if all faces of result have a name
+  :param theFeature: feature to test.
+  """
+  # Get feature result/sub-result
+  aResult = theFeature.results()[0].resultSubShapePair()[0]
+  # Get result/sub-result shape
+  shape = aResult.shape()
+  # Create shape explorer with desired shape type
+  shapeExplorer = GeomAPI_ShapeExplorer(shape, GeomAPI_Shape.FACE)
+  # Create list, and store selections in it
+  selectionList = []
+  while shapeExplorer.more():
+    selection = theModel.selection(aResult, shapeExplorer.current()) # First argument should be result/sub-result, second is sub-shape on this result/sub-result
+    selectionList.append(selection)
+    shapeExplorer.next()
+  # Create group with this selection list
+  Group_1 = theModel.addGroup(thePartDoc, selectionList)
+  theModel.do()
+  theModel.end()
+
+  # Now you can check that all selected shapes in group have right shape type and name.
+  groupFeature = Group_1.feature()
+  groupSelectionList = groupFeature.selectionList("group_list")
+  theModel.end()
+  assert(groupSelectionList.size() == len(selectionList))
+  for index in range(0, groupSelectionList.size()):
+    attrSelection = groupSelectionList.value(index)
+    shape = attrSelection.value()
+    name = attrSelection.namingName()
+    assert(shape.isFace())
+    assert(name != ""), "String empty"
+
+def testNbSubFeatures(theComposite, theKindOfSub, theExpectedCount):
+  """ Tests number of sub-features of the given type
+  :param theComposite     composite feature to check its subs
+  :param theKindOfSub     kind of sub-feature to calculate count
+  :param theExpectedCount expected number of sub-features
+  """
+  count = 0
+  for aSub in theComposite.features().list():
+    aFeature = ModelAPI_Feature.feature(aSub)
+    if aFeature is not None and aFeature.getKind() == theKindOfSub:
+       count += 1
+  assert (count == theExpectedCount), "Number of sub-features of type {}: {}, expected {}".format(theKindOfSub, count, theExpectedCount)
+
+def assertSketchArc(theArcFeature):
+  """ Tests whether the arc is correctly defined
+  """
+  aCenterPnt = geomDataAPI_Point2D(theArcFeature.attribute("center_point"))
+  aStartPnt = geomDataAPI_Point2D(theArcFeature.attribute("start_point"))
+  aEndPnt = geomDataAPI_Point2D(theArcFeature.attribute("end_point"))
+  aRadius = theArcFeature.real("radius")
+  aDistCS = sketcher.tools.distancePointPoint(aCenterPnt, aStartPnt)
+  aDistCE = sketcher.tools.distancePointPoint(aCenterPnt, aEndPnt)
+  assert math.fabs(aDistCS - aDistCE) < TOLERANCE, "Wrong arc: center-start distance {}, center-end distance {}".format(aDistCS, aDistCE)
+  assert math.fabs(aRadius.value() -aDistCS) < TOLERANCE, "Wrong arc: radius is {0}, expected {1}".format(aRadius.value(), aDistCS)