Esempio n. 1
0
class FixedParameterTest(TestCase):
    def setUp(self):
        self.param1 = FixedParameter(name="x",
                                     parameter_type=ParameterType.BOOL,
                                     value=True)
        self.param1_repr = "FixedParameter(name='x', parameter_type=BOOL, value=True)"

    def testBadCreations(self):
        with self.assertRaises(UserInputError):
            FixedParameter(
                name="x",
                parameter_type=ParameterType.BOOL,
                value=True,
                is_fidelity=True,
            )

    def testEq(self):
        param2 = FixedParameter(name="x",
                                parameter_type=ParameterType.BOOL,
                                value=True)
        self.assertEqual(self.param1, param2)

        param3 = FixedParameter(name="x",
                                parameter_type=ParameterType.BOOL,
                                value=False)
        self.assertNotEqual(self.param1, param3)

    def testProperties(self):
        self.assertEqual(self.param1.name, "x")
        self.assertEqual(self.param1.parameter_type, ParameterType.BOOL)
        self.assertEqual(self.param1.value, True)
        self.assertFalse(self.param1.is_numeric)

    def testRepr(self):
        self.assertEqual(str(self.param1), self.param1_repr)
        self.param1._is_fidelity = True
        self.assertNotEqual(str(self.param1), self.param1_repr)

    def testValidate(self):
        self.assertFalse(self.param1.validate(None))
        self.assertFalse(self.param1.validate("foo"))
        self.assertFalse(self.param1.validate(False))
        self.assertTrue(self.param1.validate(True))

    def testSetter(self):
        self.param1.set_value(False)
        self.assertEqual(self.param1.value, False)

    def testClone(self):
        param_clone = self.param1.clone()
        self.assertEqual(self.param1.value, param_clone.value)

        param_clone._value = False
        self.assertNotEqual(self.param1.value, param_clone.value)

    def testCast(self):
        self.assertEqual(self.param1.cast(1), True)
        self.assertEqual(self.param1.cast(False), False)
        self.assertEqual(self.param1.cast(None), None)
Esempio n. 2
0
class FixedParameterTest(TestCase):
    def setUp(self):
        self.param1 = FixedParameter(name="x",
                                     parameter_type=ParameterType.BOOL,
                                     value=True)
        self.param1_repr = "FixedParameter(name='x', parameter_type=BOOL, value=True)"

    def testBadCreations(self):
        with self.assertRaises(UserInputError):
            FixedParameter(
                name="x",
                parameter_type=ParameterType.BOOL,
                value=True,
                is_fidelity=True,
            )

    def testEq(self):
        param2 = FixedParameter(name="x",
                                parameter_type=ParameterType.BOOL,
                                value=True)
        self.assertEqual(self.param1, param2)

        param3 = FixedParameter(name="x",
                                parameter_type=ParameterType.BOOL,
                                value=False)
        self.assertNotEqual(self.param1, param3)

    def testProperties(self):
        self.assertEqual(self.param1.name, "x")
        self.assertEqual(self.param1.parameter_type, ParameterType.BOOL)
        self.assertEqual(self.param1.value, True)
        self.assertFalse(self.param1.is_numeric)

    def testRepr(self):
        self.assertEqual(str(self.param1), self.param1_repr)
        self.param1._is_fidelity = True
        self.assertNotEqual(str(self.param1), self.param1_repr)

    def testValidate(self):
        self.assertFalse(self.param1.validate(None))
        self.assertFalse(self.param1.validate("foo"))
        self.assertFalse(self.param1.validate(False))
        self.assertTrue(self.param1.validate(True))

    def testSetter(self):
        self.param1.set_value(False)
        self.assertEqual(self.param1.value, False)

    def testClone(self):
        param_clone = self.param1.clone()
        self.assertEqual(self.param1.value, param_clone.value)

        param_clone._value = False
        self.assertNotEqual(self.param1.value, param_clone.value)

    def testCast(self):
        self.assertEqual(self.param1.cast(1), True)
        self.assertEqual(self.param1.cast(False), False)
        self.assertEqual(self.param1.cast(None), None)

    def testHierarchicalValidation(self):
        self.assertFalse(self.param1.is_hierarchical)
        with self.assertRaises(NotImplementedError):
            self.param1.dependents

    def testHierarchical(self):
        # Test case where only some of the values entail dependents.
        hierarchical_param = FixedParameter(
            name="x",
            parameter_type=ParameterType.BOOL,
            value=True,
            dependents={True: "other_param"},
        )
        self.assertTrue(hierarchical_param.is_hierarchical)
        self.assertEqual(hierarchical_param.dependents, {True: "other_param"})

        # Test case where nonexistent value entails dependents.
        with self.assertRaises(UserInputError):
            FixedParameter(
                name="x",
                parameter_type=ParameterType.BOOL,
                value=True,
                dependents={False: "other_param"},
            )