This is an automated email from the ASF dual-hosted git repository.

hcr pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/mahout.git


The following commit(s) were added to refs/heads/main by this push:
     new c6cec499b Improve coverage for qdp.py by testing import fallback and 
stub behavior (#1084)
c6cec499b is described below

commit c6cec499b95fadedf406aa7d733318975221d5aa
Author: alisha-1000 <[email protected]>
AuthorDate: Tue Feb 24 18:40:29 2026 +0530

    Improve coverage for qdp.py by testing import fallback and stub behavior 
(#1084)
    
    * Improve coverage for qdp.py by testing import fallback and stub behavior
    
    * Fix ruff and license header issues
    
    * Fix qdp import fallback tests to safely mock missing _qdp extension
    
    * Fix EOF newline
    
    * Strengthen qdp fallback tests: restore sys.modules, capture warnings, 
validate ImportError messages
    
    * Fix end-of-file formatting (pre-commit)
    
    ---------
    
    Co-authored-by: Alisha Gupta <[email protected]>
    Co-authored-by: Ryan Huang <[email protected]>
---
 testing/qumat/test_qdp_module.py | 91 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 91 insertions(+)

diff --git a/testing/qumat/test_qdp_module.py b/testing/qumat/test_qdp_module.py
new file mode 100644
index 000000000..caf28eee0
--- /dev/null
+++ b/testing/qumat/test_qdp_module.py
@@ -0,0 +1,91 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import sys
+import importlib
+import pytest
+from unittest.mock import patch
+
+
+def _reload_qdp_without_extension():
+    """
+    Safely reload qumat.qdp while simulating missing _qdp extension.
+    Restores sys.modules after execution to avoid test pollution.
+    """
+    original_qdp = sys.modules.get("qumat.qdp")
+    original_ext = sys.modules.get("_qdp")
+
+    try:
+        # Remove cached modules
+        sys.modules.pop("qumat.qdp", None)
+        sys.modules.pop("_qdp", None)
+
+        # Simulate missing compiled extension
+        with patch.dict(sys.modules, {"_qdp": None}):
+            module = importlib.import_module("qumat.qdp")
+            return importlib.reload(module)
+
+    finally:
+        # Restore previous state
+        if original_qdp is not None:
+            sys.modules["qumat.qdp"] = original_qdp
+        else:
+            sys.modules.pop("qumat.qdp", None)
+
+        if original_ext is not None:
+            sys.modules["_qdp"] = original_ext
+        else:
+            sys.modules.pop("_qdp", None)
+
+
+def test_qdp_import_fallback_warning():
+    with pytest.warns(ImportWarning):
+        qdp = _reload_qdp_without_extension()
+    assert qdp is not None
+
+
+def test_qdp_engine_stub_raises_import_error():
+    with pytest.warns(ImportWarning):
+        qdp = _reload_qdp_without_extension()
+
+    with pytest.raises(ImportError, match="install"):
+        qdp.QdpEngine()
+
+
+def test_quantum_tensor_stub_raises_import_error():
+    with pytest.warns(ImportWarning):
+        qdp = _reload_qdp_without_extension()
+
+    with pytest.raises(ImportError, match="install"):
+        qdp.QuantumTensor()
+
+
+def test_qdp_all_exports():
+    with pytest.warns(ImportWarning):
+        qdp = _reload_qdp_without_extension()
+
+    assert "QdpEngine" in qdp.__all__
+    assert "QuantumTensor" in qdp.__all__
+
+
+def test_make_stub_direct_call():
+    with pytest.warns(ImportWarning):
+        qdp = _reload_qdp_without_extension()
+
+    StubClass = qdp._make_stub("TestStub")
+
+    with pytest.raises(ImportError, match="install"):
+        StubClass()

Reply via email to