@@ -245,12 +245,12 @@ def cleanup_json_module_for_reload():
245
245
# Explicitly register just in case it wasn't, or was cleaned up elsewhere.
246
246
# This might raise ArrowKeyError itself if already registered, which is fine here.
247
247
pa .register_extension_type (db_dtypes .json .JSONArrowType ())
248
+
248
249
except pa .ArrowKeyError :
249
250
pass # Already registered is the state we want before the test runs
250
- except ImportError :
251
- pytest .skip ("Could not import db_dtypes.json to set up test." )
252
251
253
- # Remove the module from sys.modules so importlib.reload re-executes it
252
+ # Remove the module from sys.modules so importlib.reload which will happen
253
+ # after the yield statement will re-execute it
254
254
if json_module_name in sys .modules :
255
255
del sys .modules [json_module_name ]
256
256
@@ -264,28 +264,37 @@ def cleanup_json_module_for_reload():
264
264
# If the test re-imported it but it wasn't there originally, remove it
265
265
del sys .modules [json_module_name ]
266
266
267
- # Note: PyArrow doesn't have a public API to unregister types easily.
268
- # Relying on module isolation/reloading is a common testing pattern.
267
+ # Note: PyArrow doesn't have a public API to unregister types easily,
268
+ # thus we are using the testing pattern of module isolation/reloading.
269
+
270
+
271
+ # Test specifically for the fixture's pre-yield removal logic
272
+ def test_fixture_removes_module_if_present (cleanup_json_module_for_reload ):
273
+ """
274
+ Tests that the cleanup_json_module_for_reload fixture removes
275
+ db_dtypes.json from sys.modules before yielding to the test.
276
+ This specifically targets the 'if json_module_name in sys.modules:' block.
277
+ """
278
+ # This test runs *after* the fixture's `yield`.
279
+ # The fixture should have removed the module if it was present.
280
+
281
+ json_module_name = "db_dtypes.json"
282
+
283
+ assert (
284
+ json_module_name not in sys .modules
285
+ ), f"The fixture cleanup_json_module_for_reload should have removed { json_module_name } "
269
286
270
287
271
288
def test_json_arrow_type_reregistration_is_handled (cleanup_json_module_for_reload ):
272
289
"""
273
290
Verify that attempting to re-register JSONArrowType via module reload
274
291
is caught by the except block and does not raise an error.
275
292
"""
276
- try :
277
- # Re-importing the module after the fixture removed it from sys.modules
278
- # forces Python to execute the module's top-level code again.
279
- # This includes the pa.register_extension_type call.
280
- assert (
281
- True
282
- ), "Module re-import completed without error, except block likely worked."
283
293
284
- except pa .ArrowKeyError :
285
- # If this exception escapes, the except block in db_dtypes/json.py failed.
286
- pytest .fail (
287
- "pa.ArrowKeyError was raised during module reload, "
288
- "indicating the except block failed."
289
- )
290
- except Exception as e :
291
- pytest .fail (f"An unexpected exception occurred during module reload: { e } " )
294
+ # Re-importing the module after the fixture removed it from sys.modules
295
+ # forces Python to execute the module's top-level code again.
296
+ # This includes the pa.register_extension_type call.
297
+
298
+ import db_dtypes .json # noqa: F401
299
+
300
+ assert True , "Module re-import completed without error, except block likely worked."
0 commit comments