Salome HOME
Merge branch 'master' into cgt/devCEA
[modules/shaper.git] / src / PythonAPI / model / tests / tests.py
1 from GeomAlgoAPI import *
2 import math
3
4
5 def generateTests(theFeature, theFeatureName, theTestsList = []):
6   """ Generates tests for theFeature.
7   :param theFeature: feature to test. Should be ModelHighAPI_Interface.
8   :param theFeatureName: feature name to put in test commands.
9   :param theTestsList: list of test to be generated. If empty generates all tests.
10   """
11   if "testNbResults" in theTestsList or len(theTestsList) == 0:
12     aNbResults = len(theFeature.results())
13     print "model.testNbResults({}, {})".format(theFeatureName, aNbResults)
14
15   if "testNbSubResults" in theTestsList or len(theTestsList) == 0:
16     aNbResults = len(theFeature.results())
17     aNbSubResults = []
18     for anIndex in range(0, aNbResults):
19       aNbSubResults.append(theFeature.results()[anIndex].numberOfSubs())
20     print "model.testNbSubResults({}, {})".format(theFeatureName, aNbSubResults)
21
22   if "testResultsVolumes" in theTestsList or len(theTestsList) == 0:
23     aNbResults = len(theFeature.results())
24     aResultsVolumes = []
25     for anIndex in range(0, aNbResults):
26       aResultsVolumes.append(GeomAlgoAPI_ShapeTools_volume(theFeature.results()[anIndex].resultSubShapePair()[0].shape()))
27     print "model.testResultsVolumes({}, [{}])".format(theFeatureName, ", ".join("{:0.27f}".format(i) for i in aResultsVolumes))
28
29
30 def testNbResults(theFeature, theExpectedNbResults):
31   """ Tests number of feature results.
32   :param theFeature: feature to test.
33   :param theExpectedNbResults: expected number of results.
34   """
35   aNbResults = len(theFeature.results())
36   assert (aNbResults == theExpectedNbResults), "Number of results: {}. Expected: {}.".format(aNbResults, theExpectedNbResults)
37
38
39 def testNbSubResults(theFeature, theExpectedNbSubResults):
40   """ Tests number of feature sub-results for each result.
41   :param theFeature: feature to test.
42   :param theExpectedNbSubResults: list of sub-results numbers. Size of list should be equal to len(theFeature.results()).
43   """
44   aNbResults = len(theFeature.results())
45   aListSize = len(theExpectedNbSubResults)
46   assert (aNbResults == aListSize), "Number of results: {} not equal to list size: {}.".format(aNbResults, aListSize)
47   for anIndex in range(0, aNbResults):
48     aNbSubResults = theFeature.results()[anIndex].numberOfSubs()
49     anExpectedNbSubResults = theExpectedNbSubResults[anIndex]
50     assert (aNbSubResults == anExpectedNbSubResults), "Number of sub-results for result[{}]: {}. Expected: {}.".format(anIndex, aNbSubResults, anExpectedNbSubResults)
51
52
53 def testResultsVolumes(theFeature, theExpectedResultsVolumes, theNbSignificantDigits = 10):
54   """ Tests results volumes.
55   :param theFeature: feature to test.
56   :param theExpectedResultsVolumes: list of results volumes. Size of list should be equal to len(theFeature.results()).
57   """
58   aNbResults = len(theFeature.results())
59   aListSize = len(theExpectedResultsVolumes)
60   assert (aNbResults == aListSize), "Number of results: {} not equal to list size: {}.".format(aNbResults, aListSize)
61   for anIndex in range(0, aNbResults):
62     aResultVolume = GeomAlgoAPI_ShapeTools_volume(theFeature.results()[anIndex].resultSubShapePair()[0].shape())
63     aResultVolumeStr = "{:0.27f}".format(aResultVolume).lstrip("0").lstrip(".").lstrip("0")
64     anExpectedResultVolume = theExpectedResultsVolumes[anIndex]
65     anExpectedResultVolumeStr = "{:0.27f}".format(anExpectedResultVolume).lstrip("0").lstrip(".").lstrip("0")
66     assert (aResultVolumeStr[:theNbSignificantDigits] == anExpectedResultVolumeStr[:theNbSignificantDigits]), "Volume of result[{}]: {:0.27f}. Expected: {:0.27f}. The first {} significant digits not equal.".format(anIndex, aResultVolume, anExpectedResultVolume, theNbSignificantDigits)