|
| 1 | +"""Test cases for ProcurementTools class.""" |
| 2 | +import sys |
| 3 | +import types |
| 4 | +import os |
| 5 | +from unittest.mock import MagicMock |
| 6 | +from enum import Enum |
| 7 | +import pytest |
| 8 | + |
| 9 | +# --------------------- |
| 10 | +# Step 1: Mocks |
| 11 | +# --------------------- |
| 12 | + |
| 13 | +#Mock app_config |
| 14 | +mock_app_config = types.ModuleType("app_config") |
| 15 | +mock_config = MagicMock() |
| 16 | +mock_config.get_azure_credentials.return_value = "mock_credentials" |
| 17 | +mock_config.get_cosmos_database_client.return_value = "mock_cosmos_client" |
| 18 | +mock_config.create_kernel.return_value = "mock_kernel" |
| 19 | +mock_config.get_ai_project_client.return_value = "mock_ai_project" |
| 20 | +mock_app_config.config = mock_config |
| 21 | +sys.modules["app_config"] = mock_app_config |
| 22 | + |
| 23 | +#Mock semantic_kernel base and all submodules |
| 24 | +sk_pkg = types.ModuleType("semantic_kernel") |
| 25 | +sk_pkg.__path__ = [] |
| 26 | +sys.modules["semantic_kernel"] = sk_pkg |
| 27 | + |
| 28 | +# semantic_kernel.functions |
| 29 | +sk_funcs = types.ModuleType("semantic_kernel.functions") |
| 30 | +def kernel_function(name=None, description=None): |
| 31 | + """A mock kernel function decorator.""" |
| 32 | + class DummyKernelFunction: |
| 33 | + """A dummy kernel function class.""" |
| 34 | + def __init__(self, description): |
| 35 | + self.description = description |
| 36 | + def decorator(func): |
| 37 | + setattr(func, "__kernel_name__", name or func.__name__) |
| 38 | + setattr(func, "__kernel_function__", DummyKernelFunction(description)) |
| 39 | + return func |
| 40 | + return decorator |
| 41 | +sk_funcs.kernel_function = kernel_function |
| 42 | +sys.modules["semantic_kernel.functions"] = sk_funcs |
| 43 | + |
| 44 | +# semantic_kernel.kernel |
| 45 | +sk_kernel = types.ModuleType("semantic_kernel.kernel") |
| 46 | +sk_kernel.Kernel = MagicMock(name="Kernel") |
| 47 | +sys.modules["semantic_kernel.kernel"] = sk_kernel |
| 48 | + |
| 49 | +# semantic_kernel.contents |
| 50 | +sk_contents = types.ModuleType("semantic_kernel.contents") |
| 51 | +sk_contents.ChatHistory = MagicMock(name="ChatHistory") |
| 52 | +sys.modules["semantic_kernel.contents"] = sk_contents |
| 53 | + |
| 54 | +# semantic_kernel.connectors fallback |
| 55 | +sk_connectors = types.ModuleType("semantic_kernel.connectors") |
| 56 | +sk_ai = types.ModuleType("semantic_kernel.connectors.ai") |
| 57 | +sk_chat = types.ModuleType("semantic_kernel.connectors.ai.chat_completion_client") |
| 58 | +sk_chat.ChatHistory = MagicMock(name="ChatHistory") |
| 59 | +sys.modules["semantic_kernel.connectors"] = sk_connectors |
| 60 | +sys.modules["semantic_kernel.connectors.ai"] = sk_ai |
| 61 | +sys.modules["semantic_kernel.connectors.ai.chat_completion_client"] = sk_chat |
| 62 | + |
| 63 | +#Mock semantic_kernel.agents.azure_ai.azure_ai_agent.AzureAIAgent |
| 64 | +sk_agents = types.ModuleType("semantic_kernel.agents") |
| 65 | +sk_azure_ai = types.ModuleType("semantic_kernel.agents.azure_ai") |
| 66 | +sk_azure_ai_agent = types.ModuleType("semantic_kernel.agents.azure_ai.azure_ai_agent") |
| 67 | +sk_azure_ai_agent.AzureAIAgent = MagicMock(name="AzureAIAgent") |
| 68 | +sys.modules["semantic_kernel.agents"] = sk_agents |
| 69 | +sys.modules["semantic_kernel.agents.azure_ai"] = sk_azure_ai |
| 70 | +sys.modules["semantic_kernel.agents.azure_ai.azure_ai_agent"] = sk_azure_ai_agent |
| 71 | + |
| 72 | +#Mock models.messages_kernel.AgentType |
| 73 | +models_pkg = types.ModuleType("models") |
| 74 | +msgs_mod = types.ModuleType("models.messages_kernel") |
| 75 | + |
| 76 | +class AgentType(Enum): |
| 77 | + """Mock AgentType Enum.""" |
| 78 | + HR = 'hr_agent' |
| 79 | + PROCUREMENT = 'procurement_agent' |
| 80 | + MARKETING = 'marketing_agent' |
| 81 | + PRODUCT = 'product_agent' |
| 82 | + TECH_SUPPORT = 'tech_support_agent' |
| 83 | +msgs_mod.AgentType = AgentType |
| 84 | +models_pkg.messages_kernel = msgs_mod |
| 85 | +sys.modules['models'] = models_pkg |
| 86 | +sys.modules['models.messages_kernel'] = msgs_mod |
| 87 | + |
| 88 | +#Ensure src is in sys.path |
| 89 | +PROJECT_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', '..')) |
| 90 | +SRC_PATH = os.path.join(PROJECT_ROOT, 'src') |
| 91 | +if SRC_PATH not in sys.path: |
| 92 | + sys.path.insert(0, SRC_PATH) |
| 93 | + |
| 94 | +#Import Config AFTER all mocks |
| 95 | +from backend.config_kernel import Config |
| 96 | + |
| 97 | +# --------------------- |
| 98 | +# Step 2: Fixtures |
| 99 | +# --------------------- |
| 100 | +@pytest.fixture(autouse=True) |
| 101 | +def set_env_vars(monkeypatch): |
| 102 | + """Set environment variables for the tests.""" |
| 103 | + monkeypatch.setenv("AZURE_OPENAI_DEPLOYMENT_NAME", "gpt-4o") |
| 104 | + monkeypatch.setenv("AZURE_OPENAI_API_VERSION", "2024-11-20") |
| 105 | + monkeypatch.setenv("AZURE_OPENAI_ENDPOINT", "https://example-openai-endpoint.com") |
| 106 | + monkeypatch.setenv("AZURE_AI_SUBSCRIPTION_ID", "fake-subscription-id") |
| 107 | + monkeypatch.setenv("AZURE_AI_RESOURCE_GROUP", "fake-resource-group") |
| 108 | + monkeypatch.setenv("AZURE_AI_PROJECT_NAME", "fake-project-name") |
| 109 | + monkeypatch.setenv("AZURE_AI_AGENT_PROJECT_CONNECTION_STRING", "fake-connection-string") |
| 110 | + monkeypatch.setenv("AZURE_TENANT_ID", "fake-tenant-id") |
| 111 | + monkeypatch.setenv("AZURE_CLIENT_ID", "fake-client-id") |
| 112 | + monkeypatch.setenv("AZURE_CLIENT_SECRET", "fake-client-secret") |
| 113 | + monkeypatch.setenv("COSMOSDB_ENDPOINT", "https://fake-cosmos-endpoint.com") |
| 114 | + monkeypatch.setenv("COSMOSDB_DATABASE", "fake-database") |
| 115 | + monkeypatch.setenv("COSMOSDB_CONTAINER", "fake-container") |
| 116 | + monkeypatch.setenv("AZURE_OPENAI_SCOPE", "https://customscope.com/.default") |
| 117 | + monkeypatch.setenv("FRONTEND_SITE_NAME", "http://localhost:3000") |
| 118 | + |
| 119 | +# --------------------- |
| 120 | +# Step 3: Tests |
| 121 | +# --------------------- |
| 122 | +def test_get_azure_credentials(): |
| 123 | + """Test the GetAzureCredentials method.""" |
| 124 | + result = Config.GetAzureCredentials() |
| 125 | + assert result == "mock_credentials" |
| 126 | + mock_config.get_azure_credentials.assert_called_once() |
| 127 | + |
| 128 | +def test_get_cosmos_database_client(): |
| 129 | + """Test the GetCosmosDatabaseClient method.""" |
| 130 | + result = Config.GetCosmosDatabaseClient() |
| 131 | + assert result == "mock_cosmos_client" |
| 132 | + mock_config.get_cosmos_database_client.assert_called_once() |
| 133 | + |
| 134 | +def test_create_kernel(): |
| 135 | + """Test the CreateKernel method.""" |
| 136 | + result = Config.CreateKernel() |
| 137 | + assert result == "mock_kernel" |
| 138 | + mock_config.create_kernel.assert_called_once() |
| 139 | + |
| 140 | +def test_get_ai_project_client(): |
| 141 | + """Test the GetAIProjectClient method.""" |
| 142 | + result = Config.GetAIProjectClient() |
| 143 | + assert result == "mock_ai_project" |
| 144 | + mock_config.get_ai_project_client.assert_called_once() |
0 commit comments