1212# See the License for the specific language governing permissions and
1313# limitations under the License.
1414
15+ import json
1516import os
1617import pandas as pd
1718import unittest
@@ -32,27 +33,68 @@ class TestImportDiffer(unittest.TestCase):
3233 def test_diff_analysis (self ):
3334 current_data = os .path .join (module_dir , 'test' , 'current' , '*.mcf' )
3435 previous_data = os .path .join (module_dir , 'test' , 'previous' , '*.mcf' )
35- output_location = os .path .join (module_dir )
36- self .differ = import_differ .ImportDiffer (current_data , previous_data ,
37- output_location )
38- current_mcf = differ_utils .load_data (self .differ .current_data , '.' )
39- previous_mcf = differ_utils .load_data (self .differ .previous_data , '.' )
40- current_obs_df , current_schema_df = self .differ .split_data (current_mcf )
41- previous_obs_df , previous_schema_df = self .differ .split_data (
42- previous_mcf )
43- obs_diff = self .differ .generate_diff (previous_obs_df , current_obs_df )
44- summary , _ = self .differ .observation_diff_analysis (obs_diff )
45- expected_summary = pd .read_csv (
46- os .path .join (module_dir , 'test' , 'results' , 'obs_diff_summary.csv' ))
47- assert_frame_equal (summary , expected_summary )
48-
49- schema_diff = self .differ .generate_diff (previous_schema_df ,
50- current_schema_df )
51- summary = self .differ .schema_diff_analysis (schema_diff )
52- expected_summary = pd .read_csv (
53- os .path .join (module_dir , 'test' , 'results' ,
54- 'schema_diff_summary.csv' ))
55- assert_frame_equal (summary , expected_summary )
36+ output_location = os .path .join (module_dir , 'test' , 'output' )
37+ if os .path .exists (output_location ):
38+ import shutil
39+ shutil .rmtree (output_location )
40+ os .makedirs (output_location )
41+
42+ differ = import_differ .ImportDiffer (current_data = current_data ,
43+ previous_data = previous_data ,
44+ output_location = output_location ,
45+ runner_mode = 'native' )
46+ differ .run_differ ()
47+
48+ # Check for expected files
49+ expected_files = ['import_diff.mcf' , 'differ_summary.json' ]
50+
51+ for f in expected_files :
52+ file_path = os .path .join (output_location , f )
53+ self .assertTrue (os .path .exists (file_path ),
54+ f"File { f } was not generated" )
55+
56+ # Verify content of the combined MCF file
57+ with open (os .path .join (output_location , 'import_diff.mcf' ), 'r' ) as f :
58+ content = f .read ().strip ()
59+
60+ with open (
61+ os .path .join (module_dir , 'test' , 'results' , 'import_diff.mcf' ),
62+ 'r' ) as f :
63+ expected_content = f .read ().strip ()
64+
65+ # Split into individual nodes, strip whitespace, and sort to avoid ordering issues
66+ actual_nodes = sorted (
67+ [node .strip () for node in content .split ('\n \n ' ) if node .strip ()])
68+ expected_nodes = sorted ([
69+ node .strip ()
70+ for node in expected_content .split ('\n \n ' )
71+ if node .strip ()
72+ ])
73+
74+ self .assertListEqual (actual_nodes , expected_nodes )
75+
76+ # Verify content of the summary file
77+ with open (os .path .join (output_location , 'differ_summary.json' ),
78+ 'r' ) as f :
79+ summary_content = json .load (f )
80+
81+ with open (
82+ os .path .join (module_dir , 'test' , 'results' ,
83+ 'differ_summary.json' ), 'r' ) as f :
84+ expected_summary_content = json .load (f )
85+
86+ # The version paths might differ based on where the test is run,
87+ # so we normalize or remove them for the comparison
88+ summary_content .pop ('current_version' , None )
89+ summary_content .pop ('previous_version' , None )
90+ expected_summary_content .pop ('current_version' , None )
91+ expected_summary_content .pop ('previous_version' , None )
92+
93+ self .assertDictEqual (summary_content , expected_summary_content )
94+
95+ if os .path .exists (output_location ):
96+ import shutil
97+ shutil .rmtree (output_location )
5698
5799
58100if __name__ == '__main__' :
0 commit comments