@@ -634,17 +634,15 @@ def test_exception(self):
634
634
nlp = PyomoNLP (model )
635
635
igraph = IncidenceGraphInterface (nlp )
636
636
637
- with self .assertRaises ( RuntimeError ) as exc :
637
+ with self .assertRaisesRegex ( KeyError , "does not exist" ) :
638
638
variables = [model .P ]
639
639
constraints = [model .ideal_gas ]
640
640
igraph .maximum_matching (variables , constraints )
641
- self .assertIn ("must be unindexed" , str (exc .exception ))
642
641
643
- with self .assertRaises ( RuntimeError ) as exc :
642
+ with self .assertRaisesRegex ( KeyError , "does not exist" ) :
644
643
variables = [model .P ]
645
644
constraints = [model .ideal_gas ]
646
645
igraph .block_triangularize (variables , constraints )
647
- self .assertIn ("must be unindexed" , str (exc .exception ))
648
646
649
647
650
648
@unittest .skipUnless (networkx_available , "networkx is not available." )
@@ -885,17 +883,15 @@ def test_exception(self):
885
883
model = make_gas_expansion_model ()
886
884
igraph = IncidenceGraphInterface (model )
887
885
888
- with self .assertRaises ( RuntimeError ) as exc :
886
+ with self .assertRaisesRegex ( KeyError , "does not exist" ) :
889
887
variables = [model .P ]
890
888
constraints = [model .ideal_gas ]
891
889
igraph .maximum_matching (variables , constraints )
892
- self .assertIn ("must be unindexed" , str (exc .exception ))
893
890
894
- with self .assertRaises ( RuntimeError ) as exc :
891
+ with self .assertRaisesRegex ( KeyError , "does not exist" ) :
895
892
variables = [model .P ]
896
893
constraints = [model .ideal_gas ]
897
894
igraph .block_triangularize (variables , constraints )
898
- self .assertIn ("must be unindexed" , str (exc .exception ))
899
895
900
896
@unittest .skipUnless (scipy_available , "scipy is not available." )
901
897
def test_remove (self ):
@@ -923,7 +919,7 @@ def test_remove(self):
923
919
# Say we know that these variables and constraints should
924
920
# be matched...
925
921
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 ]]
927
923
igraph .remove_nodes (vars_to_remove , cons_to_remove )
928
924
variable_set = ComponentSet (igraph .variables )
929
925
self .assertNotIn (model .F [0 ], variable_set )
@@ -1309,7 +1305,7 @@ def test_remove(self):
1309
1305
# matrix.
1310
1306
vars_to_remove = [m .flow_comp [1 ]]
1311
1307
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 )
1313
1309
var_dmp , con_dmp = igraph .dulmage_mendelsohn ()
1314
1310
var_con_set = ComponentSet (igraph .variables + igraph .constraints )
1315
1311
underconstrained_set = ComponentSet (
@@ -1460,6 +1456,42 @@ def test_remove_no_matrix(self):
1460
1456
with self .assertRaisesRegex (RuntimeError , "no incidence matrix" ):
1461
1457
igraph .remove_nodes ([m .v1 ])
1462
1458
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
+
1463
1495
1464
1496
@unittest .skipUnless (networkx_available , "networkx is not available." )
1465
1497
@unittest .skipUnless (scipy_available , "scipy is not available." )
@@ -1840,7 +1872,7 @@ def test_var_elim(self):
1840
1872
for adj_con in igraph .get_adjacent_to (m .x [1 ]):
1841
1873
for adj_var in igraph .get_adjacent_to (m .eq4 ):
1842
1874
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 ])
1844
1876
1845
1877
assert ComponentSet (igraph .variables ) == ComponentSet ([m .x [2 ], m .x [3 ], m .x [4 ]])
1846
1878
assert ComponentSet (igraph .constraints ) == ComponentSet ([m .eq1 , m .eq2 , m .eq3 ])
0 commit comments