def test_is_fixed(self): f = functional_value() self.assertEqual(f.is_fixed(), True) self.assertEqual(is_fixed(f), True) f.fn = lambda: 2 self.assertEqual(f.is_fixed(), True) self.assertEqual(is_fixed(f), True)
def test_call(self): f = functional_value() self.assertEqual(f(), None) self.assertIs(f.fn, None) f.fn = lambda: variable(value=1) self.assertIsNot(f.fn, None) # the function did not return numeric data # (always causes an exception) with self.assertRaises(TypeError): f(exception=False) with self.assertRaises(TypeError): f(exception=True) with self.assertRaises(TypeError): f() f.fn = lambda: None self.assertIsNot(f.fn, None) # the function did not return numeric data # (always causes an exception) with self.assertRaises(TypeError): f(exception=False) with self.assertRaises(TypeError): f(exception=True) with self.assertRaises(TypeError): f() def value_error(): raise ValueError() f.fn = value_error self.assertIsNot(f.fn, None) self.assertEqual(f(exception=False), None) with self.assertRaises(ValueError): f(exception=True) with self.assertRaises(ValueError): f()
def test_is_constant(self): f = functional_value() self.assertEqual(f.is_constant(), False) self.assertEqual(is_constant(f), False) f.fn = lambda: 2 self.assertEqual(f.is_constant(), False) self.assertEqual(is_constant(f), False)
def test_potentially_variable(self): f = functional_value() self.assertEqual(f.is_potentially_variable(), False) self.assertEqual(is_potentially_variable(f), False) f.fn = lambda: 2 self.assertEqual(f.is_potentially_variable(), False) self.assertEqual(is_potentially_variable(f), False)
def test_init(self): f = functional_value() self.assertTrue(f.parent is None) self.assertEqual(f.ctype, IParameter) self.assertEqual(f.fn, None) self.assertEqual(f(), None) x = [1, 2] f.fn = lambda: max(x) self.assertEqual(f(), 2) x[0] = 3 self.assertEqual(f(), 3)
def test_init(self): f = functional_value() self.assertTrue(f.parent is None) self.assertEqual(f.ctype, IParameter) self.assertEqual(f.fn, None) self.assertEqual(f(), None) x = [1,2] f.fn = lambda: max(x) self.assertEqual(f(), 2) x[0] = 3 self.assertEqual(f(), 3)
def test_polynomial_degree(self): f = functional_value() self.assertEqual(f.polynomial_degree(), 0) self.assertEqual((f**2).polynomial_degree(), 0) self.assertIs(f.fn, None) with self.assertRaises(ValueError): (f**2)() f.fn = lambda: 2 self.assertEqual(f.polynomial_degree(), 0) self.assertEqual((f**2).polynomial_degree(), 0) self.assertEqual(f(), 2) self.assertEqual((f**2)(), 4)
def test_pickle(self): f = functional_value() self.assertIs(f.fn, None) self.assertIs(f.parent, None) fup = pickle.loads(pickle.dumps(f)) self.assertIs(fup.fn, None) self.assertIs(fup.parent, None) b = block() b.f = f self.assertIs(f.parent, b) bup = pickle.loads(pickle.dumps(b)) fup = bup.f self.assertIs(fup.fn, None) self.assertIs(fup.parent, bup)
def test_pprint(self): # Not really testing what the output is, just that # an error does not occur. The pprint functionality # is still in the early stages. f = functional_value() pprint(f) b = block() b.f = f pprint(f) pprint(b) m = block() m.b = b pprint(f) pprint(b) pprint(m)
def test_pickle(self): f = functional_value() self.assertIs(f.fn, None) self.assertIs(f.parent, None) fup = pickle.loads( pickle.dumps(f)) self.assertIs(fup.fn, None) self.assertIs(fup.parent, None) b = block() b.f = f self.assertIs(f.parent, b) bup = pickle.loads( pickle.dumps(b)) fup = bup.f self.assertIs(fup.fn, None) self.assertIs(fup.parent, bup)
def test_pprint(self): import pyomo.kernel # Not really testing what the output is, just that # an error does not occur. The pprint functionality # is still in the early stages. f = functional_value() pyomo.kernel.pprint(f) b = block() b.f = f pyomo.kernel.pprint(f) pyomo.kernel.pprint(b) m = block() m.b = b pyomo.kernel.pprint(f) pyomo.kernel.pprint(b) pyomo.kernel.pprint(m)
def test_dill(self): p = parameter(1) f = functional_value(lambda: p()) self.assertEqual(f(), 1) fup = dill.loads(dill.dumps(f)) p.value = 2 self.assertEqual(f(), 2) self.assertEqual(fup(), 1) b = block() b.p = p b.f = f self.assertEqual(b.f(), 2) bup = dill.loads(dill.dumps(b)) fup = bup.f b.p.value = 4 self.assertEqual(b.f(), 4) self.assertEqual(bup.f(), 2) bup.p.value = 4 self.assertEqual(bup.f(), 4)
def test_dill(self): p = parameter(1) f = functional_value(lambda: p()) self.assertEqual(f(), 1) fup = dill.loads( dill.dumps(f)) p.value = 2 self.assertEqual(f(), 2) self.assertEqual(fup(), 1) b = block() b.p = p b.f = f self.assertEqual(b.f(), 2) bup = dill.loads( dill.dumps(b)) fup = bup.f b.p.value = 4 self.assertEqual(b.f(), 4) self.assertEqual(bup.f(), 2) bup.p.value = 4 self.assertEqual(bup.f(), 4)
def test_is_parameter_type(self): f = functional_value() # GH: apparently is_parameter_type has something # to do with mutability... self.assertEqual(f.is_parameter_type(), False)
def test_is_expression_type(self): f = functional_value() self.assertEqual(f.is_expression_type(), False)
def test_type(self): f = functional_value() self.assertTrue(isinstance(f, ICategorizedObject)) self.assertTrue(isinstance(f, IParameter)) self.assertTrue(isinstance(f, NumericValue))
def test_ctype(self): f = functional_value() self.assertIs(f.ctype, IParameter) self.assertIs(type(f), functional_value) self.assertIs(type(f)._ctype, IParameter)