|
| 1 | +# Copyright (c) Microsoft. All rights reserved. |
| 2 | + |
| 3 | +"""Tests for the azure-ai-projects version guard in the Foundry toolbox sample.""" |
| 4 | + |
| 5 | +import importlib.util |
| 6 | +import sys |
| 7 | +from pathlib import Path |
| 8 | +from types import ModuleType |
| 9 | + |
| 10 | +import pytest |
| 11 | + |
| 12 | +_SAMPLE_PATH = ( |
| 13 | + Path(__file__).parents[3] |
| 14 | + / "samples" |
| 15 | + / "02-agents" |
| 16 | + / "providers" |
| 17 | + / "foundry" |
| 18 | + / "foundry_chat_client_with_toolbox.py" |
| 19 | +) |
| 20 | + |
| 21 | + |
| 22 | +def _load_sample_module() -> ModuleType: |
| 23 | + spec = importlib.util.spec_from_file_location("foundry_chat_client_with_toolbox", _SAMPLE_PATH) |
| 24 | + assert spec is not None |
| 25 | + assert spec.loader is not None |
| 26 | + module = importlib.util.module_from_spec(spec) |
| 27 | + spec.loader.exec_module(module) |
| 28 | + return module |
| 29 | + |
| 30 | + |
| 31 | +sample_module = _load_sample_module() |
| 32 | + |
| 33 | + |
| 34 | +class _StopBeforeNetwork(Exception): |
| 35 | + pass |
| 36 | + |
| 37 | + |
| 38 | +def _install_models_surface(monkeypatch: pytest.MonkeyPatch, exported: tuple[str, ...]) -> None: |
| 39 | + """Replace azure.ai.projects.models with a module exposing only the given toolbox names.""" |
| 40 | + models = ModuleType("azure.ai.projects.models") |
| 41 | + for name in exported: |
| 42 | + setattr(models, name, type(name, (), {})) |
| 43 | + monkeypatch.setitem(sys.modules, "azure.ai.projects.models", models) |
| 44 | + |
| 45 | + |
| 46 | +@pytest.mark.parametrize( |
| 47 | + "exported", |
| 48 | + [ |
| 49 | + pytest.param((), id="azure-ai-projects-2.2"), |
| 50 | + pytest.param(("MCPToolboxTool", "ToolboxTool"), id="azure-ai-projects-2.3"), |
| 51 | + ], |
| 52 | +) |
| 53 | +def test_create_sample_toolbox_reports_upgrade_for_older_sdk( |
| 54 | + monkeypatch: pytest.MonkeyPatch, exported: tuple[str, ...] |
| 55 | +) -> None: |
| 56 | + _install_models_surface(monkeypatch, exported) |
| 57 | + |
| 58 | + def fail_if_called() -> None: |
| 59 | + raise AssertionError("The version check must run before any credential is created.") |
| 60 | + |
| 61 | + monkeypatch.setattr(sample_module, "AzureCliCredential", fail_if_called) |
| 62 | + |
| 63 | + with pytest.raises(ImportError, match=r"azure-ai-projects>=2\.4\.0"): |
| 64 | + sample_module.create_sample_toolbox("toolbox") |
| 65 | + |
| 66 | + |
| 67 | +def test_create_sample_toolbox_accepts_tool_search_sdk(monkeypatch: pytest.MonkeyPatch) -> None: |
| 68 | + _install_models_surface(monkeypatch, ("MCPToolboxTool", "ToolboxTool", "ToolSearchToolboxTool")) |
| 69 | + |
| 70 | + def stop_before_network() -> None: |
| 71 | + raise _StopBeforeNetwork |
| 72 | + |
| 73 | + monkeypatch.setattr(sample_module, "AzureCliCredential", stop_before_network) |
| 74 | + |
| 75 | + with pytest.raises(_StopBeforeNetwork): |
| 76 | + sample_module.create_sample_toolbox("toolbox") |
0 commit comments