@@ -1045,6 +1045,101 @@ def test_remove_column_removes_relationships(self):
10451045 assert list (manufacturer_mock .columns .keys ()) == ['country' , 'id' ]
10461046 assert metadata ._multi_table_updated
10471047
1048+ def test__remove_column_relationships_only_removes_matching_table (self ):
1049+ """Test that only relationships for the given table and column are removed."""
1050+ # Setup
1051+ metadata = Metadata ()
1052+ metadata .relationships = [
1053+ {
1054+ 'parent_table_name' : 'parent' ,
1055+ 'parent_primary_key' : 'id' ,
1056+ 'child_table_name' : 'child_a' ,
1057+ 'child_foreign_key' : 'fk' ,
1058+ },
1059+ {
1060+ 'parent_table_name' : 'parent' ,
1061+ 'parent_primary_key' : 'id' ,
1062+ 'child_table_name' : 'child_b' ,
1063+ 'child_foreign_key' : 'fk' ,
1064+ },
1065+ ]
1066+
1067+ # Run
1068+ metadata ._remove_column_relationships ('child_a' , 'fk' )
1069+
1070+ # Assert
1071+ assert metadata .relationships == [
1072+ {
1073+ 'parent_table_name' : 'parent' ,
1074+ 'parent_primary_key' : 'id' ,
1075+ 'child_table_name' : 'child_b' ,
1076+ 'child_foreign_key' : 'fk' ,
1077+ },
1078+ ]
1079+
1080+ def test_remove_column_only_removes_relationship_for_that_table (self ):
1081+ """Test removing a foreign key column only removes the relationship for that table."""
1082+ # Setup
1083+ metadata = Metadata .load_from_dict ({
1084+ 'tables' : {
1085+ 'table1' : {
1086+ 'primary_key' : 'id' ,
1087+ 'columns' : {
1088+ 'id' : {'sdtype' : 'id' },
1089+ 'A' : {'sdtype' : 'numerical' },
1090+ 'B' : {'sdtype' : 'categorical' },
1091+ },
1092+ },
1093+ 'table2' : {
1094+ 'primary_key' : 'id' ,
1095+ 'columns' : {
1096+ 'id' : {'sdtype' : 'id' },
1097+ 'fk_1' : {'sdtype' : 'id' },
1098+ 'A' : {'sdtype' : 'numerical' },
1099+ 'B' : {'sdtype' : 'categorical' },
1100+ },
1101+ },
1102+ 'table3' : {
1103+ 'primary_key' : 'id' ,
1104+ 'columns' : {
1105+ 'id' : {'sdtype' : 'id' },
1106+ 'fk_1' : {'sdtype' : 'id' },
1107+ 'A' : {'sdtype' : 'numerical' },
1108+ 'B' : {'sdtype' : 'categorical' },
1109+ },
1110+ },
1111+ },
1112+ 'relationships' : [
1113+ {
1114+ 'parent_table_name' : 'table1' ,
1115+ 'parent_primary_key' : 'id' ,
1116+ 'child_table_name' : 'table2' ,
1117+ 'child_foreign_key' : 'fk_1' ,
1118+ },
1119+ {
1120+ 'parent_table_name' : 'table1' ,
1121+ 'parent_primary_key' : 'id' ,
1122+ 'child_table_name' : 'table3' ,
1123+ 'child_foreign_key' : 'fk_1' ,
1124+ },
1125+ ],
1126+ })
1127+
1128+ # Run
1129+ metadata .remove_column ('fk_1' , 'table2' )
1130+
1131+ # Assert
1132+ assert metadata .relationships == [
1133+ {
1134+ 'parent_table_name' : 'table1' ,
1135+ 'parent_primary_key' : 'id' ,
1136+ 'child_table_name' : 'table3' ,
1137+ 'child_foreign_key' : 'fk_1' ,
1138+ },
1139+ ]
1140+ assert 'fk_1' not in metadata .tables ['table2' ].columns
1141+ assert 'fk_1' in metadata .tables ['table3' ].columns
1142+
10481143 def test_remove_column_sequence_key (self ):
10491144 """Test the method also remove the sequence key if the column is one."""
10501145 # Setup
0 commit comments