def test_empty_singleton(self): a = Objective() a.construct() # # Even though we construct a SimpleObjective, # if it is not initialized that means it is "empty" # and we should encounter errors when trying to access the # _ObjectiveData interface methods until we assign # something to the objective. # self.assertEqual(a._constructed, True) self.assertEqual(len(a), 0) try: a() self.fail("Component is empty") except ValueError: pass try: a.expr self.fail("Component is empty") except ValueError: pass try: a.sense self.fail("Component is empty") except ValueError: pass x = Var(initialize=1.0) x.construct() a.set_value(x + 1) self.assertEqual(len(a), 1) self.assertEqual(a(), 2) self.assertEqual(a.expr(), 2) self.assertEqual(a.sense, minimize)
def test_empty_singleton(self): a = Constraint() a.construct() # # Even though we construct a SimpleConstraint, # if it is not initialized that means it is "empty" # and we should encounter errors when trying to access the # _ConstraintData interface methods until we assign # something to the constraint. # self.assertEqual(a._constructed, True) self.assertEqual(len(a), 0) try: a() self.fail("Component is empty") except ValueError: pass try: a.body self.fail("Component is empty") except ValueError: pass try: a.lower self.fail("Component is empty") except ValueError: pass try: a.upper self.fail("Component is empty") except ValueError: pass try: a.equality self.fail("Component is empty") except ValueError: pass try: a.strict_lower self.fail("Component is empty") except ValueError: pass try: a.strict_upper self.fail("Component is empty") except ValueError: pass x = Var(initialize=1.0) x.construct() a.set_value((0, x, 2)) self.assertEqual(len(a), 1) self.assertEqual(a(), 1) self.assertEqual(a.body(), 1) self.assertEqual(a.lower(), 0) self.assertEqual(a.upper(), 2) self.assertEqual(a.equality, False) self.assertEqual(a.strict_lower, False) self.assertEqual(a.strict_upper, False)
def test_unconstructed_singleton(self): a = Constraint() self.assertEqual(a._constructed, False) self.assertEqual(len(a), 0) try: a() self.fail("Component is unconstructed") except ValueError: pass try: a.body self.fail("Component is unconstructed") except ValueError: pass try: a.lower self.fail("Component is unconstructed") except ValueError: pass try: a.upper self.fail("Component is unconstructed") except ValueError: pass try: a.equality self.fail("Component is unconstructed") except ValueError: pass try: a.strict_lower self.fail("Component is unconstructed") except ValueError: pass try: a.strict_upper self.fail("Component is unconstructed") except ValueError: pass x = Var(initialize=1.0) x.construct() a.construct() a.set_value((0, x, 2)) self.assertEqual(len(a), 1) self.assertEqual(a(), 1) self.assertEqual(a.body(), 1) self.assertEqual(a.lower(), 0) self.assertEqual(a.upper(), 2) self.assertEqual(a.equality, False) self.assertEqual(a.strict_lower, False) self.assertEqual(a.strict_upper, False)
def test_unconstructed_singleton(self): a = Constraint() self.assertEqual(a._constructed, False) self.assertEqual(len(a), 0) with self.assertRaisesRegex( RuntimeError, "Cannot access .* on AbstractSimpleConstraint" ".*before it has been constructed"): a() with self.assertRaisesRegex( RuntimeError, "Cannot access .* on AbstractSimpleConstraint" ".*before it has been constructed"): a.body with self.assertRaisesRegex( RuntimeError, "Cannot access .* on AbstractSimpleConstraint" ".*before it has been constructed"): a.lower with self.assertRaisesRegex( RuntimeError, "Cannot access .* on AbstractSimpleConstraint" ".*before it has been constructed"): a.upper with self.assertRaisesRegex( RuntimeError, "Cannot access .* on AbstractSimpleConstraint" ".*before it has been constructed"): a.equality with self.assertRaisesRegex( RuntimeError, "Cannot access .* on AbstractSimpleConstraint" ".*before it has been constructed"): a.strict_lower with self.assertRaisesRegex( RuntimeError, "Cannot access .* on AbstractSimpleConstraint" ".*before it has been constructed"): a.strict_upper x = Var(initialize=1.0) x.construct() a.construct() a.set_value((0, x, 2)) self.assertEqual(len(a), 1) self.assertEqual(a(), 1) self.assertEqual(a.body(), 1) self.assertEqual(a.lower(), 0) self.assertEqual(a.upper(), 2) self.assertEqual(a.equality, False) self.assertEqual(a.strict_lower, False) self.assertEqual(a.strict_upper, False)