1515 enforce_parent_reference ,
1616 enforce_schema ,
1717)
18+ from data_pipeline .contract .id_registrar import map_uuid_to_int , id_mapping
1819
1920# ------------------------------------------------------------
2021# FIXTURES
@@ -156,6 +157,64 @@ def test_enforce_schema():
156157 assert filtered ["state" ].dtype == pl .Categorical
157158
158159
160+ # ------------------------------------------------------------
161+ # ID REGISTRAR UNIT TESTS
162+ # ------------------------------------------------------------
163+
164+
165+ def test_map_uuid_to_int_new_file (tmp_path ):
166+ df = pl .DataFrame ({"user_id" : ["u1" , "u2" , "u3" ]})
167+ mapping_file = tmp_path / "user_id_mapping.parquet"
168+
169+ map_uuid_to_int (df , mapping_file , "user_id" )
170+
171+ assert mapping_file .exists ()
172+ mapping_df = pl .read_parquet (mapping_file )
173+ assert mapping_df .height == 3
174+ assert "user_id" in mapping_df .columns
175+ assert "user_id_int" in mapping_df .columns
176+ assert mapping_df ["user_id_int" ].to_list () == [1 , 2 , 3 ]
177+ assert mapping_df ["user_id_int" ].dtype == pl .UInt32
178+
179+
180+ def test_map_uuid_to_int_update_existing (tmp_path ):
181+ mapping_file = tmp_path / "user_id_mapping.parquet"
182+ initial_df = pl .DataFrame ({"user_id" : ["u1" , "u2" ], "user_id_int" : [1 , 2 ]}).cast (
183+ {"user_id_int" : pl .UInt32 }
184+ )
185+ initial_df .write_parquet (mapping_file )
186+
187+ new_df = pl .DataFrame ({"user_id" : ["u2" , "u3" , "u4" ]})
188+ map_uuid_to_int (new_df , mapping_file , "user_id" )
189+
190+ mapping_df = pl .read_parquet (mapping_file ).sort ("user_id_int" )
191+ assert mapping_df .height == 4
192+ assert set (mapping_df ["user_id" ].to_list ()) == {"u1" , "u2" , "u3" , "u4" }
193+ assert mapping_df ["user_id_int" ].to_list () == [1 , 2 , 3 , 4 ]
194+
195+
196+ def test_id_mapping_orchestration (tmp_path ):
197+ runtime_dir = tmp_path / "runtime"
198+ destination = tmp_path / "destination"
199+ runtime_dir .mkdir ()
200+ destination .mkdir ()
201+
202+ df = pl .DataFrame ({"order_id" : ["o1" , "o2" ], "customer_id" : ["c1" , "c2" ]})
203+
204+ mapping_dict = {"df_orders" : ["order_id" , "customer_id" ]}
205+
206+ lf_mapped = id_mapping (df , "df_orders" , mapping_dict , runtime_dir , destination )
207+ result_df = lf_mapped .collect ()
208+
209+ assert "order_id_int" in result_df .columns
210+ assert "customer_id_int" in result_df .columns
211+ assert result_df .height == 2
212+
213+ # Check if mapping files were promoted to destination
214+ assert (destination / "order_id_mapping.parquet" ).exists ()
215+ assert (destination / "customer_id_mapping.parquet" ).exists ()
216+
217+
159218# ------------------------------------------------------------
160219# EXECUTOR INTEGRATION TESTS
161220# ------------------------------------------------------------
@@ -170,7 +229,6 @@ def test_apply_contract_orders_success(tmp_path, sample_orders_df):
170229 run_context .raw_snapshot_path / f"df_orders_{ suffix } .csv"
171230 )
172231
173- # New 3-tuple return signature
174232 report , inv_ids , val_ids = apply_contract (run_context , "df_orders" )
175233
176234 assert report ["status" ] == "success"
@@ -186,7 +244,6 @@ def test_apply_contract_cascade_and_valid_propagation(
186244 run_context = RunContext .create (base = tmp_path , storage = tmp_path / "storage" )
187245 run_context .initialize_directories ()
188246
189- # o1: valid, o2: unparsable, o3: impossible
190247 sample_orders_df = sample_orders_df .with_columns (
191248 pl .when (pl .col ("order_id" ) == "o2" )
192249 .then (pl .lit ("garbage" ))
@@ -206,13 +263,11 @@ def test_apply_contract_cascade_and_valid_propagation(
206263 run_context .raw_snapshot_path / f"df_payments_{ suffix } .csv"
207264 )
208265
209- # 1. Process Orders
210266 rep_o , inv_o , val_o = apply_contract (run_context , "df_orders" )
211267 assert "o2" in inv_o # unparsable
212268 assert "o3" in inv_o # impossible
213269 assert "o1" in val_o # only one valid
214270
215- # 2. Process Payments (should cascade drop o2, o3 and only keep o1)
216271 rep_p , inv_p , val_p = apply_contract (
217272 run_context , "df_payments" , invalid_order_ids = inv_o , valid_order_ids = val_o
218273 )
0 commit comments