Example #1
0
class ChoiceParameterTest(TestCase):
    def setUp(self):
        self.param1 = ChoiceParameter(name="x",
                                      parameter_type=ParameterType.STRING,
                                      values=["foo", "bar", "baz"])
        self.param1_repr = ("ChoiceParameter(name='x', parameter_type=STRING, "
                            "values=['foo', 'bar', 'baz'])")
        self.param2 = ChoiceParameter(
            name="x",
            parameter_type=ParameterType.STRING,
            values=["foo", "bar", "baz"],
            is_ordered=True,
            is_task=True,
        )
        self.param3 = ChoiceParameter(
            name="x",
            parameter_type=ParameterType.STRING,
            values=["foo", "bar"],
            is_fidelity=True,
            target_value="bar",
        )
        self.param3_repr = (
            "ChoiceParameter(name='x', parameter_type=STRING, "
            "values=['foo', 'bar'], fidelity=True, target_value='bar')")

    def testBadCreations(self):
        with self.assertRaises(UserInputError):
            ChoiceParameter(
                name="x",
                parameter_type=ParameterType.STRING,
                values=["foo", "foo2"],
                is_fidelity=True,
            )

    def testEq(self):
        param4 = ChoiceParameter(name="x",
                                 parameter_type=ParameterType.STRING,
                                 values=["foo", "bar", "baz"])
        self.assertEqual(self.param1, param4)
        self.assertNotEqual(self.param1, self.param2)

        param5 = ChoiceParameter(name="x",
                                 parameter_type=ParameterType.STRING,
                                 values=["foo", "foobar"])
        self.assertNotEqual(self.param1, param5)

    def testProperties(self):
        self.assertEqual(self.param1.name, "x")
        self.assertEqual(self.param1.parameter_type, ParameterType.STRING)
        self.assertEqual(len(self.param1.values), 3)
        self.assertFalse(self.param1.is_numeric)
        self.assertFalse(self.param1.is_ordered)
        self.assertFalse(self.param1.is_task)
        self.assertTrue(self.param2.is_ordered)
        self.assertTrue(self.param2.is_task)

    def testRepr(self):
        self.assertEqual(str(self.param1), self.param1_repr)
        self.assertEqual(str(self.param3), self.param3_repr)

    def testValidate(self):
        self.assertFalse(self.param1.validate(None))
        self.assertFalse(self.param1.validate(3))
        for value in ["foo", "bar", "baz"]:
            self.assertTrue(self.param1.validate(value))

    def testSetter(self):
        self.param1.add_values(["bin"])
        self.assertTrue(self.param1.validate("bin"))

        self.param1.set_values(["bar", "biz"])
        self.assertTrue(self.param1.validate("biz"))
        self.assertTrue(self.param1.validate("bar"))
        self.assertFalse(self.param1.validate("foo"))

    def testSingleValue(self):
        with self.assertRaises(UserInputError):
            ChoiceParameter(name="x",
                            parameter_type=ParameterType.STRING,
                            values=["foo"])
        with self.assertRaises(UserInputError):
            self.param1.set_values(["foo"])

    def testClone(self):
        param_clone = self.param1.clone()
        self.assertEqual(len(self.param1.values), len(param_clone.values))
        self.assertEqual(self.param1._is_ordered, param_clone._is_ordered)

        param_clone._values.append("boo")
        self.assertNotEqual(len(self.param1.values), len(param_clone.values))
Example #2
0
class ChoiceParameterTest(TestCase):
    def setUp(self):
        self.param1 = ChoiceParameter(name="x",
                                      parameter_type=ParameterType.STRING,
                                      values=["foo", "bar", "baz"])
        self.param1_repr = (
            "ChoiceParameter(name='x', parameter_type=STRING, "
            "values=['foo', 'bar', 'baz'], is_ordered=False, sort_values=False)"
        )
        self.param2 = ChoiceParameter(
            name="x",
            parameter_type=ParameterType.STRING,
            values=["foo", "bar", "baz"],
            is_ordered=True,
            is_task=True,
        )
        self.param3 = ChoiceParameter(
            name="x",
            parameter_type=ParameterType.STRING,
            values=["foo", "bar"],
            is_fidelity=True,
            target_value="bar",
        )
        self.param3_repr = (
            "ChoiceParameter(name='x', parameter_type=STRING, "
            "values=['foo', 'bar'], is_ordered=False, sort_values=False, "
            "is_fidelity=True, target_value='bar')")
        self.param4 = ChoiceParameter(
            name="x",
            parameter_type=ParameterType.INT,
            values=[1, 2],
        )
        self.param4_repr = (
            "ChoiceParameter(name='x', parameter_type=INT, "
            "values=[1, 2], is_ordered=True, sort_values=True)")

    def testBadCreations(self):
        with self.assertRaises(UserInputError):
            ChoiceParameter(
                name="x",
                parameter_type=ParameterType.STRING,
                values=["foo", "foo2"],
                is_fidelity=True,
            )

    def testEq(self):
        param4 = ChoiceParameter(name="x",
                                 parameter_type=ParameterType.STRING,
                                 values=["foo", "bar", "baz"])
        self.assertEqual(self.param1, param4)
        self.assertNotEqual(self.param1, self.param2)

        param5 = ChoiceParameter(name="x",
                                 parameter_type=ParameterType.STRING,
                                 values=["foo", "foobar"])
        self.assertNotEqual(self.param1, param5)

    def testProperties(self):
        self.assertEqual(self.param1.name, "x")
        self.assertEqual(self.param1.parameter_type, ParameterType.STRING)
        self.assertEqual(len(self.param1.values), 3)
        self.assertFalse(self.param1.is_numeric)
        self.assertFalse(self.param1.is_ordered)
        self.assertFalse(self.param1.is_task)
        self.assertTrue(self.param2.is_ordered)
        self.assertTrue(self.param2.is_task)
        # check is_ordered defaults
        bool_param = ChoiceParameter(name="x",
                                     parameter_type=ParameterType.BOOL,
                                     values=[True, False])
        self.assertTrue(bool_param.is_ordered)
        int_param = ChoiceParameter(name="x",
                                    parameter_type=ParameterType.INT,
                                    values=[2, 1, 3])
        self.assertTrue(int_param.is_ordered)
        self.assertListEqual(int_param.values, sorted(int_param.values))
        float_param = ChoiceParameter(name="x",
                                      parameter_type=ParameterType.FLOAT,
                                      values=[1.5, 2.5, 3.5])
        self.assertTrue(float_param.is_ordered)
        string_param = ChoiceParameter(name="x",
                                       parameter_type=ParameterType.STRING,
                                       values=["foo", "bar", "baz"])
        self.assertFalse(string_param.is_ordered)

    def testRepr(self):
        self.assertEqual(str(self.param1), self.param1_repr)
        self.assertEqual(str(self.param3), self.param3_repr)
        self.assertEqual(str(self.param4), self.param4_repr)

    def testValidate(self):
        self.assertFalse(self.param1.validate(None))
        self.assertFalse(self.param1.validate(3))
        for value in ["foo", "bar", "baz"]:
            self.assertTrue(self.param1.validate(value))

    def testSetter(self):
        self.param1.add_values(["bin"])
        self.assertTrue(self.param1.validate("bin"))

        self.param1.set_values(["bar", "biz"])
        self.assertTrue(self.param1.validate("biz"))
        self.assertTrue(self.param1.validate("bar"))
        self.assertFalse(self.param1.validate("foo"))

    def testSingleValue(self):
        with self.assertRaises(UserInputError):
            ChoiceParameter(name="x",
                            parameter_type=ParameterType.STRING,
                            values=["foo"])
        with self.assertRaises(UserInputError):
            self.param1.set_values(["foo"])

    def testClone(self):
        param_clone = self.param1.clone()
        self.assertEqual(len(self.param1.values), len(param_clone.values))
        self.assertEqual(self.param1._is_ordered, param_clone._is_ordered)

        param_clone._values.append("boo")
        self.assertNotEqual(len(self.param1.values), len(param_clone.values))

    def testHierarchicalValidation(self):
        self.assertFalse(self.param1.is_hierarchical)
        with self.assertRaises(NotImplementedError):
            self.param1.dependents
        with self.assertRaises(UserInputError):
            ChoiceParameter(
                name="x",
                parameter_type=ParameterType.BOOL,
                values=[True, False],
                dependents={"not_a_value": "other_param"},
            )

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

        # Test case where all of the values entail dependents.
        hierarchical_param_2 = ChoiceParameter(
            name="x",
            parameter_type=ParameterType.STRING,
            values=["a", "b"],
            dependents={
                "a": "other_param",
                "b": "third_param"
            },
        )
        self.assertTrue(hierarchical_param_2.is_hierarchical)
        self.assertEqual(hierarchical_param_2.dependents, {
            "a": "other_param",
            "b": "third_param"
        })

        # Test case where nonexisted value entails dependents.
        with self.assertRaises(UserInputError):
            ChoiceParameter(
                name="x",
                parameter_type=ParameterType.STRING,
                values=["a", "b"],
                dependents={"c": "other_param"},
            )