@@ -912,6 +912,41 @@ def test__add_foreign_key_columns(self):
912912 pd .testing .assert_frame_equal (expected_parent_table , parent_table )
913913 pd .testing .assert_frame_equal (expected_child_table , child_table )
914914
915+ def test__add_foreign_key_columns_fk_already_in_child (self ):
916+ """Test that ``_add_foreign_key_columns`` does not add a FK already in child table."""
917+ # Setup
918+ instance = Mock ()
919+ mock_child_metadata = Mock ()
920+ mock_child_metadata .primary_key = 'parent_id'
921+ mock_parent_metadata = Mock ()
922+ mock_parent_metadata .primary_key = 'parent_id_pk'
923+ mock_metadata = Mock ()
924+ mock_metadata .tables = {
925+ 'child' : mock_child_metadata ,
926+ 'parent' : mock_parent_metadata ,
927+ }
928+ mock_metadata ._get_foreign_keys .return_value = ['parent_id' ]
929+ instance .metadata = mock_metadata
930+ child_table = pd .DataFrame ({'parent_id' : [1 , 2 , 3 ], 'col_num' : [10.1 , 11.2 , 12.3 ]})
931+ parent_table = pd .DataFrame ({'parent_id_pk' : [1 , 2 , 3 , 4 ], 'col_cat' : ['A' , 'B' , 'B' , 'C' ]})
932+
933+ # Run
934+ HMASynthesizer ._add_foreign_key_columns (
935+ instance , child_table , parent_table , child_name = 'child' , parent_name = 'parent'
936+ )
937+
938+ # Assert
939+ expected_parent_table = pd .DataFrame ({
940+ 'parent_id_pk' : pd .Series ([1 , 2 , 3 , 4 ], dtype = np .int64 ),
941+ 'col_cat' : pd .Series (['A' , 'B' , 'B' , 'C' ], dtype = object ),
942+ })
943+ expected_child_table = pd .DataFrame ({
944+ 'parent_id' : pd .Series ([1 , 2 , 3 ], dtype = np .int64 ),
945+ 'col_num' : pd .Series ([10.1 , 11.2 , 12.3 ], dtype = 'float64' ),
946+ })
947+ pd .testing .assert_frame_equal (expected_parent_table , parent_table )
948+ pd .testing .assert_frame_equal (expected_child_table , child_table )
949+
915950 def test__estimate_num_columns_to_be_modeled_multiple_foreign_keys (self ):
916951 """Test it when there are two relationships between a parent and a child tables.
917952
0 commit comments