From d4cf42a4c79b69af5d26d0c01449df1ed96df3a2 Mon Sep 17 00:00:00 2001 From: spo Date: Wed, 15 Jul 2015 11:48:40 +0300 Subject: [PATCH] Issue #644 - Parameters tests --- src/ParametersPlugin/CMakeLists.txt | 2 + .../Test/TestParameterChangeValue.py | 115 +++++++++++++++ .../Test/TestParameterRename.py | 138 ++++++++++++++++++ 3 files changed, 255 insertions(+) create mode 100644 src/ParametersPlugin/Test/TestParameterChangeValue.py create mode 100644 src/ParametersPlugin/Test/TestParameterRename.py diff --git a/src/ParametersPlugin/CMakeLists.txt b/src/ParametersPlugin/CMakeLists.txt index 82f7d25d6..39dd4aca9 100644 --- a/src/ParametersPlugin/CMakeLists.txt +++ b/src/ParametersPlugin/CMakeLists.txt @@ -51,4 +51,6 @@ INSTALL(TARGETS ParametersPlugin DESTINATION plugins) INSTALL(FILES ${XML_RESOURCES} DESTINATION plugins) ADD_UNIT_TESTS(TestParameterCreation.py + TestParameterRename.py + TestParameterChangeValue.py ) diff --git a/src/ParametersPlugin/Test/TestParameterChangeValue.py b/src/ParametersPlugin/Test/TestParameterChangeValue.py new file mode 100644 index 000000000..cfc99c262 --- /dev/null +++ b/src/ParametersPlugin/Test/TestParameterChangeValue.py @@ -0,0 +1,115 @@ +""" + TestParameterCreation.py + + class ParametersPlugin_Parameter + static const std::string MY_PARAMETER_ID("Parameter"); + static const std::string MY_VARIABLE_ID("variable"); + static const std::string MY_EXPRESSION_ID("expression"); + + data()->addAttribute(ParametersPlugin_Parameter::VARIABLE_ID(), + ModelAPI_AttributeString::typeId()); + data()->addAttribute(ParametersPlugin_Parameter::EXPRESSION_ID(), + ModelAPI_AttributeString::typeId()); + + class ModelAPI_ResultParameter + static const std::string MY_VALUE_ID("Value"); + static const std::string MY_VALUE_ID("State"); +""" + +#========================================================================= +# Initialization of the test +#========================================================================= +from GeomDataAPI import * +from ModelAPI import * +import math +import unittest + +__updated__ = "2015-04-27" + +#========================================================================= +# Create several parameters and a feature. +# 1. Basic parameter definition: +# x1 = 150.0, y1 = 200.0 +# 2. Referencing between parameters: +# x2 = x1 + y1 + 100.0 +# 3. Referencing in feature SketchCircle +# CircleCenter = (x1 + 10.0, x1 + 20.0), CircleRadius = x1 +# 3. Change value +# x1 = 200.0 +#========================================================================= +class TestParameterRename(unittest.TestCase): + def setUp(self): + self.aSession = ModelAPI_Session.get() + self.aDocument = self.aSession.moduleDocument() + self.createParameters() + self.createFeature() + + def tearDown(self): + self.aSession.closeAll() + + def createParameters(self): + ltNames = ["x1", "y1", "x2"] + ltExpressions = ["150.", "200.", "x1 + y1 + 100.0"] + self.dtParams = {} + self.aSession.startOperation() + for name, expr in zip(ltNames, ltExpressions): + aParam = self.aDocument.addFeature("Parameter") + aParamName = aParam.string("variable") + aParamName.setValue(name) + aParamExpr = aParam.string("expression") + aParamExpr.setValue(expr) + self.dtParams[name] = aParam + self.aSession.finishOperation() + self.assertEqual(len(self.dtParams), len(ltNames)) + + aParam = self.dtParams["x2"] + aResultAttr = modelAPI_ResultParameter(aParam.firstResult()) + self.assertEqual(aResultAttr.data().real("Value").value(), 450.) + + def createFeature(self): + self.aSession.startOperation() + aSketchCommonFeature = self.aDocument.addFeature("Sketch") + aSketchFeature = featureToCompositeFeature(aSketchCommonFeature) + origin = geomDataAPI_Point(aSketchFeature.attribute("Origin")) + origin.setValue(0, 0, 0) + dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX")) + dirx.setValue(1, 0, 0) + norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm")) + norm.setValue(0, 0, 1) + aSketchCircle = aSketchFeature.addFeature("SketchCircle") + anCircleCentr = geomDataAPI_Point2D(aSketchCircle.attribute("CircleCenter")) + aRadiusAttr = aSketchCircle.real("CircleRadius") + anCircleCentr.setText("x1 + 10.0", "x1 + 20.0") + aRadiusAttr.setText("x1") + self.aSession.finishOperation() + + self.anCircleCentr = anCircleCentr + self.aRadiusAttr = aRadiusAttr + + self.assertEqual(self.anCircleCentr.x(), 160.) + self.assertEqual(self.anCircleCentr.y(), 170.) + self.assertEqual(aRadiusAttr.value(), 150.) + + def test_change_value(self): + aParam = self.dtParams["x1"] + aResultAttr = modelAPI_ResultParameter(aParam.firstResult()) + self.aSession.startOperation() + aParam.string("expression").setValue("200.0") + self.aSession.finishOperation() + + # Check value of the parameter + self.assertEqual(aResultAttr.data().real("Value").value(), 200.) + # Check value of references + aParam = self.dtParams["x2"] + aResultAttr = modelAPI_ResultParameter(aParam.firstResult()) + self.assertEqual(aResultAttr.data().real("Value").value(), 500.) + # Check value in the feature + self.assertEqual(self.anCircleCentr.x(), 210.) + self.assertEqual(self.anCircleCentr.y(), 220.) + self.assertEqual(self.aRadiusAttr.value(), 200.) + +if __name__ == '__main__': + unittest.main() +#========================================================================= +# End of test +#========================================================================= diff --git a/src/ParametersPlugin/Test/TestParameterRename.py b/src/ParametersPlugin/Test/TestParameterRename.py new file mode 100644 index 000000000..ef8bd2e6b --- /dev/null +++ b/src/ParametersPlugin/Test/TestParameterRename.py @@ -0,0 +1,138 @@ +""" + TestParameterCreation.py + + class ParametersPlugin_Parameter + static const std::string MY_PARAMETER_ID("Parameter"); + static const std::string MY_VARIABLE_ID("variable"); + static const std::string MY_EXPRESSION_ID("expression"); + + data()->addAttribute(ParametersPlugin_Parameter::VARIABLE_ID(), + ModelAPI_AttributeString::typeId()); + data()->addAttribute(ParametersPlugin_Parameter::EXPRESSION_ID(), + ModelAPI_AttributeString::typeId()); + + class ModelAPI_ResultParameter + static const std::string MY_VALUE_ID("Value"); + static const std::string MY_VALUE_ID("State"); +""" + +#========================================================================= +# Initialization of the test +#========================================================================= +from GeomDataAPI import * +from ModelAPI import * +import math +import unittest + +__updated__ = "2015-04-27" + +#========================================================================= +# Create several parameters and a feature. +# 1. Basic parameter definition: +# x1 = 150.0, y1 = 200.0 +# 2. Referencing between parameters: +# x2 = x1 + y1 + 100.0 +# 3. Referencing in feature SketchCircle +# CircleCenter = (x1 + 10.0, x1 + 20.0), CircleRadius = x1 +# 3. Check renaming +# x1 -> a1 +# 4. Check renaming to not unique name +# x1 -> y1 +#========================================================================= +class TestParameterRename(unittest.TestCase): + def setUp(self): + self.aSession = ModelAPI_Session.get() + self.aDocument = self.aSession.moduleDocument() + self.createParameters() + self.createFeature() + + def tearDown(self): + self.aSession.closeAll() + + def createParameters(self): + ltNames = ["x1", "y1", "x2"] + ltExpressions = ["150.", "200.", "x1 + y1 + 100.0"] + self.dtParams = {} + self.aSession.startOperation() + for name, expr in zip(ltNames, ltExpressions): + aParam = self.aDocument.addFeature("Parameter") + aParamName = aParam.string("variable") + aParamName.setValue(name) + aParamExpr = aParam.string("expression") + aParamExpr.setValue(expr) + self.dtParams[name] = aParam + self.aSession.finishOperation() + self.assertEqual(len(self.dtParams), len(ltNames)) + + aParam = self.dtParams["x2"] + aResultAttr = modelAPI_ResultParameter(aParam.firstResult()) + self.assertEqual(aResultAttr.data().real("Value").value(), 450.) + + def createFeature(self): + self.aSession.startOperation() + aSketchCommonFeature = self.aDocument.addFeature("Sketch") + aSketchFeature = featureToCompositeFeature(aSketchCommonFeature) + origin = geomDataAPI_Point(aSketchFeature.attribute("Origin")) + origin.setValue(0, 0, 0) + dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX")) + dirx.setValue(1, 0, 0) + norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm")) + norm.setValue(0, 0, 1) + aSketchCircle = aSketchFeature.addFeature("SketchCircle") + anCircleCentr = geomDataAPI_Point2D(aSketchCircle.attribute("CircleCenter")) + aRadiusAttr = aSketchCircle.real("CircleRadius") + anCircleCentr.setText("x1 + 10.0", "x1 + 20.0") + aRadiusAttr.setText("x1") + self.aSession.finishOperation() + + self.anCircleCentr = anCircleCentr + self.aRadiusAttr = aRadiusAttr + + self.assertEqual(self.anCircleCentr.x(), 160.) + self.assertEqual(self.anCircleCentr.y(), 170.) + self.assertEqual(aRadiusAttr.value(), 150.) + + def test_rename(self): + # Rename + aParam = self.dtParams["x1"] + aResultAttr = modelAPI_ResultParameter(aParam.firstResult()) + self.aSession.startOperation() + aResultAttr.data().setName("a1") + self.aSession.finishOperation() + + # Check rename in the parameter + self.assertEqual(aParam.name(), "a1") + self.assertEqual(aParam.string("variable").value(), "a1") + self.assertEqual(aResultAttr.data().name(), "a1") + # Check rename in references + aParam = self.dtParams["x2"] + self.assertEqual(aParam.string("expression").value(), "a1 + y1 + 100.0") + # Check rename in the feature + self.assertEqual(self.anCircleCentr.textX(), "a1 + 10.0") + self.assertEqual(self.anCircleCentr.textY(), "a1 + 20.0") + self.assertEqual(self.aRadiusAttr.text(), "a1") + + def test_rename_not_unique(self): + # Rename to not unique name + aParam = self.dtParams["x1"] + aResultAttr = modelAPI_ResultParameter(aParam.firstResult()) + self.aSession.startOperation() + aResultAttr.data().setName("y1") + self.aSession.finishOperation() + # Check rename in the parameter (Expected: not renamed) + self.assertEqual(aParam.name(), "x1") + self.assertEqual(aParam.string("variable").value(), "x1") + self.assertEqual(aResultAttr.data().name(), "x1") + # Check rename in references (Expected: not renamed) + aParam = self.dtParams["x2"] + self.assertEqual(aParam.string("expression").value(), "x1 + y1 + 100.0") + # Check rename in the feature (Expected: not renamed) + self.assertEqual(self.anCircleCentr.textX(), "x1 + 10.0") + self.assertEqual(self.anCircleCentr.textY(), "x1 + 20.0") + self.assertEqual(self.aRadiusAttr.text(), "x1") + +if __name__ == '__main__': + unittest.main() +#========================================================================= +# End of test +#========================================================================= -- 2.39.2