@@ -634,17 +634,15 @@ def test_exception(self):
634634 nlp = PyomoNLP (model )
635635 igraph = IncidenceGraphInterface (nlp )
636636
637- with self .assertRaises ( RuntimeError ) as exc :
637+ with self .assertRaisesRegex ( KeyError , "does not exist" ) :
638638 variables = [model .P ]
639639 constraints = [model .ideal_gas ]
640640 igraph .maximum_matching (variables , constraints )
641- self .assertIn ("must be unindexed" , str (exc .exception ))
642641
643- with self .assertRaises ( RuntimeError ) as exc :
642+ with self .assertRaisesRegex ( KeyError , "does not exist" ) :
644643 variables = [model .P ]
645644 constraints = [model .ideal_gas ]
646645 igraph .block_triangularize (variables , constraints )
647- self .assertIn ("must be unindexed" , str (exc .exception ))
648646
649647
650648@unittest .skipUnless (networkx_available , "networkx is not available." )
@@ -885,17 +883,15 @@ def test_exception(self):
885883 model = make_gas_expansion_model ()
886884 igraph = IncidenceGraphInterface (model )
887885
888- with self .assertRaises ( RuntimeError ) as exc :
886+ with self .assertRaisesRegex ( KeyError , "does not exist" ) :
889887 variables = [model .P ]
890888 constraints = [model .ideal_gas ]
891889 igraph .maximum_matching (variables , constraints )
892- self .assertIn ("must be unindexed" , str (exc .exception ))
893890
894- with self .assertRaises ( RuntimeError ) as exc :
891+ with self .assertRaisesRegex ( KeyError , "does not exist" ) :
895892 variables = [model .P ]
896893 constraints = [model .ideal_gas ]
897894 igraph .block_triangularize (variables , constraints )
898- self .assertIn ("must be unindexed" , str (exc .exception ))
899895
900896 @unittest .skipUnless (scipy_available , "scipy is not available." )
901897 def test_remove (self ):
@@ -923,7 +919,7 @@ def test_remove(self):
923919 # Say we know that these variables and constraints should
924920 # be matched...
925921 vars_to_remove = [model .F [0 ], model .F [2 ]]
926- cons_to_remove = ( model .mbal [1 ], model .mbal [2 ])
922+ cons_to_remove = [ model .mbal [1 ], model .mbal [2 ]]
927923 igraph .remove_nodes (vars_to_remove , cons_to_remove )
928924 variable_set = ComponentSet (igraph .variables )
929925 self .assertNotIn (model .F [0 ], variable_set )
@@ -1309,7 +1305,7 @@ def test_remove(self):
13091305 # matrix.
13101306 vars_to_remove = [m .flow_comp [1 ]]
13111307 cons_to_remove = [m .flow_eqn [1 ]]
1312- igraph .remove_nodes (vars_to_remove + cons_to_remove )
1308+ igraph .remove_nodes (vars_to_remove , cons_to_remove )
13131309 var_dmp , con_dmp = igraph .dulmage_mendelsohn ()
13141310 var_con_set = ComponentSet (igraph .variables + igraph .constraints )
13151311 underconstrained_set = ComponentSet (
@@ -1460,6 +1456,42 @@ def test_remove_no_matrix(self):
14601456 with self .assertRaisesRegex (RuntimeError , "no incidence matrix" ):
14611457 igraph .remove_nodes ([m .v1 ])
14621458
1459+ def test_remove_bad_node (self ):
1460+ m = pyo .ConcreteModel ()
1461+ m .x = pyo .Var ([1 , 2 , 3 ])
1462+ m .eq = pyo .Constraint (pyo .PositiveIntegers )
1463+ m .eq [1 ] = m .x [1 ] * m .x [2 ] == m .x [3 ]
1464+ m .eq [2 ] = m .x [1 ] + 2 * m .x [2 ] == 3 * m .x [3 ]
1465+ igraph = IncidenceGraphInterface (m )
1466+ with self .assertRaisesRegex (KeyError , "does not exist" ):
1467+ # Suppose we think something like this should work. We should get
1468+ # an error, and not silently do nothing.
1469+ igraph .remove_nodes ([m .x ], [m .eq [1 ]])
1470+
1471+ with self .assertRaisesRegex (KeyError , "does not exist" ):
1472+ igraph .remove_nodes (None , [m .eq ])
1473+
1474+ with self .assertRaisesRegex (KeyError , "does not exist" ):
1475+ igraph .remove_nodes ([[m .x [1 ], m .x [2 ]], [m .eq [1 ]]])
1476+
1477+ def test_remove_varcon_samelist_deprecated (self ):
1478+ m = pyo .ConcreteModel ()
1479+ m .x = pyo .Var ([1 , 2 , 3 ])
1480+ m .eq = pyo .Constraint (pyo .PositiveIntegers )
1481+ m .eq [1 ] = m .x [1 ] * m .x [2 ] == m .x [3 ]
1482+ m .eq [2 ] = m .x [1 ] + 2 * m .x [2 ] == 3 * m .x [3 ]
1483+
1484+ igraph = IncidenceGraphInterface (m )
1485+ # This raises a deprecation warning. When the deprecated functionality
1486+ # is removed, this will fail, and this test should be updated accordingly.
1487+ igraph .remove_nodes ([m .eq [1 ], m .x [1 ]])
1488+ self .assertEqual (len (igraph .variables ), 2 )
1489+ self .assertEqual (len (igraph .constraints ), 1 )
1490+
1491+ igraph .remove_nodes ([], [m .eq [2 ], m .x [2 ]])
1492+ self .assertEqual (len (igraph .variables ), 1 )
1493+ self .assertEqual (len (igraph .constraints ), 0 )
1494+
14631495
14641496@unittest .skipUnless (networkx_available , "networkx is not available." )
14651497@unittest .skipUnless (scipy_available , "scipy is not available." )
@@ -1840,7 +1872,7 @@ def test_var_elim(self):
18401872 for adj_con in igraph .get_adjacent_to (m .x [1 ]):
18411873 for adj_var in igraph .get_adjacent_to (m .eq4 ):
18421874 igraph .add_edge (adj_var , adj_con )
1843- igraph .remove_nodes ([m .x [1 ], m .eq4 ])
1875+ igraph .remove_nodes ([m .x [1 ]], [ m .eq4 ])
18441876
18451877 assert ComponentSet (igraph .variables ) == ComponentSet ([m .x [2 ], m .x [3 ], m .x [4 ]])
18461878 assert ComponentSet (igraph .constraints ) == ComponentSet ([m .eq1 , m .eq2 , m .eq3 ])
0 commit comments