@@ -68,9 +68,8 @@ def test_brotli_decode_encode(s):
6868 assert decoded == s_bytes
6969
7070
71-
7271@patch ("pypdf.filters.brotli" , None )
73- def test_brotli_missing_installation_mocked ():
72+ def test_brotli_missing_installation ():
7473 """Verify BrotliDecode raises ImportError if brotli is not installed."""
7574 from pypdf .filters import BrotliDecode , decode_stream_data
7675
@@ -93,20 +92,24 @@ def test_brotli_missing_installation_mocked():
9392 decode_stream_data (stream )
9493 assert "Brotli library not installed" in str (exc_info_stream .value )
9594
96- def test_brotli_import_handling ():
97- """Verify the try-except block for brotli import in pypdf.filters."""
98- # Simulate brotli import failure
99- if "brotli" in sys .modules :
100- del sys .modules ["brotli" ]
101- with patch .dict (sys .modules , {"brotli" : None }):
102- import pypdf .filters
103- importlib .reload (pypdf .filters ) # Re-evaluate module-level try-except
104- assert pypdf .filters .brotli is None , "brotli should be None when import fails"
105-
106- # Test successful import if brotli is installed
107- if importlib .util .find_spec ("brotli" ):
108- importlib .reload (pypdf .filters ) # Re-evaluate with brotli available
109- assert pypdf .filters .brotli is not None , "brotli should be imported when available"
95+
96+ @patch ("importlib.util.find_spec" , return_value = None )
97+ def test_brotli_import_handling_not_available ():
98+ """Verify the brotli=None assignment in pypdf.filters when brotli is not available."""
99+ # Re-import the module with patched find_spec to simulate missing brotli
100+ with patch .dict ("sys.modules" , {}, clear = False ):
101+ if "pypdf.filters" in sys .modules :
102+ del sys .modules ["pypdf.filters" ]
103+ from pypdf import filters
104+ assert filters .brotli is None , "brotli should be None when import fails"
105+
106+ @pytest .mark .skipif (importlib .util .find_spec ("brotli" ) is None , reason = "brotli library not installed" )
107+ def test_brotli_import_handling_available ():
108+ """Verify brotli module is properly imported in pypdf.filters when available."""
109+ # This test only runs when brotli is actually installed
110+ from pypdf import filters
111+ assert filters .brotli is not None , "brotli should be imported when available"
112+
110113
111114def test_flatedecode_unsupported_predictor ():
112115 """
@@ -751,14 +754,10 @@ def test_main_decode_brotli_installed():
751754 if importlib .util .find_spec ("brotli" ) is None :
752755 pytest .skip ("brotli library not installed" )
753756
754- # Use the test PDF generated by resources/create_brotli_test_pdf.py
755757 pdf_path = RESOURCE_ROOT / "brotli-test-pdfs" / "minimal-brotli-compressed.pdf"
756758
757759 reader = PdfReader (pdf_path )
758760 page = reader .pages [0 ]
759-
760- # Extract text - this will implicitly use the BrotliDecode filter
761761 extracted_text = page .extract_text ()
762762
763- # Verify the expected text content
764763 assert extracted_text .strip () == "Hello, Brotli!"
0 commit comments