microsoft/qdk
Publicmirrored from https://github.com/microsoft/qdkAvailable
source/qdk_package/tests/test_reexports.py
43lines · modecode
| 1 | # Copyright (c) Microsoft Corporation. |
| 2 | # Licensed under the MIT License. |
| 3 | |
| 4 | import pytest, importlib |
| 5 | |
| 6 | |
| 7 | def test_qdk_qsharp_submodule_available(): |
| 8 | # Import the qsharp submodule explicitly. |
| 9 | qdk_qsharp = importlib.import_module("qdk.qsharp") |
| 10 | # Ensure a core API is reachable via submodule |
| 11 | assert hasattr(qdk_qsharp, "run"), "qsharp.run missing in submodule" |
| 12 | |
| 13 | |
| 14 | def test_estimator_and_openqasm_shims(): |
| 15 | est = importlib.import_module("qdk.estimator") |
| 16 | oq = importlib.import_module("qdk.openqasm") |
| 17 | assert hasattr(est, "__doc__") |
| 18 | assert hasattr(oq, "__doc__") |
| 19 | |
| 20 | |
| 21 | def test_missing_optional_direct_imports(): |
| 22 | # If optional extras truly not installed, importing their submodules should raise ImportError. |
| 23 | # We probe without using mocks here. |
| 24 | for mod in ("qdk.widgets", "qdk.azure", "qdk.qiskit"): |
| 25 | base_dep = { |
| 26 | "qdk.widgets": "qsharp_widgets", |
| 27 | "qdk.azure": "azure.quantum", |
| 28 | "qdk.qiskit": "qiskit", |
| 29 | }[mod] |
| 30 | try: |
| 31 | importlib.import_module(base_dep) |
| 32 | dep_installed = True |
| 33 | except Exception: |
| 34 | dep_installed = False |
| 35 | if not dep_installed: |
| 36 | try: |
| 37 | importlib.import_module(mod) |
| 38 | except ImportError as e: |
| 39 | # Expected path: verify helpful hint present |
| 40 | assert "pip install qdk[" in str(e) |
| 41 | else: |
| 42 | # If it imported anyway, treat as environment providing the feature (e.g. via dev install) |
| 43 | pass |